-
Notifications
You must be signed in to change notification settings - Fork 15
/
entrypoint.sh
executable file
·476 lines (423 loc) · 13.9 KB
/
entrypoint.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/env bash
set -e
if [ "${DEBUG}" == 'true' ]; then
set -x
fi
_term() {
# shellcheck disable=SC2317
_echo "Caught SIGTERM signal!"
# shellcheck disable=SC2317
_cleanup
}
_cleanup(){
_echo "Cleaning up"
docker compose --project-name "${LAUNCH_PROJECT_NAME}" down --remove-orphans
exit $?
}
_startup_check(){
_echo "Doing startup check"
# Check that no conflicting options are passed and display warning
if [ "${LAUNCH_HOST_NETWORK}" == true ] || [ -n "${LAUNCH_NETWORK_MODE}" ]; then
if [ "${LAUNCH_HOST_NETWORK}" == true ] && [ -n "${LAUNCH_NETWORK_MODE}" ]; then
_echo "WARNING! LAUNCH_HOST_NETWORK is set. Ignoring LAUNCH_NETWORK_MODE"
fi
if [ -n "${LAUNCH_PORTS}" ]; then
_echo "WARNING! LAUNCH_HOST_NETWORK or LAUNCH_NETWORK_MODE is set. Ignoring LAUNCH_PORTS"
fi
if [ -n "${LAUNCH_NETWORKS}" ]; then
_echo "WARNING! LAUNCH_HOST_NETWORK or LAUNCH_NETWORK_MODE is set. Ignoring LAUNCH_NETWORKS"
fi
if [ -n "${LAUNCH_EXT_NETWORKS}" ]; then
_echo "WARNING! LAUNCH_HOST_NETWORK or LAUNCH_NETWORK_MODE is set. Ignoring LAUNCH_EXT_NETWORKS"
fi
else
# Check if the networks are attachable
if [ -n "${LAUNCH_EXT_NETWORKS}" ]; then
for NETWORK in ${LAUNCH_EXT_NETWORKS}; do
ATTACHABLE=$(docker network inspect "${NETWORK}"|jq -r ".[].Attachable")
if [ -z "${ATTACHABLE}" ]; then
_echo "ERROR! Network ${NETWORK} does not exist. Exiting."
NEED_EXIT=true
fi
if [ "${ATTACHABLE}" == 'false' ]; then
_echo "ERROR! Network '${NETWORK}' is not attachable. Exiting."
NEED_EXIT=true
fi
done
fi
fi
if [ -n "${LAUNCH_ARG_ENVFILE}" ]; then
if [ ! -f "${LAUNCH_ARG_ENVFILE}" ]; then
_echo "ERROR! LAUNCH_ARG_ENVFILE is set but ${LAUNCH_ARG_ENVFILE} cannot be found. Exiting."
fi
fi
# Check if there's a need to exit now
if [ "${NEED_EXIT}" == true ]; then
exit 1
fi
}
_echo(){
echo "swarm-launcher: $*"
}
COMPOSE_FILE="/docker-compose.yml"
CREATE_COMPOSE_FILE=true
if [ -f "${COMPOSE_FILE}" ]; then
_echo "Detected mounted docker-compose.yml file"
LAUNCH_VARIABLES=(
'LAUNCH_IMAGE'
'LAUNCH_PROJECT_NAME'
'LAUNCH_SERVICE_NAME'
'LAUNCH_CONTAINER_NAME'
'LAUNCH_PRIVILEGED'
'LAUNCH_HOSTNAME'
'LAUNCH_ENVIRONMENTS'
'LAUNCH_ENVFILES'
'LAUNCH_DEVICES'
'LAUNCH_VOLUMES'
'LAUNCH_HOST_NETWORK'
'LAUNCH_PORTS'
'LAUNCH_NETWORKS'
'LAUNCH_NETWORK_MODE'
'LAUNCH_EXT_NETWORKS'
'LAUNCH_EXT_NETWORKS_IPV4'
'LAUNCH_EXT_NETWORKS_IPV6'
'LAUNCH_EXT_NETWORKS_MIXED'
'LAUNCH_CAP_ADD'
'LAUNCH_CAP_DROP'
'LAUNCH_SECURITY_OPT'
'LAUNCH_SHM_SIZE'
'LAUNCH_LABELS'
'LAUNCH_PULL'
'LAUNCH_SYSCTLS'
'LAUNCH_COMMAND'
'LAUNCH_CGROUP_PARENT'
'LAUNCH_STOP_GRACE_PERIOD'
'LAUNCH_PID_MODE'
'LAUNCH_ULIMITS'
'LAUNCH_EXTRA_HOSTS'
'LAUNCH_DNS'
'LAUNCH_DNS_SEARCH'
)
for LAUNCH_VARIABLE in "${LAUNCH_VARIABLES[@]}"; do
if [ -n "${!LAUNCH_VARIABLE}" ]; then
_echo "WARNING: ${LAUNCH_VARIABLE} is set, but a docker-compose.yml file has been provided. ${LAUNCH_VARIABLE} will be ignored!"
fi
done
CREATE_COMPOSE_FILE=false
fi
# creates a docker-compose.yml file
if [ "${CREATE_COMPOSE_FILE}" == "true" ]; then
_startup_check
# exits if there's no LAUNCH_IMAGE set
if [ -z "${LAUNCH_IMAGE}" ]; then
_echo "LAUNCH_IMAGE is not set! Exiting!"
exit 1
fi
if [ -z "${LAUNCH_PROJECT_NAME}" ]; then
LAUNCH_PROJECT_NAME="sl-$(tr -cd '[:alnum:]' < /dev/urandom | fold -w6 | head -n1)"
fi
# sets a default name for the service
if [ -z "${LAUNCH_SERVICE_NAME}" ]; then
LAUNCH_SERVICE_NAME="$(tr -cd '[:alnum:]' < /dev/urandom | fold -w6 | head -n1)"
fi
cat <<xEOF > "${COMPOSE_FILE}"
version: "3.8"
services:
"${LAUNCH_SERVICE_NAME}":
image: "${LAUNCH_IMAGE}"
restart: "no"
labels:
- "ai.ix.started-by=ix.ai/swarm-launcher"
xEOF
# additional labels for the container
if [ -n "${LAUNCH_LABELS}" ]; then
for LABEL in ${LAUNCH_LABELS}; do
echo " - \"${LABEL}\"" >> "${COMPOSE_FILE}"
done
fi
# name the container
if [ -n "${LAUNCH_CONTAINER_NAME}" ]; then
echo " container_name: \"${LAUNCH_CONTAINER_NAME}\"" >> "${COMPOSE_FILE}"
fi
# set container hostname
if [ -n "${LAUNCH_HOSTNAME}" ]; then
echo " hostname: \"${LAUNCH_HOSTNAME}\"" >> "${COMPOSE_FILE}"
fi
# run in privileged mode
if [ "${LAUNCH_PRIVILEGED}" = true ]; then
echo " privileged: true" >> "${COMPOSE_FILE}"
fi
# specify an optional parent cgroup for the container
if [ "${LAUNCH_CGROUP_PARENT}" = true ]; then
echo " cgroup_parent: ${LAUNCH_CGROUP_PARENT}" >> "${COMPOSE_FILE}"
fi
# the environment variables
if [ -n "${LAUNCH_ENVIRONMENTS}" ]; then
echo " environment:" >> "${COMPOSE_FILE}"
read -ra ARR <<<"${LAUNCH_ENVIRONMENTS}"
for ENVIRONMENT in "${ARR[@]}"; do
echo " - ${ENVIRONMENT//@_@/' '}" >> "${COMPOSE_FILE}"
done
fi
# the environment files
if [ -n "${LAUNCH_ENVFILES}" ]; then
echo " env_file:" >> "${COMPOSE_FILE}"
read -ra ARR <<<"${LAUNCH_ENVFILES}"
for ENVFILE in "${ARR[@]}"; do
echo " - ${ENVFILE}" >> "${COMPOSE_FILE}"
done
fi
# devices
if [ -n "${LAUNCH_DEVICES}" ]; then
echo " devices:" >> "${COMPOSE_FILE}"
for DEVICE in ${LAUNCH_DEVICES}; do
echo " - ${DEVICE}" >> "${COMPOSE_FILE}"
done
fi
# the volumes
if [ -n "${LAUNCH_VOLUMES}" ]; then
echo " volumes:" >> "${COMPOSE_FILE}"
for VOLUME in ${LAUNCH_VOLUMES}; do
echo " - ${VOLUME}" >> "${COMPOSE_FILE}"
done
fi
# Add capabilities
if [ -n "${LAUNCH_CAP_ADD}" ]; then
echo " cap_add:" >> "${COMPOSE_FILE}"
for CAP in ${LAUNCH_CAP_ADD}; do
echo " - ${CAP}" >> "${COMPOSE_FILE}"
done
fi
# Drop capabilities
if [ -n "${LAUNCH_CAP_DROP}" ]; then
echo " cap_drop:" >> "${COMPOSE_FILE}"
for CAP in ${LAUNCH_CAP_DROP}; do
echo " - ${CAP}" >> "${COMPOSE_FILE}"
done
fi
# Security opt
if [ -n "${LAUNCH_SECURITY_OPT}" ]; then
echo " security_opt:" >> "${COMPOSE_FILE}"
for SECURITY_OPT in ${LAUNCH_SECURITY_OPT}; do
echo " - ${SECURITY_OPT}" >> "${COMPOSE_FILE}"
done
fi
# sysctls
if [ -n "${LAUNCH_SYSCTLS}" ]; then
echo " sysctls:" >> "${COMPOSE_FILE}"
for SYSCTL in ${LAUNCH_SYSCTLS}; do
echo " - ${SYSCTL}" >> "${COMPOSE_FILE}"
done
fi
# Override the command
if [ -n "${LAUNCH_COMMAND}" ]; then
echo " command: \"${LAUNCH_COMMAND}\"" >> "${COMPOSE_FILE}"
fi
# stop grace period
if [ -n "${LAUNCH_STOP_GRACE_PERIOD}" ]; then
echo " stop_grace_period: ${LAUNCH_STOP_GRACE_PERIOD}" >> "${COMPOSE_FILE}"
fi
# stop grace period
if [ "${LAUNCH_PID_MODE:-}x" = "hostx" ]; then
echo " pid: host" >> "${COMPOSE_FILE}"
fi
# ulimits
if [ -n "${LAUNCH_ULIMITS}" ]; then
echo " ulimits:" >> "${COMPOSE_FILE}"
read -ra ARR <<<"${LAUNCH_ULIMITS}"
for ULIMIT in "${ARR[@]}"; do
IFS='=' read -r ULIMIT_KEY ULIMIT_VALUE <<< "${ULIMIT}"
echo " ${ULIMIT_KEY}: ${ULIMIT_VALUE}" >> "${COMPOSE_FILE}"
done
fi
# extra_hosts
if [ -n "${LAUNCH_EXTRA_HOSTS}" ]; then
echo " extra_hosts:" >> "${COMPOSE_FILE}"
for EXTRA_HOST in ${LAUNCH_EXTRA_HOSTS}; do
echo " - \"${EXTRA_HOST}\"" >> "${COMPOSE_FILE}"
done
fi
# Custom DNS
if [ -n "${LAUNCH_DNS}" ]; then
echo " dns:" >> "${COMPOSE_FILE}"
for DNS in ${LAUNCH_DNS}; do
echo " - \"${DNS}\"" >> "${COMPOSE_FILE}"
done
fi
# Custom DNS Search domains
if [ -n "${LAUNCH_DNS_SEARCH}" ]; then
echo " dns_search:" >> "${COMPOSE_FILE}"
for DOMAIN in ${LAUNCH_DNS_SEARCH}; do
echo " - ${DOMAIN}" >> "${COMPOSE_FILE}"
done
fi
# Specific Mac Address for the container
if [ -n "${LAUNCH_MAC_ADDRESS}" ]; then
echo " mac_address: ${LAUNCH_MAC_ADDRESS}" >> "${COMPOSE_FILE}"
fi
# SHM size
if [ -n "${LAUNCH_SHM_SIZE}" ]; then
echo " shm_size: '${LAUNCH_SHM_SIZE}'" >> "${COMPOSE_FILE}"
fi
# run on the host network - it's incompatible with ports or with named networks
if [ "${LAUNCH_HOST_NETWORK}" = true ]; then
echo " network_mode: host" >> "${COMPOSE_FILE}"
elif [ -n "${LAUNCH_NETWORK_MODE}" ]; then
echo " network_mode: ${LAUNCH_NETWORK_MODE}" >>"${COMPOSE_FILE}"
else
if [ -n "${LAUNCH_PORTS}" ]; then
echo " ports:" >> "${COMPOSE_FILE}"
for PORT in ${LAUNCH_PORTS}; do
echo " - \"$PORT\"" >> "${COMPOSE_FILE}"
done
fi
##
# The four major network variables:
# LAUNCH_NETWORKS
# LAUNCH_EXT_NETWORKS
# LAUNCH_EXT_NETWORKS_IPV4
# LAUNCH_EXT_NETWORKS_IPV6
# LAUNCH_EXT_NETWORKS_MIXED
#
# Keep them separated for better readability and easier treoubleshooting, at the cost of code duplication
##
if \
[ -n "${LAUNCH_NETWORKS}" ] || \
[ -n "${LAUNCH_EXT_NETWORKS}" ] || \
[ -n "${LAUNCH_EXT_NETWORKS_IPV4}" ] || \
[ -n "${LAUNCH_EXT_NETWORKS_IPV6}" ] || \
[ -n "${LAUNCH_EXT_NETWORKS_MIXED}" ] \
; then
# LAUNCH_NETWORKS are networks that get created on the fly, at start
echo " networks:" >> "${COMPOSE_FILE}"
for NETWORK in ${LAUNCH_NETWORKS}; do
echo " ${NETWORK}:" >> "${COMPOSE_FILE}"
done
# LAUNCH_EXT_NETWORKS are existing attachable networks
for NETWORK in ${LAUNCH_EXT_NETWORKS}; do
echo " ${NETWORK}:" >> "${COMPOSE_FILE}"
done
# LAUNCH_EXT_NETWORKS_IPV4 are existing attachable networks, where the IP is manually assigned
# The format is `network1:ip1 network2:ip2 ... networkN:ipN`
if [ -n "${LAUNCH_EXT_NETWORKS_IPV4}" ]; then
read -ra ARR <<<"${LAUNCH_EXT_NETWORKS_IPV4}"
for NETWORK in "${ARR[@]}"; do
IFS=':' read -r NETWORK IPV4 <<< "${NETWORK}"
{
echo " ${NETWORK}:"
echo " ipv4_address: '${IPV4}'"
} >> "${COMPOSE_FILE}"
done
fi
# LAUNCH_EXT_NETWORKS_IPV6 are existing attachable networks, where the IP is manually assigned
# The format is `network1-ip1 network2-ip2 ... networkN-ipN`
if [ -n "${LAUNCH_EXT_NETWORKS_IPV6}" ]; then
read -ra ARR <<<"${LAUNCH_EXT_NETWORKS_IPV6}"
for NETWORK in "${ARR[@]}"; do
IFS='-' read -r NETWORK IPV6 <<< "${NETWORK}"
{
echo " ${NETWORK}:"
echo " ipv6_address: '${IPV6}'"
} >> "${COMPOSE_FILE}"
done
fi
# LAUNCH_EXT_NETWORKS_MIXED are existing attachable networks, where the IPV4 and IPV6 is manually assigned
# The format is `network1-ipv4-ipv6 network2-ipv4-ipv6 ... networkN-ipv4-ipv6`
if [ -n "${LAUNCH_EXT_NETWORKS_MIXED}" ]; then
read -ra ARR <<<"${LAUNCH_EXT_NETWORKS_MIXED}"
for NETWORK in "${ARR[@]}"; do
IFS='-' read -r NETWORK IPV4 IPV6 <<< "${NETWORK}"
{
echo " ${NETWORK}:"
echo " ipv4_address: '${IPV4}'"
echo " ipv6_address: '${IPV6}'"
} >> "${COMPOSE_FILE}"
done
fi
# Here starts the `networks` section
echo "networks:" >> "${COMPOSE_FILE}"
for NETWORK in ${LAUNCH_NETWORKS}; do
{
echo " ${NETWORK}:";
echo " driver: bridge";
echo " attachable: true";
} >> "${COMPOSE_FILE}"
done
for NETWORK in ${LAUNCH_EXT_NETWORKS}; do
{
echo " ${NETWORK}:";
echo " external: true";
} >> "${COMPOSE_FILE}"
done
if [ -n "${LAUNCH_EXT_NETWORKS_IPV4}" ]; then
read -ra ARR <<<"${LAUNCH_EXT_NETWORKS_IPV4}"
for NETWORK in "${ARR[@]}"; do
IFS=':' read -r NETWORK IPV4 <<< "${NETWORK}"
{
echo " ${NETWORK}:";
echo " external: true";
} >> "${COMPOSE_FILE}"
done
fi
if [ -n "${LAUNCH_EXT_NETWORKS_IPV6}" ]; then
read -ra ARR <<<"${LAUNCH_EXT_NETWORKS_IPV6}"
for NETWORK in "${ARR[@]}"; do
IFS='-' read -r NETWORK IPV6 <<< "${NETWORK}"
{
echo " ${NETWORK}:";
echo " external: true";
} >> "${COMPOSE_FILE}"
done
fi
if [ -n "${LAUNCH_EXT_NETWORKS_MIXED}" ]; then
read -ra ARR <<<"${LAUNCH_EXT_NETWORKS_MIXED}"
for NETWORK in "${ARR[@]}"; do
IFS='-' read -r NETWORK IPV4 IPV6 <<< "${NETWORK}"
{
echo " ${NETWORK}:";
echo " external: true";
} >> "${COMPOSE_FILE}"
done
fi
fi
fi
fi
# does a docker login
if [[ -n "${LOGIN_USER}" && ( -f "${LOGIN_PASSWORD_FILE}" || -n "${LOGIN_PASSWORD}" ) ]]; then
if [ -f "${LOGIN_PASSWORD_FILE}" ] ; then
LOGIN_PASSWORD=$(cat "${LOGIN_PASSWORD_FILE}")
fi
_echo "Logging in"
echo "${LOGIN_PASSWORD}" | docker login -u "${LOGIN_USER}" --password-stdin "${LOGIN_REGISTRY}"
fi
# tests the config file
_echo "Testing compose file:"
_echo "-----------------------------------"
cat "${COMPOSE_FILE}"
_echo "-----------------------------------"
COMMAND=(
--project-name "${LAUNCH_PROJECT_NAME}"
config
)
[[ -n "${LAUNCH_ARG_ENVFILE}" ]] && COMMAND=(--env-file "${LAUNCH_ARG_ENVFILE}" "${COMMAND[@]}")
docker compose "${COMMAND[@]}"
# pull latest image version
if [ "${LAUNCH_PULL}" = true ] && [ -n "${LAUNCH_IMAGE}" ] ; then
_echo "Pulling ${LAUNCH_IMAGE}"
docker pull "${LAUNCH_IMAGE}"
fi
# Ensures that a stop of this container cleans up any stranded services
trap _term SIGTERM
_echo "-----------------------------------"
COMMAND=(
--project-name "${LAUNCH_PROJECT_NAME}" up
--always-recreate-deps
--force-recreate
--abort-on-container-exit
--remove-orphans
--no-color
)
[[ -n "${LAUNCH_ARG_ENVFILE}" ]] && COMMAND=(--env-file "${LAUNCH_ARG_ENVFILE}" "${COMMAND[@]}")
# Here is the container started
docker compose "${COMMAND[@]}" & wait $!
_cleanup