Enforce static configuration instead of DHCP
[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     -i "$(dirname "$(readlink -f "${PHAROS_IA}")")" \
54     -j "${PHAROS_IA}" -v > "${image_dir}/pod_config.yml"; then
55     notify_e "[ERROR] Could not convert PDF+IDF to reclass model input!"
56   fi
57   printenv | \
58     awk '/^(SALT|MCP|MAAS).*=/ { gsub(/=/,": "); print }' >> "${LOCAL_PDF}"
59   j2args=$(find "${scenario_dir}" -name '*.j2' -exec echo -j {} \;)
60   # shellcheck disable=SC2086
61   if ! "${PHAROS_GEN_CFG}" -y "${LOCAL_PDF}" ${j2args} -b -v \
62     -i "$(dirname "$(readlink -f "${PHAROS_IA}")")"; then
63     notify_e '[ERROR] Could not convert j2 scenario definitions!'
64   fi
65 }
66
67 # Expand reclass and virsh network templates based on PDF + IDF + others
68 function do_templates_cluster {
69   local image_dir=$1; shift
70   local target_lab=$1; shift
71   local target_pod=$1; shift
72   local git_repo_root=$1; shift
73   local extra_yaml=("$@")
74
75   RECLASS_CLUSTER_DIR=$(cd "${git_repo_root}/mcp/reclass/classes/cluster"; pwd)
76   LOCAL_PDF="${image_dir}/${target_pod}.yaml"
77
78   for _yaml in "${extra_yaml[@]}"; do
79     awk '/^---$/{f=1;next;}f' "${_yaml}" >> "${LOCAL_PDF}"
80   done
81   # shellcheck disable=SC2046
82   j2args=$(find "${RECLASS_CLUSTER_DIR}" "$(readlink -f virsh_net)" $(readlink -f ./*j2) \
83            -name '*.j2' -exec echo -j {} \;)
84   # shellcheck disable=SC2086
85   if ! "${PHAROS_GEN_CFG}" -y "${LOCAL_PDF}" ${j2args} -b -v \
86     -i "$(dirname "$(readlink -f "${PHAROS_IA}")")"; then
87     notify_e '[ERROR] Could not convert PDF to network definitions!'
88   fi
89 }