Skip to content

Commit 64d141d

Browse files
committed
docker: move docker-update-hosts restart into the script
1 parent b818f17 commit 64d141d

File tree

2 files changed

+46
-14
lines changed

2 files changed

+46
-14
lines changed

roles/docker/files/docker-update-hosts

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,53 @@ set -e -u -o pipefail
44
hosts_file=/etc/hosts
55
begin_block="# BEGIN DOCKER CONTAINERS"
66
end_block="# END DOCKER CONTAINERS"
7+
monitor_interval=${1:-3600} # Use the first argument as the interval, with a default of 3600 seconds
8+
9+
# Cleanup function to execute upon receiving a signal
10+
cleanup() {
11+
echo "Caught signal, exiting..."
12+
exit 0 # Exit cleanly
13+
}
14+
15+
# Trap SIGTERM and SIGINT to call the cleanup function
16+
trap cleanup SIGTERM SIGINT
717

818
if ! grep -Fxq "$begin_block" "$hosts_file"; then
919
echo -e "\n${begin_block}\n${end_block}\n" >> "$hosts_file"
1020
fi
1121

12-
(echo "| container start |" && docker events) | \
13-
while read event; do
14-
if [[ "$event" == *" container start "* ]] || [[ "$event" == *" network disconnect "* ]]; then
15-
hosts_file_tmp="$(mktemp)"
16-
docker container ls -q | xargs -r docker container inspect | \
17-
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' | \
18-
sed -ne "/^${begin_block}$/ {p; r /dev/stdin" -e ":a; n; /^${end_block}$/ {p; b}; ba}; p" "$hosts_file" \
19-
> "$hosts_file_tmp"
20-
chmod 644 "$hosts_file_tmp"
21-
mv "$hosts_file_tmp" "$hosts_file"
22-
fi
23-
done
22+
# Function to update the hosts file with current Docker containers' info
23+
update_hosts_file() {
24+
local hosts_file_tmp="$(mktemp)"
25+
docker container ls -q | xargs -r docker container inspect | \
26+
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' | \
27+
sed -ne "/^${begin_block}$/ {p; r /dev/stdin" -e ":a; n; /^${end_block}$/ {p; b}; ba}; p" "$hosts_file" \
28+
> "$hosts_file_tmp"
29+
chmod 644 "$hosts_file_tmp"
30+
mv "$hosts_file_tmp" "$hosts_file"
31+
}
32+
33+
# Main monitoring function
34+
monitor_docker_events() {
35+
# Immediately update the hosts file at the start
36+
update_hosts_file
37+
38+
# Listen for Docker events and update hosts file accordingly
39+
docker events --format '{{json .}}' | while read event; do
40+
update_hosts_file
41+
done
42+
}
43+
44+
# Main loop
45+
while true; do
46+
# Run the monitoring in the background
47+
monitor_docker_events &
48+
monitor_pid=$!
49+
50+
# Wait for the specified interval or until the monitoring process exits
51+
sleep "$monitor_interval" & wait $!
52+
53+
# After the interval, or if sleep is interrupted, proceed to stop the monitoring process
54+
kill "$monitor_pid" 2>/dev/null
55+
wait "$monitor_pid" 2>/dev/null || true # Suppress errors if the process has already exited
56+
done

roles/docker/templates/docker-update-hosts.service.j2

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ After=docker.service
1414
PartOf=docker.service
1515

1616
[Service]
17-
ExecStart=/usr/local/bin/docker-update-hosts
18-
RuntimeMaxSec={{ docker_update_hosts_service_runtime_max }}
17+
ExecStart=/usr/local/bin/docker-update-hosts {{ docker_update_hosts_service_runtime_max }}
1918
Restart=always
2019
RestartSec=20s
2120
StartLimitInterval=0

0 commit comments

Comments
 (0)