0fbe628b77d58740a594730ca73ace28312fd702
[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
27
28   BASE_CONFIG_PDF="${lab_config_uri}/labs/${target_lab}/${target_pod}.yaml"
29   BASE_CONFIG_IDF="${lab_config_uri}/labs/${target_lab}/idf-${target_pod}.yaml"
30   LOCAL_PDF="${image_dir}/$(basename "${BASE_CONFIG_PDF}")"
31   LOCAL_IDF="${image_dir}/$(basename "${BASE_CONFIG_IDF}")"
32
33   # Make sample PDF/IDF available via default lab-config (pharos submodule)
34   ln -sf "$(readlink -f "../config/labs/local")" "./pharos/labs/"
35
36   # Expand scenario file and main reclass input (pod_config.yaml) based on PDF
37   if ! curl --create-dirs -o "${LOCAL_PDF}" "${BASE_CONFIG_PDF}"; then
38     notify_e "[ERROR] Could not retrieve PDF (Pod Descriptor File)!"
39   elif ! curl -o "${LOCAL_IDF}" "${BASE_CONFIG_IDF}"; then
40     notify_e "[ERROR] Could not retrieve IDF (Installer Descriptor File)!"
41   fi
42   # Check first if configuration files are valid
43   if [[ ! "$target_pod" =~ "virtual" ]]; then
44     if ! "${PHAROS_VALIDATE_SCHEMA_SCRIPT}" -y "${LOCAL_PDF}" \
45       -s "${PHAROS_SCHEMA_PDF}"; then
46       notify_e "[ERROR] PDF does not match yaml schema!"
47     elif ! "${PHAROS_VALIDATE_SCHEMA_SCRIPT}" -y "${LOCAL_IDF}" \
48       -s "${PHAROS_SCHEMA_IDF}"; then
49       notify_e "[ERROR] IDF does not match yaml schema!"
50     fi
51   fi
52   if ! "${PHAROS_GEN_CFG}" -y "${LOCAL_PDF}" \
53     -j "${PHAROS_IA}" -v > "${image_dir}/pod_config.yml"; then
54     notify_e "[ERROR] Could not convert PDF+IDF to reclass model input!"
55   fi
56   printenv | \
57     awk '/^(SALT|MCP|MAAS).*=/ { gsub(/=/,": "); print }' >> "${LOCAL_PDF}"
58   j2args=$(find "${scenario_dir}" -name '*.j2' -exec echo -j {} \;)
59   # shellcheck disable=SC2086
60   if ! "${PHAROS_GEN_CFG}" -y "${LOCAL_PDF}" ${j2args} -b -v \
61     -i "$(dirname "$(readlink -f "${PHAROS_IA}")")"; then
62     notify_e '[ERROR] Could not convert j2 scenario definitions!'
63   fi
64 }
65
66 # Expand reclass and virsh network templates based on PDF + IDF + others
67 function do_templates_cluster {
68   local image_dir=$1; shift
69   local target_lab=$1; shift
70   local target_pod=$1; shift
71   local git_repo_root=$1; shift
72   local extra_yaml=("$@")
73
74   RECLASS_CLUSTER_DIR=$(cd "${git_repo_root}/mcp/reclass/classes/cluster"; pwd)
75   LOCAL_PDF="${image_dir}/${target_pod}.yaml"
76
77   for _yaml in "${extra_yaml[@]}"; do
78     awk '/^---$/{f=1;next;}f' "${_yaml}" >> "${LOCAL_PDF}"
79   done
80   # shellcheck disable=SC2046
81   j2args=$(find "${RECLASS_CLUSTER_DIR}" "$(readlink -f virsh_net)" $(readlink -f ./*j2) \
82            -name '*.j2' -exec echo -j {} \;)
83   # shellcheck disable=SC2086
84   if ! "${PHAROS_GEN_CFG}" -y "${LOCAL_PDF}" ${j2args} -b -v \
85     -i "$(dirname "$(readlink -f "${PHAROS_IA}")")"; then
86     notify_e '[ERROR] Could not convert PDF to network definitions!'
87   fi
88 }