-
Notifications
You must be signed in to change notification settings - Fork 0
/
comp
executable file
·623 lines (528 loc) · 20.8 KB
/
comp
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
#!/usr/bin/env bash
# shellcheck disable=SC2016,SC2154,SC2155,SC2164,SC2317 source=/dev/null
set -Eumo pipefail
SELF_DIR="$(cd -P -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd -P)"
LIB_DIR="$SELF_DIR/.lib"
SCRIPTS_DIR="$SELF_DIR/_scripts"
source "$SCRIPTS_DIR/colors.sh"
COMP_SELF_CALL_DEPTH="${COMP_SELF_CALL_DEPTH:-0}"
DOCKER_CMD=docker
DOCKER_COMPOSE_CMD="$LIB_DIR/compose"
YQ_CMD="$LIB_DIR/yq"
EXIT_CODE_GENERIC_ERROR=1
EXIT_CODE_USAGE_ERROR=250
EXIT_CODE_OPTIONS_CONF_ERROR=240
EXIT_CODE_DEP_INSTALL_FAILURE=230
EXIT_CODE_COMPOSITION_NOT_FOUND=220
EXIT_CODE_SETUP_ERROR=10
EXIT_CODE_PRE_HOOK_SCRIPT_ERROR=100
EXIT_CODE_SIMPLE_VERB_FAILURE=110
EXIT_CODE_POST_HOOK_SCRIPT_ERROR=120
FLAG_FAIL_FAST=""
FLAG_SKIP_OVERRIDES=""
FLAG_SKIP_PREREQS=""
FLAG_SKIP_REGENERATE=""
OPTION_DEVICES=""
OPTION_HOOKS=""
OPTION_LABELS=""
OPTION_LOGGING=""
OPTION_PORTS=""
# # # # # # # # # # # # # # # # # # # HELPER ROUTINES # # # # # # # # # # # # # # # # # # #
__append_env_from () {
if [ -f "$1" ] ; then
local script_file="${1/#./$_CUR_COMP_}"
echo -n " ${script_file#"$SELF_DIR"/}"
if bash -c "set -a ; source .env ; $1" > .env.add ; then
cat .env.add >> .env ; rm .env.add
else
echo " ${_fg_red_}${_bold_}FAILED!${_normal_}" ; return 1
fi
fi
}
__CALL_SELF__ () {
local cur_dir="$(pwd)"
cd "$SELF_DIR"
COMP_SELF_CALL_DEPTH=$(( COMP_SELF_CALL_DEPTH + 1 )) bash "$0" "$@"
local exit_code=$?
cd "$cur_dir"
return $exit_code
}
__create_external_networks () {
local query='.networks | with_entries(select(.value.external == true)) | keys | .[]'
local EXTERNAL_NETWORKS=( )
for yml in "${_CUR_COMPOSE_FILES_[@]}" "${_CUR_COMPOSE_OVERRIDE_FILES_[@]}" ; do
for ext_net in $( "$YQ_CMD" -M "$query" "$yml" ) ; do
# NOTE: `echo ... | xargs` to get rid of quotes around network name during extraction via `yq`.
EXTERNAL_NETWORKS+=( $(echo "$ext_net" | xargs) )
done
done
for ext_net in $( printf "%s\n" "${EXTERNAL_NETWORKS[@]}" | sort -u ) ; do
"$SCRIPTS_DIR/create-network.sh" "$ext_net" || return 1
done
}
__do_prereqs () {
[ "$FLAG_SKIP_PREREQS" != yes ] || return 0
local verb="$1"
local ensure_running="${2:-no}"
while IFS= read -r prereq_comp_dir ; do
if [ "$verb" = status ] ; then
if ! __CALL_SELF__ status -F "$prereq_comp_dir" ; then
[ "$ensure_running" = yes ] || return 1
__CALL_SELF__ up -F "$prereq_comp_dir" || return 1
fi
else
__CALL_SELF__ "$verb" -F "$prereq_comp_dir" || return 1
fi
done < <( "$YQ_CMD" "${_CUR_YQ_META_QUERY_PFX_} .pre_reqs | .[]" meta.yml 2>/dev/null )
}
__error () {
echo "${_fg_red_}${_bold_}[X] ERROR${_normal_}: $1" >&2
}
__gen_env () {
[ ! -f .env ] || [ "$FLAG_SKIP_REGENERATE" != yes ] || return 0
rm -rf .env
printf "[*] Generating '.env':"
cp "$SELF_DIR/static.global.env" .env || return 1
if [ "$FLAG_SKIP_OVERRIDES" != yes ] && [ -f "$SELF_DIR/static.global.override.env" ] ; then
cat "$SELF_DIR/static.global.override.env" >> .env || return 1
fi
__append_env_from "$SELF_DIR/dynamic.global.env.sh" || return 1
[ "$FLAG_SKIP_OVERRIDES" = yes ] || \
__append_env_from "$SELF_DIR/dynamic.global.override.env.sh" || return 1
[ ! -f static.env ] || cat static.env >> .env || return 1
[ "$FLAG_SKIP_OVERRIDES" = yes ] || \
[ ! -f static.override.env ] || cat static.override.env >> .env || return 1
__append_env_from "./dynamic.env.sh" || return 1
[ "$FLAG_SKIP_OVERRIDES" = yes ] || \
__append_env_from "./dynamic.override.env.sh" || return 1
env -i bash --norc --noprofile -c "set -a ; source .env ; env" \
| sed 's;^\(_\|PWD\|SHLVL\)=.*$;;g' | grep . > .env.new
mv .env.new .env
echo " ${_fg_green_}DONE${_normal_}"
}
__gen_templates () {
find ./extra -name '*.template.*' 2>/dev/null | grep -q . || return 0
echo "[~] Generating templated files ..."
while IFS= read -r template_file ; do
local generated_file="./generated${template_file#./extra}"
mkdir -p "$(dirname "$generated_file")"
generated_file="${generated_file/.template/}"
if [ ! -f "$generated_file" ] || [ "$FLAG_SKIP_REGENERATE" != yes ] ; then
"$SCRIPTS_DIR/generate-and-verify.sh" "$template_file" "$generated_file" \
|| return 1
fi
done < <(find ./extra -name '*.template.*')
}
__grab_ps () {
# FIXME: Best effort project name guessing without running compose.
local proj="$_CUR_COMP_"
! [[ "$_CUR_COMP_" =~ ^_ ]] || proj=${proj:1}
echo "$_CUR_COMP_" | grep -qv '\.' || proj=${proj//.}
[ "$proj" = "$_CUR_COMP_" ] || __warning "Guessing project name to be $proj"
# FIXME: `podman` doesn't print health status!
local _ps_res="$( $DOCKER_CMD ps \
--format '{{.Label "com.docker.compose.service"}}\t{{.Status}}' \
--filter label=com.docker.compose.project="$proj" \
)"
eval "$1"='$_ps_res'
}
__maybe_fail_fast () {
[ "$FLAG_FAIL_FAST" != yes ] || exit "$1"
if [ "$_FINAL_EXIT_CODE_" -eq 0 ] ; then
_FINAL_EXIT_CODE_=$1
elif [ "$_FINAL_EXIT_CODE_" -ne "$1" ] ; then
_FINAL_EXIT_CODE_=$EXIT_CODE_GENERIC_ERROR
fi
return "$1"
}
__print_sepator_line () {
case "$COMP_SELF_CALL_DEPTH" in
0 ) printf "%.0s–" {1..80} ;;
1 ) printf "%.0s·" {1..80} ;;
* ) ;;
esac
}
__read_option () {
local OPTION="OPTION_$1"
local OVERRIDE_OPTION=""
if [ -z "${!OPTION}" ] && [ "$FLAG_SKIP_OVERRIDES" != yes ]; then
OVERRIDE_OPTION="$(grep -s "$1" options.override.conf | cut -d= -f2)"
if [ -n "$OVERRIDE_OPTION" ] ; then
validate_and_set_option "$OVERRIDE_OPTION" "$1" override || return 1
fi
fi
[ -n "${!OPTION}" ] || printf -v "$OPTION" "%s" yes
if [ "${!OPTION}" != yes ] ; then
[ -z "$OVERRIDE_OPTION" ] && _DISABLED_OPTIONS_+=("$1 (arg)") || _DISABLED_OPTIONS_+=("$1 (conf)")
else
if [ -f "docker-compose.${1,,}.yml" ] ; then
_CUR_COMPOSE_FILES_+=( "docker-compose.${1,,}.yml" )
if [ "$FLAG_SKIP_OVERRIDES" != yes ] && [ -f "docker-compose.${1,,}.override.yml" ] ; then
_CUR_COMPOSE_OVERRIDE_FILES_+=( "docker-compose.${1,,}.override.yml" )
fi
fi
fi
}
__run_hook () {
( set -a && source "$SCRIPTS_DIR/colors.sh" && source .env && ./"$1" ) \
|| { __error "HOOK '$1' FAILED!" ; return 1 ; }
}
__run_hooks () {
local stage="$1"
[ -z "${2:-}" ] || stage="$1.$2"
ls docker-compose."$stage"_hook*.sh &> /dev/null || return 0
[ -n "${2:-}" ] && echo "[>] Running '$2' hooks for '$1' ..." \
|| echo "[>] Running '$1' hooks ..."
[ ! -f "docker-compose.${stage}_hook.sh" ] \
|| __run_hook "docker-compose.${stage}_hook.sh" || return 1
if [ "$FLAG_SKIP_OVERRIDES" != yes ] ; then
while IFS= read -r hook_file ; do
__run_hook "$hook_file" || return 1
done < <(find . -maxdepth 1 -type f -name "docker-compose.${stage}_hook.override*.sh")
fi
}
__show_message () {
local stage="$1"
[ -z "${2:-}" ] || stage="$1.$2"
while IFS= read -r msg ; do
echo -n "${_fg_white_}${_bg_magenta_}${_bold_} * ${_normal_} "
_CUR_COMP_="$_CUR_COMP_" _CUR_VERB_="$_CUR_VERB_" \
bash -c "set -a ; source '$SCRIPTS_DIR/colors.sh' ; source .env ; echo \"$msg\""
done < <( "$YQ_CMD" "${_CUR_YQ_META_QUERY_PFX_} .messages.$stage | .[]" meta.yml 2>/dev/null )
}
__verify_volumes () {
local query='.services.[] | with_entries(select(.key == "volumes")) | .[] | .[] as $v | $v'
local mounted_volumes=( )
for yml in "${_CUR_COMPOSE_FILES_[@]}" "${_CUR_COMPOSE_OVERRIDE_FILES_[@]}" ; do
for vol in $( "$YQ_CMD" -M "$query" "$yml" ) ; do
case $vol in
./* | /* ) mounted_volumes+=( "$vol" ) ;;
* ) ;;
esac
done
done
for vol in $(printf "%s\n" "${mounted_volumes[@]}" | sort -u) ; do
vol="$(echo "$vol" | cut -d: -f1)"
if ! [ -e "$vol" ] ; then
__error "VOLUME PATH '${vol/#./$_CUR_COMP_}' NOT FOUND!" ; return 1
fi
done
}
__warning () {
echo "${_fg_yellow_}${_bold_}[W] WARNING${_normal_}: $1" >&2
}
__will_invoke_compose () {
[ "$_CUR_VERB_" = down ] || [ "$_CUR_VERB_" = pull ] || [ "$_CUR_VERB_" = up ]
}
__will_do_pre_reqs () {
[ "$FLAG_SKIP_PREREQS" != yes ] && {
[ "$_CUR_VERB_" = overrides ] || [ "$_CUR_VERB_" = pull ] \
|| [ "$_CUR_VERB_" = status ] || [ "$_CUR_VERB_" = up ] \
|| [ "$_CUR_VERB_" = validate ] ;
}
}
# # # # # # # # # # # # # # # # # # # TOP-LEVEL VERBS # # # # # # # # # # # # # # # # # # #
do_validate () {
local non_failing="${1:-}"
local err=
if [[ "$_CUR_COMP_" =~ ^_ ]] ; then
if [ -n "$non_failing" ] ; then
__warning "$DOCKER_COMPOSE_CMD drops leading '_' from project name."
else
__error "$DOCKER_COMPOSE_CMD drops leading '_' from project name." ; err=1
fi
fi
if echo "$_CUR_COMP_" | grep -q '\.' ; then
if [ -n "$non_failing" ] ; then
__warning "$DOCKER_COMPOSE_CMD drops all '.' from project name."
else
__error "$DOCKER_COMPOSE_CMD drops all '.' from project name." ; err=1
fi
fi
[ -z "$err" ] || return 1
[ -n "$non_failing" ] || printf '[~] Validating service:'
for svc in $("$YQ_CMD" -M '.services | keys | .[]' docker-compose.yml) ; do
local attrs=( $("$YQ_CMD" -M ".services.\"$svc\" | keys | .[]" docker-compose.yml) )
[ -n "$non_failing" ] || echo -n " $svc"
for bad_attr in devices labels logging ports ; do
if printf '%s\0' "${attrs[@]}" | grep -Fxqz -- $bad_attr ; then
if [ -n "$non_failing" ] ; then
echo ; __warning "Consider moving '$bad_attr' for '$svc' into docker_compose.$bad_attr.yml."
else
echo ; __error "'$bad_attr' for '$svc' should be in docker_compose.$bad_attr.yml." ; return 1
fi
fi
done
done
[ -n "$non_failing" ] || echo
}
do_clean () {
__grab_ps __ps_before_clean
if [ -n "$__ps_before_clean" ] ; then
__error "Cannot clean while ${_bold_}$_CUR_COMP_${_normal_} is running!" ; return 1
fi
rm -rfv generated .env
if [ -d data ] ; then
echo -n "Remove '$_CUR_COMP_/data' (y/N)? " ; read -r
[[ $REPLY =~ ^[Yy]$ ]] || return 0
fi
rm -rfv data
}
do_down () {
$DOCKER_COMPOSE_CMD down
}
do_overrides () {
local any_override=
while IFS= read -r ofile ; do
any_override=YES
printf '[G] ' ; realpath -s --relative-to="$(pwd)/.." "$ofile"
done < <(find .. -maxdepth 1 -type f -iname '*override*')
[ -n "$any_override" ] || echo '[-] No global override files.'
any_override=
while IFS= read -r ofile ; do
any_override=YES
printf '[L] ' ; realpath -s --relative-to="$(pwd)/.." "$ofile"
done < <(find . -maxdepth 1 -type f -iname '*override*')
[ -n "$any_override" ] || echo '[-] No local override files.'
}
do_pull () {
$DOCKER_COMPOSE_CMD pull
}
do_status () {
__grab_ps __ps_before_status
printf '[?] Querying service:'
for svc in $( "$YQ_CMD" -M '.services | keys | .[]' docker-compose.yml ) ; do
echo -n " $svc"
if ! ( echo "$__ps_before_status" | grep "$svc" | grep -q "(healthy)" ) ; then
echo ; __error "Unhealthy service: ${_fg_red_}$svc${_normal_}" ; return 1
fi
done
echo
}
do_up () {
local all__CUR_COMPOSE_FILES_=()
for cf in "${_CUR_COMPOSE_FILES_[@]}" "${_CUR_COMPOSE_OVERRIDE_FILES_[@]}" ; do
all__CUR_COMPOSE_FILES_+=(-f "$cf")
done
$DOCKER_COMPOSE_CMD "${all__CUR_COMPOSE_FILES_[@]}" up -d
}
# # # # # # # # # # # # # # # # # # # OPTIONS PARSING # # # # # # # # # # # # # # # # # # #
usage () {
[ $# -eq 0 ] || echo -e "\nERROR: $1" >&2
local num_comps="$(find . -maxdepth 2 -type f -name docker-compose.yml | wc -l)"
readarray -t compositions < <(find . -maxdepth 2 -type f -name docker-compose.yml -printf '%h\0' | xargs -0 -I{} basename {})
echo -e "
Usage:
$0 <verb>[,<verb>,...] [flags] [options] <comp_dir> [<comp_dir> ...]
Verbs: (short forms within <>)
<c>lean Delete '<comp_dir>/data'
<d>own Stop a composition
<o>verrides List all override files in a composition
<p>ull Pull all images for a composition
<s>tatus Display health / status of a composition
<u>p Start a composition
<v>alidate Validate a composition
Flags:
[-P | --skip-prereqs] Ignore verifying/starting prerequisite compositions
[-F | --fail-fast] Fail on the first verb failure
[-O | --skip-overrides] Ignore overrides in scripts, environments, flags etc.
[-R | --skip-regenerate] Use existing '.env' and 'generated/'
Options: { yes | no }
[-d | --devices] Attach devices listed in 'docker-compose.devices.yml'
[-g | --logging] Configure logging as specified in 'docker-compose.logging.yml'
[-h | --hooks] Run pre and post hook 'docker-compose.*.yml' scripts
[-l | --labels] Use labels specified in 'docker-compose.labels.yml'
[-p | --ports] Expose ports listed in 'docker-compose.ports.yml'
<omitted> = Configure the option unless overridden in options.override.conf:
use 'docker-compose.*.{sh,yml}' unless overriden in options.override.conf.
no = Never configure the option (and use docker default instead):
ignores 'docker-compose.*.{sh,yml}' files.
yes = Always configure the option as specified:
ignores options*.conf and uses all 'docker-compose.*.{sh,yml}' files.
Compositions Found ($num_comps):" >&2
for (( i=0 ; i<${#compositions[@]} ; i++ )) ; do
printf "%-13s" "${compositions[i]}"
(( (i + 1) % 7 != 0 )) || printf "\n"
done >&2
exit $EXIT_CODE_USAGE_ERROR
}
[ $# -gt 0 ] || usage
expand_verbs () {
local expanded=
while read -rn1 char; do
case "$char" in
c) expanded="$expanded clean" ;;
d) expanded="$expanded down" ;;
o) expanded="$expanded overrides" ;;
p) expanded="$expanded pull" ;;
s) expanded="$expanded status" ;;
u) expanded="$expanded up" ;;
v) expanded="$expanded validate" ;;
*) return 1
esac
done < <( echo -n "$1" )
VERBS="$expanded"
}
VERBS="${1//,/ }"
for V in $VERBS ; do
[ "$V" = clean ] || [ "$V" = down ] || [ "$V" = overrides ] || [ "$V" = pull ] \
|| [ "$V" = status ] || [ "$V" = up ] || [ "$V" = validate ] \
|| expand_verbs "$VERBS" \
|| usage "Unknown verb: $V"
done
shift
for opt in "$@" ; do
shift
case "$opt" in
--fail-fast) set -- "$@" "-F" ;;
--skip-prereqs) set -- "$@" "-P" ;;
--skip-overrides) set -- "$@" "-O" ;;
--skip-regenerate) set -- "$@" "-R" ;;
--devices) set -- "$@" "-d" ;;
--hooks) set -- "$@" "-h" ;;
--labels) set -- "$@" "-l" ;;
--logging) set -- "$@" "-g" ;;
--ports) set -- "$@" "-p" ;;
--) set -- "$@" "--" ;;
--*) usage "Unrecognized option: $opt." ;;
*) set -- "$@" "$opt"
esac
done
validate_and_set_option () {
local OPTION="OPTION_$2"
case "$1" in
yes | no ) printf -v "$OPTION" "%s" "$1" ; return 0
esac
if [ "$3" = override ] ; then
echo ; __error "Invalid value '$1' for '$2' in $_CUR_COMP_/options.override.conf"
else
usage "Invalid value '$1' for '${2,,}' option"
fi
return 1
}
OPTIND=1
while getopts ':FOPRd:g:h:l:p:' OPTION ; do
case "$OPTION" in
F ) FLAG_FAIL_FAST=yes ;;
O ) FLAG_SKIP_OVERRIDES=yes ;;
P ) FLAG_SKIP_PREREQS=yes ;;
R ) FLAG_SKIP_REGENERATE=yes ;;
d ) validate_and_set_option "$OPTARG" DEVICES usage ;;
g ) validate_and_set_option "$OPTARG" LOGGING usage ;;
h ) validate_and_set_option "$OPTARG" HOOKS usage ;;
l ) validate_and_set_option "$OPTARG" LABELS usage ;;
p ) validate_and_set_option "$OPTARG" PORTS usage ;;
* ) usage "Unrecognized option: -$OPTARG." ;;
esac
done
shift $((OPTIND -1))
if [ $# -le 0 ] ; then
__error "No <comp_dir>' provided!"
exit $EXIT_CODE_COMPOSITION_NOT_FOUND
fi
# # # # # # # # # # # # # # # # # # # DEPENDENCIES # # # # # # # # # # # # # # # # # # #
# FIXME: Improve `podman` support and make it default.
if ! $DOCKER_CMD version &> /dev/null ; then
if ! podman version &> /dev/null ; then
__error 'Failed to locate: `docker` or `podman`!'
exit $EXIT_CODE_DEP_INSTALL_FAILURE
else
DOCKER_CMD=podman
fi
fi
if ! [ -x "$DOCKER_COMPOSE_CMD" ] ; then
if ! "$SCRIPTS_DIR/install-compose.sh" "$LIB_DIR/compose" ; then
__error 'Failed to locate or download: `'"$DOCKER_CMD"'-compose`!'
exit $EXIT_CODE_DEP_INSTALL_FAILURE
fi
fi
if ! [ -x "$YQ_CMD" ] ; then
if ! "$SCRIPTS_DIR/install-yq.sh" "$LIB_DIR/yq" ; then
__error 'Failed to locate or download: `yq`!'
exit $EXIT_CODE_DEP_INSTALL_FAILURE
fi
fi
# # # # # # # # # # # # # # # # # # # MAIN FUNCTION # # # # # # # # # # # # # # # # # # #
COMPOSITIONS=( "$@" )
OPTIONS_FROM_ARGS=( "$OPTION_DEVICES" "$OPTION_HOOKS" "$OPTION_LABELS" "$OPTION_LOGGING" "$OPTION_PORTS" )
_FINAL_EXIT_CODE_=0 ; _CUR_STAGE_=0 ; _CUR_YQ_META_QUERY_PFX_=""
unset _CUR_COMP_ _CUR_VERB_ ; __print_sepator_line
for _CUR_COMP_ in "${COMPOSITIONS[@]}" ; do
_CUR_COMP_="${_CUR_COMP_%/}"
if ! cd "$SELF_DIR/$_CUR_COMP_" 2>/dev/null ; then
echo ; __error "${_bold_}$_CUR_COMP_${_normal_} is not a base directory at '$SELF_DIR'!"
__maybe_fail_fast $EXIT_CODE_COMPOSITION_NOT_FOUND || continue
fi
if ! [ -f "$SELF_DIR/$_CUR_COMP_/docker-compose.yml" ] ; then
echo ; __error "No 'docker-compose.yml' found under ${_bold_}$_CUR_COMP_${_normal_}!"
__maybe_fail_fast $EXIT_CODE_COMPOSITION_NOT_FOUND || continue
fi
[ ! -f "meta.override.yml" ] || _CUR_YQ_META_QUERY_PFX_='. *= load("meta.override.yml") | '
_CUR_COMPOSE_FILES_=( "docker-compose.yml" ) ; _CUR_COMPOSE_OVERRIDE_FILES_=()
if [ "$FLAG_SKIP_OVERRIDES" != yes ] && [ -f docker-compose.override.yml ] ; then
_CUR_COMPOSE_OVERRIDE_FILES_=( docker-compose.override.yml)
fi
IFS=, read -r OPTION_DEVICES OPTION_HOOKS OPTION_LABELS OPTION_LOGGING OPTION_PORTS \
<<< "$( IFS=, ; echo "${OPTIONS_FROM_ARGS[*]}" )"
_DISABLED_OPTIONS_=() ; opt_error=0
for opt in DEVICES HOOKS LABELS LOGGING PORTS ; do
__read_option $opt || opt_error=1
done
[ "$opt_error" -eq 0 ] || __maybe_fail_fast $EXIT_CODE_OPTIONS_CONF_ERROR || continue
for _CUR_VERB_ in $VERBS ; do
(( ++_CUR_STAGE_ ))
if __will_do_pre_reqs ; then
[ "$_CUR_STAGE_" -gt 1 ] || [ "$COMP_SELF_CALL_DEPTH" -gt 0 ] || echo
[ "$_CUR_VERB_" != overrides ] || __do_prereqs overrides \
|| __maybe_fail_fast $EXIT_CODE_SIMPLE_VERB_FAILURE || continue
[ "$_CUR_VERB_" != pull ] || __do_prereqs pull \
|| __maybe_fail_fast $EXIT_CODE_SIMPLE_VERB_FAILURE || continue
[ "$_CUR_VERB_" != status ] || __do_prereqs status \
|| __maybe_fail_fast $EXIT_CODE_SIMPLE_VERB_FAILURE || continue
[ "$_CUR_VERB_" != up ] || __do_prereqs status yes \
|| __maybe_fail_fast $EXIT_CODE_SIMPLE_VERB_FAILURE || continue
[ "$_CUR_VERB_" != validate ] || __do_prereqs validate \
|| __maybe_fail_fast $EXIT_CODE_SIMPLE_VERB_FAILURE || continue
fi
[ "$_CUR_VERB_" = validate ] || do_validate non_failing
_CUR_VERB_PREFIX_="${_CUR_VERB_:0:1}"
if [ "$COMP_SELF_CALL_DEPTH" -eq 0 ] ; then
_CUR_VERB_PREFIX_="${_fg_white_}${_bg_blue_}${_bold_} ${_CUR_VERB_PREFIX_^^}"
else
_CUR_VERB_PREFIX_="${_fg_white_}${_bg_black_}${_bold_} ${_CUR_VERB_PREFIX_}"
fi
echo -ne "\n$_CUR_VERB_PREFIX_ ${_normal_} Executing ${_bold_}$_CUR_VERB_${_normal_} on "
[ "$COMP_SELF_CALL_DEPTH" -eq 0 ] || printf 'pre-req '
echo "${_bold_}$_CUR_COMP_${_normal_} ... "
[ ${#_DISABLED_OPTIONS_[@]} -eq 0 ] || echo "[o] Disabled options: ${_DISABLED_OPTIONS_[*]}"
if __will_invoke_compose ; then
__gen_env || __maybe_fail_fast $EXIT_CODE_SETUP_ERROR || continue
__create_external_networks || __maybe_fail_fast $EXIT_CODE_SETUP_ERROR || continue
__gen_templates || __maybe_fail_fast $EXIT_CODE_SETUP_ERROR || continue
[ "$OPTION_HOOKS" != yes ] || __run_hooks pre \
|| __maybe_fail_fast $EXIT_CODE_PRE_HOOK_SCRIPT_ERROR || continue
__show_message pre
fi
[ "$OPTION_HOOKS" != yes ] || __run_hooks "$_CUR_VERB_" pre \
|| __maybe_fail_fast $EXIT_CODE_PRE_HOOK_SCRIPT_ERROR || continue
__show_message "$_CUR_VERB_" pre
if __will_invoke_compose ; then
__verify_volumes || __maybe_fail_fast $EXIT_CODE_SETUP_ERROR || continue
fi
verb_ret=0 ; "do_${_CUR_VERB_}" ; verb_ret=$?
[ "$_CUR_VERB_" != validate ] || [ $verb_ret -ne 0 ] || echo "[=] ${_bold_}$_CUR_COMP_${_normal_} is valid!"
[ "$_CUR_VERB_" != status ] || [ $verb_ret -ne 0 ] || echo "[=] ${_bold_}$_CUR_COMP_${_normal_} is healthy!"
[ $verb_ret -eq 0 ] || __maybe_fail_fast $EXIT_CODE_SIMPLE_VERB_FAILURE || continue
if __will_invoke_compose ; then
[ "$OPTION_HOOKS" != yes ] || __run_hooks post \
|| __maybe_fail_fast $EXIT_CODE_POST_HOOK_SCRIPT_ERROR || continue
__show_message post
fi
[ "$OPTION_HOOKS" != yes ] || __run_hooks "$_CUR_VERB_" post \
|| __maybe_fail_fast $EXIT_CODE_POST_HOOK_SCRIPT_ERROR
__show_message "$_CUR_VERB_" post
done
unset _CUR_VERB_ ; __print_sepator_line
done
[ "$COMP_SELF_CALL_DEPTH" -gt 0 ] || echo
exit "$_FINAL_EXIT_CODE_"