105 lines
2.3 KiB
Plaintext
105 lines
2.3 KiB
Plaintext
|
#!/usr/bin/env bash
|
||
|
|
||
|
#
|
||
|
# Generate all the configuration files
|
||
|
# Get all the values from the VLS_DIR
|
||
|
# Process each template from the TPL_DIR with this values
|
||
|
#
|
||
|
|
||
|
ENV_FILE=${ENV_FILE:-/var/run/one-context/one_env}
|
||
|
TPL_DIR="/usr/share/builder/templates"
|
||
|
VLS_DIR="/usr/share/builder/values"
|
||
|
CONFIG=""
|
||
|
|
||
|
if [ -f "${ENV_FILE}" ]; then
|
||
|
. ${ENV_FILE}
|
||
|
fi
|
||
|
|
||
|
BTR="$(command -v btr)"
|
||
|
if [ "${?}" -ne 0 ]; then
|
||
|
echo "Warning: Nothing to do the templater is not installed"
|
||
|
exit 0
|
||
|
fi
|
||
|
|
||
|
if [ ! -e "${TPL_DIR}" ]; then
|
||
|
echo "Error: The template dir is missing (${TPL_DIR})"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
if [ ! -e "${VLS_DIR}" ]; then
|
||
|
echo "Error: The template dir is missing (${VLS_DIR})"
|
||
|
exit 1
|
||
|
fi
|
||
|
|
||
|
jsonQuery() {
|
||
|
local data="${1}"
|
||
|
local query="${2}"
|
||
|
echo "${data}" | jq -cr "${query}"
|
||
|
}
|
||
|
|
||
|
# NAME: @jsonMerge
|
||
|
# AIM: Merge two json structures
|
||
|
# NOTES:
|
||
|
# The last one has de last word
|
||
|
# if you have the same key in A and B
|
||
|
# this keeps the value of the B structure.
|
||
|
# PARAMS:
|
||
|
# $1: original JSON Structure
|
||
|
# $2: updated JSON Structure
|
||
|
jsonMerge() {
|
||
|
local data="${1}"
|
||
|
local data2="${2}"
|
||
|
|
||
|
echo "${data} ${data2}" | jq -cr -s ".[0] * .[1]"
|
||
|
}
|
||
|
|
||
|
jsonUpdateVal() {
|
||
|
local json="${1}"
|
||
|
local key="${2}"
|
||
|
local value="${3}"
|
||
|
|
||
|
echo "${json}" | jq --arg a "${value}" "${key} = \$a"
|
||
|
}
|
||
|
|
||
|
getValues() {
|
||
|
|
||
|
local values=""
|
||
|
|
||
|
for file in $(find ${VLS_DIR} -name "*.json"); do
|
||
|
values="${values}$(cat ${file})"
|
||
|
done
|
||
|
|
||
|
if [ -n "${RAW_CONFIG}" ]; then
|
||
|
values="$(jsonMerge ${values} ${RAW_CONFIG})"
|
||
|
fi
|
||
|
|
||
|
for svc in $(echo ${values} | jq -cr '.Services|keys[]'); do
|
||
|
for key in $(echo ${values} | jq -cr ".Services.${svc}.Vars|keys[]"); do
|
||
|
ukey=${key^^}
|
||
|
vkeys="$(echo ${values} | jq -cr \".Services.${svc}.Vars.${key}\|keys[]\")"
|
||
|
if [ ${?} -eq 0 ]; then
|
||
|
for var in $(echo ${values} | jq -cr ".Services.${svc}.Vars.${key}|keys[]"); do
|
||
|
uvar=${var^^}
|
||
|
val=$(eval echo "\$${ukey}_${uvar}")
|
||
|
if [ -n "${val}" ]; then
|
||
|
values=$(jsonUpdateVal "${values}" ".Services.${svc}.Vars.${key}.${var}" "${val}")
|
||
|
fi
|
||
|
done
|
||
|
else
|
||
|
values=$(jsonUpdateVal "${values}" ".Services.${svc}.Vars.${key}" "${!ukey}")
|
||
|
fi
|
||
|
done
|
||
|
done
|
||
|
echo ${values}
|
||
|
}
|
||
|
|
||
|
processTemplates() {
|
||
|
${BTR} -t ${TPL_DIR} -c "${1}"
|
||
|
}
|
||
|
|
||
|
VALUES=$(getValues)
|
||
|
file=$(mktemp)
|
||
|
echo "${VALUES}" > "${file}"
|
||
|
processTemplates "${file}"
|
||
|
rm -rf "${file}"
|