AutoDevReverseProxy/script/docker-templater.sh

134 lines
3.6 KiB
Bash
Raw Normal View History

2022-12-13 17:04:48 +01:00
#!/usr/bin/env bash
# Export Variables so they can be picked up by envsubst
set -ae
TEMPLATE_SRC=$1;
DOCKER_DATA=$2;
NETWORK_NAME="${NETWORK_NAME:-proxy}"
get_date() {
printf '%s' "$(date +'%Y.%m.%d_%H:%M:%S')"
}
2022-12-13 17:04:48 +01:00
# Import Template
# Required Variables:
# - TEMPLATE :: The template string which will be read by envsubst
# Optional Variables:
# - WRAP_START :: String to prepend in front of the mapped template result
# - WRAP_END :: String to append to the mapped template result
# - SEPARATOR :: String/Charater to separate TEMPLATE lines (DEFAULT: \n)
# - OUT :: if set the result will be written to $OUT instead of stdout
# Optional Functions:
# - label_hook :: modify/override label query results
# - template_hook :: hook to verify/modify template rsults (0 => OK; 1 => SKIP)
# - finally_hook :: gets executed after (and also only if) the final result has been written to disk
2022-12-13 17:04:48 +01:00
if [ -f "${TEMPLATE_SRC}" ]
then
source "${TEMPLATE_SRC}"
if [ -z "${TEMPLATE}" ]
then
printf '%s | [ERR]: Template "%s" does define a template string.\n' "$(get_date)" "${TEMPLATE_SRC}"
2022-12-13 17:04:48 +01:00
exit 1
fi
else
printf '%s | [ERR]: No such File "%s".\n' "$(get_date)" "${TEMPLATE_SRC}";
2022-12-13 17:04:48 +01:00
exit 1
fi
# Check Hooks
if type label_hook 2>/dev/null | grep -q "label_hook is a function"
then
RUN_LABEL_HOOK=true
else
RUN_LABEL_HOOK=false
fi
if type template_hook 2>/dev/null | grep -q "template_hook is a function"
then
RUN_TEMPLATE_HOOK=true
else
RUN_TEMPLATE_HOOK=false
fi
if type finally_hook 2>/dev/null | grep -q "finally_hook is a function"
then
RUN_FINALLY_HOOK=true
else
RUN_FINALLY_HOOK=false
fi
2022-12-13 17:04:48 +01:00
if [ -z "${SEPARATOR+.}" ]
then
SEPARATOR='\n'
fi
for row in $(jq -r '.[] | @base64' <<< "${DOCKER_DATA}")
do
CONTAINER_DATA=$(base64 -d <<< "${row}");
2022-12-13 17:04:48 +01:00
# Set non Label values
if [ -z "${LOCAL_IP}" ]
then
LOCAL_IP=$(jq -r '.NetworkSettings.Networks | if has($ENV.NETWORK_NAME) then .[$ENV.NETWORK_NAME].IPAddress else first(.[].IPAddress) end' <<< "${CONTAINER_DATA}");
fi
# export LOCAL_IP
# Read Label values
CONTAINER_LABELS=$(jq '.Labels' <<< "${CONTAINER_DATA}");
for var in $(envsubst -v "${TEMPLATE}")
do
if [ -z "${!var}" ]
then
VAR_LABEL=$(tr '[:upper:]_' '[:lower:].' <<< "${var}")
LABEL_VAL=$(jq --arg KEY "${VAR_LABEL}" -r 'if has($KEY) then .[$KEY] else "" end' <<< "${CONTAINER_LABELS}")
if [ -n "${LABEL_VAL}" ]
then
declare "${var}"="${LABEL_VAL}";
fi
fi
done
if ${RUN_LABEL_HOOK}
then
label_hook
fi
PARTIAL_RESULT="$(envsubst <<< "${TEMPLATE}")";
# Verify template result if check_template is defined
if ${RUN_TEMPLATE_HOOK}
then
if template_hook
then
# FIX with printf
RESULT=$(printf '%s' "${RESULT}" "${RESULT:+$SEPARATOR}" "${PARTIAL_RESULT}")
2022-12-13 17:04:48 +01:00
fi
else
RESULT=$(printf '%s' "${RESULT}" "${RESULT:+$SEPARATOR}" "${PARTIAL_RESULT}")
fi
# Unset label-environment variables for next container
for var in $(envsubst -v "${TEMPLATE}")
do
unset "${var}"
2022-12-13 17:04:48 +01:00
done
done
RESULT="$(printf "%s" "${WRAP_START}" "${RESULT}" "${WRAP_END}")"
2022-12-13 17:04:48 +01:00
if [ -z "${OUT}" ]
then
printf "%b" "${RESULT}";
2022-12-13 17:04:48 +01:00
else
if [ "$(cat "${OUT}")" != "$(printf '%b'"${RESULT}")" ]
2022-12-13 17:04:48 +01:00
then
printf "%s | Template Task: '%s' has been written to: '%s'\n" "$(get_date)" "${TEMPLATE_SRC}" "${OUT}"
printf "%b" "${RESULT}" > "${OUT}";
if ${RUN_FINALLY_HOOK}
then
finally_hook
fi
2022-12-13 17:04:48 +01:00
fi
fi