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