-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.sh
1699 lines (1356 loc) · 45.4 KB
/
functions.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
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
# Check what the error exiting policy currently is
#set -o|grep errexit
# Disable automatic exiting in case of errors
#set +e
#set +o errexit
# Test BASH Variable Expansion
#test_expansion() {
# local ltest1="${@:2}"
# local ltest2="${*:2}"
#
# echo ${ltest1}
# echo ${ltest2}
#}
# Print Debug Message if Environment DEBUG_CONTAINER is set to something
debug_message() {
# Debug Message processes all arguments
local lmessage="${*}"
# Calling Stack
#local lcallingstack=("${FUNCNAME[@]:1}")
#local lstack="${FUNCNAME[@]}"
local lstack="${FUNCNAME}"
# Print Stack
#echo "Calling Debug from <${FUNCNAME[1]}>" >&2
#echo "Calling Stack Size: <${#FUNCNAME[@]}>" >&2
# Check if Environment Variable is Set
if [[ -n "${DEBUG_CONTAINER}" ]]
then
# Show the Debug Message
echo "${lmessage}" >&2
if [[ -n "${DEBUG_CONTAINER_STACK}" ]]
then
# Show the Debug Stack
echo "Call Stack:" >&2
# Show the Debug Stack
debug_stack "${lstack}"
fi
fi
}
# Print Stack Size
debug_stack() {
# Debug Stack Local Variable
#local lstack="${*}"
local lstack=("${FUNCNAME[@]:1}")
# Number of Elements
local lnum=${#lstack[@]}
# Debug
#echo "${FUNCNAME[0]} - Stack has <${lnum}> Elements."
#echo "First: ${lstack[0]}"
#echo "Second: ${lstack[1]}"
#echo "Third: ${lstack[2]}"
# Last Index
local llast=$((lnum-1))
# Iterate
local lindex=0
local lindent=""
for lindex in $(seq 0 ${llast})
do
lindent=$(repeat_character "\t" "${lindex}")
echo -e "${lindent} [${lindex}] ${lstack[${lindex}]}" >&2
done
}
# Repeat Character N times
repeat_character() {
# Character to repeat
local lcharacter=${1}
# Number of Repetitions
local lrepetitions=${2}
# Print using Brace Expansion
#for i in {1 ... ${lrepetitions}}
for i in $(seq 1 1 ${lrepetitions})
do
echo -n "${lcharacter}"
done
}
# Add Line Separator
add_separator() {
local lcharacter=${1-"#"}
local lrows=${2-"1"}
# Get width of Terminal
local lwidth=$(tput cols)
# Repeat Character
for r in $(seq 1 1 ${lrows})
do
repeat_character "${lcharacter}" "${lwidth}"
done
}
# Add Line Separator with Description
add_section() {
local lcharacter=${1-"#"}
local lrows=${2-"1"}
local ldescription=${3-""}
# Determine number of Separators BEFORE and AFTER the Description
#local lrowsseparatorsbefore=$(echo "${lrows-1} / ( 2 )" | bc -l)
#local lrowsseparatorafter="${lrowsseparatorsbefore}"
local lrowsbefore="${lrows}"
local lrowsafter="${lrows}"
# Add Separator
add_separator "${lcharacter}" "${lrowsbefore}"
# Add Header with Description
add_description "${lcharacter}" "${ldescription}"
# Add Separator
add_separator "${lcharacter}" "${lrowsafter}"
}
add_description() {
# User Inputs
local lcharacter=${1-"#"}
local ldescription=${2-""}
# Add one Space before and after the original String
ldescription=" ${ldescription} "
# Get width of Terminal
local lwidth=$(tput cols)
# Get length of Description
local llengthdescription=${#ldescription}
# Get width of Terminal
local lwidth=$(tput cols)
# Subtract Description from Terminal Width
local llengthseparator=$((lwidth - llengthdescription))
# Divide by two
local llengtheachseparator=$(echo "${llengthseparator} / ( 2 )" | bc -l)
# Remainer
local lremainer=$((llengthseparator % 2))
local lextrastr=$(repeat_character "${lcharacter}" "${lremainer}")
# Get String of Characters for BEFORE and AFTER the Description
local lseparator=$(repeat_character "${lcharacter}" "${llengtheachseparator}")
# Print Description Line
echo "${lseparator}${ldescription}${lextrastr}${lseparator}"
}
# Check if Array Contains Element
array_contains() {
local larr=${1}
local lsearch=${2}
# Initialize Return Status
local lstatus=0
# Loop over elements
local litem=""
for litem in "${larr[@]}"
do
if [[ "${litem}" == "${lsearch}" ]]
then
# Found it
lstatus=1
fi
done
# Return Status
echo lstatus
# Exit Code
if [[ "${lstatus}" == "1" ]]
then
# Exit Normally
return 0
else
# Issue a Warning when quitting
return 1
fi
}
# Replace Text in Template
replace_text() {
local lfilepath=${1}
local lnargin=$#
local lnparameters=$(($((${lnargin}-1)) / 2))
local lARGV=("$@")
#Debug
debug_message "${FUNCNAME[0]} - Passed <${lnargin}> arguments and <${lnparameters}> parameter"
# Initialize Variables
local p=1
for ((p=1;p<=${lnparameters};p++))
do
local liname=$((2*p-1))
local livalue=$((${liname}+1))
local lname=${lARGV[${liname}]}
local lvalue=${lARGV[${livalue}]}
# Debug
debug_message "${FUNCNAME[0]} - Replace <{{${lname}}}> -> <${lvalue}> in <${lfilepath}>"
# Execute Replacement
sed -Ei "s|\{\{${lname}\}\}|${lvalue}|g" "${lfilepath}"
done
}
# Schedule Mode is NOT Supported
schedule_mode_not_supported() {
local lschedulemode=${1}
echo "Scheduling Mode <${lschedulemode}> is NOT supported. Possible choices are <cron> or <systemd>. Aborting !"
return 2
#break
}
# Get Homedir
get_homedir() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
# First check if user exists
getent passwd "${luser}" 2>&1 >/dev/null
# Store Exit Code
local luserexists=$?
if [[ ${luserexists} -eq 0 ]]
then
# Get homedir
local lhomedir=$(getent passwd "${luser}" | cut -d: -f6)
# Debug
debug_message "${FUNCNAME[0]} - Local Home Directory of User <${luser}> is <${lhomedir}>."
# Check if it makes sense
# Empty Value means that the requested User hasn't been found
# Return result
echo ${lhomedir}
else
# Print Error
echo "ERROR: User <${luser}> does NOT exist. ABORTING !"
#break
echo ""
return 1
fi
}
# Get Local Bin Path
get_localbinpath() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
# Declare Variable
local llocalbinpath=""
if [[ "${luser}" == "root" ]]
then
# Simply use System's /usr/local/bin
llocalbinpath="/usr/local/bin"
else
# Get User Home Directory
local lhomedir=$( get_homedir "${luser}" )
# Store in $HOME/.local/bin
llocalbinpath="${lhomedir}/.local/bin"
fi
# Debug
debug_message "${FUNCNAME[0]} - Local bin Path of User <${luser}> is <${llocalbinpath}>."
# Return result
echo ${llocalbinpath}
}
# Get Systemdconfig
get_systemdconfigdir() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
# Initialize Variable
local lsystemdconfigdir=""
if [[ "${luser}" == "root" ]]
then
lsystemdconfigdir="/etc/systemd/system"
else
local luserhomedir=$(get_homedir "${luser}")
lsystemdconfigdir="${luserhomedir}/.config/systemd/user"
fi
# Debug
debug_message "${FUNCNAME[0]} - Systemd Config Directory of User <${luser}> is <${lsystemdconfigdir}>."
# Make sure to create it if not existing already
if [[ ! -d "${lsystemdconfigdir}" ]]
then
# Debug
debug_message "${FUNCNAME[0]} - Folder <${lsystemdconfigdir}> does NOT Currently Exist."
debug_message "${FUNCNAME[0]} - Creating Folder <${lsystemdconfigdir}> now."
mkdir -p "${lsystemdconfigdir}"
fi
# Return result
echo ${lsystemdconfigdir}
}
# Execute Systemd Command
generic_cmd() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lcommand=${2}
# When used with Systemd: Arguments includes "action", possible options, "service" (e.g. systemd <status> <container-test.service>)
local larguments=${@:3}
# Who is executing the Script
local lexecutingUser=$(whoami)
# Debug
debug_message "${FUNCNAME[0]} - Execute generic command targeting user <${luser}> with command <${lcommand}> arguments <${larguments}>"
# Check who is the target User
if [[ "${luser}" == "root" ]]
then
# Run without runuser and without --user
# Run Command System-Wide
${lcommand} ${larguments}
else
if [[ "${lexecutingUser}" == "root" ]]
then
# Run Command as root user and target a different non-root User
runuser -l ${luser} -c ${lcommand} ${larguments}
elif [[ "${luser}" == "${lexecutingUser}" ]]
then
# Run Command directly (target user is the same as the user that is executing the script / function)
${lcommand} ${larguments}
fi
fi
}
# Execute Systemd Command
systemd_cmd() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local laction=${2}
local lservice=${3}
#local loptions=${*:2} # Old
local loptions=${*:4} # New
#local loptions=("${[@]:1}") # Untested
# Who is executing the Script
local lexecutingUser=$(whoami)
# Debug
debug_message "${FUNCNAME[0]} - Execute systemd command targeting user <${luser}> with action <${laction}> and options <${loptions}> for service <${lservice}>"
if [[ "${luser}" == "root" ]]
then
# Run without runuser and without --user
# Run Command System-Wide
systemctl ${laction} ${lservice} ${loptions}
else
if [[ "${lexecutingUser}" == "root" ]]
then
# Run with runuser and with --user
# Run Command as root using "runuser" and target a different non-root User
runuser -l "${luser}" -c "systemctl --user ${laction} ${lservice} ${loptions}"
elif [[ "${luser}" == "${lexecutingUser}" ]]
then
# Run Systemd Command directly with --user Option (target user is the same as the user that is executing the script / function)
systemctl --user ${laction} ${lservice} ${loptions}
fi
fi
}
# Execute Systemd Command
journald_cmd() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local laction=${2}
local lservice=${3}
#local loptions=${*:2} # Old
local loptions=${*:4} # New
# Who is executing the Script
local lexecutingUser=$(whoami)
# Debug
debug_message "${FUNCNAME[0]} - Execute journald command targeting user <${luser}> with action <${laction}> and options <${loptions}> for service <${lservice}>"
if [[ "${luser}" == "root" ]]
then
# Run without runuser and without --user
# Run Command System-Wide
journalctl "${laction}" "${lservice}" ${loptions}
else
if [[ "${lexecutingUser}" == "root" ]]
then
# Run with runuser and with --user
# Run Command as root user and target a different non-root User
runuser -l ${luser} -c "journalctl --user \"${laction}\" \"${lservice}\" ${loptions}"
elif [[ "${luser}" == "${lexecutingUser}" ]]
then
# Run without runuser and with --user
# Run Systemd Command directly with --user Option (target user is the same as the user that is executing the script / function)
journalctl --user "${laction}" "${lservice}" ${loptions}
fi
fi
}
# Mask service(s)
systemd_mask() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=$1
local lservice=$2
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Run Command using Wrapper
systemd_cmd "${luser}" "mask" "${lservice}"
fi
}
# Unmask service(s)
systemd_unmask() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=$1
local lservice=$2
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Run Command using Wrapper
systemd_cmd "${luser}" "unmask" "${lservice}"
fi
}
# Enable service
systemd_enable() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Run Command using Wrapper
systemd_cmd "${luser}" "enable" "${lservice}"
fi
}
# Disable service
systemd_disable() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Run Command using Wrapper
systemd_cmd "${luser}" "disable" "${lservice}"
fi
}
# Add servicee
systemd_add() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservicefile=${3}
# Service Name is just the basename of the service file
local lservicename=$(basename "${lservicefile}")
# To be completed ...
}
# Delete service
systemd_delete() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Debug
debug_message "${FUNCNAME[0]} - Disable + Stop Systemd Service <${lcontainer}>"
# Disable Service
systemd_disable "${luser}" "${lservice}"
# Stop Service
systemd_stop "${luser}" "${lservice}"
# Get Systemd Configuration Folder
local lsystemdfolder=$(get_systemdconfigdir "${luser}")
# Get Systemd Service File Name
local lservicefile="${lservice}"
# Define Systemd Service File Path
local lservicepath="${lsystemdfolder}/${lservice}"
# Remove Service File
if [[ -f "${lservicepath}" ]]
then
# Debug
debug_message "${FUNCNAME[0]} - Remove Systemd Service <${lservicepath}> from Disk"
# Remove it
rm "${lservicepath}"
fi
# Debug
debug_message "${FUNCNAME[0]} - Reload Systemd Daemon"
# Reload Systemd Daemon again
sleep 0.5
systemd_daemon_reload "${luser}"
sleep 0.5
fi
}
# Status of service(s)
systemd_status() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Run Command using Wrapper
systemd_cmd "${luser}" "status" "${lservice}" "--no-pager"
fi
}
systemd_restart() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Run Command using Wrapper
systemd_cmd "${luser}" "restart" "${lservice}"
fi
}
systemd_stop() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Run Command using Wrapper
systemd_cmd "${luser}" "stop" "${lservice}"
fi
}
systemd_start() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Run Command using Wrapper
systemd_cmd "${luser}" "start" "${lservice}"
fi
}
systemd_exists() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Get Systemd Config Directory
local lsystemdfolder=$(get_systemdconfigdir "${luser}")
# Build Systemd Service Path
local lservicepath="${lsystemdfolder}/${lservice}"
# Debug
debug_message "Check if Systemd Service <${lservice}> exists for User <${luser}>"
# Check if Service Exists
if [[ -f "${lservicepath}" ]]
then
# Debug
debug_message "Systemd Service <${lservice}> exists at <${lservicepath}>"
# Return Code
return 0
else
# Debug
debug_message "Systemd Service <${lservice}> does NOT exists at <${lservicepath}>"
# Return Code
return 1
fi
}
systemd_reload() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
if [[ -z "${lservice}" ]]
then
# Just run systemd_daemon_reload since no service was specified
systemd_daemon_reload "${luser}"
else
# Check if Service provides a <reload> method
local lcanreload=$(systemctl show "${lservice}" --property=CanReload --value)
if [[ "${lcanreload}" == "yes" ]]
then
# If yes, execute reload of the service only
# Run Command using Wrapper
systemd_cmd "${luser}" "reload" "${lservice}"
# Also Reset Errors for that Service
systemd_reset "${luser}" "${lservice}"
else
# Just run systemd_daemon_reload since the service doesn't provide a <reload> method
systemd_daemon_reload "${luser}"
fi
fi
}
systemd_reset() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Run Command using Wrapper
systemd_cmd "${luser}" "reset-failed" "${lservice}"
fi
}
systemd_daemon_reload() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
# Run Command using Wrapper
systemd_cmd "${luser}" "daemon-reload"
}
systemd_daemon_reexec() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
# Run Command using Wrapper
systemd_cmd "${luser}" "daemon-reexec"
}
journald_log() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Check if Service Exists
systemd_exists "${luser}" "${lservice}"
local lexistscode=$?
if [[ ${lexistscode} -eq 0 ]]
then
# Run Command using Wrapper
journald_cmd "${luser}" "-xeu" "${lservice}" "--no-pager"
fi
}
# Shortcut to Systemd daemon-reload + enable + restart service
systemd_reload_enable() {
# User is the TARGET user, NOT (necessarily) the user executing the script / function !
local luser=${1}
local lservice=${2}
# Reload Systemd Service Files
sleep 0.5
systemd_reload "${luser}" "${lservice}"
sleep 0.5
# Enable the Service to start automatically at each boot
systemd_enable "${luser}" "${lservice}"
# Start the Service
systemd_restart "${luser}" "${lservice}"
# Verify the Status is OK
systemd_status "${luser}" "${lservice}" "--no-pager"
# Check the logs from time to time and in case of issues
journald_log "${luser}" "${lservice}" "--no-pager"
}
# List subuid / subgid
list_subuid_subgid() {
local lSUBUID=/etc/subuid
local lSUBGID=/etc/subgid
local lUSERS
local li=0
for li in ${lSUBUID} ${lSUBGID}; do [[ -f "${li}" ]] || { echo "ERROR: ${li} does not exist, but is required."; return 1; }; done
[[ -n "${1}" ]] && lUSERS=${1} || lUSERS=$(awk -F : '{x=x " " ${1}} END{print x}' ${lSUBUID})
for i in ${lUSERS}; do
awk -F : "\${1} ~ /${li}/ {printf(\"%-16s sub-UIDs: %6d..%6d (%6d)\", \${1} \",\", \${2}, \${2}+\${3}, \${3})}" ${lSUBUID}
awk -F : "\${1} ~ /${li}/ {printf(\", sub-GIDs: %6d..%6d (%6d)\", \${2}, \${2}+\${3}, \${3})}" ${lSUBGID}
echo ""
done
}
# Get Homedir
get_homedir() {
local luser=${1}
# Get homedir
local lhomedir=$(getent passwd "${luser}" | cut -d: -f6)
# Return$ result
echo ${lhomedir}
}
# Make Mutable if Exist
make_mutable_if_exist() {
local ltarget=${1}
if [ -d "${ltarget}" ] || [ -f "${ltarget}" ]
then
# Remove the Immutable Flag
chattr -i "${ltarget}"
fi
}
# Make Immutable if Exist
make_immutable_if_exist() {
local ltarget=${1}
if [ -d "${ltarget}" ] || [ -f "${ltarget}" ]
then
# Add the Immutable Flag
chattr +i "${ltarget}"
fi
}
# Move if Exist
move_if_exist() {
local lorigin=${1}
local ldestination=${2}
if [ -d "${lorigin}" ] || [ -f "${lorigin}" ]
then
# Move to Destination
mv "${lorigin}" "${ldestination}"
fi
}
# Remove empty Folder if Exist
rmdir_if_exist() {
local ltarget=${1}
if [ -d "${ltarget}" ]
then
# Attempt to remove Empty Folder
rmdir "${ltarget}"
# Check Return Code
if [[ "$?" -ne 0 ]]
then
echo "FAILED to remove Folder <${ltarget}>. Error code of `rmdir` was $?. Possible NON-EMPTY Directory ?"
fi
fi
}
# Remove leading and/or trailing Slashes ("/")
remove_leading_trailing_slashes() {
# Init Variable
local lsanitized=${1}
# Remove leading and trailing Slashes
lsanitized=${lsanitized%/};
lsanitized=${lsanitized#/}
# Echo & Return
echo ${lsanitized}
}
# Get Containers Associated with Compose File
get_containers_from_compose_dir() {
# The return Array is passed by nameref
# Reference to output array
declare -n lreturnarray="${1}"
# The compose Directory is passed as an Argument
local lcomposedir=${2-""}
if [[ -z "${lcomposedir}" ]]
then
lcomposedir=$(pwd)
fi
# Optional Parameter stating if we want all Containers in the Compose file (to bring them DOWN) or just the ones that are enabled in the compose File
local lwhichcontainers=${3-""}
# Debug
debug_message "Get list of Containers from Compose Directory <${lcomposedir}> based on <compose.yml> File."
# Extract from the File itself
#mapfile list < <( grep -r -h "container_name:" "${lcomposedir}/compose.yml" | sed -E "s|^\s*?#?\s*?container_name:\s*?([a-zA-Z0-9_-]+)\s*?$|\1|g" )
# Extract from the File itself
if [[ "${lwhichcontainers}" == "enabled" ]]
then
# Exclude DISABLED (commented out) Containers
mapfile llist < <( grep -r -h "container_name:" "${lcomposedir}/compose.yml" | grep -Ev "^\s+?#")
else
# Include ALL
mapfile llist < <( grep -r -h "container_name:" "${lcomposedir}/compose.yml")
fi
# Perform line-by-line matching using sed
local litem=""
local lcleanitem=""
local lchk=""
for litem in "${llist[@]}"
do
# Debug
#debug_message "${FUNCNAME[0]} - Processing Item <${litem}>"
# Perfom Cleaning of the Item String
lcleanitem=$(echo ${litem} | sed -E "s|^\s*?#?\s*?container_name:\s*?\"?([a-zA-Z0-9_-]+)\"?\s*?$|\1|g")
# Debug
debug_message "${FUNCNAME[0]} - Cleaned Item: <${lcleanitem}>"
# Check if it's already in Array
lchk=$(array_contains locallist "${lcleanitem}")
# Add it to array anyways
# Needs to be better handled in a future version
lreturnarray+=("${lcleanitem}")
# If Status is 0 then add to return array
#if [[ ${lchk} -eq 0 ]]
#then
# lreturnarray+=("${lcleanitem}")
#fi
done
}
# Get Systemd Service File from Container Name
get_systemd_file_from_container() {
# The Container Name is passed as an Argument
local lcontainer=${1}
# Define Service File
local lservicefile="container-${lcontainer}.service"
# Return
echo ${lservicefile}
}
# Get Container Name from Systemd Service File
get_container_from_systemd_file() {
# The Service Name is passed as an Argument
local lservice=${1}
# Declare local variable
local lcontainer=""
# Strip "container-" from string
lcontainer="${lservice}"
lcontainer=${lcontainer/"container-"/""}
lcontainer=${lcontainer/".service"/""}
# Return
echo ${lcontainer}
}
# Get Systemd File
#get_systemd_file_from_container() {
# # The Container Name is passed as an Argument
# local lname=${1}
#
# # The User is passed as Optional Argument
# local luser=${2-""}
# if [[ -z "${luser}" ]]
# then
# luser=$(whoami)
# fi
#
# # Extract Systemd File from Container
# local lservicefile=$(generic_cmd ${luser} "podman" inspect ${lname} | jq -r '.[0].Config.Labels."PODMAN_SYSTEMD_UNIT"')
#
# # Return
# echo ${lservicefile}
#}
# Get Container Compose File
get_compose_dir_from_container() {
# The Container Name is passed as an Argument
local lcontainer=${1}
# The User is passed as Optional Argument
local luser=${2-""}
if [[ -z "${luser}" ]]
then
luser=$(whoami)
fi
# Declare variable
local lcomposedir=""
# Check if Container Exists first of all
#local lexists=$(exists_container "${lcontainer}" "${luser}")
exists_container "${lcontainer}" "${luser}"
local lexistscode=$?
debug_message "${FUNCNAME[0]} - Last Exit Code from <exists_container> was <${lexistscode}>"
# If Container exists
if [[ ${lexistscode} -eq 0 ]]
then
# Extract compodir from Container