Moving check-jinja2 to pharos.
[pharos.git] / config / utils / check-jinja2.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2016 Linux Foundation and others.
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9 ##############################################################################
10
11 set +x
12 set +o errexit
13
14 GEN_CFG='./config/utils/generate_config.py'
15 INSTALLER_ADAPTERS='./config/installers/*'
16 TMPF='/tmp/out.yml' # should be outside Jenkins WS to prevent data leakage
17 RC=0
18
19 # Build a table header, using ';' as column sep
20 SUMMARY='PDF Verify Matrix;YAML Lint;'
21 for adapter in ${INSTALLER_ADAPTERS}; do
22     SUMMARY+="$(basename "${adapter}");"
23 done
24
25 # Iterate all PDFs, check with each installer adapter, log results
26 while IFS= read -r lab_config; do
27     valid_yaml='OK'
28     echo -e "\n\nyamllint -s ${lab_config}"
29     if ! yamllint -s "${lab_config}"; then valid_yaml='FAIL'; fi
30     SUMMARY+="\n${lab_config#labs/};${valid_yaml};"
31     for adapter in ${INSTALLER_ADAPTERS}; do
32         pdf_inst=0
33         pdf_inst_pass=0
34         pdf_yaml_pass=0
35         while IFS= read -r jinja_template; do
36             echo -e "\n${GEN_CFG} -y ${lab_config} -j ${jinja_template}"
37             if "${GEN_CFG}" -y "${lab_config}" \
38                             -j "${jinja_template}" > "${TMPF}"; then
39                 echo 'Result: PASS'
40                 ((pdf_inst_pass+=1))
41                 echo -e "\nyamllint -s ${jinja_template%.j2}"
42                 if yamllint -s "${TMPF}"; then ((pdf_yaml_pass+=1)); fi
43             else
44                 echo 'Result: FAIL'
45                 RC=1
46             fi
47             ((pdf_inst+=1))
48         done < <(find "${adapter}" -name '*.j2')
49         SUMMARY+="${pdf_yaml_pass}/${pdf_inst_pass}/${pdf_inst};"
50     done
51 done < <(find 'config' 'labs' -name 'pod*.yaml')
52
53 rm -f "${TMPF}"
54 echo -e '\n\nNOTE: tuple fmt: (valid YAML output/sucessful parse/templates).\n'
55 echo -e "${SUMMARY}" | sed -e 's/^/;/g' -e 's/;/;| /g' | column -t -s ';'
56
57 cat <<EOF
58
59 To troubleshoot PDF parsing against a specific installer adapter,
60 execute the following commands locally (e.g. for zte-pod2/joid):
61 $ ./config/utils/generate_config.py \\
62     -y labs/zte/pod2.yaml \\
63     -j ./pharos/config/installers/joid/pod_config.yaml.j2
64
65 EOF
66 exit "${RC}"