Make hostman listen for docker events.

This commit is contained in:
Kevin Baensch 2022-11-17 14:51:49 +01:00
parent 2e3a1ae389
commit c5f889bb4d

View file

@ -7,17 +7,17 @@
[ -z $DOCKER_HOSTNAME_VAR ] && DOCKER_HOSTNAME_VAR="VIRTUAL_HOST"
query_docker () {
echo $(curl --unix-socket $DOCKER_SOCK_PATH --silent -g http://v1.41/$1/json$2)
curl --unix-socket $DOCKER_SOCK_PATH --silent -g http://v1.41/$1$2
}
get_host_list() {
PROXY_HOST_CONF=""
CONTAINER_LIST=$(query_docker "containers" "?filters={%22network%22:[%22${NETWORK_NAME}%22],%22status%22:[%22running%22]}" | jq -cr '.[].Id')
CONTAINER_LIST=$(query_docker "containers/json" "?filters={%22network%22:[%22${NETWORK_NAME}%22],%22status%22:[%22running%22]}" | jq -cr '.[].Id')
for id in $CONTAINER_LIST
do
# Query individual container to access relevant data
CONTAINER_DATA=$(query_docker "containers/${id}")
CONTAINER_DATA=$(query_docker "containers/${id}/json")
if $RESOLVE_LOCALHOST
then
HOST_IP=$(echo $CONTAINER_DATA | jq -cr '.NetworkSettings.Networks.proxy.IPAddress')
@ -35,9 +35,20 @@ get_host_list() {
echo $PROXY_HOST_CONF
}
while true
do
update_host_list() {
FILTERED_HOSTS=$(grep -ve "# Added by hostman$" $HOST_CONF_PATH)
echo -e "$FILTERED_HOSTS$(get_host_list)" > $HOST_CONF_PATH
sleep 10
}
update_host_list
# cannot filter because reailine no longer recognized lines otherwise (check how IFS changes)
query_docker "events" | while true
do
read -r;
# wait for related events to finish
while [ $? -eq 0 ]
do
read -t 5 -r;
done
update_host_list
done