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