Merge "lf-pod4: Align with PDF spec, minor updates"
[securedlab.git] / check-jinja2.sh
1 #!/bin/bash
2 set +x
3 set +o errexit
4
5 git submodule update --init --remote 2>/dev/null
6 GEN_CFG='./pharos/config/utils/generate_config.py'
7 INSTALLER_ADAPTERS='./pharos/config/installers/*'
8 TMPF='/tmp/out.yml' # should be outside Jenkins WS to prevent data leakage
9 RC=0
10
11 # Build a table header, using ';' as column sep
12 SUMMARY='PDF Verify Matrix;YAML Lint;'
13 for adapter in ${INSTALLER_ADAPTERS}; do
14     SUMMARY+="$(basename "${adapter}");"
15 done
16
17 # Iterate all PDFs, check with each installer adapter, log results
18 while IFS= read -r lab_config; do
19     valid_yaml='OK'
20     echo -e "\n\nyamllint -s ${lab_config}"
21     if ! yamllint -s "${lab_config}"; then valid_yaml='FAIL'; fi
22     SUMMARY+="\n${lab_config#labs/};${valid_yaml};"
23     for adapter in ${INSTALLER_ADAPTERS}; do
24         pdf_inst=0
25         pdf_inst_pass=0
26         pdf_yaml_pass=0
27         while IFS= read -r jinja_template; do
28             echo -e "\n${GEN_CFG} -y ${lab_config} -j ${jinja_template}"
29             if "${GEN_CFG}" -y "${lab_config}" \
30                             -j "${jinja_template}" > "${TMPF}"; then
31                 echo 'Result: PASS'
32                 ((pdf_inst_pass+=1))
33                 echo -e "\nyamllint -s ${jinja_template%.j2}"
34                 if yamllint -s "${TMPF}"; then ((pdf_yaml_pass+=1)); fi
35             else
36                 echo 'Result: FAIL'
37                 RC=1
38             fi
39             ((pdf_inst+=1))
40         done < <(find "${adapter}" -name '*.j2')
41         SUMMARY+="${pdf_yaml_pass}/${pdf_inst_pass}/${pdf_inst};"
42     done
43 done < <(find 'pharos/config' 'labs' -name 'pod*.yaml')
44
45 rm -f "${TMPF}"
46 echo -e '\n\nNOTE: tuple fmt: (valid YAML output/sucessful parse/templates).\n'
47 echo -e "${SUMMARY}" | sed -e 's/^/;/g' -e 's/;/;| /g' | column -t -s ';'
48
49 cat <<EOF
50
51 To troubleshoot PDF parsing against a specific installer adapter,
52 execute the following commands locally (e.g. for zte-pod2/joid):
53 $ git submodule update --remote --init
54 $ ./pharos/config/utils/generate_config.py \\
55     -y labs/zte/pod2.yaml \\
56     -j ./pharos/config/installers/joid/pod_config.yaml.j2
57
58 EOF
59 exit "${RC}"