AutoDevReverseProxy/script/hostman.sh

55 lines
1.8 KiB
Bash
Raw Normal View History

2022-11-04 13:01:36 +01:00
#!/usr/bin/env sh
2022-11-04 13:01:36 +01:00
# Configurable Variables
DOCKER_SOCK_PATH="${DOCKER_SOCK_PATH:-/tmp/docker.sock}"
NETWORK_NAME="${NETWORK_NAME:-proxy}"
# Path to Docker Templater and Templates
TEMPLATER_PATH="/docker-templater.sh"
TEMPLATE_FOLDER_PATH="/templates"
2022-11-04 13:01:36 +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
}
update_templates() {
CONTAINER_LIST=$(query_docker "containers/json" "?filters={%22network%22:[%22${NETWORK_NAME}%22],%22status%22:[%22running%22]}")
2023-01-18 09:24:05 +01:00
LABELS_NEW="$(echo "${CONTAINER_LIST}" | jq '.[].Labels | to_entries | map(select(.key | startswith("local.")) | .key + .value) | sort | .[] | @base64')"
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}"/*
2022-11-04 13:01:36 +01:00
do
if ! "${TEMPLATER_PATH}" "${template}" "${CONTAINER_LIST}"
then
printf '%s | Error Processing Template: %s\n' "$(get_date)" "${template}"
fi
2022-11-04 13:01:36 +01:00
done
fi
2022-11-11 15:39:33 +01:00
}
2022-11-04 13:01:36 +01:00
# Initial Generation
update_templates
2022-11-17 14:51:49 +01:00
LAST_CHECK="$(date +%s)"
while true
2022-11-17 14:51:49 +01:00
do
EVENTS=""
printf '%s | Listening for docker events\n' "$(get_date)"
while [ -z "${EVENTS}" ]
2022-11-17 14:51:49 +01:00
do
NEXT_CHECK="$(($(date +%s) + 5))"
EVENTS=$(query_docker 'events' "?since=${LAST_CHECK}&until=${NEXT_CHECK}&filters={%22event%22:[%22die%22,%22kill%22,%22oom%22,%22pause%22,%22restart%22,%22start%22,%22stop%22,%22unpause%22,%22update%22]}")
LAST_CHECK="${NEXT_CHECK}"
2022-11-17 14:51:49 +01:00
done
printf '%s | Checking for changes in container label list.\n' "$(get_date)"
update_templates
2022-11-04 13:01:36 +01:00
done