-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcontainer_install.sh
More file actions
833 lines (750 loc) · 28.3 KB
/
container_install.sh
File metadata and controls
833 lines (750 loc) · 28.3 KB
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
#!/usr/bin/bash
RELECOVPLATFORM_VERSION="1.0.0"
usage() {
cat << EOF
This script installs and upgrades the iskylims app.
Usage : $0 [--demo_data] [--git_revision] [--compose_file] [--install_conf] [--action] [--script] [--script_before] [--script_after] [--engine] [--test]
Optional input data:
--demo_data | Provide already downloaded demo data from Zenodo
--git_revision | Specify the Git revision to install (default: main, or 'current' to use copied local sources)
--compose_file | Compose file to use (overrides default)
--install_conf | Settings file consumed during container image build (mandatory for production)
--install_conf_map | Service-specific settings file: service,path (can be repeated)
--action | install (default) or upgrade, to control DB initialisation steps
--script | Run a Django migration script after migrations (can be repeated)
--script_before | Run a Django migration script before migrations (can be repeated)
--script_after | Run a Django migration script after migrations (can be repeated)
--skip_demo_data | Skip downloading/copying demo data to samba container
--skip_test_data | Skip loading test fixtures (test/test_data.json)
--engine | Container engine to use: docker (default) or podman
--test | Use development/test compose file and sample data
Examples:
Deploy production container pointing to an external DB/Samba:
bash $0 --install_conf conf/my_prod_settings.txt
Deploy production with per-service settings:
bash $0 --install_conf_map app,conf/docker_production_platform_settings.txt --install_conf_map iskylims_app,conf/docker_production_iskylims_settings.txt
Upgrade an existing production deployment using the same database:
bash $0 --install_conf conf/my_prod_settings.txt --action upgrade
Install demo container system with local services
bash $0 --test
Install test stack from current local committed sources without checking out a branch in-container
bash $0 --test --git_revision current
Provide already downloaded data from Zenodo (compressed) for test environment
bash $0 --demo_data /path/to/iskylims_demo_data.tar.gz
EOF
}
# translate long options to short
reset=true
for arg in "$@"
do
if [ -n "$reset" ]; then
unset reset
set -- # this resets the "$@" array so we can rebuild it
fi
case "$arg" in
# OPTIONAL
--demo_data) set -- "$@" -d ;;
--git_revision) set -- "$@" -g ;;
--compose_file) set -- "$@" -c ;;
--install_conf) set -- "$@" -s ;;
--install_conf_map) set -- "$@" -j ;;
--action) set -- "$@" -a ;;
--script) set -- "$@" -m ;;
--script_before) set -- "$@" -b ;;
--script_after) set -- "$@" -f ;;
--skip_demo_data) set -- "$@" -n ;;
--skip_test_data) set -- "$@" -t ;;
--test) set -- "$@" -p ;;
--engine) set -- "$@" -e ;;
# ADDITIONAL
--help) set -- "$@" -h ;;
--version) set -- "$@" -v ;;
# PASSING VALUE IN PARAMETER
*) set -- "$@" "$arg" ;;
esac
done
# SETTING DEFAULT VALUES
demo_data=false
git_revision="main"
compose_file=""
install_conf=""
install_conf_container=""
install_conf_map_entries=()
skip_demo_data=""
skip_test_data=""
mode="production"
action="install"
run_script=false
run_script_before=false
migration_script=()
migration_script_before=()
engine="docker"
ENGINE_CMD=()
COMPOSE_CMD=()
set_engine() {
if [ "$engine" = "docker" ]; then
if ! command -v docker >/dev/null 2>&1; then
echo "docker not found. Install docker or use --engine podman."
exit 1
fi
ENGINE_CMD=("docker")
COMPOSE_CMD=("docker" "compose")
else
if ! command -v podman >/dev/null 2>&1; then
echo "podman not found. Install podman or use --engine docker."
exit 1
fi
ENGINE_CMD=("podman")
if command -v podman-compose >/dev/null 2>&1; then
COMPOSE_CMD=("podman-compose")
elif podman compose version >/dev/null 2>&1; then
COMPOSE_CMD=("podman" "compose")
else
echo "podman compose not available. Install podman-compose or use --engine docker."
exit 1
fi
fi
}
engine_exec() {
"${ENGINE_CMD[@]}" "$@"
}
compose_exec() {
"${COMPOSE_CMD[@]}" "$@"
}
copy_with_podman_fallback() {
local src="$1"
local dst="$2"
if cp "$src" "$dst" 2>/dev/null; then
return 0
fi
if [ "$engine" = "podman" ]; then
if podman unshare cp "$src" "$dst"; then
return 0
fi
fi
echo "Failed to copy '$src' to '$dst'" >&2
return 1
}
# PARSE VARIABLE ARGUMENTS WITH getopts
options=":d:g:c:s:j:a:m:b:f:e:vhntp"
while getopts $options opt; do
case $opt in
d)
demo_data=$OPTARG
;;
g)
git_revision=$OPTARG
;;
c)
compose_file=$OPTARG
;;
s)
install_conf=$OPTARG
;;
j)
install_conf_map_entries+=("$OPTARG")
;;
a)
action=$OPTARG
if [[ "$action" != "install" && "$action" != "upgrade" ]]; then
echo "Invalid action '$action'. Use install or upgrade."
exit 1
fi
;;
m)
run_script=true
migration_script+=("$OPTARG")
;;
b)
run_script_before=true
migration_script_before+=("$OPTARG")
;;
e)
engine=$OPTARG
if [[ "$engine" != "docker" && "$engine" != "podman" ]]; then
echo "Invalid engine '$engine'. Use docker or podman."
exit 1
fi
;;
f)
run_script=true
migration_script+=("$OPTARG")
;;
n)
skip_demo_data=true
;;
t)
skip_test_data=true
;;
p)
mode="test"
;;
h)
usage
exit 1
;;
v)
echo $RELECOVPLATFORM_VERSION
exit 1
;;
\?)
echo "Invalid Option: -$OPTARG" 1>&2
usage
exit 1
;;
: )
echo "Option -$OPTARG requires an argument." >&2
exit 1
;;
* )
echo "Unimplemented option: -$OPTARG" >&2;
exit 1
;;
esac
done
shift $((OPTIND-1))
if [ "$mode" = "test" ]; then
if [ -z "$compose_file" ]; then
compose_file="docker-compose.test.yml"
fi
else
if [ -z "$compose_file" ]; then
compose_file="docker-compose.prod.yml"
fi
fi
if [ -z "$skip_demo_data" ]; then
if [ "$mode" = "test" ]; then
skip_demo_data=false
else
skip_demo_data=true
fi
fi
if [ -z "$skip_test_data" ]; then
if [ "$mode" = "test" ]; then
skip_test_data=false
else
skip_test_data=true
fi
fi
if [ "$action" = "upgrade" ]; then
skip_demo_data=true
skip_test_data=true
fi
if [ ! -f "$compose_file" ]; then
echo "Compose file '$compose_file' not found"
exit 1
fi
repo_root="$(pwd)"
temp_install_conf_files=()
declare -A service_install_conf_input=()
declare -A install_conf_host_by_service=()
declare -A install_conf_container_by_service=()
declare -A install_path_by_service=()
declare -A local_head_hash_by_service=()
declare -A local_head_short_by_service=()
declare -A image_id_before_by_service=()
declare -A image_id_after_by_service=()
install_services=("iskylims_app" "app")
cleanup_temp_confs() {
local f
for f in "${temp_install_conf_files[@]}"; do
if [ -n "$f" ] && [ -f "$f" ]; then
rm -f "$f"
fi
done
}
trap cleanup_temp_confs EXIT
read_install_conf_value() {
local key="$1"
local file="$2"
grep -E "^${key}=" "$file" | tail -n 1 | cut -d= -f2- | sed "s/^['\"]//;s/['\"]$//"
}
default_service_install_conf() {
case "$1" in
iskylims_app) echo "conf/docker_test_settings.txt" ;;
app) echo "conf/docker_test_settings.txt" ;;
*) echo "conf/docker_test_settings.txt" ;;
esac
}
service_build_context_dir() {
case "$1" in
app) echo "$repo_root" ;;
iskylims_app) echo "$repo_root/../relecov-iskylims" ;;
*) echo "$repo_root" ;;
esac
}
service_is_install_target() {
local wanted="$1"
local s
for s in "${install_services[@]}"; do
if [ "$s" = "$wanted" ]; then
return 0
fi
done
return 1
}
prepare_service_conf() {
local svc="$1"
local conf_value="$2"
local service_context_dir=""
local host_path="$conf_value"
local resolved_path="$conf_value"
local temp_path=""
service_context_dir="$(service_build_context_dir "$svc")"
if [ ! -d "$service_context_dir" ]; then
echo "Build context directory '$service_context_dir' for service '$svc' not found"
exit 1
fi
if [[ "$resolved_path" != /* ]]; then
host_path="$repo_root/$resolved_path"
else
host_path="$resolved_path"
fi
if [ ! -f "$host_path" ]; then
echo "Install configuration '$conf_value' for service '$svc' not found"
exit 1
fi
if [[ "$host_path" = /* ]] && [[ "$host_path" != "$service_context_dir/"* ]]; then
temp_path="$service_context_dir/.tmp_docker_install_conf_${svc}_$$.txt"
echo "Copying $host_path into temporary file $temp_path for service '$svc'."
cp "$host_path" "$temp_path"
host_path="$temp_path"
temp_install_conf_files+=("$temp_path")
fi
if [[ "$host_path" = "$service_context_dir/"* ]]; then
install_conf_container="${host_path#$service_context_dir/}"
else
install_conf_container="$host_path"
fi
install_conf_host_by_service["$svc"]="$host_path"
install_conf_container_by_service["$svc"]="$install_conf_container"
case "$svc" in
app)
install_path_by_service["$svc"]="${APP_INSTALL_PATH:-$(read_install_conf_value "INSTALL_PATH" "$host_path")}"
if [ -z "${install_path_by_service[$svc]}" ]; then
install_path_by_service["$svc"]="/opt/relecov-platform"
fi
;;
iskylims_app)
install_path_by_service["$svc"]="/opt/iskylims"
;;
esac
}
for map_entry in "${install_conf_map_entries[@]}"; do
svc_name="${map_entry%%,*}"
conf_name="${map_entry#*,}"
if [ -z "$svc_name" ] || [ -z "$conf_name" ] || [ "$svc_name" = "$map_entry" ]; then
echo "Invalid --install_conf_map value '$map_entry'. Expected format: service,path"
exit 1
fi
if ! service_is_install_target "$svc_name"; then
echo "Unknown service '$svc_name' in --install_conf_map. Valid services: ${install_services[*]}"
exit 1
fi
service_install_conf_input["$svc_name"]="$conf_name"
done
for target_service in "${install_services[@]}"; do
chosen_conf="${service_install_conf_input[$target_service]}"
if [ -z "$chosen_conf" ]; then
if [ -n "$install_conf" ]; then
chosen_conf="$install_conf"
elif [ "$mode" = "test" ]; then
chosen_conf="$(default_service_install_conf "$target_service")"
else
echo "Production deployments require --install_conf or --install_conf_map for service '$target_service'."
exit 1
fi
fi
prepare_service_conf "$target_service" "$chosen_conf"
done
set_engine
# Check if a service exists in the compose file
#
# Parameters:
# $1 - Service name to check
#
# Returns:
# 0 if the service exists, 1 otherwise
service_exists() {
compose_exec -f "$compose_file" config --services 2>/dev/null | grep -Fxq "$1"
}
# Return the name of the container for a given service name.
# The container name is different based on whether we are in test mode or not.
#
# Parameters:
# $1 - Service name to return the container name for
#
# Returns:
# The name of the container for the given service name
service_container_name() {
local service_name="$1"
if [ "$mode" = "test" ]; then
case "$service_name" in
db) echo "relecov_test_db" ;;
iskylims_app) echo "relecov_test_iskylims_app" ;;
app) echo "relecov_test_app" ;;
nextstrain) echo "relecov_test_nextstrain" ;;
*) echo "" ;;
esac
else
case "$service_name" in
iskylims_app) echo "relecov_iskylims_app" ;;
app) echo "relecov_app" ;;
nextstrain) echo "relecov_nextstrain" ;;
*) echo "" ;;
esac
fi
}
# Resolve the container ID for a given service name.
#
# Parameters:
# $1 - Service name to resolve the container ID for
#
# Returns:
# The container ID for the given service name, or an error code if unable to resolve.
#
# Errors:
# 1 - Unable to resolve container ID for given service name.
resolve_service_container() {
local service_name="$1"
local service_container
local container_name
container_name="$(service_container_name "$service_name")"
if [ -n "$container_name" ] && engine_exec inspect -f '{{.Id}}' "$container_name" >/dev/null 2>&1; then
service_container="$container_name"
else
service_container="$(engine_exec ps -a --filter "label=com.docker.compose.service=${service_name}" --format '{{.ID}}' | head -n 1)"
fi
if [ -z "$service_container" ]; then
echo "Error: unable to resolve container ID for service '$service_name'." >&2
return 1
fi
echo "$service_container"
}
# Ensure a service is running.
#
# Parameters:
# $1 - Service name to ensure is running
# $2 - Container ID for the service
#
# Returns:
# The container ID if the service is running, or an error code if unable to resolve.
#
# Errors:
# 1 - Service container does not exist.
# 2 - Service container is not running.
ensure_service_running() {
local service_name="$1"
local service_container="$2"
if ! engine_exec inspect -f '{{.State.Running}}' "$service_container" >/dev/null 2>&1; then
echo "Error: service '$service_name' container does not exist."
exit 1
fi
if [ "$(engine_exec inspect -f '{{.State.Running}}' "$service_container")" != "true" ]; then
echo "Error: service '$service_name' container is not running. Showing logs:"
engine_exec logs --tail 200 "$service_container"
exit 1
fi
echo "$service_container"
}
# Return the repository path for a given service name.
#
# Parameters:
# $1 - Service name to retrieve the repository path for.
#
# Returns:
# The repository path for the given service name.
#
# Errors:
# 1 - Unknown service name passed to the function.
service_repo_path() {
case "$1" in
iskylims_app) echo "/srv/iskylims" ;;
app) echo "/srv/relecov-platform" ;;
*) echo "Error: unknown service '$1'" >&2; exit 1 ;;
esac
}
service_install_path() {
case "$1" in
iskylims_app) echo "${install_path_by_service[$1]:-/opt/iskylims}" ;;
app) echo "${install_path_by_service[$1]:-/opt/relecov-platform}" ;;
*) echo "Error: unknown service '$1'" >&2; exit 1 ;;
esac
}
compose_service_image_id() {
compose_exec -f "$compose_file" images -q "$1" 2>/dev/null | tail -n 1
}
print_local_source_diagnostics() {
local service_name="$1"
local service_context_dir=""
service_context_dir="$(service_build_context_dir "$service_name")"
echo "Local source diagnostics for $service_name:"
if command -v git >/dev/null 2>&1 && git -C "$service_context_dir" rev-parse --is-inside-work-tree >/dev/null 2>&1; then
local_head_hash_by_service["$service_name"]="$(git -C "$service_context_dir" rev-parse HEAD)"
local_head_short_by_service["$service_name"]="$(git -C "$service_context_dir" rev-parse --short HEAD)"
echo " local HEAD: $(git -C "$service_context_dir" log -1 --oneline)"
echo " local HEAD hash: ${local_head_hash_by_service[$service_name]}"
else
echo " local git metadata unavailable"
fi
}
print_existing_artifact_diagnostics() {
local service_name="$1"
echo "Image diagnostics before build for $service_name:"
image_id_before_by_service["$service_name"]="$(compose_service_image_id "$service_name")"
if [ -n "${image_id_before_by_service[$service_name]}" ]; then
echo " image before build: ${image_id_before_by_service[$service_name]}"
else
echo " image before build: none"
fi
}
print_service_image_after_build() {
local service_name="$1"
local previous_id="${image_id_before_by_service[$service_name]}"
echo "Image diagnostics after build for $service_name:"
image_id_after_by_service["$service_name"]="$(compose_service_image_id "$service_name")"
if [ -n "${image_id_after_by_service[$service_name]}" ]; then
echo " image after build: ${image_id_after_by_service[$service_name]}"
if [ -n "$previous_id" ] && [ "$previous_id" = "${image_id_after_by_service[$service_name]}" ]; then
echo " image id check: unchanged"
elif [ -n "$previous_id" ] && [ "$previous_id" != "${image_id_after_by_service[$service_name]}" ]; then
echo " image id check: changed"
else
echo " image id check: created"
fi
else
echo " image after build: not found"
fi
}
print_container_source_diagnostics() {
local service_name="$1"
local service_container="$2"
local label="$3"
local service_repo=""
local container_repo_head_hash=""
local container_repo_head_short=""
service_repo="$(service_repo_path "$service_name")"
echo "$label"
container_repo_head_hash="$(engine_exec exec "$service_container" sh -lc "
if [ -d '$service_repo/.git' ]; then
cd '$service_repo' && git rev-parse HEAD
fi
" 2>/dev/null | tail -n 1)"
container_repo_head_short="$(engine_exec exec "$service_container" sh -lc "
if [ -d '$service_repo/.git' ]; then
cd '$service_repo' && git rev-parse --short HEAD
fi
" 2>/dev/null | tail -n 1)"
if [ -n "$container_repo_head_hash" ]; then
echo " container /srv HEAD hash: $container_repo_head_hash"
fi
if [ -n "${local_head_hash_by_service[$service_name]}" ] && [ -n "$container_repo_head_hash" ]; then
if [ "${local_head_hash_by_service[$service_name]}" = "$container_repo_head_hash" ]; then
echo " HEAD check: OK local=${local_head_short_by_service[$service_name]} container=$container_repo_head_short"
else
echo " HEAD check: MISMATCH local=${local_head_short_by_service[$service_name]} container=$container_repo_head_short"
fi
fi
engine_exec exec "$service_container" sh -lc "
echo ' $service_repo HEAD:'
if [ -d '$service_repo/.git' ]; then
cd '$service_repo' && git log -1 --oneline
else
echo 'not a git checkout'
fi
" || true
}
# Remove stale test containers left over from previous runs.
#
# This function will only be executed in "test" mode when the engine is "podman".
#
# Parameters:
# None
#
# Returns:
# None
#
# Errors:
# None
cleanup_stale_test_containers() {
if [ "$mode" != "test" ] || [ "$engine" != "podman" ]; then
return 0
fi
local svc cname cstate
for svc in db iskylims_app app nextstrain samba; do
cname="$(service_container_name "$svc")"
if [ -z "$cname" ]; then
continue
fi
if engine_exec inspect -f '{{.Id}}' "$cname" >/dev/null 2>&1; then
cstate="$(engine_exec inspect -f '{{.State.Status}}' "$cname" 2>/dev/null || true)"
if [ "$cstate" != "running" ]; then
echo "Removing stale test container '$cname' (state: ${cstate:-unknown})"
engine_exec rm -f "$cname" >/dev/null 2>&1 || true
fi
fi
done
}
cleanup_stale_test_containers
echo "Deploying containers (compose file: $compose_file) with INSTALL_TYPE=dep and GIT_REVISION=$git_revision..."
platform_install_path="$(service_install_path "app")"
if service_exists "apache"; then
mkdir -p "$platform_install_path/conf" "$platform_install_path/logs/apache"
if [ -f "$repo_root/conf/relecov_apache_reverse_proxy.conf" ]; then
copy_with_podman_fallback "$repo_root/conf/relecov_apache_reverse_proxy.conf" \
"$platform_install_path/conf/relecov_apache_reverse_proxy.conf"
fi
fi
for target_service in "${install_services[@]}"; do
if service_exists "$target_service"; then
service_install_conf="${install_conf_container_by_service[$target_service]}"
target_install_path="$(service_install_path "$target_service")"
print_local_source_diagnostics "$target_service"
print_existing_artifact_diagnostics "$target_service"
echo "Building $target_service with INSTALL_CONF=$service_install_conf"
INSTALL_TYPE="dep" GIT_REVISION="$git_revision" INSTALL_CONF="$service_install_conf" APP_INSTALL_PATH="$target_install_path" \
compose_exec -f "$compose_file" build --no-cache \
--build-arg INSTALL_TYPE="dep" \
--build-arg GIT_REVISION="$git_revision" \
--build-arg INSTALL_CONF="$service_install_conf" \
--build-arg APP_INSTALL_PATH="$target_install_path" \
"$target_service"
print_service_image_after_build "$target_service"
fi
done
APP_INSTALL_PATH="$platform_install_path" compose_exec -f "$compose_file" up -d
echo "Waiting 20 seconds for starting database and web services..."
sleep 20
# Set uid and gid for app user in the container, to ensure runtime directories are created with correct ownership
# If the user has specified custom values for APP_UID and APP_GID in env variables, these will be used
# Otherwise, default to 1212 which is the value used in the Dockerfile.
app_uid="${APP_UID:-1212}"
app_gid="${APP_GID:-1212}"
script_args_before=""
if [ "$run_script_before" = true ]; then
for val in "${migration_script_before[@]}"; do
script_args_before+=" --script_before $(printf '%q' "$val")"
done
fi
script_args_after=""
if [ "$run_script" = true ]; then
for val in "${migration_script[@]}"; do
script_args_after+=" --script_after $(printf '%q' "$val")"
done
fi
installed_iskylims=false
installed_platform=false
for target_service in "${install_services[@]}"; do
if ! target_container="$(resolve_service_container "$target_service")"; then
exit 1
fi
ensure_service_running "$target_service" "$target_container"
print_container_source_diagnostics "$target_service" "$target_container" "Container diagnostics after startup for $target_service:"
target_repo_path="$(service_repo_path "$target_service")"
target_install_path="$(service_install_path "$target_service")"
echo "Ensuring runtime directories for $target_service are writable by ${app_uid}:${app_gid}"
engine_exec exec -u 0 -it "$target_container" sh -lc "mkdir -p ${target_install_path}/documents ${target_install_path}/logs ${target_install_path}/static ${target_install_path}/cron ${target_install_path}/tmp && chown -R ${app_uid}:${app_gid} ${target_install_path}/documents ${target_install_path}/logs ${target_install_path}/static ${target_install_path}/cron ${target_install_path}/tmp"
host_install_conf_path="${install_conf_host_by_service[$target_service]}"
service_install_conf="${install_conf_container_by_service[$target_service]}"
container_install_conf_path="$service_install_conf"
if [[ "$container_install_conf_path" != /* ]]; then
container_install_conf_path="${target_repo_path}/$container_install_conf_path"
fi
if ! engine_exec exec -it "$target_container" test -f "$container_install_conf_path"; then
echo "Copying install configuration into $target_service at $container_install_conf_path"
if [ ! -f "$host_install_conf_path" ]; then
echo "Error: host install configuration not found for $target_service at $host_install_conf_path"
exit 1
fi
engine_exec cp "$host_install_conf_path" "${target_container}:$container_install_conf_path"
fi
if [ "$action" = "upgrade" ]; then
echo "Running install.sh bootstrap in $target_service (upgrade mode)"
engine_exec exec -it "$target_container" bash -c "cd $target_repo_path && bash install.sh --bootstrap upgrade --git_revision \"$git_revision\" --conf \"$service_install_conf\" --skip_apache_restart$script_args_before$script_args_after"
else
echo "Running install.sh bootstrap in $target_service (install mode)"
engine_exec exec -it "$target_container" bash -c "cd $target_repo_path && bash install.sh --bootstrap install --git_revision \"$git_revision\" --conf \"$service_install_conf\" --skip_apache_restart$script_args_before$script_args_after"
fi
print_container_source_diagnostics "$target_service" "$target_container" "Container diagnostics after bootstrap for $target_service:"
if ! engine_exec exec -it "$target_container" test -f "$target_install_path/manage.py"; then
echo "Error: $target_install_path/manage.py not found after bootstrap for service $target_service. Showing logs:"
engine_exec logs --tail 200 "$target_container"
exit 1
fi
if [ "$target_service" = "iskylims_app" ]; then
installed_iskylims=true
if [ "$skip_test_data" = false ]; then
if engine_exec exec -it "$target_container" test -f test/test_data.json; then
engine_exec exec -it "$target_container" python3 manage.py loaddata test/test_data.json
else
echo "No test/test_data.json found in $target_service. Skipping fixture load."
fi
else
echo "Skipping test data fixtures for $target_service as requested"
fi
fi
if [ "$target_service" = "app" ]; then
installed_platform=true
fi
done
if [ "$installed_iskylims" = true ] && [ "$skip_demo_data" = false ] && service_exists "samba"; then
echo "Downloading and copying test files to the Samba container"
if [ "$demo_data" == "false" ]; then
wget https://zenodo.org/record/8091169/files/iskylims_demo_data.tar.gz
demo_data="./iskylims_demo_data.tar.gz"
fi
engine_exec cp "$demo_data" samba:/mnt
engine_exec exec -it samba tar -xf /mnt/iskylims_demo_data.tar.gz -C /mnt
engine_exec exec -it samba sh -lc '
for root in /mnt/test_ngs_data /mnt/Runs; do
if [ -d "$root" ]; then
find "$root" -type d -exec chmod o+rx {} +
find "$root" -type f -exec chmod o+r {} +
fi
done
'
echo "Deleting compressed test file"
engine_exec exec -it samba rm /mnt/iskylims_demo_data.tar.gz
if [ "$demo_data" == "false" ]; then
rm -f "$demo_data"
fi
else
echo "Skipping Samba demo data load (flag enabled or service not present)"
fi
echo "Skipping crontab add/start (cron is managed by the container entrypoint)"
dns_url=""
local_ip=""
host_install_conf_path="${install_conf_host_by_service[app]}"
if [ -z "$host_install_conf_path" ]; then
host_install_conf_path="${install_conf_host_by_service[iskylims_app]}"
fi
if [ -f "$host_install_conf_path" ]; then
dns_url=$(grep -E "^DNS_URL=" "$host_install_conf_path" | tail -n 1 | cut -d= -f2- | sed "s/^['\"]//;s/['\"]$//")
local_ip=$(grep -E "^LOCAL_SERVER_IP=" "$host_install_conf_path" | tail -n 1 | cut -d= -f2- | sed "s/^['\"]//;s/['\"]$//")
fi
access_urls=()
if [ -n "$dns_url" ] && [ "$dns_url" != "*" ]; then
if [ "$installed_iskylims" = true ]; then
access_urls+=("iSkyLIMS: http://${dns_url}:8001")
fi
if [ "$installed_platform" = true ]; then
access_urls+=("RELECOV Platform: http://${dns_url}:8000")
fi
fi
if [ -n "$local_ip" ] && [ "$local_ip" != "*" ]; then
if [ "$installed_iskylims" = true ]; then
access_urls+=("iSkyLIMS: http://${local_ip}:8001")
fi
if [ "$installed_platform" = true ]; then
access_urls+=("RELECOV Platform: http://${local_ip}:8000")
fi
fi
if [ ${#access_urls[@]} -eq 0 ]; then
if [ "$installed_iskylims" = true ]; then
access_urls+=("iSkyLIMS: http://localhost:8001")
fi
if [ "$installed_platform" = true ]; then
access_urls+=("RELECOV Platform: http://localhost:8000")
fi
fi
echo "You can now access services via: ${access_urls[*]}"