AutoDevReverseProxy/script/docker-templater.sh
2023-01-05 09:48:19 +01:00

133 lines
3.5 KiB
Bash
Executable file

#!/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')"
}
# 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
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}"
exit 1
fi
else
printf '%s | [ERR]: No such File "%s".\n' "$(get_date)" "${TEMPLATE_SRC}";
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
if [ -z "${SEPARATOR+.}" ]
then
SEPARATOR='\n'
fi
for row in $(jq -r '.[] | @base64' <<< "${DOCKER_DATA}")
do
CONTAINER_DATA=$(base64 -d <<< "${row}");
# 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
# 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}")
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}"
done
done
RESULT="$(printf "%s" "${WRAP_START}" "${RESULT}" "${WRAP_END}")"
if [ -z "${OUT}" ]
then
printf "%b" "${RESULT}";
else
if [ "$(cat "${OUT}")" != "$(printf '%b'"${RESULT}")" ]
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
fi
fi