-
Notifications
You must be signed in to change notification settings - Fork 0
/
compose-dirs
executable file
·350 lines (336 loc) · 9.35 KB
/
compose-dirs
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/bash
# vim: set ts=2 sts=2 sw=2:
version=1.6.1
conf=/etc/compose-dirs.conf
[ -f "$conf" ] || exit 1
# shellcheck disable=SC1090,SC1091
. "$conf"
[ -n "$compose_dir" ] && [ -n "$compose_user" ] && [ -n "$tmpl_name" ] && [ -n "$deps_file" ] || exit 1
[ -d "$compose_dir" ] || exit 1
cd "$compose_dir"
docker="$(which docker)"
if "$docker" compose version | grep -q 'Docker Compose'; then
dc="$docker compose"
else # legacy
dc="$(which docker-compose)"
fi
usage() {
cat <<EOF
Version $version. By Cyrille Pontvieux, 2020-2023, MIT licence
compose-dirs install OPTIONS ACTION [OPTS]
OPTIONS:
-h, --help: this help message
-V, --version: show version
-v, --verbose: verbose output
ACTION:
install: install compose-dirs on the system
update [service]: update systemd unit files (or just the service provided)
start, stop, reload, restart, status [service]: do the acton on all (or only provided service) systemd unit files
CONFIG:
compose directory: $compose_dir
compose user: $compose_user
systemd base service name: [email protected]
dependencies file: $compose_dir/$deps_file
DEPS FORMAT:
compose_to_run:compose_dep_1,compose_dep_2
EOF
}
install_wait_for_cpu_idle() {
set -e
# shellcheck disable=SC2046
[ $(id -u) -eq 0 ]
mkdir -p /usr/local/bin
cat > /usr/local/bin/wait-for-cpu-idle <<'EOF'
#!/usr/bin/env -S python3 -u
from os import execvp, cpu_count, getloadavg
from random import randint
from sys import argv, exit, stderr
from time import sleep
THRESHOLD = 0.6
DELAY_SEC = 10
MAX_DELAY_SEC = 60 * 5
NB_PROC = cpu_count()
def get_cpu_load() -> float:
return getloadavg()[0] / NB_PROC
args = argv[1:]
total_delay = 0
pre_sleep = randint(DELAY_SEC // 2, 2 * DELAY_SEC)
print(f"Waiting {pre_sleep} seconds")
sleep(pre_sleep)
total_delay += pre_sleep
cpu_load = get_cpu_load()
while total_delay < MAX_DELAY_SEC and cpu_load > THRESHOLD:
print(f"Average CPU load too high ({cpu_load} > {THRESHOLD})")
print(f"Delay ({DELAY_SEC}\") starting {args}")
sleep(DELAY_SEC)
total_delay += DELAY_SEC
cpu_load = get_cpu_load()
if total_delay < MAX_DELAY_SEC:
execvp(args[0], args)
else:
print(f"Max delay of {MAX_DELAY_SEC}\" exceeded, exit with error", file=stderr)
exit(1)
EOF
chmod +x /usr/local/bin/wait-for-cpu-idle
}
install_template() {
set -e
# shellcheck disable=SC2046
[ $(id -u) -eq 0 ]
cat > "/etc/systemd/system/[email protected]" <<EOF
[Unit]
Description=Service for docker compose in %I
BindsTo=docker.service
After=docker.service
[Service]
Type=simple
ProtectSystem=yes
ProtectHome=yes
User=$compose_user
WorkingDirectory=$compose_dir/%I
StandardOutput=journal
StandardError=journal
SyslogIdentifier=compose-%i
SyslogLevel=debug
SyslogLevelPrefix=false
ExecStartPre=@/usr/local/bin/wait-for-cpu-idle compose-wait-%i echo "Ready to start %i"
EOF
if echo "$dc" | grep -q ' '; then # 'docker compose' case
cat >> "/etc/systemd/system/[email protected]" <<EOF
ExecStart=@$docker compose-%i compose up --no-color --build --remove-orphans
ExecReload=@$docker compose-reload-%i compose up --no-color --build --remove-orphans -d --wait
ExecStopPost=@$docker compose-stop-%i compose down
EOF
else # legacy 'docker-compose' case
cat >> "/etc/systemd/system/[email protected]" <<EOF
ExecStart=@$dc compose-%i up --no-color --build --remove-orphans
ExecReload=@$dc compose-reload-%i up --no-color --build --remove-orphans -d
ExecStopPost=@$dc compose-stop-%i down
EOF
fi
cat >> "/etc/systemd/system/[email protected]" <<EOF
TimeoutStartSec=infinity
RestartSec=5
TimeoutStopSec=1min
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
}
find_loaded_instances() {
systemctl list-units -q --plain --no-legend --state=loaded --type=service "$tmpl_name@*" | cut -d' ' -f1 | xargs -I @ systemd-escape -u --instance "@"
}
find_instances() {
cut -d: -f1 "$compose_dir/$deps_file"
}
find_instance_deps() {
grep "^$1:" "$compose_dir/$deps_file" | cut -d: -f2 | tr ',' '\n' | sort
}
find_instances_to_update() {
limited_instances="$1"
to_delete=""
to_update=""
loaded_instances="$(find_loaded_instances)"
instances="$(find_instances)"
if [ -n "$limited_instances" ]; then
loaded_instances=$(for inst in $loaded_instances; do echo "$limited_instances" | grep -q "^$inst\$" && echo "$inst"; done)
instances=$(for inst in $instances; do echo "$limited_instances" | grep -q "^$inst\$" && echo "$inst"; done)
fi
for i in $loaded_instances; do
if ! echo "$instances" | grep -q "^$i\$"; then
to_delete="$to_delete $i"
fi
done
for i in $instances; do
i_escaped=$(systemd-escape "$i")
defined_deps=$(find_instance_deps "$i")
actual_deps=$(systemctl list-dependencies -q --plain --no-legend "$tmpl_name@$i_escaped.service" | sed -rn "/ *$tmpl_name@/{s/ *$tmpl_name@(.*)\.service/\1/;p}" | sort)
if [ "$defined_deps" != "$actual_deps" ]; then
to_update="$to_update $i"
fi
done
echo "$to_delete:$to_update"
}
update() {
set -e
# shellcheck disable=SC2046
[ $(id -u) -eq 0 ]
instances=$(find_instances_to_update "$opts")
to_delete=$(echo "$instances" | cut -d: -f1)
to_update=$(echo "$instances" | cut -d: -f2)
for i in $to_delete; do
i_escaped=$(systemd-escape "$i")
systemctl disable --no-reload "$tmpl_name@$i_escaped.service"
confd="/etc/systemd/system/$tmpl_name@$i_escaped.service.d"
if [ -d "$confd" ]; then
rm -rf "$confd"
fi
done
for i in $to_update; do
deps=$(find_instance_deps "$i")
i_escaped=$(systemd-escape "$i")
confd="/etc/systemd/system/$tmpl_name@$i_escaped.service.d"
rm -rf "$confd" 2>/dev/null || true
if [ -n "$deps" ]; then
mkdir -p "$confd"
requires=""
afters="docker.service"
for dep in $deps; do
[ -n "$requires" ] && sep=" " || sep=""
dep_escaped=$(systemd-escape "$dep")
requires="$requires$sep$tmpl_name@$dep_escaped.service"
afters="$afters $tmpl_name@$dep_escaped.service"
done
cat > "$confd/deps.conf" <<EOF
[Unit]
Requires=$requires
After=$afters
EOF
fi
systemctl enable --no-reload "$tmpl_name@$i_escaped.service"
done
systemctl daemon-reload
}
find_order() {
python3 - "$compose_dir/$deps_file" <<'EOF'
from sys import argv
deps_file = argv[1]
lines = map(lambda l: l.rstrip(), list(open(deps_file)))
def order_deps_first(lines):
tree = dict([line.split(':') for line in lines])
prev_svc = []
new_lines = []
for svc, deps in tree.items():
if svc not in prev_svc:
if deps:
for dep in deps.split(','):
if dep not in prev_svc:
prev_svc.append(dep)
new_lines.append(':'.join([dep, tree[dep]]))
prev_svc.append(svc)
new_lines.append(':'.join([svc, deps]))
return new_lines
while True:
ordered_lines = order_deps_first(lines)
if ordered_lines == lines:
break
else:
lines = ordered_lines
for line in lines:
print(line)
EOF
}
start() {
set -e
# shellcheck disable=SC2046
[ $(id -u) -eq 0 ]
if [ -n "$opts" ]; then
for i in $opts; do
[ -n "$verbose" ] && echo "Starting $tmpl_name@$i"
systemctl start --no-block "$tmpl_name@$i.service"
done
else
for i in $(find_order | cut -d: -f1 | xargs -I @ systemd-escape "@"); do
[ -n "$verbose" ] && echo "Starting $tmpl_name@$i"
systemctl start --no-block "$tmpl_name@$i.service"
done
fi
}
stop() {
set -e
# shellcheck disable=SC2046
[ $(id -u) -eq 0 ]
if [ -n "$opts" ]; then
for i in $opts; do
[ -n "$verbose" ] && echo "Stopping $tmpl_name@$i"
systemctl stop --no-block "$tmpl_name@$i.service"
done
else
for i in $(find_order | cut -d: -f1 | xargs -I @ systemd-escape "@" | tac); do
[ -n "$verbose" ] && echo "Stopping $tmpl_name@$i"
systemctl stop --no-block "$tmpl_name@$i.service"
done
fi
}
reload() {
set -e
# shellcheck disable=SC2046
[ $(id -u) -eq 0 ]
if [ -n "$opts" ]; then
for i in $opts; do
[ -n "$verbose" ] && echo "Reloading $tmpl_name@$i"
systemctl reload --no-block "$tmpl_name@$i.service"
done
else
for i in $(find_order | cut -d: -f1 | xargs -I @ systemd-escape "@" | tac); do
[ -n "$verbose" ] && echo "Reloading $tmpl_name@$i"
systemctl reload --no-block "$tmpl_name@$i.service"
done
fi
}
status() {
if [ -n "$opts" ]; then
instances="$opts"
else
instances="$(find_instances)"
fi
[ -n "$verbose" ] && lines=10 || lines=0
for i in $instances; do
systemctl status --no-pager "--lines=$lines" -o cat "$tmpl_name@$(systemd-escape "$i").service"
if [ -n "$verbose" ]; then
(cd "$compose_dir/$i" && $dc ps)
fi
done
}
verbose=0
action=
opts=
while [ -n "$1" ]; do
arg="$1"; shift
if [ "$arg" == "-h" ] || [ "$arg" == "--help" ]; then
usage
exit 0
elif [ "$arg" == "-V" ] || [ "$arg" == "--version" ]; then
echo "$version"
exit 0
elif [ "$arg" == "-v" ] || [ "$arg" == "--verbose" ]; then
verbose=1
fi
if [ -z "$action" ]; then
action="$arg"
elif [ -z "$opts" ]; then
opts="$arg"
else
opts="$opts $arg"
fi
done
case "$action" in
install)
install_wait_for_cpu_idle
install_template
;;
update)
update
;;
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
stop
sleep 3
start
;;
status)
status
;;
*)
usage >&2
exit 1
;;
esac