AutoDevReverseProxy/script/hostman.sh
2022-12-21 19:26:49 +01:00

53 lines
1.6 KiB
Bash
Executable file

#!/usr/bin/env sh
# Configurable Variables
DOCKER_SOCK_PATH="${DOCKER_SOCK_PATH:-/tmp/docker.sock}"
NETWORK_NAME="${NETWORK_NAME:-proxy}"
TEMPLATER_PATH="${TEMPLATER_PATH:-/docker-templater.sh}"
TEMPLATE_FOLDER_PATH="${TEMPLATE_FOLDER_PATH:-/templates}"
get_date() {
printf '%s' "$(date +'%Y.%m.%d_%H:%M:%S')"
}
query_docker () {
curl --unix-socket $DOCKER_SOCK_PATH --silent -g http://v1.41/$1$2
}
update_templates() {
CONTAINER_LIST=$(query_docker "containers/json" "?filters={%22network%22:[%22${NETWORK_NAME}%22],%22status%22:[%22running%22]}")
LABELS_NEW="$(echo "${CONTAINER_LIST}" | jq '.[].Labels | @base64' | sort)"
if [ "${LABELS_NEW}" != "${LABELS_OLD}" ]
then
if [ -n "${LABELS_OLD}" ]
then
printf '%s | Container label list change detected.\n' "$(get_date)"
fi
LABELS_OLD="${LABELS_NEW}"
for template in "${TEMPLATE_FOLDER_PATH}"/*
do
if ! "${TEMPLATER_PATH}" "${template}" "${CONTAINER_LIST}"
then
printf '%s | Error Processing Template: %s\n' "$(get_date)" "${template}"
fi
done
fi
}
# Initial Generation
update_templates
# cannot filter because reailine no longer recognized lines otherwise (check how IFS changes)
query_docker "events" | while true
do
printf '%s | Listening for docker events\n' "$(get_date)"
read -r;
# wait for related events to finish
printf '%s | Waiting for event messages to settle.\n' "$(get_date)"
while [ $? -eq 0 ]
do
read -t 8 -r
done
printf '%s | Checking for changes in container label list.\n' "$(get_date)"
update_templates
done