[fuel] IA: Add PXE/admin static IPs
[pharos.git] / config / utils / check-jinja2.sh
1 #!/bin/bash
2 # SPDX-license-identifier: Apache-2.0
3 ##############################################################################
4 # Copyright (c) 2018 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 # Optional filtering of test matrix: per-lab, per-pod, per-installer
16 # e.g. To check zte-pod{2,3} against all installer adapters:
17 # ./config/utils/check-jinja2.sh zte 'pod(2|3)'
18 FILTER_LAB=${1:-*}                           # e.g. 'zte'  (glob)
19 FILTER_POD=${2:-(pod|virtual)[[:digit:]]+}   # e.g. 'pod1' (regex)
20 FILTER_IA=${3:-*}                            # e.g. 'fuel' (glob)
21
22 GEN_CFG='python ./config/utils/generate_config.py'
23 INSTALLER_ADAPTERS="./config/installers/${FILTER_IA}"
24 TMPF='/tmp/out.yml' # should be outside Jenkins WS to prevent data leakage
25 RC=0
26
27 echo "Using $(yamllint --version)"
28
29 # Build a table header, using ';' as column sep
30 for adapter in 'PDF Verify Matrix' ${INSTALLER_ADAPTERS}; do
31     SUMMARY+="$(basename "${adapter}");"
32 done
33
34 # Iterate all PDFs, check with each installer adapter, log results
35 # shellcheck disable=SC2086
36 while IFS= read -r lab_config; do
37     SUMMARY+="\n${lab_config#labs/};"
38     idf_config="$(dirname "${lab_config}")/idf-$(basename "${lab_config}")"
39     idf_installer=$(grep 'installer:' "${idf_config}" 2>/dev/null || echo)
40     echo "###################### ${lab_config} ######################"
41     for adapter in ${INSTALLER_ADAPTERS}; do
42         pdf_inst=0
43         pdf_inst_pass=0
44         pdf_yaml_pass=0
45         installer_name=$(basename "${adapter}")
46         if [ -n "${idf_installer}" ] && echo "${idf_installer}" | \
47                 grep -vq "${installer_name}"; then
48             SUMMARY+='-;'
49             echo -n "[GENERATE] [SKIP] idf.installer defined and "
50             echo -e "${installer_name} not listed, 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                         "-i $(dirname "${jinja_template}")"
56             if ${pdf_gen_cmd} > "${TMPF}"; then
57                 ((pdf_inst_pass+=1))
58                 echo "[GENERATE] [OK] ${pdf_gen_cmd}"
59                 if yamllint -s <(sed 's|ENC\[PKCS.*\]|opnfv|g' "${TMPF}"); then
60                     ((pdf_yaml_pass+=1));
61                     echo "[YAMLLINT] [OK] yamllint -s ${jinja_template%.j2}"
62                 else
63                     echo "[YAMLLINT] [ERROR] yamllint -s ${jinja_template%.j2}"
64                 fi
65             else
66                 echo "[GENERATE] [ERROR] ${pdf_gen_cmd}"
67                 RC=1
68             fi
69             ((pdf_inst+=1))
70             echo ''
71         done < <(find "${adapter}" -name '*.j2')
72         SUMMARY+="${pdf_yaml_pass}/${pdf_inst_pass}/${pdf_inst};"
73     done
74 done < <(find labs/${FILTER_LAB} -regextype egrep \
75                                  -regex "labs/.+/${FILTER_POD}.yaml")
76 rm -f "${TMPF}"
77
78 cat <<EOF
79 ###################### Result Matrix ######################
80
81 NOTE: tuple fmt: (valid YAML output/sucessful parse/templates).
82
83 $(echo -e "${SUMMARY}" | sed -e 's/^/;/g' -e 's/;/;| /g' | column -t -s ';')
84
85 To troubleshoot PDF parsing against a specific installer adapter,
86 execute the following commands locally (e.g. for zte-pod2/joid):
87 $ ./config/utils/generate_config.py \\
88     -y labs/zte/pod2.yaml \\
89     -j config/installers/joid/pod_config.yaml.j2
90
91 EOF
92 exit "${RC}"