forked from nashcom/domino-startscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomino_container
executable file
·1754 lines (1376 loc) · 46.5 KB
/
domino_container
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
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
###########################################################################
# Nash!Com Domino Container Management Script #
# Version 1.0.3 10.12.2021 #
# #
# (C) Copyright Daniel Nashed/NashCom 2021 #
# Feedback domino_unix@nashcom.de #
# #
# Licensed under the Apache License, Version 2.0 (the "License"); #
# you may not use this file except in compliance with the License. #
# You may obtain a copy of the License at #
# #
# http://www.apache.org/licenses/LICENSE-2.0 #
# #
# Unless required by applicable law or agreed to in writing, software #
# distributed under the License is distributed on an "AS IS" BASIS, #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.#
# See the License for the specific language governing permissions and #
# limitations under the License. #
###########################################################################
SCRIPT_NAME=$0
if [ -x /usr/bin/realpath ]; then
FULL_SCRIPT_NAME=$(realpath $0)
else
FULL_SCRIPT_NAME=/opt/nashcom/start_script
fi
SCRIPT_DIR_NAME=$(dirname $SCRIPT_NAME)
FULL_SCRIPT_DIR_NAME=$(dirname $FULL_SCRIPT_NAME)
NAME_PREFIX="container_"
PARAM1=$1
PARAM2=$2
PARAM3=$3
# Default Configuration parameters overwritten by config file if available in same directory
CONTAINER_NAME=
IMAGE_NAME=
CONTAINER_SYSTEMD_NAME=domino_container.service
CONTAINER_HOSTNAME=
# CONTAINER_NETWORK_NAME=host
# CONTAINER_SPECIAL_OPTIONS="--ip 172.17.0.1"
# CONTAINER_PORTS="-p 1352:1352 -p 80:80 -p 443:443"
# Environment file is used for first container start (run) to pass setup parameters
CONTAINER_ENV_FILE=
# Shutdown timeout increased for Domino proper shutdown
DOMINO_SHUTDOWN_TIMEOUT=120
# Flexnet download links and current software defaults
DOWNLOAD_LINK_FLEXNET="https://hclsoftware.flexnetoperations.com/flexnet/operationsportal/DownloadSearchPage.action?search="
DOWNLOAD_LINK_FLEXNET_OPTIONS="+&resultType=Files&sortBy=eff_date&listButton=Search"
# Default config directory. Can be overwritten by environment
if [ -z "$DOMINO_CFG_DIR" ]; then
DOMINO_CFG_DIR=/etc/sysconfig
fi
CONTAINER_SCRIPT_VERSION=1.0.3
# Get configuration from config file
CFG_PREFIX=$(basename $SCRIPT_NAME | awk -F "$NAME_PREFIX" '{print $2}')
if [ -z "$CFG_PREFIX" ]; then
CONFIG_FILE=$DOMINO_CFG_DIR/domino_container
if [ -e "$CONFIG_FILE" ]; then
echo "(Using config file $CONFIG_FILE)"
else
echo
echo "Configuration not found at [$CONFIG_FILE]"
exit 1
fi
else
CONFIG_FILE=$DOMINO_CFG_DIR/config_$CFG_PREFIX
if [ -e "$CONFIG_FILE" ]; then
echo "(Using config file $CONFIG_FILE)"
else
CONFIG_FILE=$SCRIPT_DIR_NAME/config_$CFG_PREFIX
if [ -e "$CONFIG_FILE" ]; then
echo "(Using config file $CONFIG_FILE)"
else
CONFIG_FILE=
fi
if [ -z "$" ]; then
echo
echo "No configuration found!"
echo
exit 1
fi
fi
fi
# Source in the config file
if [ -e "$CONFIG_FILE" ]; then
. $CONFIG_FILE
else
echo "No configuration found"
exit 1
fi
CONTAINER_FULL_ENV_FILE=
if [ -n "$CONTAINER_ENV_FILE" ]; then
if [ -r "$DOMINO_CFG_DIR/$CONTAINER_ENV_FILE" ]; then
CONTAINER_FULL_ENV_FILE="$DOMINO_CFG_DIR/$CONTAINER_ENV_FILE"
fi
fi
IMAGE_BUILD_VERSION=$IMAGE_VERSION
# Special configuration
CONTAINER_SCRIPT_USE_SUDO=yes
BUILDTIME=$(date +"%d.%m.%Y %H:%M:%S")
if [ -z "$EDIT_COMMAND" ]; then
EDIT_COMMAND="vi"
fi
# Get OS type
LARCH=$(uname)
SOFTWARE_PORT=7777
SOFTWARE_CONTAINER=hclsoftware
SYSTEMCTL_CMD="systemctl"
log_space ()
{
echo
echo "$@"
echo
}
log_and_exit ()
{
echo
echo "$@"
echo
exit 1
}
print_delim ()
{
echo "------------------------------------------------------------------------------------------"
}
header ()
{
echo
print_delim
echo "$@"
print_delim
echo
}
config_warnings ()
{
CONTAINER_STORAGE_DIRVER=$($CONTAINER_CMD info 2>/dev/null |grep "Storage Driver: " | cut -d ":" -f2 | awk '{ sub(/^[ ]+/, ""); print }')
if [ "$CONTAINER_STORAGE_DIRVER" = "overlay" ]; then
echo
echo "Warning: It's strongly recommended to use the 'Overlay2' driver!"
fi
if [ ! -z "$CONTAINER_IMAGE_ID" ]; then
if [ ! "$CONTAINER_IMAGE_ID" = "$IMAGE_ID" ]; then
log_space "Info: New Image Version available!"
fi
fi
JQ_VERSION=$(jq --version 2>/dev/null)
if [ -z "$JQ_VERSION" ] ; then
log_space "Warning: For a better 'inspect' command experience install 'jq' from repository -- infos: https://stedolan.github.io/jq/"
fi
return 0
}
check_version ()
{
count=1
while true
do
VER=$(echo $1|cut -d"." -f $count)
CHECK=$(echo $2|cut -d"." -f $count)
if [ -z "$VER" ]; then return 0; fi
if [ -z "$CHECK" ]; then return 0; fi
if [ $VER -gt $CHECK ]; then return 0; fi
if [ $VER -lt $CHECK ]; then
log_space "Warning: Unsupported $3 version $1 - Must be at least $2 !"
sleep 1
return 1
fi
count=$(expr $count + 1)
done
return 0
}
check_container_environment()
{
DOCKER_MINIMUM_VERSION="18.09.0"
PODMAN_MINIMUM_VERSION="1.7.0"
CONTAINER_CMD=
CONTAINER_ENV_NAME=
CONTAINER_RUNTIME_VERSION=
if [ -x /usr/bin/podman ]; then
if [ -z "$USE_DOCKER" ]; then
# Podman environment detected
CONTAINER_CMD=podman
CONTAINER_ENV_NAME=Podman
local CONTAINER_RUNTIME_VERSION_STR=$(podman version)
CONTAINER_RUNTIME_VERSION=$(echo $CONTAINER_RUNTIME_VERSION_STR | cut -d" " -f2)
check_version "$CONTAINER_RUNTIME_VERSION" "$PODMAN_MINIMUM_VERSION" "$CONTAINER_CMD"
fi
fi
if [ -z "$CONTAINER_CMD" ]; then
if [ -x "/usr/bin/docker" ]; then
CONTAINER_CMD=docker
# Docker doesn't uses systemd
CONTAINER_SYSTEMD_NAME=
CONTAINER_ENV_NAME=Docker
# Check container environment
CONTAINER_RUNTIME_VERSION_STR=$(docker -v)
CONTAINER_RUNTIME_VERSION=$(echo $CONTAINER_RUNTIME_VERSION_STR | cut -d" " -f3|cut -d"," -f1)
check_version "$CONTAINER_RUNTIME_VERSION" "$DOCKER_MINIMUM_VERSION" "$CONTAINER_CMD"
TAG_CONTAINER_ID=".Id"
# For Docker we are done here
return 0
fi
fi
if [ -z "$CONTAINER_CMD" ]; then
if [ ! "$1" = "install" ]; then
log_and_exit "No container environment detected! Tip: use 'install' option to install Podman"
fi
fi
if [ -z "$DOMINO_SYSTEMD_NAME" ]; then
DOMINO_SYSTEMD_NAME=domino_container.service
fi
CONTAINER_PID=/tmp/$DOMINO_SYSTEMD_NAME.pid
CONTAINER_CID=/tmp/$DOMINO_SYSTEMD_NAME.cid
TAG_CONTAINER_ID=".Id"
PODMAN_OPTIONS="--cgroups=no-conmon --conmon-pidfile $CONTAINER_PID --cidfile $CONTAINER_CID"
if [ -z "$CONTAINER_HEALTH_CHECK" ]; then
CONTAINER_HEALTH_CHECK="--health-cmd=/domino_docker_healthcheck.sh --health-interval=60s --health-retries=4 --health-start-period=120s"
fi
return 0
}
check_root_permissions ()
{
# Use sudo for su for container command if not root
if [ "$EUID" = "0" ]; then
return 0 # root does works for all commands -- no sudo needed
fi
if [ "$CONTAINER_SCRIPT_USE_SUDO" = "yes" ]; then
CONTAINER_CMD="sudo $CONTAINER_CMD"
SYSTEMCTL_CMD="sudo systemctl"
return 0
fi
log_and_exit "$CONTAINER_ENV_NAME needs root permissions on Linux!"
}
print_runtime ()
{
echo
# the following line does not work on OSX
# echo "Completed in" $(date -d@$SECONDS -u +%T)
hours=$((SECONDS / 3600))
seconds=$((SECONDS % 3600))
minutes=$((seconds / 60))
seconds=$((seconds % 60))
h=""; m=""; s=""
if [ ! $hours = "1" ] ; then h="s"; fi
if [ ! $minutes = "1" ] ; then m="s"; fi
if [ ! $seconds = "1" ] ; then s="s"; fi
if [ ! $hours = 0 ] ; then echo "Completed in $hours hour$h, $minutes minute$m and $seconds second$s"
elif [ ! $minutes = 0 ] ; then echo "Completed in $minutes minute$m and $seconds second$s"
else echo "Completed in $seconds second$s"; fi
}
pull_image ()
{
if [ -z "$CONTAINER_IMAGE" ]; then
log_space "No container image specified"
fi
header "Pulling image $CONTAINER_IMAGE ..."
$CONTAINER_CMD pull "$CONTAINER_IMAGE"
}
upload_image ()
{
if [ -z "$SOFTWARE_FILE" ]; then
SOFTWARE_FILE=/opt/nashcom/startscript/software.txt
fi
if [ ! -r "$SOFTWARE_FILE" ]; then
log_and_exit "No Domino software.txt found"
fi
DOMINO_IMAGE_NAME=domino-docker
local CURRENT_SOFTWARE=$(grep "^$DOMINO_IMAGE_NAME|" $SOFTWARE_FILE | head -1)
DOMINO_VERSION=`echo $CURRENT_SOFTWARE|cut -d'|' -f2`
DOMINO_IMAGE_FILE_NAME=`echo $CURRENT_SOFTWARE|cut -d'|' -f3`
DOMINO_IMAGE_VERSION=`echo $CURRENT_SOFTWARE|cut -d'|' -f4`
DOMINO_IMAGE_HASH=`echo $CURRENT_SOFTWARE|cut -d'|' -f5`
if [ -z "$DOMINO_IMAGE_FILE_NAME" ]; then
log_and_exit "No container image name configured!"
fi
local SEARCH_IMAGE_ID=$($CONTAINER_CMD inspect --format "{{.ID}}" "$DOMINO_IMAGE_NAME:$DOMINO_IMAGE_VERSION")
if [ -n "$SEARCH_IMAGE_ID" ]; then
if [ "$1" = "force" ]; then
log_space "Replacing existing image"
else
log_space "Image already uploaded"
return 0
fi
fi
# Try current dir first
DOMINO_IMAGE_FILE_PATH=$(find . -name "$DOMINO_IMAGE_FILE_NAME")
# Than try /local
if [ -z "$DOMINO_IMAGE_FILE_PATH" ]; then
DOMINO_IMAGE_FILE_PATH=$(find /local -name "$DOMINO_IMAGE_FILE_NAME" 2>/dev/null | head -1)
fi
# And finally the whole machine
if [ -z "$DOMINO_IMAGE_FILE_PATH" ]; then
DOMINO_IMAGE_FILE_PATH=$(find / -name "$DOMINO_IMAGE_FILE_NAME" 2>/dev/null | head -1)
fi
if [ -z "$DOMINO_IMAGE_FILE_PATH" ]; then
CURRENT_DOWNLOAD_URL="$DOWNLOAD_LINK_FLEXNET$DOMINO_IMAGE_FILE_NAME$DOWNLOAD_LINK_FLEXNET_OPTIONS"
echo
echo "Please download container image from Flexnet:"
echo "$CURRENT_DOWNLOAD_URL"
echo
log_and_exit "Container image file not found!"
fi
if [ -n "$DOMINO_IMAGE_HASH" ]; then
local HASH=`sha256sum "$DOMINO_IMAGE_FILE_PATH" -b | cut -d" " -f1`
if [ ! "$DOMINO_IMAGE_HASH" = "$HASH" ]; then
log_and_exit "Software image was not downloaded correctly! Checksum for [$DOMINO_IMAGE_FILE_PATH] does not match!"
fi
fi
header "Uploading image [$DOMINO_IMAGE_FILE_PATH]"
log_space "Upload takes a moment ..."
$CONTAINER_CMD load --input "$DOMINO_IMAGE_FILE_PATH"
$CONTAINER_CMD tag "$DOMINO_IMAGE_NAME:$DOMINO_IMAGE_VERSION" "$DOMINO_IMAGE_NAME:latest"
}
nginx_start ()
{
# Create a nginx container hosting software download locally
# Check if we already have this container in status exited
STATUS="$($CONTAINER_CMD inspect --format "{{ .State.Status }}" $SOFTWARE_CONTAINER 2>/dev/null)"
if [ -z "$STATUS" ]; then
log_space "Creating container: $SOFTWARE_CONTAINER"
$CONTAINER_CMD run --name $SOFTWARE_CONTAINER -p $SOFTWARE_PORT:80 -v $SOFTWARE_DIR:/usr/share/nginx/html:ro -d nginx
elif [ "$STATUS" = "exited" ]; then
log_space "Starting existing container: $SOFTWARE_CONTAINER"
$CONTAINER_CMD start $SOFTWARE_CONTAINER
fi
log_space "Starting container: $SOFTWARE_CONTAINER"
# Start local nginx container to host SW Repository
SOFTWARE_REPO_IP="$($CONTAINER_CMD inspect --format "{{ .NetworkSettings.IPAddress }}" $SOFTWARE_CONTAINER 2>/dev/null)"
if [ -z "$SOFTWARE_REPO_IP" ]; then
log_space "Unable to locate software repository."
else
DOWNLOAD_FROM=http://$SOFTWARE_REPO_IP
log_space "Hosting Software repository on $DOWNLOAD_FROM"
fi
}
nginx_stop ()
{
# Stop and remove SW repository
$CONTAINER_CMD stop $SOFTWARE_CONTAINER
$CONTAINER_CMD container rm $SOFTWARE_CONTAINER
log_space "Stopped & Removed Software Repository Container"
}
container_show_status()
{
if [ -z "$CONTAINER_STATUS" ]; then
echo "Status: notexisting"
else
echo Status: "$CONTAINER_STATUS"
fi
return 0
}
container_status ()
{
CONTAINER_STATUS="$($CONTAINER_CMD inspect --format "{{ .State.Status }}" $CONTAINER_NAME 2>/dev/null)"
}
inspect_image_native ()
{
IMAGE_ID="$($CONTAINER_CMD inspect --format "{{.ID}}" $CONTAINER_IMAGE 2>/dev/null)"
IMAGE_SHORTID=$(echo $IMAGE_ID | cut -d":" -f2 | cut -c1-12)
if [ -z "$1" ]; then return 0; fi
IMAGE_VERSION="$($CONTAINER_CMD inspect --format "{{ .Config.Labels.version }}" $CONTAINER_IMAGE 2>/dev/null)"
IMAGE_BUILDTIME="$($CONTAINER_CMD inspect --format "{{ .Config.Labels.buildtime }}" $CONTAINER_IMAGE 2>/dev/null)"
IMAGE_SIZE="$($CONTAINER_CMD inspect --format "{{.Size}}" $CONTAINER_IMAGE 2>/dev/null)"
if [ -z "$IMAGE_SIZE" ]; then
IMAGE_SIZE_MB=0
else
IMAGE_SIZE_MB=$(($IMAGE_SIZE / 1000 / 1000 ))
fi
IMAGE_DOMINO_VERSION="$($CONTAINER_CMD inspect --format "{{ index .Config.Labels \"DominoDocker.version\" }}" $CONTAINER_IMAGE 2>/dev/null)"
IMAGE_TRAVELER_VERSION="$($CONTAINER_CMD inspect --format "{{ index .Config.Labels \"TravelerDocker.version\" }}" $CONTAINER_IMAGE 2>/dev/null)"
return 0
}
inspect_image ()
{
if [ ! -e /bin/jq ]; then
inspect_image_native "$@"
return 0
fi
local FULL_JSON=$($CONTAINER_CMD inspect $CONTAINER_IMAGE 2>/dev/null)
if [ -z "$FULL_JSON" ]; then
return 0
fi
if [ "$FULL_JSON" = "[]" ]; then
return 0
fi
local JSON=$(echo $FULL_JSON | jq .[0])
IMAGE_ID=$(echo "$JSON" | jq -r .Id | sed "s/null//")
IMAGE_SHORTID=$(echo $IMAGE_ID | cut -d":" -f2 | cut -c1-12)
if [ -z "$1" ]; then return 0; fi
IMAGE_VERSION="$(echo "$JSON" | jq -r .Config.Labels.version | sed "s/null//")"
IMAGE_BUILDTIME="$(echo "$JSON" | jq -r .Config.Labels.buildtime | sed "s/null//")"
IMAGE_SIZE="$(echo "$JSON" | jq -r .Size | sed "s/null//")"
if [ -z "$IMAGE_SIZE" ]; then
IMAGE_SIZE_MB=0
else
IMAGE_SIZE_MB=$(($IMAGE_SIZE / 1000 / 1000 ))
fi
IMAGE_DOMINO_VERSION="$(echo "$JSON" | jq -r '.Config.Labels."DominoDocker.version"' | sed "s/null//")"
IMAGE_TRAVELER_VERSION="$(echo "$JSON" | jq -r '.Config.Labels."TravelerDocker.version"' | sed "s/null//")"
return 0
}
inspect_container_native ()
{
CONTAINER_KEY=
if [ -n "$CONTAINER_CID" ]; then
if [ -e "$CONTAINER_CID" ]; then
CONTAINER_ID=$(cat "$CONTAINER_CID")
CONTAINER_KEY=$CONTAINER_ID
fi
fi
if [ -z "$CONTAINER_KEY" ]; then
CONTAINER_KEY=$CONTAINER_NAME
fi
CONTAINER_STATUS="$($CONTAINER_CMD inspect --format "{{ .State.Status }}" $CONTAINER_KEY 2>/dev/null)"
CONTAINER_VERSION="$($CONTAINER_CMD inspect --format "{{ .Config.Labels.version }}" $CONTAINER_KEY 2>/dev/null)"
if [ -z "$CONTAINER_STATUS" ]; then return 0; fi
CONTAINER_IMAGE_ID="$($CONTAINER_CMD inspect --format "{{ .Image }}" $CONTAINER_KEY 2>/dev/null)"
if [ -z "$CONTAINER_ID" ]; then
CONTAINER_ID="$($CONTAINER_CMD inspect --format "{{ $TAG_CONTAINER_ID }}" $CONTAINER_KEY 2>/dev/null)"
fi
CONTAINER_SHORTID=$(echo $CONTAINER_ID | cut -d":" -f2 | cut -c1-12)
CONTAINER_IMAGE_ID="$($CONTAINER_CMD inspect --format "{{ .Image }}" $CONTAINER_KEY 2>/dev/null)"
CONTAINER_IMAGE_SHORTID=$(echo $CONTAINER_IMAGE_ID | cut -d":" -f2 | cut -c1-12)
if [ -z "$1" ]; then return 0; fi
CONTAINER_NAME_DISPLAY="$($CONTAINER_CMD inspect --format "{{ .Name }}" $CONTAINER_KEY | cut -d/ -f2 2>/dev/null)"
CURRENT_CONTAINER_IMAGE="$($CONTAINER_CMD inspect --format "{{ .Config.Image }}" $CONTAINER_KEY 2>/dev/null)"
CONTAINER_HOSTNAME="$($CONTAINER_CMD inspect --format "{{ .Config.Hostname }}" $CONTAINER_KEY 2>/dev/null)"
CONTAINER_DRIVER="$($CONTAINER_CMD inspect --format "{{ .Driver }}" $CONTAINER_KEY 2>/dev/null)"
CONTAINER_PLATFORM="$($CONTAINER_CMD inspect --format "{{ .Platform }}" $CONTAINER_KEY 2>/dev/null)"
CONTAINER_NETWORKMODE="$($CONTAINER_CMD inspect --format "{{ .HostConfig.NetworkMode }}" $CONTAINER_KEY 2>/dev/null)"
if [ -z "$CONTAINER_NETWORK_NAME" ]; then
CONTAINER_IPADDRESS="$($CONTAINER_CMD inspect --format "{{ .NetworkSettings.IPAddress }}" $CONTAINER_KEY 2>/dev/null)"
else
CONTAINER_IPADDRESS="$($CONTAINER_CMD inspect --format "{{ .NetworkSettings.Networks.$CONTAINER_NETWORK_NAME.IPAddress }}" $CONTAINER_KEY 2>/dev/null)"
fi
if [ "$CONTAINER_IPADDRESS" = "<no value>" ]; then
CONTAINER_IPADDRESS=
fi
CONTAINER_BUILDTIME="$($CONTAINER_CMD inspect --format "{{ .Config.Labels.buildtime }}" $CONTAINER_KEY 2>/dev/null)"
CONTAINER_VOLUMES="$($CONTAINER_CMD inspect --format '{{range .Mounts}} {{ .Source }} {{end}}' $CONTAINER_KEY | xargs 2>/dev/null)"
CONTAINER_MOUNTS="$($CONTAINER_CMD inspect --format '{{range .Mounts}} {{ .Destination }} {{end}}' $CONTAINER_KEY | xargs 2>/dev/null)"
if [ "$CONTAINER_CMD" = "docker" ]; then
CONTAINER_HEALTH="$($CONTAINER_CMD inspect --format "{{ .State.Health.Status }}" $CONTAINER_KEY 2>/dev/null)"
else
CONTAINER_HEALTH="$($CONTAINER_CMD inspect --format "{{ .State.Healthcheck.Status }}" $CONTAINER_KEY 2>/dev/null)"
fi
CONTAINER_DOMINO_VERSION="$($CONTAINER_CMD inspect --format "{{ index .Config.Labels \"DominoDocker.version\" }}" $CONTAINER_KEY 2>/dev/null)"
CONTAINER_TRAVELER_VERSION="$($CONTAINER_CMD inspect --format "{{ index .Config.Labels \"TravelerDocker.version\" }}" $CONTAINER_KEY 2>/dev/null)"
STARTDATE="$($CONTAINER_CMD inspect --format "{{ .State.StartedAt }}" $CONTAINER_KEY | cut -f1 -d+ 2>/dev/null)"
CONTAINER_STARTED_AT=$(date +"%d.%m.%Y %H:%M:%S" -d "$STARTDATE")
# Docker specific information
if [ "$CONTAINER_CMD" = "docker" ]; then
CONTAINER_RESTART_POLICY="$($CONTAINER_CMD inspect --format "{{ .HostConfig.RestartPolicy.Name }}" $CONTAINER_KEY 2>/dev/null)"
CONTAINER_RESTART_POLICY_MAX_RETRY="$($CONTAINER_CMD inspect --format "{{ .HostConfig.RestartPolicy.MaximumRetryCount }}" $CONTAINER_KEY 2>/dev/null)"
fi
return 0
}
inspect_container ()
{
CONTAINER_STATUS=
CONTAINER_VERSION=
if [ ! -e /usr/bin/jq ]; then
inspect_container_native "$@"
return 0
fi
CONTAINER_KEY=
if [ -n "$CONTAINER_CID" ]; then
if [ -e "$CONTAINER_CID" ]; then
CONTAINER_ID=$(cat "$CONTAINER_CID")
CONTAINER_KEY=$CONTAINER_ID
fi
fi
if [ -z "$CONTAINER_KEY" ]; then
CONTAINER_KEY=$CONTAINER_NAME
fi
local FULL_JSON=$($CONTAINER_CMD inspect $CONTAINER_KEY 2>/dev/null)
if [ -z "$FULL_JSON" ]; then
return 0
fi
if [ "$FULL_JSON" = "[]" ]; then
return 0
fi
local JSON=$(echo $FULL_JSON | jq .[0])
CONTAINER_STATUS=$(echo "$JSON" | jq -r .State.Status | sed "s/null//")
CONTAINER_VERSION=$(echo "$JSON" | jq -r .Config.Labels.version | sed "s/null//")
if [ -z "$CONTAINER_STATUS" ]; then return 0; fi
if [ -z "$CONTAINER_ID" ]; then
CONTAINER_ID=$(echo "$JSON" | jq -r $TAG_CONTAINER_ID | sed "s/null//")
fi
CONTAINER_SHORTID=$(echo $CONTAINER_ID | cut -d":" -f2 | cut -c1-12)
CONTAINER_IMAGE_ID=$(echo "$JSON" | jq -r .Image | sed "s/null//")
CONTAINER_IMAGE_SHORTID=$(echo $CONTAINER_IMAGE_ID | cut -d":" -f2 | cut -c1-12)
if [ -z "$1" ]; then return 0; fi
CONTAINER_NAME_DISPLAY=$(echo "$JSON" | jq -r .Name | sed "s/null//")
CURRENT_CONTAINER_IMAGE=$(echo "$JSON" | jq -r .Config.Image | sed "s/null//")
CONTAINER_HOSTNAME=$(echo "$JSON" | jq -r .Config.Hostname | sed "s/null//")
CONTAINER_DRIVER=$(echo "$JSON" | jq -r .Driver | sed "s/null//")
CONTAINER_PLATFORM=$(echo "$JSON" | jq -r .Platform | sed "s/null//")
CONTAINER_NETWORKMODE=$(echo "$JSON" | jq -r .HostConfig.NetworkMode | sed "s/null//")
if [ -z "$CONTAINER_NETWORK_NAME" ]; then
CONTAINER_IPADDRESS=$(echo "$JSON" | jq -r .NetworkSettings.IPAddress | sed "s/null//")
else
CONTAINER_IPADDRESS=$(echo "$JSON" | jq -r .NetworkSettings.Networks.$CONTAINER_NETWORK_NAME.IPAddress | sed "s/null//")
fi
if [ "$CONTAINER_IPADDRESS" = "<no value>" ]; then
CONTAINER_IPADDRESS=
fi
CONTAINER_BUILDTIME=$(echo "$JSON" | jq -r .Config.Labels.buildtime | sed "s/null//")
CONTAINER_VOLUMES=$(echo "$JSON" | jq -r .Mounts[].Source | sed "s/null//" | xargs)
CONTAINER_MOUNTS=$(echo "$JSON" | jq -r .Mounts[].Destination | sed "s/null//" | xargs)
if [ "$CONTAINER_CMD" = "docker" ]; then
CONTAINER_HEALTH=$(echo "$JSON" | jq -r .State.Health.Status | sed "s/null//")
else
CONTAINER_HEALTH=$(echo "$JSON" | jq -r .State.Healthcheck.Status | sed "s/null//")
fi
CONTAINER_DOMINO_VERSION=$(echo "$JSON" | jq -r '.Config.Labels."DominoDocker.version"' | sed "s/null//")
CONTAINER_TRAVELER_VERSION=$(echo "$JSON" | jq -r '.Config.Labels."TravelerDocker.version"' | sed "s/null//")
STARTDATE=$(echo "$JSON" | jq -r .State.StartedAt | sed "s/null//" | cut -f1 -d+ 2>/dev/null)
CONTAINER_STARTED_AT=$(date +"%d.%m.%Y %H:%M:%S" -d "$STARTDATE")
# Docker specific information
if [ "$CONTAINER_CMD" = "docker" ]; then
CONTAINER_RESTART_POLICY="$(echo "$JSON" | jq -r .HostConfig.RestartPolicy.Name | sed "s/null//")"
CONTAINER_RESTART_POLICY_MAX_RETRY="$(echo "$JSON" | jq -r .HostConfig.RestartPolicy.MaximumRetryCount | sed "s/null//")"
fi
return 0
}
container_inspect_extras ()
{
if [ -z "$1" ]; then return 0; fi
if [ "$CONTAINER_STATUS" = "running" ]; then
# CONTAINER_OS_VERSION=$($CONTAINER_CMD exec -it $CONTAINER_NAME cat /etc/os-release)
# CONTAINER_OS_KERNEL=$($CONTAINER_CMD exec -it $CONTAINER_NAME uname -r)
CONTAINER_DOMINO_REV=$($CONTAINER_CMD exec -it $CONTAINER_NAME cat /opt/hcl/domino/.install.dat | awk -F '(=| =)' -v SEARCH_STR="rev" '{if (tolower($1) == tolower(SEARCH_STR)) print $2}' | cut -d'"' -f2)
fi
}
container_show_infos ()
{
if [ -z "$CONTAINER_STATUS" ]; then
if [ -z "$IMAGE_ID" ]; then
log_space "Container and image not found"
return 0
else
log_space "Container not found"
fi
#return 0
fi
inspect_image
inspect_container
container_inspect_extras
print_delim
echo " Status : $CONTAINER_STATUS"
if [ "$IMAGE_VERSION" = "$CONTAINER_VERSION" ]; then
echo " Version : $IMAGE_VERSION"
else
echo " Version CNT : $CONTAINER_VERSION"
echo " Version IMG : $IMAGE_VERSION"
fi
echo " Container ID : $CONTAINER_SHORTID"
if [ "$IMAGE_ID" = "$CONTAINER_IMAGE_ID" ]; then
echo " Image-ID : $IMAGE_SHORTID"
else
echo " Image-ID CNT : $CONTAINER_IMAGE_SHORTID"
echo " Image-ID IMG : $IMAGE_SHORTID"
fi
print_delim
return 0
}
container_show_inspect ()
{
if [ -z "$CONTAINER_STATUS" ]; then
if [ -z "$IMAGE_ID" ]; then
echo "Container and image not found"
return 0
else
echo "Container not found"
fi
#return 0
fi
inspect_image "$1"
inspect_container "$1"
container_inspect_extras "$1"
echo
print_delim
echo " Runtime : $CONTAINER_ENV_NAME $CONTAINER_RUNTIME_VERSION"
echo " Status : $CONTAINER_STATUS"
echo " Health : $CONTAINER_HEALTH"
echo " Started : $CONTAINER_STARTED_AT"
echo
echo " Name : $CONTAINER_NAME_DISPLAY"
if [ ! "$CURRENT_CONTAINER_IMAGE" = "$CONTAINER_IMAGE" ]; then
echo " Current Image : $CURRENT_CONTAINER_IMAGE"
echo " New Image : $CONTAINER_IMAGE"
else
echo " Image : $CONTAINER_IMAGE"
fi
if [ "$IMAGE_VERSION" = "$CONTAINER_VERSION" ]; then
echo " Version : $IMAGE_VERSION"
else
echo " Version CNT : $CONTAINER_VERSION"
echo " Version IMG : $IMAGE_VERSION"
fi
if [ "$CONTAINER_DOMINO_VERSION" = "$IMAGE_DOMINO_VERSION" ]; then
echo " Domino Ver : $CONTAINER_DOMINO_VERSION"
else
echo " Domino Ver CNT : $CONTAINER_DOMINO_VERSION"
echo " Domino Ver IMG : $IMAGE_DOMINO_VERSION"
fi
if [ "$IMAGE_BUILDTIME" = "$CONTAINER_BUILDTIME" ]; then
echo " BuildTime : $IMAGE_BUILDTIME"
else
echo " BuildTime CNT : $CONTAINER_BUILDTIME"
echo " BuildTime IMG : $IMAGE_BUILDTIME"
fi
if [ ! -z "$CONTAINER_IMAGE_ID" ]; then
echo
echo " Hostname : $CONTAINER_HOSTNAME"
if [ -n "$IPAddress" ]; then
echo " IPAddress : $CONTAINER_ADDRESS"
fi
echo " Volumes : $CONTAINER_VOLUMES"
echo " Mounts : $CONTAINER_MOUNTS"
echo
fi
print_delim
echo " Container ID : $CONTAINER_SHORTID"
if [ "$IMAGE_ID" = "$CONTAINER_IMAGE_ID" ]; then
echo " Image-ID : $IMAGE_SHORTID"
else
echo " Image-ID CNT : $CONTAINER_IMAGE_SHORTID"
echo " Image-ID IMG : $IMAGE_SHORTID"
fi
print_delim
echo
if [ ! -z "$CONTAINER_IMAGE_ID" ]; then
echo " Image Size : $IMAGE_SIZE_MB MB"
echo " NetworkMode : $CONTAINER_NETWORKMODE"
echo " Driver : $CONTAINER_DRIVER"
# echo " Container OS : $CONTAINER_OS_VERSION"
# echo " Kernel : $CONTAINER_OS_KERNEL"
CONTAINER_PORTS=$($CONTAINER_CMD port "$CONTAINER_NAME")
if [ ! -z "$CONTAINER_PORTS" ]; then
echo " Container Ports:"
echo "$CONTAINER_PORTS" | awk '{print " " $0}'
print_delim
fi
if [ -n "$CONTAINER_RESTART_POLICY" ]; then
echo " Restart Policy : $CONTAINER_RESTART_POLICY"
if [ -n "$CONTAINER_RESTART_POLICY_MAX_RETRY" ]; then
echo " Restart Retries: $CONTAINER_RESTART_POLICY_MAX_RETRY"
fi
fi
fi
}
container_logs ()
{
if [ -z "$CONTAINER_STATUS" ]; then log_space "Container [$CONTAINER_NAME] does not exist"; return 1; fi
log_space "--- [ $CONTAINER_NAME ] ---"
$CONTAINER_CMD logs $CONTAINER_NAME
log_space "--- [ $CONTAINER_NAME ] ---"
return 0
}
container_build ()
{
if [ ! -e build.cfg ]; then
log_and_exit "No build config found in current directory"
fi
. build.cfg
# Podman started to use OCI images by default. We still want Docker image format
if [ -z "$BUILDAH_FORMAT" ]; then
BUILDAH_FORMAT=docker
fi
if [ -z "$CONTAINER_FILE" ]; then
if [ -r "Containerfile" ]; then
CONTAINER_FILE=Containerfile
elif [ -r "dockerfile" ]; then
CONTAINER_FILE=dockerfile
fi
if [ -z "$CONTAINER_FILE" ]; then
log_and_exit "No container file specified"
fi
fi
if [ ! -r "$CONTAINER_FILE" ]; then
log_and_exit "Cannot read container file [$CONTAINER_FILE]"
fi
if [ -z "$IMAGE_NAME" ]; then
log_and_exit "No target image defined."
fi
if [ -z "$BASE_IMAGE" ]; then
log_and_exit "No source image defined"
fi
IMAGE_NAMEVERSION=$IMAGE_NAME:$IMAGE_VERSION
log_space "Building Image : " $IMAGE_NAMEVERSION
if [ -n "$SOFTWARE_DIR" ]; then
SOFTWARE_USE_NGINX=1
fi
if [ "$SOFTWARE_USE_NGINX" = "1" ]; then
nginx_start
fi
BUILDTIME=$(date +"%d.%m.%Y %H:%M:%S")
if [ -n "$IMAGE_LATEST_TAG" ]; then
CONTAINER_TAG_LATEST_CMD="-t $IMAGE_NAME:$IMAGE_LATEST_TAG"
fi
$CONTAINER_CMD build --no-cache \
--label "version"="$IMAGE_VERSION" \
--label "buildtime"="$BUILDTIME" \
--label "description"="$IMAGE_LABEL_DESCRIPTION" \
--label "vendor"="$IMAGE_LABEL_VENDOR" \
--label "maintainer"="$IMAGE_LABEL_MAINTAINER" \
-t $IMAGE_NAMEVERSION $CONTAINER_TAG_LATEST_CMD \
-f $CONTAINER_FILE \
--build-arg "DownloadFrom=$DOWNLOAD_FROM" \
--build-arg LINUX_UPDATE=$LINUX_UPDATE \
--build-arg "SPECIAL_CURL_ARGUMENTS=$SPECIAL_CURL_ARGUMENTS" \
--build-arg BASE_IMAGE=$BASE_IMAGE .
echo
if [ "$SOFTWARE_USE_NGINX" = "1" ]; then
nginx_stop
fi
print_runtime
echo
return 0
}
docker_deamon()
{
if [ -z "$(uname -r|grep microsoft)" ]; then
echo "Docker deamon command only available on WSL"
return 0
fi
local DOCKERD_PID=$(pgrep dockerd)
case "$1" in
status|"")
echo
if [ -z "$DOCKERD_PID" ]; then
echo "Docker deamon: Stopped"
else
echo "Docker deamon: Running"
fi
echo
;;
start)
echo
if [ -n "$DOCKERD_PID" ]; then
echo "Docker deamon: Already running"
else
echo "Docker deamon: Starting ..."
if [ "0" = "$(id -u)" ]; then
dockerd > /dev/null 2>&1 &
else
su - -c "dockerd > /dev/null 2>&1 &"
fi
disown
fi
echo
;;
stop)
echo
if [ -z "$DOCKERD_PID" ]; then
echo "Docker deamon: Not running"
else
echo "Docker deamon: Stopping ..."
if [ "0" = "$(id -u)" ]; then
pkill dockerd
else
su - -c "pkill dockerd"
fi
seconds=0
DOCKERD_PID=$(pgrep dockerd)
while [ -n "$DOCKERD_PID" ]; do
sleep 1
seconds=$(expr $seconds + 1)
sec_mod=$(expr $seconds "%" 10)
if [ "$sec_mod" = "0" ]; then
echo " ... waiting $seconds seconds"
fi
# Terminate Domino Server if still running
if [ "$seconds" -ge "$DOMINO_SHUTDOWN_TIMEOUT" ]; then
echo "Docker deamon is still running after $seconds seconds"
echo " ... now for the ungraceful method"
if [ "0" = "$(id -u)" ]; then
pkill -9 dockerd
else
su - -c "pkill -9 dockerd"
fi