@@ -4,20 +4,53 @@ set -e -u -o pipefail
4
4
hosts_file=/etc/hosts
5
5
begin_block=" # BEGIN DOCKER CONTAINERS"
6
6
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
7
17
8
18
if ! grep -Fxq " $begin_block " " $hosts_file " ; then
9
19
echo -e " \n${begin_block} \n${end_block} \n" >> " $hosts_file "
10
20
fi
11
21
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
0 commit comments