Merge "Fix First MAC Address on LF-POD5 Jumphost"
[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 export PATH=$PATH:/usr/local/bin/
14
15 GEN_CFG='./config/utils/generate_config.py'
16 INSTALLER_ADAPTERS='./config/installers/*'
17 TMPF='/tmp/out.yml' # should be outside Jenkins WS to prevent data leakage
18 RC=0
19
20 # Build a table header, using ';' as column sep
21 SUMMARY='PDF Verify Matrix;YAML Lint;'
22 for adapter in ${INSTALLER_ADAPTERS}; do
23     SUMMARY+="$(basename "${adapter}");"
24 done
25
26 # Iterate all PDFs, check with each installer adapter, log results
27 while IFS= read -r lab_config; do
28     valid_yaml='OK'
29     echo -e "\n\nyamllint -s ${lab_config}"
30     if ! yamllint -s "${lab_config}"; then valid_yaml='FAIL'; fi
31     SUMMARY+="\n${lab_config#labs/};${valid_yaml};"
32     for adapter in ${INSTALLER_ADAPTERS}; do
33         pdf_inst=0
34         pdf_inst_pass=0
35         pdf_yaml_pass=0
36         while IFS= read -r jinja_template; do
37             echo -e "\n${GEN_CFG} -y ${lab_config} -j ${jinja_template}"
38             if "${GEN_CFG}" -y "${lab_config}" \
39                             -j "${jinja_template}" > "${TMPF}"; then
40                 echo 'Result: PASS'
41                 ((pdf_inst_pass+=1))
42                 echo -e "\nyamllint -s ${jinja_template%.j2}"
43                 if yamllint -s "${TMPF}"; then ((pdf_yaml_pass+=1)); fi
44             else
45                 echo 'Result: FAIL'
46                 RC=1
47             fi
48             ((pdf_inst+=1))
49         done < <(find "${adapter}" -name '*.j2')
50         SUMMARY+="${pdf_yaml_pass}/${pdf_inst_pass}/${pdf_inst};"
51     done
52 done < <(find 'config' 'labs' -name 'pod*.yaml')
53
54 rm -f "${TMPF}"
55 echo -e '\n\nNOTE: tuple fmt: (valid YAML output/sucessful parse/templates).\n'
56 echo -e "${SUMMARY}" | sed -e 's/^/;/g' -e 's/;/;| /g' | column -t -s ';'
57
58 cat <<EOF
59
60 To troubleshoot PDF parsing against a specific installer adapter,
61 execute the following commands locally (e.g. for zte-pod2/joid):
62 $ ./config/utils/generate_config.py \\
63     -y labs/zte/pod2.yaml \\
64     -j config/installers/joid/pod_config.yaml.j2
65
66 EOF
67 exit "${RC}"