Merge "[submodule] Bump Pharos for maas:machines sync"
[fuel.git] / mcp / scripts / lib_template.sh
1 #!/bin/bash -e
2 # shellcheck disable=SC2155,SC1001,SC2015,SC2128
3 ##############################################################################
4 # Copyright (c) 2018 Mirantis Inc., Enea AB 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 # Library of shell functions dedicated to j2 template handling
12 #
13
14 PHAROS_GEN_CFG='./pharos/config/utils/generate_config.py'
15 PHAROS_IA='./pharos/config/installers/fuel/pod_config.yml.j2'
16 PHAROS_VALIDATE_SCHEMA_SCRIPT='./pharos/config/utils/validate_schema.py'
17 PHAROS_SCHEMA_PDF='./pharos/config/pdf/pod1.schema.yaml'
18 PHAROS_SCHEMA_IDF='./pharos/config/pdf/idf-pod1.schema.yaml'
19
20 # Handles pod_config and scenarios only
21 function do_templates_scenario {
22   local image_dir=$1; shift
23   local target_lab=$1; shift
24   local target_pod=$1; shift
25   local lab_config_uri=$1; shift
26   local scenario_dir=$1; shift
27   local extra_yaml=("$@")
28
29   BASE_CONFIG_PDF="${lab_config_uri}/labs/${target_lab}/${target_pod}.yaml"
30   BASE_CONFIG_IDF="${lab_config_uri}/labs/${target_lab}/idf-${target_pod}.yaml"
31   LOCAL_PDF="${image_dir}/$(basename "${BASE_CONFIG_PDF}")"
32   LOCAL_IDF="${image_dir}/$(basename "${BASE_CONFIG_IDF}")"
33
34   # Make sample PDF/IDF available via default lab-config (pharos submodule)
35   ln -sf "$(readlink -f "../config/labs/local")" "./pharos/labs/"
36
37   # Expand scenario file and main reclass input (pod_config.yaml) based on PDF
38   if ! curl --create-dirs -o "${LOCAL_PDF}" "${BASE_CONFIG_PDF}"; then
39     notify_e "[ERROR] Could not retrieve PDF (Pod Descriptor File)!"
40   elif ! curl -o "${LOCAL_IDF}" "${BASE_CONFIG_IDF}"; then
41     notify_e "[ERROR] Could not retrieve IDF (Installer Descriptor File)!"
42   fi
43   # Check first if configuration files are valid
44   if [[ ! "$target_pod" =~ "virtual" ]]; then
45     if ! "${PHAROS_VALIDATE_SCHEMA_SCRIPT}" -y "${LOCAL_PDF}" \
46       -s "${PHAROS_SCHEMA_PDF}"; then
47       notify_e "[ERROR] PDF does not match yaml schema!"
48     elif ! "${PHAROS_VALIDATE_SCHEMA_SCRIPT}" -y "${LOCAL_IDF}" \
49       -s "${PHAROS_SCHEMA_IDF}"; then
50       notify_e "[ERROR] IDF does not match yaml schema!"
51     fi
52   fi
53   for _yaml in "${extra_yaml[@]}"; do
54     awk '/^---$/{f=1;next;}f' "${_yaml}" >> "${LOCAL_PDF}"
55   done
56   if ! "${PHAROS_GEN_CFG}" -y "${LOCAL_PDF}" \
57     -i "$(dirname "$(readlink -f "${PHAROS_IA}")")" \
58     -j "${PHAROS_IA}" -v > "${image_dir}/pod_config.yml"; then
59     notify_e "[ERROR] Could not convert PDF+IDF to reclass model input!"
60   fi
61   printenv | \
62     awk '/^(SALT|MCP|MAAS).*=/ { gsub(/=/,": "); print }' >> "${LOCAL_PDF}"
63   j2args=$(find "${scenario_dir}" -name '*.j2' -exec echo -j {} \;)
64   # shellcheck disable=SC2086
65   if ! "${PHAROS_GEN_CFG}" -y "${LOCAL_PDF}" ${j2args} -b -v \
66     -i "$(dirname "$(readlink -f "${PHAROS_IA}")")"; then
67     notify_e '[ERROR] Could not convert j2 scenario definitions!'
68   fi
69 }
70
71 # Expand reclass and virsh network templates based on PDF + IDF + others
72 function do_templates_cluster {
73   local image_dir=$1; shift
74   local target_lab=$1; shift
75   local target_pod=$1; shift
76   local git_repo_root=$1; shift
77   local extra_yaml=("$@")
78
79   RECLASS_CLUSTER_DIR=$(cd "${git_repo_root}/mcp/reclass/classes/cluster"; pwd)
80   LOCAL_PDF="${image_dir}/${target_pod}.yaml"
81
82   for _yaml in "${extra_yaml[@]}"; do
83     awk '/^---$/{f=1;next;}f' "${_yaml}" >> "${LOCAL_PDF}"
84   done
85   # shellcheck disable=SC2046
86   j2args=$(find "${RECLASS_CLUSTER_DIR}" "$(readlink -f virsh_net)" \
87            "$(readlink -f docker-compose)" $(readlink -f ./*j2) \
88            -name '*.j2' -exec echo -j {} \;)
89   # shellcheck disable=SC2086
90   if ! "${PHAROS_GEN_CFG}" -y "${LOCAL_PDF}" ${j2args} -b -v \
91     -i "$(dirname "$(readlink -f "${PHAROS_IA}")")"; then
92     notify_e '[ERROR] Could not convert PDF to network definitions!'
93   fi
94 }