Skip to content

Commit

Permalink
docker: move docker-update-hosts restart into the script
Browse files Browse the repository at this point in the history
  • Loading branch information
saltydk committed Feb 4, 2024
1 parent b818f17 commit 64d141d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
57 changes: 45 additions & 12 deletions roles/docker/files/docker-update-hosts
Original file line number Diff line number Diff line change
Expand Up @@ -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
# 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
3 changes: 1 addition & 2 deletions roles/docker/templates/docker-update-hosts.service.j2
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 64d141d

Please sign in to comment.