-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker-lifecycle-listener.sh
executable file
·198 lines (162 loc) · 3.6 KB
/
docker-lifecycle-listener.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#! /usr/bin/env bash
valid_commands=("start" "stop")
main() {
IFS=$'\n\t'
set -euo pipefail
local script_dir=${1:?'You must pass a script directory'}
local port=${2:-47200}
check_all_directory_permissions
if docker_running; then
run_on start "$script_dir"
else
log "Docker not running at the moment"
fi
trap 'cleanup; log Stopped; exit 0' HUP INT TERM
log "Listening for commands on port $port"
while read -r command; do
run_command "$command"
done < <(nc -kl "$port")
log 'Exiting'
}
run_command() {
local command=$1
if is_valid_command "$command"; then
run_on "$command" "$script_dir"
else
unknown "$command"
fi
}
is_valid_command() {
local command=$1
contains_element "$command" "${valid_commands[@]}"
}
unknown() {
local cmd=$1
log "Ignoring unknown command [$cmd]; accepts start and stop"
}
check_all_directory_permissions() {
check_directory_permissions "$script_dir"
for command in "${valid_commands[@]}"; do
check_directory_permissions "$script_dir/on_$command"
done
}
check_directory_permissions() {
local dir=$1
if [ -e "$dir" ] && ! permissions_are_ok_for "$dir"; then
error "$dir must be owned by the root user & only writable by the root user for this script to be run as root"
return 1
fi
}
run_on() {
local command="$1"
local script_dir="$2"
log "Handling $command"
local dir; dir="$script_dir/on_$command/"
if [ -d "$dir" ]; then
if ! is_empty_dir "$dir"; then
for file in "$dir"/*; do
run_if_possible "$file"
done
else
log "$dir is empty"
fi
else
log "$dir does not exist"
fi
log "Finished handling $command"
}
run_if_possible() {
local possible_script=$1
if [ -x "$possible_script" ]; then
if permissions_are_ok_for "$possible_script"; then
run_script "$possible_script"
else
log "Skipping $(basename "$possible_script"); must be owned by root and not writable by anyone else"
fi
else
log "Skipping $(basename "$possible_script"); not executable"
fi
}
run_script() {
local script=$1
local script_name; script_name="$(basename "$script")"
log "Running $script_name"
if "$script"; then
log "$script_name Succeeded"
else
log "$script_name Failed"
fi
}
permissions_are_ok_for() {
local file=$1
if running_as_root; then
root_is_owner_of "$file" && only_owner_can_write_to "$file"
fi
}
running_as_root() {
[ "$(id -u)" -eq 0 ]
}
root_is_owner_of() {
local file=$1
[ "$(owner_of "$file")" -eq 0 ]
}
owner_of() {
local file=$1
if os_is_macOS; then
stat -L -f '%u' "$file"
else
stat -L -c '%u' "$file"
fi
}
only_owner_can_write_to() {
local file=$1
! [[ "$(group_other_permissions "$file")" =~ w ]]
}
group_other_permissions() {
local file=$1
if os_is_macOS; then
stat -L -f "%SMp%SLp" "$file"
else
local all_permissions; all_permissions=$(stat -L -c "%A" "$file")
echo "${all_permissions: -6}"
fi
}
os_is_macOS() {
test "$(uname -s)" = 'Darwin'
}
docker_running() {
docker info 1>/dev/null 2>&1
}
log() {
echo "$(date +%Y-%m-%dT%H:%M:%S%z) $1"
}
error() {
log >&2 "$1"
}
contains_element() {
local item="$1"
shift
local array=("$@")
for array; do [[ "$array" == "$item" ]] && return 0; done
return 1
}
is_empty_dir() {
local dir="$1"
[ -z "$(ls -A "$dir")" ]
}
cleanup() {
set +e
kill_descendants $$
set -e
}
kill_descendants() {
declare children; children=$(pgrep -P "$1")
for i in $children; do
declare child="$i"
kill_descendants "$child"
kill "$child" 2>/dev/null
done
}
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
main "$@"
fi