From 64d141d8b6067b9d145c6440bacbb876305de952 Mon Sep 17 00:00:00 2001 From: saltydk Date: Sun, 4 Feb 2024 15:58:53 +0100 Subject: [PATCH] docker: move docker-update-hosts restart into the script --- roles/docker/files/docker-update-hosts | 57 +++++++++++++++---- .../templates/docker-update-hosts.service.j2 | 3 +- 2 files changed, 46 insertions(+), 14 deletions(-) diff --git a/roles/docker/files/docker-update-hosts b/roles/docker/files/docker-update-hosts index 8b53030a23..b1c4329b9b 100644 --- a/roles/docker/files/docker-update-hosts +++ b/roles/docker/files/docker-update-hosts @@ -4,20 +4,53 @@ set -e -u -o pipefail hosts_file=/etc/hosts begin_block="# BEGIN DOCKER CONTAINERS" end_block="# END DOCKER CONTAINERS" +monitor_interval=${1:-3600} # Use the first argument as the interval, with a default of 3600 seconds + +# Cleanup function to execute upon receiving a signal +cleanup() { + echo "Caught signal, exiting..." + exit 0 # Exit cleanly +} + +# Trap SIGTERM and SIGINT to call the cleanup function +trap cleanup SIGTERM SIGINT if ! grep -Fxq "$begin_block" "$hosts_file"; then echo -e "\n${begin_block}\n${end_block}\n" >> "$hosts_file" fi -(echo "| container start |" && docker events) | \ -while read event; do - if [[ "$event" == *" container start "* ]] || [[ "$event" == *" network disconnect "* ]]; then - hosts_file_tmp="$(mktemp)" - docker container ls -q | xargs -r docker container inspect | \ - jq -r '.[] | if (.NetworkSettings.Networks[].IPAddress | length > 0) then "\(.NetworkSettings.Networks[].IPAddress) \(.NetworkSettings.Networks[].Aliases | select(length > 0) | join(" ")) \(.Name | sub("^/"; "") | sub("_1$"; "") | sub("-1$"; "")).saltbox" else "# no ip address: \(.Name | sub("^/"; ""))" end' | \ - sed -ne "/^${begin_block}$/ {p; r /dev/stdin" -e ":a; n; /^${end_block}$/ {p; b}; ba}; p" "$hosts_file" \ - > "$hosts_file_tmp" - chmod 644 "$hosts_file_tmp" - mv "$hosts_file_tmp" "$hosts_file" - fi -done \ No newline at end of file +# Function to update the hosts file with current Docker containers' info +update_hosts_file() { + local hosts_file_tmp="$(mktemp)" + docker container ls -q | xargs -r docker container inspect | \ + jq -r '.[] | if (.NetworkSettings.Networks[].IPAddress | length > 0) then "\(.NetworkSettings.Networks[].IPAddress) \(.NetworkSettings.Networks[].Aliases | select(length > 0) | join(" ")) \(.Name | sub("^/"; "") | sub("_1$"; "") | sub("-1$"; "")).saltbox" else "# no ip address: \(.Name | sub("^/"; ""))" end' | \ + sed -ne "/^${begin_block}$/ {p; r /dev/stdin" -e ":a; n; /^${end_block}$/ {p; b}; ba}; p" "$hosts_file" \ + > "$hosts_file_tmp" + chmod 644 "$hosts_file_tmp" + mv "$hosts_file_tmp" "$hosts_file" +} + +# Main monitoring function +monitor_docker_events() { + # Immediately update the hosts file at the start + update_hosts_file + + # Listen for Docker events and update hosts file accordingly + docker events --format '{{json .}}' | while read event; do + update_hosts_file + done +} + +# Main loop +while true; do + # Run the monitoring in the background + monitor_docker_events & + monitor_pid=$! + + # Wait for the specified interval or until the monitoring process exits + sleep "$monitor_interval" & wait $! + + # After the interval, or if sleep is interrupted, proceed to stop the monitoring process + kill "$monitor_pid" 2>/dev/null + wait "$monitor_pid" 2>/dev/null || true # Suppress errors if the process has already exited +done diff --git a/roles/docker/templates/docker-update-hosts.service.j2 b/roles/docker/templates/docker-update-hosts.service.j2 index 7930dc5c9c..373c0e0384 100644 --- a/roles/docker/templates/docker-update-hosts.service.j2 +++ b/roles/docker/templates/docker-update-hosts.service.j2 @@ -14,8 +14,7 @@ After=docker.service PartOf=docker.service [Service] -ExecStart=/usr/local/bin/docker-update-hosts -RuntimeMaxSec={{ docker_update_hosts_service_runtime_max }} +ExecStart=/usr/local/bin/docker-update-hosts {{ docker_update_hosts_service_runtime_max }} Restart=always RestartSec=20s StartLimitInterval=0