1001b1565ff31a86ee94fda1a6243eb5757e71ac
[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 echo "Using $(yamllint --version)"
21
22 # Build a table header, using ';' as column sep
23 for adapter in 'PDF Verify Matrix' ${INSTALLER_ADAPTERS}; do
24     SUMMARY+="$(basename "${adapter}");"
25 done
26
27 # Iterate all PDFs, check with each installer adapter, log results
28 while IFS= read -r lab_config; do
29     SUMMARY+="\n${lab_config#labs/};"
30     lab_nodes=$(grep -ce 'node:' "${lab_config}")
31     lab_tmacs=$(grep -ce 'mac_address:' "${lab_config}")
32     ((lab_amacs=lab_tmacs/lab_nodes)); ((lab_nodes-=1))
33     echo "###################### ${lab_config} ######################"
34     for adapter in ${INSTALLER_ADAPTERS}; do
35         pdf_inst=0
36         pdf_inst_pass=0
37         pdf_yaml_pass=0
38         ia_nodes=$(grep -hPo 'nodes\W+\K\d+' -R "${adapter}" | tail -1)
39         ia_rmacs=$(grep -hPo 'interfaces\W+\K\d+' -R "${adapter}" | sort | tail -1)
40         ((ia_nodes+=1)); ((ia_rmacs+=1))
41         if [[ ${ia_nodes} -gt ${lab_nodes} ]]; then
42             SUMMARY+='-;'
43             echo -n "[GENERATE] [SKIP] $(basename "${adapter}") requires at least"
44             echo -e " ${ia_nodes} nodes, but found only ${lab_nodes}, skipping.\n"
45             continue
46         fi
47         if [[ ${ia_rmacs} -ge ${lab_amacs} ]]; then
48             SUMMARY+='-;'
49             echo -n "[GENERATE] [SKIP] $(basename "${adapter}") requires at least"
50             echo -e " ${ia_rmacs} nics, but found ~ ${lab_amacs}, skipping.\n"
51             continue
52         fi
53         while IFS= read -r jinja_template; do
54             pdf_gen_cmd="${GEN_CFG} -y ${lab_config} -j ${jinja_template}"
55             if ${pdf_gen_cmd} > "${TMPF}"; then
56                 ((pdf_inst_pass+=1))
57                 echo "[GENERATE] [OK] ${pdf_gen_cmd}"
58                 if yamllint -s <(sed 's|ENC\[PKCS.*\]|opnfv|g' "${TMPF}"); then
59                     ((pdf_yaml_pass+=1));
60                     echo "[YAMLLINT] [OK] yamllint -s ${jinja_template%.j2}"
61                 else
62                     echo "[YAMLLINT] [ERROR] yamllint -s ${jinja_template%.j2}"
63                 fi
64             else
65                 echo "[GENERATE] [ERROR] ${pdf_gen_cmd}"
66                 RC=1
67             fi
68             ((pdf_inst+=1))
69             echo ''
70         done < <(find "${adapter}" -name '*.j2')
71         SUMMARY+="${pdf_yaml_pass}/${pdf_inst_pass}/${pdf_inst};"
72     done
73 done < <(find 'labs' -name 'pod*.yaml')
74 rm -f "${TMPF}"
75
76 cat <<EOF
77 ###################### Result Matrix ######################
78
79 NOTE: tuple fmt: (valid YAML output/sucessful parse/templates).
80
81 $(echo -e "${SUMMARY}" | sed -e 's/^/;/g' -e 's/;/;| /g' | column -t -s ';')
82
83 To troubleshoot PDF parsing against a specific installer adapter,
84 execute the following commands locally (e.g. for zte-pod2/joid):
85 $ ./config/utils/generate_config.py \\
86     -y labs/zte/pod2.yaml \\
87     -j config/installers/joid/pod_config.yaml.j2
88
89 EOF
90 exit "${RC}"