forked from essentialkaos/rpmbuilder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpmbuilder-farm
executable file
·1717 lines (1352 loc) · 31.9 KB
/
rpmbuilder-farm
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
#!/usr/bin/env bash
# shellcheck disable=SC2120
########################################################################################
if [[ -n "${SCRIPT_DEBUG}" ]] ; then
set -x
fi
########################################################################################
# App name
APP="rpmbuilder-farm"
# App version
VER="1.1.0"
# App description
DESC="Helper for bootstrapping rpmbuilder build farm"
########################################################################################
# shellcheck disable=SC2034
CL_PROMPT="\e[1m\e[7;${CYAN};49m"
################################################################################
CMD_INSTALL="install"
CMD_UNINSTALL="uninstall"
CMD_USER_ADD="add-user"
CMD_USER_DEL="del-user"
CMD_USER_MOD="mod-user"
CMD_USER_LIST="list-users"
CMD_UPDATE_IMAGES="update-images"
CMD_LIST_IMAGES="list-images"
CMD_CONTAINERS="containers"
CMD_CONFIG="config"
CMD_START="start"
CMD_RESTART="restart"
CMD_STOP="stop"
CMD_START_ALL="start-all"
CMD_RESTART_ALL="restart-all"
CMD_STOP_ALL="stop-all"
###############################################################################
KEYS_DIR="/etc/rpmbuilder/keys"
START_PORT=10000
###############################################################################
# Default image name
image="ghcr.io/essentialkaos/rpmbuilder"
# List of images
images=("centos7" "ol7" "ol8" "ol9")
###############################################################################
# Main function
#
# *: All arguments passed to script
#
# Code: No
# Echo: No
main() {
prepare
if [[ "$1" == "--version" || "$1" == "-v" ]] ; then
about
exit 0
fi
if [[ $# -eq 0 || "$1" == "--help" || "$1" == "-h" ]] ; then
usage
exit 0
fi
execCommand "$@"
exit $?
}
# Make some checks before executing actions
#
# Code: No
# Echo: No
prepare() {
local answer
if ! isRoot ; then
exit 1
fi
if ! hasApp "docker" ; then
error "docker is required, install it first"
exit 1
fi
if ! hasApp "ssh-keygen" ; then
error "ssh-keygen is required, install it first"
exit 1
fi
if ! hasApp "curl" ; then
error "curl is required, install it first"
exit 1
fi
if ! hasApp "path" ; then
show ""
show "\e[1mpath is required, install it?\e[0m"
read -e -r -p "$(getPrompt 'Y/N')" answer
show ""
if [[ ${answer^^} != "Y" ]] ; then
error "Okay, you can install it from https://kaos.sh/path"
exit 1
fi
curl --max-redirs 2 -L -# -o "/usr/bin/path" "https://apps.kaos.st/path/latest/linux/x86_64/path"
chmod +x /usr/bin/path
show "\e[32mpath successfully installed\e[0m"
fi
if ! hasApp "fmtc" ; then
show ""
show "\e[1mfmtc is required, install it?\e[0m"
read -e -r -p "$(getPrompt 'Y/N')" answer
show ""
if [[ ${answer^^} != "Y" ]] ; then
error "Okay, you can install it from https://kaos.sh/fmtc"
exit 1
fi
curl --max-redirs 2 -L -# -o "/usr/bin/fmtc" "https://apps.kaos.st/fmtc/latest/linux/x86_64/fmtc"
chmod +x /usr/bin/fmtc
show "\e[32mfmtc successfully installed\e[0m"
fi
}
# Execute command
#
# 1: Command name (String)
# *: Command arguments
#
# Code: No
# Echo: No
execCommand() {
local cmd="$1"
shift
show ""
if [[ "$cmd" != "$CMD_INSTALL" ]] && ! isFarmInstalled ; then
error "Farm is not installed on the system"
show ""
return 1
fi
case "$cmd" in
"$CMD_INSTALL") cmdInstall "$@" ;;
"$CMD_UNINSTALL") cmdUninstall "$@" ;;
"$CMD_USER_ADD") cmdUserAdd "$@" ;;
"$CMD_USER_DEL") cmdUserDel "$@" ;;
"$CMD_USER_MOD") cmdUserMod "$@" ;;
"$CMD_USER_LIST") cmdUserList "$@" ;;
"$CMD_LIST_IMAGES") cmdListImages "$@" ;;
"$CMD_UPDATE_IMAGES") cmdUpdateImages "$@" ;;
"$CMD_CONTAINERS") cmdContainers "$@" ;;
"$CMD_CONFIG") cmdConfig "$@" ;;
"$CMD_START") cmdStart "$@" ;;
"$CMD_RESTART") cmdRestart "$@" ;;
"$CMD_STOP") cmdStop "$@" ;;
"$CMD_START_ALL") cmdStartAll "$@" ;;
"$CMD_RESTART_ALL") cmdRestartAll "$@" ;;
"$CMD_STOP_ALL") cmdStopAll "$@" ;;
*) error "Unknown command \"$cmd\"\n"
exit 1 ;;
esac
local ec=$?
show ""
return "$ec"
}
################################################################################
# "install" command handler
#
# 1: Restart delay in hours (Number) [Optional]
#
# Code: Yes
# Echo: No
cmdInstall() {
local delay="${1:-12}"
if isFarmInstalled ; then
error "Farm already installed on the system"
return 1
fi
mkdir -p "$KEYS_DIR" &> /dev/null
action "$?" "Create directory for keys"
cat << EOF > /etc/systemd/system/rpmbuilder-farm-restart.timer
[Unit]
Description=This is the timer for restating build farm contenders
Requires=rpmbuilder-farm-restart.service
[Timer]
OnCalendar=*-*-* 00/$delay:00:00
Persistent=true
[Install]
WantedBy=timers.target
EOF
cat << EOF > /etc/systemd/system/rpmbuilder-farm-restart.service
[Unit]
Description=Restart all build farm containers
Wants=rpmbuilder-farm-restart.timer
[Service]
Type=oneshot
ExecStart=/usr/bin/rpmbuilder-farm restart-all
[Install]
WantedBy=multi-user.target
EOF
cat << EOF > /etc/systemd/system/rpmbuilder-farm.service
[Unit]
Description=RDS Sync Daemon
Documentation=https://kaos.sh/rds
Requires=network.target remote-fs.target
After=network.target remote-fs.target
[Service]
Type=oneshot
ExecStart=/usr/bin/rpmbuilder-farm start-all
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload &> /dev/null
systemctl enable rpmbuilder-farm.service &> /dev/null
systemctl start rpmbuilder-farm-restart.timer &> /dev/null
action 0 "Install services and timer to systemd"
ln -sf /usr/bin/rpmbuilder-farm /usr/bin/farm
action $? "Create alias 'farm' → 'rpmbuilder-farm'"
show ""
show "{g}{*}RPMBuilder Farm{!*} successfully installed on the system{!}"
show ""
cmdUpdateImages
return $?
}
# "uninstall" command handler
#
# Code: Yes
# Echo: No
cmdUninstall() {
rm -rf "$KEYS_DIR" &> /dev/null
action "$?" "Remove directory for keys"
systemctl disable rpmbuilder-farm.service &> /dev/null
systemctl stop rpmbuilder-farm-restart.timer &> /dev/null
rm -f /etc/systemd/system/rpmbuilder-farm* &> /dev/null
systemctl daemon-reload &> /dev/null
action 0 "Remove services and timer from systemd"
rm -f /usr/bin/farm &> /dev/null
action $? "Remove alias 'farm' → 'rpmbuilder-farm'"
show ""
show "{g}{*}RPMBuilder Farm{!*} successfully uninstalled from the system{!}"
return 0
}
# "list-images" command handler
#
# Code: Yes
# Echo: No
cmdListImages() {
local image_name image_id
for image_name in "${images[@]}" ; do
image_id=$(docker image ls | grep "node-${image_name}" | tr -s ' ' | cut -f3 -d' ')
show " {s-}•{!} $(getImageWithColor "$image_name") {s-}(${image_id:-—}){!}"
done
return 0
}
# "update-images" command handler
#
# Code: Yes
# Echo: No
cmdUpdateImages() {
local image_name has_errors
show "{*}Updating images…{!}"
for image_name in "${images[@]}" ; do
show ""
show "{c}{*}↓{!*} ${image}:{*}node-${image_name}{!*}…{!}"
if ! docker pull "$image:node-$image_name" ; then
has_errors=true
fi
done
show ""
if [[ -n "$has_errors" ]] ; then
warn "Can't update one or more images"
return 1
fi
show "{*}Restarting containers…{!}"
if ! cmdRestartAll ; then
return
fi
show "{*}Cleaning old images…{!}"
docker image prune -f &> /dev/null
show ""
show "{g}Images successfully updated{!}"
return 0
}
# "containers" command handler
#
# Code: Yes
# Echo: No
cmdContainers() {
if ! hasUsers ; then
warn "There are no users or containers"
return 0
fi
local user image_name stat_info uptime_info fmtline
local container_stat_info container_uptime_info cpu mem status host_port
fmtc -L "{s-}Collecting containers info…{!}"
stat_info=$(docker stats --no-stream --format '{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}')
uptime_info=$(docker ps --format "{{.Names}}\t{{.Status}}")
printf '\e[2K\r'
printf "{*}%-16s %-10s %-10s %-12s %-10s %-24s %s{!}\n" "OWNER" "IMAGE" "PORT" "STATE" "CPU" "MEMORY" "STATUS" | fmtc
showSeparator 100
for user in $(getUserList) ; do
for image_name in "${images[@]}" ; do
if isCurrentUser "$user" ; then
fmtline="{*}%-16s{!}"
else
fmtline="{s}%-16s{!}"
fi
container_name="${user}-${image_name}"
container_stat_info=$(grep "$container_name" <<< "$stat_info")
container_uptime_info=$(grep "$container_name" <<< "$uptime_info")
fmtline="$fmtline $(getImageColor "$image_name")%-10s{!}"
if [[ -n "$container_stat_info" ]] ; then
fmtline="$fmtline {s-}:{!}{s}%-9s{!} {g}%-12s{!} %-10s %-24s %s\n"
cpu=$(echo "$container_stat_info" | cut -f2)
mem=$(echo "$container_stat_info" | cut -f3)
status=$(echo "$container_uptime_info" | cut -f2)
host_port=$(getHostPort "$user" "$image_name")
# shellcheck disable=SC2059
printf "$fmtline" "$user" "$image_name" "$host_port" "running" "$cpu" "$mem" "$status" | fmtc
else
fmtline="$fmtline {s-}%-10s{!} {s-}%-12s{!} {s-}%-10s{!} {s-}%-24s{!} {s-}%s{!}\n"
unset cpu mem status
# shellcheck disable=SC2059
printf "$fmtline" "$user" "$image_name" "-" "stopped" "-" "-" "-" | fmtc
fi
done
showSeparator 100
done
return 0
}
# "add-user" command handler
#
# 1: User name (String)
#
# Code: Yes
# Echo: No
cmdConfig() {
local user="$1"
if [[ -z "$user" ]] ; then
error "You must provide user name"
return 1
fi
if ! hasContainers "$user" ; then
error "There are no containers for user $user"
return 1
fi
local ip image_name host_port
read -e -r -p "$(getPrompt 'HOST PUBLIC IP / HOSTNAME')" ip
show ""
show "Add next lines to the file {#172}~/.config/rpmbuilder/buildnodes.list{!}"
show ""
for image_name in "${images[@]}" ; do
if ! isContainerWorks "$user" "$image_name" ; then
continue
fi
host_port=$(getHostPort "$user" "$image_name")
show "{s}builder@${ip}:${host_port}{!}"
done
show ""
fmtc << EOF
Add next lines to the file {#172}~/.config/rpmbuilder/rpmbuilder.conf{!}
{s-}# File with build nodes{!}
{s}remote: ~/.config/rpmbuilder/buildnodes.list{!}
{s-}# Path to private key{!}
{s}key: ~/.ssh/buildnode{!}
{y}Note that {*}~/.ssh/buildnode{!*} must be the path to your private key.{!}
EOF
}
# "add-user" command handler
#
# 1: User name (String)
#
# Code: Yes
# Echo: No
cmdUserAdd() {
local user="$1"
if [[ -z "$user" ]] ; then
error "You must provide user name"
return 1
fi
if hasUser "$user" ; then
error "User \"$user\" already exist"
return 1
fi
if ! isValidUser "$user" ; then
error "\"$user\" is not valid user name"
return 1
fi
local pub_key answer
while : ; do
read -e -r -p "$(getPrompt 'PUBLIC KEY')" pub_key
show ""
if ! isValidKey "$pub_key" ; then
warn "Given public key is not valid"
else
break
fi
done
if ! addUser "$user" "$pub_key" ; then
error "Can't create new user"
return 1
fi
show "{g}User successfully created{!}"
show ""
show "Run containers for this user?"
read -e -r -p "$(getPrompt 'Y/N')" answer
if [[ ${answer^^} != "Y" ]] ; then
return 0
fi
# cmdStart "$user"
return $?
}
# "del-user" command handler
#
# 1: User name (String)
#
# Code: Yes
# Echo: No
cmdUserDel() {
local user="$1"
if [[ -z "$user" ]] ; then
error "You must provide user name"
return 1
fi
if ! hasUser "$user" ; then
error "There is no user \"$user\""
return 1
fi
if hasContainers "$user" ; then
if ! cmdStop "$user" ; then
return 1
fi
show ""
fi
if ! delUser "$user" ; then
error "Can't delete user"
return 1
fi
show "{g}User successfully deleted{!}"
return 0
}
# "mod-user" command handler
#
# 1: User name (String)
#
# Code: Yes
# Echo: No
cmdUserMod() {
local user="$1"
if [[ -z "$user" ]] ; then
error "You must provide user name"
return 1
fi
if ! hasUser "$user" ; then
error "There is no user \"$user\""
return 1
fi
if hasContainers "$user" ; then
if ! cmdStop "$user" ; then
return 1
fi
show ""
fi
local pub_key
while : ; do
read -e -r -p "$(getPrompt 'PUBLIC KEY')" pub_key
show ""
if ! isValidKey "$pub_key" ; then
warn "Given public key is not valid"
else
break
fi
done
if ! modUser "$user" "$pub_key" ; then
error "Can't update public key"
return 1
fi
show "{g}Public key successfully updated{!}"
return 0
}
# "list-users" command handler
#
# Code: Yes
# Echo: No
cmdUserList() {
if ! hasUsers ; then
warn "There are no users"
return 0
fi
local user key_info
for user in $(getUserList) ; do
key_info=$(getUserPubKeyInfo "$user")
if isCurrentUser "$user" ; then
show " {s-}•{!} {*}$user{!} {s}—{!} {s-}$key_info{!}"
else
show " {s-}•{!} {s}$user{!} {s}—{!} {s-}$key_info{!}"
fi
done
return 0
}
# "start" command handler
#
# 1: User name (String)
# 2: Image name (String) [Optional]
#
# Code: Yes
# Echo: No
cmdStart() {
local user="$1"
local image_name="$2"
if [[ -z "$user" ]] ; then
error "You must provide user name"
return 1
fi
if ! hasUsers ; then
error "There are no farm users. Use \"add-user\" command to create a new user."
return 1
fi
if ! hasUser "$user" ; then
error "There is no user \"$user\""
return 1
fi
if [[ -n "$image_name" ]] ; then
cmdStartOne "$user" "$image_name"
return $?
fi
cmdStartMany "$user"
return $?
}
# "start" command handler for one container
#
# 1: User name (String)
# 2: Image name (String)
#
# Code: Yes
# Echo: No
cmdStartOne() {
local user="$1"
local image_name="$2"
if ! hasImage "$image_name" ; then
error "There is no image \"$image_name\""
return 1
fi
if isContainerWorks "$user" "$image_name" ; then
error "Container \"$user/$image_name\" already works"
return 1
fi
show "{*}Starting container {#166}$user{!}{s}/{!}$(getImageWithColor "$image_name"){*}…{!}"
if ! startContainer "$user" "$image_name" ; then
error "Can't start container \"$user/$image_name\""
return 1
fi
show "{g}Container successfully started{!}"
return 0
}
# "start" command handler for many containers
#
# 1: User name (String)
# 2: Quiet flag (Boolean) [Optional]
#
# Code: Yes
# Echo: No
cmdStartMany() {
local user="$1"
local quiet="$2"
local image_name has_actions has_problems
for image_name in "${images[@]}" ; do
if isContainerWorks "$user" "$image_name" ; then
continue
fi
has_actions=true
show "{*}Starting container {#166}$user{!}{s}/{!}$(getImageWithColor "$image_name"){*}…{!}"
if ! startContainer "$user" "$image_name" ; then
has_problems=true
error "Can't start container"
show ""
continue
fi
show "{g}Container successfully started{!}"
show ""
done
if [[ -z "$has_actions" && -z "$quiet" ]] ; then
warn "All containers of user \"$user\" already work"
return 0
fi
if [[ -n "$has_problems" ]] ; then
warn "One or more containers started with errors"
return 1
fi
if [[ -z "$quiet" ]] ; then
show "{g}All contenders successfully started{!}"
fi
return 0
}
# "restart" command handler
#
# Code: Yes
# Echo: No
cmdRestart() {
local user="$1"
local image_name="$2"
if [[ -z "$user" ]] ; then
error "You must provide user name"
return 1
fi
if ! hasUsers ; then
error "There are no farm users. Use \"add-user\" command to create a new user."
return 1
fi
if ! hasUser "$user" ; then
error "There is no user \"$user\""
return 1
fi
if [[ -n "$image_name" ]] ; then
cmdRestartOne "$user" "$image_name"
return $?
fi
cmdRestartMany "$user"
return $?
}
# "restart" command handler for one container
#
# 1: User name (String)
# 2: Image name (String)
#
# Code: Yes
# Echo: No
cmdRestartOne() {
local user="$1"
local image_name="$2"
if ! hasImage "$image_name" ; then
error "There is no image \"$image_name\""
return 1
fi
if ! isContainerWorks "$user" "$image_name" ; then
error "Container \"$user/$image_name\" is not running"
return 1
fi
show "{*}Restarting container {#166}$user{!}{s}/{!}$(getImageWithColor "$image_name"){*}…{!}"
if ! stopContainer "$user" "$image_name" ; then
error "Can't stop container \"$user/$image_name\""
return 1
fi
if ! startContainer "$user" "$image_name" ; then
error "Can't start container \"$user/$image_name\""
return 1
fi
show "{g}Container successfully restarted{!}"
return 0
}
# "restart" command handler for many containers
#
# 1: User name (String)
# 2: Quiet flag (Boolean) [Optional]
#
# Code: Yes
# Echo: No
cmdRestartMany() {
local user="$1"
local quiet="$2"
local image_name has_actions has_problems
for image_name in "${images[@]}" ; do
if ! isContainerWorks "$user" "$image_name" ; then
continue
fi
has_actions=true
show "{*}Restarting container {#166}$user{!}{s}/{!}$(getImageWithColor "$image_name"){*}…{!}"
if ! stopContainer "$user" "$image_name" ; then
has_problems=true
error "Can't stop container \"$user/$image_name\""
show ""
continue
fi
if ! startContainer "$user" "$image_name" ; then
has_problems=true
error "Can't start container \"$user/$image_name\""
show ""
continue
fi
show "{g}Container successfully restarted{!}"
show ""
done
if [[ -z "$has_actions" && -z "$quiet" ]] ; then
warn "All containers of user \"$user\" are stopped and no restart is required"
return 0
fi
if [[ -n "$has_problems" ]] ; then
warn "One or more containers restarted with errors"
return 1
fi
if [[ -z "$quiet" ]] ; then
show "{g}All contenders successfully restarted{!}"
fi
return 0
}
# "stop" command handler
#
# Code: Yes
# Echo: No
cmdStop() {
local user="$1"
local image_name="$2"
if [[ -z "$user" ]] ; then
error "You must provide user name"
return 1
fi
if ! hasUser "$user" ; then
error "There is no user \"$user\""
return 1
fi
if [[ -n "$image_name" ]] ; then
cmdStopOne "$user" "$image_name"
return $?
fi
cmdStopMany "$user"
return $?
}
# "stop" command handler for one container
#
# 1: User name (String)
# 2: Image name (String)
#
# Code: Yes
# Echo: No
cmdStopOne() {
local user="$1"
local image_name="$2"
if ! hasImage "$image_name" ; then
error "There is no image \"$image_name\""
return 1
fi
if ! isContainerWorks "$user" "$image_name" ; then
error "Container \"$user/$image_name\" is stopped"
return 1
fi
show "{*}Stopping container {#166}$user{!}{s}/{!}$(getImageWithColor "$image_name"){*}…{!}"
if ! stopContainer "$user" "$image_name" ; then
error "Can't stop container \"$user/$image_name\""
fi
show "{g}Container successfully stopped{!}"
return 0
}
# "stop" command handler for many containers
#
# 1: User name (String)
# 2: Quiet flag (Boolean) [Optional]
#
# Code: Yes
# Echo: No
cmdStopMany() {
local user="$1"
local quiet="$2"
local image_name has_actions has_problems
for image_name in "${images[@]}" ; do
if ! isContainerWorks "$user" "$image_name" ; then
continue
fi
has_actions=true
show "{*}Stopping container {#166}$user{!}{s}/{!}{#81}$image_name{!}{*}…{!}"
if ! stopContainer "$user" "$image_name" ; then
has_problems=true
error "Can't stop container"
show ""
continue
fi
show "{g}Container successfully stopped{!}"
show ""
done
if [[ -z "$has_actions" && -z "$quiet" ]] ; then
warn "All containers of user \"$user\" already stopped"
return 0
fi
if [[ -n "$has_problems" ]] ; then
warn "One or more containers stopped with errors"
return 1
fi
if [[ -z "$quiet" ]] ; then
show "{g}All contenders successfully stopped{!}"
fi
return 0
}
# "start-all" command handler
#
# Code: Yes
# Echo: No
cmdStartAll() {
if ! hasUsers ; then
warn "There are no users or containers"
return 0