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}"
|
|
|
|
|
|
|
|
# "NetworkSettings"
|
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
if [ -f "${TEMPLATE_SRC}" ]
|
|
|
|
then
|
|
|
|
source "${TEMPLATE_SRC}"
|
|
|
|
if [ -z "${TEMPLATE}" ]
|
|
|
|
then
|
|
|
|
echo "[ERR]: Template \"${TEMPLATE_SRC}\" does define a template string."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "[ERR]: No such File \"${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 [ -z "${SEPARATOR+.}" ]
|
|
|
|
then
|
|
|
|
SEPARATOR='\n'
|
|
|
|
fi
|
|
|
|
|
|
|
|
for row in $(jq -r '.[] | @base64' <<< "${DOCKER_DATA}")
|
|
|
|
do
|
2022-12-16 14:17:11 +01:00
|
|
|
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
|
2022-12-16 14:17:11 +01:00
|
|
|
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
|
2022-12-16 14:17:11 +01:00
|
|
|
unset "${var}"
|
2022-12-13 17:04:48 +01:00
|
|
|
done
|
|
|
|
done
|
|
|
|
|
2022-12-16 14:17:11 +01:00
|
|
|
RESULT="$(printf "%b" "${WRAP_START}" "${RESULT}" "${WRAP_END}")"
|
2022-12-13 17:04:48 +01:00
|
|
|
|
|
|
|
if [ -z "${OUT}" ]
|
|
|
|
then
|
2022-12-16 14:17:11 +01:00
|
|
|
printf "%b" "${RESULT}";
|
2022-12-13 17:04:48 +01:00
|
|
|
else
|
2022-12-16 14:17:11 +01:00
|
|
|
if [ "$(cat "${OUT}")" != "${RESULT}" ]
|
2022-12-13 17:04:48 +01:00
|
|
|
then
|
2022-12-16 14:17:11 +01:00
|
|
|
printf "%s | Template Task: '%s' has been written to: '%s'\n" "$(date --iso-8601)" "${TEMPLATE_SRC}" "${OUT}"
|
|
|
|
printf "%b" "${RESULT}" > "${OUT}";
|
2022-12-13 17:04:48 +01:00
|
|
|
fi
|
|
|
|
fi
|