From c5f889bb4d10a260440796246a74190ad13431d5 Mon Sep 17 00:00:00 2001 From: Kevin Baensch Date: Thu, 17 Nov 2022 14:51:49 +0100 Subject: [PATCH] Make hostman listen for docker events. --- script/hostman.sh | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/script/hostman.sh b/script/hostman.sh index 634b22f..4b9e9d9 100755 --- a/script/hostman.sh +++ b/script/hostman.sh @@ -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