forked from MRColorR/money4band
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runme.sh
1676 lines (1565 loc) · 97 KB
/
runme.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
#!/usr/bin/env bash
export LC_NUMERIC="C" # address possible locale issues that uses different notations for decimal numbers
### Variables and constants ###
## Env file related constants and variables ##
# env file name and template file name #
readonly ENV_TEMPLATE_FILENAME='.env.template'
readonly ENV_FILENAME='.env'
# Env file default #
readonly DEVICE_NAME_PLACEHOLDER='yourDeviceName'
DEVICE_NAME='yourDeviceName'
# Proxy config #
PROXY_CONF='false'
CURRENT_PROXY=''
NEW_STACK_PROXY=''
## Config file related constants and variables ##
readonly APP_CONFIG_JSON_FILE="app_config.json"
readonly MAINMENU_JSON_FILE="mainmenu.json"
## Docker compose related constants and variables ##
# docker compose yaml file name and template file name #
readonly DKCOM_TEMPLATE_FILENAME="docker-compose.yaml.template"
readonly DKCOM_FILENAME="docker-compose.yaml"
## Script init and variables ##
# Script default sleep time #
readonly SLEEP_TIME=1.5
### Resources, Scripts and Files folders ###
readonly RESOURCES_DIR="$PWD/.resources"
readonly CONFIG_DIR="$RESOURCES_DIR/.www/.configs"
readonly SCRIPTS_DIR="$RESOURCES_DIR/.scripts"
readonly FILES_DIR="$RESOURCES_DIR/.files"
## Architecture and OS related constants and variables ##
# Architecture default. Also define a map for the recognized architectures #
ARCH='unknown'
DKARCH='unknown'
declare -A arch_map=(
["x86_64"]="amd64"
["amd64"]="amd64"
["aarch64"]="arm64"
["arm64"]="arm64"
)
# OS default. Also define a map for the recognized OSs #
OS_TYPE='unknown'
declare -A os_map=(
["win32nt"]="Windows"
["windows_nt"]="Windows"
["windows"]="Windows"
["linux"]="Linux"
["darwin"]="MacOS"
["macos"]="MacOS"
["macosx"]="MacOS"
["mac"]="MacOS"
["osx"]="MacOS"
["cygwin"]="Cygwin"
["mingw"]="MinGw"
["msys"]="Msys"
["freebsd"]="FreeBSD"
)
## Colors ##
# Colors used inside the script #
ESC=$(printf '\033') DEFAULT="${ESC}[0m"
declare -A colors=(
[DEFAULT]="${ESC}[1;0m"
[GREEN]="${ESC}[1;32m"
[BLUE]="${ESC}[1;34m"
[RED]="${ESC}[1;31m"
[YELLOW]="${ESC}[1;33m"
[MAGENTA]="${ESC}[1;35m"
[CYAN]="${ESC}[1;36m"
)
# Color functions #
colorprint() {
if [[ -n "${colors[$1]}" ]]; then
printf "${colors[$1]}%s${DEFAULT}\n" "$2"
else
# Join the array elements into a string
color_list=$(IFS=','; echo "${!colors[*]}")
printf "Unknown color: %s. Available colors are: %s\n" "$1" "$color_list"
fi
}
# initialize the env file with the default values if there is no env file already present
# Check if the ${ENV_FILENAME} file is already present in the current directory, if it is not present copy from the .env.template file renaming it to ${ENV_FILENAME}, if it is present ask the user if they want to reset it or keep it as it is
if [ ! -f "${ENV_FILENAME}" ]; then
echo "No ${ENV_FILENAME} file found, copying ${ENV_FILENAME} and ${DKCOM_FILENAME} from the template files"
cp "${ENV_TEMPLATE_FILENAME}" "${ENV_FILENAME}"
cp "${DKCOM_TEMPLATE_FILENAME}" "${DKCOM_FILENAME}"
echo "Copied ${ENV_FILENAME} and ${DKCOM_FILENAME} from the template files"
else
echo "Already found ${ENV_FILENAME} file, proceeding with setup"
# check if the release version in the local env fileis the same of the local template file , if not align it
LOCAL_SCRIPT_VERSION=$(grep -oP 'PROJECT_VERSION=\K[^#\r]+' ${ENV_FILENAME})
LOCAL_SCRIPT_TEMPLATE_VERSION=$(grep -oP 'PROJECT_VERSION=\K[^#\r]+' ${ENV_TEMPLATE_FILENAME})
if [[ "$LOCAL_SCRIPT_VERSION" != "$LOCAL_SCRIPT_TEMPLATE_VERSION" ]]; then
echo "Local ${ENV_FILENAME} file version differs from local ${ENV_TEMPLATE_FILENAME} file version"
echo "This could be the result of an updated project using an outdated ${ENV_FILENAME} file"
sleep $SLEEP_TIME
echo "Generating new ${ENV_FILENAME} and ${DKCOM_FILENAME} files from the local template files and backing up the old files as ${ENV_FILENAME}.bak and ${DKCOM_FILENAME}.bak"
cp "${ENV_FILENAME}" "${ENV_FILENAME}.bak"
cp "${ENV_TEMPLATE_FILENAME}" "${ENV_FILENAME}"
cp "${DKCOM_FILENAME}" "${DKCOM_FILENAME}.bak"
cp "${DKCOM_TEMPLATE_FILENAME}" "${DKCOM_FILENAME}"
echo "New local ${ENV_FILENAME} and ${DKCOM_FILENAME} files generated from the local template files"
echo "If you are unsure, download the latest version directly from GitHub."
sleep $SLEEP_TIME
read -r -p "Press Enter to continue"
fi
fi
# Script version getting it from ${ENV_FILENAME} file #
SCRIPT_VERSION=$(grep -oP 'PROJECT_VERSION=\K[^#\r]+' ${ENV_FILENAME})
# Script name #
readonly SCRIPT_NAME=$(basename "$0") # save the script name in a variable not the full path
# Project Discord URL #
readonly DS_PROJECT_SERVER_URL=$(grep -oP 'DS_PROJECT_SERVER_URL=\K[^#\r]+' ${ENV_FILENAME})
# Script URL for update #
readonly PROJECT_BRANCH="main"
readonly PROJECT_URL="https://raw.githubusercontent.com/MRColorR/money4band/${PROJECT_BRANCH}"
# Script log file #
readonly DEBUG_LOG="debug_${SCRIPT_NAME}.log"
# Function to manage unexpected choices of flags #
fn_unknown() {
colorprint "RED" "Unknown choice $REPLY, please choose a valid option";
}
# Function to exit the script gracefully #
fn_bye(){
colorprint "GREEN" "Share this app with your friends thank you!"
colorprint "CYAN" "Support the M4B development <3 check the donation options in the README, on GitHub or in our Discord. Every bit helps!"
print_and_log "GREEN" "Exiting the application...Bye!Bye!"
exit 0
}
### Log, Update and Utility functions ###
# Function to write info/debug/warn/error messages to the log file if debug flag is true #
toLog_ifDebug() {
local log_level="[DEBUG]"
local message=""
while [[ "$#" -gt 0 ]]; do
case "$1" in
-l|--log_level)
log_level="$2"
shift 2
;;
-m|--message)
message="$2"
shift 2
;;
*)
echo "Unknown parameter passed: $1"
exit 1
;;
esac
done
# Only log if DEBUG mode is enabled
if [[ "$DEBUG" == "true" ]]; then
echo "$(date +'%Y-%m-%d %H:%M:%S') - $log_level - $message" >> "$DEBUG_LOG"
fi
}
## Enable or disable logging using debug mode ##
# Check if the first argument is -d or --debug if so, enable debug mode
if [[ $1 == '-d' || $1 == '--debug' ]]; then
DEBUG=true
# Remove the first argument so it doesn't interfere with the rest of the script
shift
toLog_ifDebug -l "[DEBUG]" -m "Debug mode enabled."
else
DEBUG=false
fi
# Function to print an info message that will be also logged to the log file #
print_and_log() {
local color="$1"
local message="$2"
colorprint "$color" "$message"
toLog_ifDebug -l "[INFO]" -m "$message"
}
# Function to print an error message and write it to the log file #
errorprint_and_log() {
printf "%s\n" "$1" >&2
toLog_ifDebug -l "[ERROR]" -m "$1"
}
# Function to print criticals errors that will stop the script execution, write them to the log file and exit the script with code 1 #
fn_fail() {
errorprint_and_log "$1"
read -r -p "Press Enter to exit..."
exit 1
}
## Utility functions ##
# Function to check if the env file is already configured #
check_configuration_status() {
local envFileArg=$1
# Check if ${envFileArg} file is already configured
ENV_CONFIGURATION_STATUS=$(grep -oP '# ENV_CONFIGURATION_STATUS=\K[^#\r]+' "$envFileArg")
toLog_ifDebug -l "[DEBUG]" -m "Current ENV_CONFIGURATION_STATUS: $ENV_CONFIGURATION_STATUS"
PROXY_CONFIGURATION_STATUS=$(grep -oP '# PROXY_CONFIGURATION_STATUS=\K[^#\r]+' "$envFileArg")
toLog_ifDebug -l "[DEBUG]" -m "Current PROXY_CONFIGURATION_STATUS: $PROXY_CONFIGURATION_STATUS"
NOTIFICATIONS_CONFIGURATION_STATUS=$(grep -oP '# NOTIFICATIONS_CONFIGURATION_STATUS=\K[^#\r]+' "$envFileArg")
toLog_ifDebug -l "[DEBUG]" -m "Current NOTIFICATIONS_CONFIGURATION_STATUS: $NOTIFICATIONS_CONFIGURATION_STATUS"
}
# Function to round up to the nearest power of 2
RoundUpPowerOf2() {
local value=$(printf "%.0f" $1) # Convert to an integer by rounding
local i=1
while [ $i -lt $value ]; do
i=$(( i * 2 ))
done
echo $i
}
# Function to adapt the limits in .env for CPU and RAM taking into account the number of CPU cores and the amount of RAM installed on the machine #
fn_adaptLimits() {
# Define minimum values for CPU and RAM limits
MIN_CPU_LIMIT="0.2" # Minimum CPU limit (reasonable value)
MIN_RAM_LIMIT="6" # Minimum RAM limit is 6 MB (enforced by Docker)
# check if lscpu is installed, if yes then get the number of CPU cores the machine has and the amount of RAM the machine has and adapt the limits in .env for CPU and RAM taking into account the number of CPU cores the machine has and the amount of RAM the machine has if not then print a warning message and leave the limits to the default values
if command -v lscpu &> /dev/null; then
# Get the number of CPU cores the machine has and others CPU related info
# CPU_SOCKETS=$(lscpu | awk '/^Socket\(s\)/{ print $2 }')
# # CPU_SOCKETS='-' # Uncomment to simulate incorrect socket number reporting
# if ! [[ "$CPU_SOCKETS" =~ ^[0-9]+$ ]]; then
# CPU_SOCKETS=1 # Default to 1 if CPU_SOCKETS is not a number
# fi
# CPU_CORES=$(lscpu | awk '/^Core\(s\) per socket/{ print $4 }')
# TOTAL_CPUS_OLD=$((CPU_CORES * CPU_SOCKETS)) # old calculations not working on some systems as sockets or cpus per socket are not reported correctly
TOTAL_CPUS=$(lscpu -b -p=Core,Socket | grep -v '^#' | sort -u | wc -l)
# Adapt the limits in .env file for CPU and RAM taking into account the number of CPU cores the machine has and the amount of RAM the machine has
# CPU limits: little should use max 25% of the CPU power , medium should use max 50% of the CPU power , big should use max 75% of the CPU power , huge should use max 100% of the CPU power
if command -v awk &> /dev/null; then
local APP_CPU_LIMIT_LITTLE=$(awk "BEGIN {print $TOTAL_CPUS * 25 / 100}")
local APP_CPU_LIMIT_MEDIUM=$(awk "BEGIN {print $TOTAL_CPUS * 50 / 100}")
local APP_CPU_LIMIT_BIG=$(awk "BEGIN {print $TOTAL_CPUS * 75 / 100}")
local APP_CPU_LIMIT_HUGE=$(awk "BEGIN {print $TOTAL_CPUS * 100 / 100}")
else
local APP_CPU_LIMIT_LITTLE=$(( TOTAL_CPUS * 25 / 100 ))
local APP_CPU_LIMIT_MEDIUM=$(( TOTAL_CPUS * 50 / 100 ))
local APP_CPU_LIMIT_BIG=$(( TOTAL_CPUS * 75 / 100 ))
local APP_CPU_LIMIT_HUGE=$(( TOTAL_CPUS * 100 / 100 ))
print_and_log "YELLOW" "Warning: awk command not found. Leaving limits setted using nearest integer values."
fi
# Ensure CPU limits are not below minimum
local APP_CPU_LIMIT_LITTLE=$(awk "BEGIN { if ($APP_CPU_LIMIT_LITTLE < $MIN_CPU_LIMIT) print $MIN_CPU_LIMIT; else print $APP_CPU_LIMIT_LITTLE; }")
local APP_CPU_LIMIT_MEDIUM=$(awk "BEGIN { if ($APP_CPU_LIMIT_MEDIUM < $MIN_CPU_LIMIT) print $MIN_CPU_LIMIT; else print $APP_CPU_LIMIT_MEDIUM; }")
local APP_CPU_LIMIT_BIG=$(awk "BEGIN { if ($APP_CPU_LIMIT_BIG < $MIN_CPU_LIMIT) print $MIN_CPU_LIMIT; else print $APP_CPU_LIMIT_BIG; }")
local APP_CPU_LIMIT_HUGE=$(awk "BEGIN { if ($APP_CPU_LIMIT_HUGE < $MIN_CPU_LIMIT) print $MIN_CPU_LIMIT; else print $APP_CPU_LIMIT_HUGE; }")
# Get the total RAM of the machine in MB
TOTAL_RAM_MB=$(( $(grep MemTotal /proc/meminfo | awk '{print $2}') / 1024 ))
# Get current limits values from .env file
local CURRENT_APP_CPU_LIMIT_LITTLE=$(grep -oP 'APP_CPU_LIMIT_LITTLE=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_CPU_LIMIT_MEDIUM=$(grep -oP 'APP_CPU_LIMIT_MEDIUM=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_CPU_LIMIT_BIG=$(grep -oP 'APP_CPU_LIMIT_BIG=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_CPU_LIMIT_HUGE=$(grep -oP 'APP_CPU_LIMIT_HUGE=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_MEM_RESERV_LITTLE=$(grep -oP 'APP_MEM_RESERV_LITTLE=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_MEM_LIMIT_LITTLE=$(grep -oP 'APP_MEM_LIMIT_LITTLE=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_MEM_RESERV_MEDIUM=$(grep -oP 'APP_MEM_RESERV_MEDIUM=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_MEM_LIMIT_MEDIUM=$(grep -oP 'APP_MEM_LIMIT_MEDIUM=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_MEM_RESERV_BIG=$(grep -oP 'APP_MEM_RESERV_BIG=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_MEM_LIMIT_BIG=$(grep -oP 'APP_MEM_LIMIT_BIG=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_MEM_RESERV_HUGE=$(grep -oP 'APP_MEM_RESERV_HUGE=\K[^#\r]+' ${ENV_FILENAME})
local CURRENT_APP_MEM_LIMIT_HUGE=$(grep -oP 'APP_MEM_LIMIT_HUGE=\K[^#\r]+' ${ENV_FILENAME})
# RAM limits: little should reserve at least MIN_RAM_LIMIT MB or the next near power of 2 in MB of 5% of RAM as upperbound and use as max limit the 250% of this value, medium should reserve double of the little value or the next near power of 2 in MB of 10% of RAM as upperbound and use as max limit the 250% of this value, big should reserve double of the medium value or the next near power of 2 in MB of 20% of RAM as upperbound and use as max limit the 250% of this value, huge should reserve double of the big value or the next near power of 2 in MB of 40% of RAM as upperbound and use as max limit the 400% of this value
# Implementing a cap for high RAM devices reading value from .env.template file it will be like RAM_CAP_MB_DEFAULT=6144m we need the value 6144
RAM_CAP_MB_DEFAULT=$(grep -oP 'RAM_CAP_MB_DEFAULT=\K[^#\r]+' ${ENV_TEMPLATE_FILENAME} | sed 's/m//')
# Uncomment the following to simulate a specific amount of RAM for the device
# TOTAL_RAM_MB=1024
RAM_CAP_MB=$(( TOTAL_RAM_MB > RAM_CAP_MB_DEFAULT ? RAM_CAP_MB_DEFAULT : TOTAL_RAM_MB ))
MAX_USE_RAM_MB=$(( TOTAL_RAM_MB > RAM_CAP_MB ? RAM_CAP_MB : TOTAL_RAM_MB ))
# Calculate RAM limits
if command -v awk &> /dev/null; then
local APP_MEM_RESERV_LITTLE=$(RoundUpPowerOf2 $(awk "BEGIN {printf \"%d\", $MAX_USE_RAM_MB * 5 / 100}"))
local APP_MEM_LIMIT_LITTLE=$(RoundUpPowerOf2 $(awk "BEGIN {printf \"%d\", $APP_MEM_RESERV_LITTLE * 200 / 100}"))
local APP_MEM_RESERV_MEDIUM=$(RoundUpPowerOf2 $(awk "BEGIN {printf \"%d\", $MAX_USE_RAM_MB * 10 / 100}"))
local APP_MEM_LIMIT_MEDIUM=$(RoundUpPowerOf2 $(awk "BEGIN {printf \"%d\", $APP_MEM_RESERV_MEDIUM * 200 / 100}"))
local APP_MEM_RESERV_BIG=$(RoundUpPowerOf2 $(awk "BEGIN {printf \"%d\", $MAX_USE_RAM_MB * 20 / 100}"))
local APP_MEM_LIMIT_BIG=$(RoundUpPowerOf2 $(awk "BEGIN {printf \"%d\", $APP_MEM_RESERV_BIG * 200 / 100}"))
local APP_MEM_RESERV_HUGE=$(RoundUpPowerOf2 $(awk "BEGIN {printf \"%d\", $MAX_USE_RAM_MB * 40 / 100}"))
local APP_MEM_LIMIT_HUGE=$(RoundUpPowerOf2 $(awk "BEGIN {printf \"%d\", $APP_MEM_RESERV_HUGE * 200 / 100}"))
else
local APP_MEM_RESERV_LITTLE=$(RoundUpPowerOf2 $(( MAX_USE_RAM_MB * 5 / 100 )))
local APP_MEM_LIMIT_LITTLE=$(RoundUpPowerOf2 $(( APP_MEM_RESERV_LITTLE * 200 / 100 )))
local APP_MEM_RESERV_MEDIUM=$(RoundUpPowerOf2 $(( MAX_USE_RAM_MB * 10 / 100 )))
local APP_MEM_LIMIT_MEDIUM=$(RoundUpPowerOf2 $(( APP_MEM_RESERV_MEDIUM * 200 / 100 )))
local APP_MEM_RESERV_BIG=$(RoundUpPowerOf2 $(( MAX_USE_RAM_MB * 20 / 100 )))
local APP_MEM_LIMIT_BIG=$(RoundUpPowerOf2 $(( APP_MEM_RESERV_BIG * 200 / 100 )))
local APP_MEM_RESERV_HUGE=$(RoundUpPowerOf2 $(( MAX_USE_RAM_MB * 40 / 100 )))
local APP_MEM_LIMIT_HUGE=$(RoundUpPowerOf2 $(( APP_MEM_RESERV_HUGE * 200 / 100 )))
print_and_log "YELLOW" "Warning: awk command not found. Limits setted using nearest integer values."
fi
# Ensure the calculated values do not exceed RAM_CAP_MB_DEFAULT
APP_MEM_RESERV_LITTLE=$((APP_MEM_RESERV_LITTLE > RAM_CAP_MB_DEFAULT ? RAM_CAP_MB_DEFAULT : APP_MEM_RESERV_LITTLE))
APP_MEM_LIMIT_LITTLE=$((APP_MEM_LIMIT_LITTLE > RAM_CAP_MB_DEFAULT ? RAM_CAP_MB_DEFAULT : APP_MEM_LIMIT_LITTLE))
APP_MEM_RESERV_MEDIUM=$((APP_MEM_RESERV_MEDIUM > RAM_CAP_MB_DEFAULT ? RAM_CAP_MB_DEFAULT : APP_MEM_RESERV_MEDIUM))
APP_MEM_LIMIT_MEDIUM=$((APP_MEM_LIMIT_MEDIUM > RAM_CAP_MB_DEFAULT ? RAM_CAP_MB_DEFAULT : APP_MEM_LIMIT_MEDIUM))
APP_MEM_RESERV_BIG=$((APP_MEM_RESERV_BIG > RAM_CAP_MB_DEFAULT ? RAM_CAP_MB_DEFAULT : APP_MEM_RESERV_BIG))
APP_MEM_LIMIT_BIG=$((APP_MEM_LIMIT_BIG > RAM_CAP_MB_DEFAULT ? RAM_CAP_MB_DEFAULT : APP_MEM_LIMIT_BIG))
APP_MEM_RESERV_HUGE=$((APP_MEM_RESERV_HUGE > RAM_CAP_MB_DEFAULT ? RAM_CAP_MB_DEFAULT : APP_MEM_RESERV_HUGE))
APP_MEM_LIMIT_HUGE=$((APP_MEM_LIMIT_HUGE > RAM_CAP_MB_DEFAULT ? RAM_CAP_MB_DEFAULT : APP_MEM_LIMIT_HUGE))
# Ensure RAM limits are not below minimum
APP_MEM_RESERV_LITTLE=$(awk "BEGIN { if ($APP_MEM_RESERV_LITTLE < $MIN_RAM_LIMIT) print $MIN_RAM_LIMIT; else print $APP_MEM_RESERV_LITTLE; }")
APP_MEM_LIMIT_LITTLE=$(awk "BEGIN { if ($APP_MEM_LIMIT_LITTLE < $MIN_RAM_LIMIT) print $MIN_RAM_LIMIT; else print $APP_MEM_LIMIT_LITTLE; }")
APP_MEM_RESERV_MEDIUM=$(awk "BEGIN { if ($APP_MEM_RESERV_MEDIUM < $MIN_RAM_LIMIT) print $MIN_RAM_LIMIT; else print $APP_MEM_RESERV_MEDIUM; }")
APP_MEM_LIMIT_MEDIUM=$(awk "BEGIN { if ($APP_MEM_LIMIT_MEDIUM < $MIN_RAM_LIMIT) print $MIN_RAM_LIMIT; else print $APP_MEM_LIMIT_MEDIUM; }")
APP_MEM_RESERV_BIG=$(awk "BEGIN { if ($APP_MEM_RESERV_BIG < $MIN_RAM_LIMIT) print $MIN_RAM_LIMIT; else print $APP_MEM_RESERV_BIG; }")
APP_MEM_LIMIT_BIG=$(awk "BEGIN { if ($APP_MEM_LIMIT_BIG < $MIN_RAM_LIMIT) print $MIN_RAM_LIMIT; else print $APP_MEM_LIMIT_BIG; }")
APP_MEM_RESERV_HUGE=$(awk "BEGIN { if ($APP_MEM_RESERV_HUGE < $MIN_RAM_LIMIT) print $MIN_RAM_LIMIT; else print $APP_MEM_RESERV_HUGE; }")
APP_MEM_LIMIT_HUGE=$(awk "BEGIN { if ($APP_MEM_LIMIT_HUGE < $MIN_RAM_LIMIT) print $MIN_RAM_LIMIT; else print $APP_MEM_LIMIT_HUGE; }")
# Update the CPU limits with the new values
sed -i "s/APP_CPU_LIMIT_LITTLE=${CURRENT_APP_CPU_LIMIT_LITTLE}/APP_CPU_LIMIT_LITTLE=${APP_CPU_LIMIT_LITTLE}/" $ENV_FILENAME
sed -i "s/APP_CPU_LIMIT_MEDIUM=${CURRENT_APP_CPU_LIMIT_MEDIUM}/APP_CPU_LIMIT_MEDIUM=${APP_CPU_LIMIT_MEDIUM}/" $ENV_FILENAME
sed -i "s/APP_CPU_LIMIT_BIG=${CURRENT_APP_CPU_LIMIT_BIG}/APP_CPU_LIMIT_BIG=${APP_CPU_LIMIT_BIG}/" $ENV_FILENAME
sed -i "s/APP_CPU_LIMIT_HUGE=${CURRENT_APP_CPU_LIMIT_HUGE}/APP_CPU_LIMIT_HUGE=${APP_CPU_LIMIT_HUGE}/" $ENV_FILENAME
# Update RAM limits with the new values unsing as unit MB
sed -i "s/APP_MEM_RESERV_LITTLE=${CURRENT_APP_MEM_RESERV_LITTLE}/APP_MEM_RESERV_LITTLE=${APP_MEM_RESERV_LITTLE}m/" $ENV_FILENAME
sed -i "s/APP_MEM_LIMIT_LITTLE=${CURRENT_APP_MEM_LIMIT_LITTLE}/APP_MEM_LIMIT_LITTLE=${APP_MEM_LIMIT_LITTLE}m/" $ENV_FILENAME
sed -i "s/APP_MEM_RESERV_MEDIUM=${CURRENT_APP_MEM_RESERV_MEDIUM}/APP_MEM_RESERV_MEDIUM=${APP_MEM_RESERV_MEDIUM}m/" $ENV_FILENAME
sed -i "s/APP_MEM_LIMIT_MEDIUM=${CURRENT_APP_MEM_LIMIT_MEDIUM}/APP_MEM_LIMIT_MEDIUM=${APP_MEM_LIMIT_MEDIUM}m/" $ENV_FILENAME
sed -i "s/APP_MEM_RESERV_BIG=${CURRENT_APP_MEM_RESERV_BIG}/APP_MEM_RESERV_BIG=${APP_MEM_RESERV_BIG}m/" $ENV_FILENAME
sed -i "s/APP_MEM_LIMIT_BIG=${CURRENT_APP_MEM_LIMIT_BIG}/APP_MEM_LIMIT_BIG=${APP_MEM_LIMIT_BIG}m/" $ENV_FILENAME
sed -i "s/APP_MEM_RESERV_HUGE=${CURRENT_APP_MEM_RESERV_HUGE}/APP_MEM_RESERV_HUGE=${APP_MEM_RESERV_HUGE}m/" $ENV_FILENAME
sed -i "s/APP_MEM_LIMIT_HUGE=${CURRENT_APP_MEM_LIMIT_HUGE}/APP_MEM_LIMIT_HUGE=${APP_MEM_LIMIT_HUGE}m/" $ENV_FILENAME
# If debug mode is enabled print the calculated limits values
if [[ "$DEBUG" == "true" ]]; then
print_and_log "DEFAULT" "Total CPUs: $TOTAL_CPUS"
print_and_log "DEFAULT" "APP_CPU_LIMIT_LITTLE: $APP_CPU_LIMIT_LITTLE"
print_and_log "DEFAULT" "APP_CPU_LIMIT_MEDIUM: $APP_CPU_LIMIT_MEDIUM"
print_and_log "DEFAULT" "APP_CPU_LIMIT_BIG: $APP_CPU_LIMIT_BIG"
print_and_log "DEFAULT" "APP_CPU_LIMIT_HUGE: $APP_CPU_LIMIT_HUGE"
print_and_log "DEFAULT" "Total RAM: $TOTAL_RAM_MB MB"
print_and_log "DEFAULT" "APP_MEM_RESERV_LITTLE: $APP_MEM_RESERV_LITTLE MB"
print_and_log "DEFAULT" "APP_MEM_LIMIT_LITTLE: $APP_MEM_LIMIT_LITTLE MB"
print_and_log "DEFAULT" "APP_MEM_RESERV_MEDIUM: $APP_MEM_RESERV_MEDIUM MB"
print_and_log "DEFAULT" "APP_MEM_LIMIT_MEDIUM: $APP_MEM_LIMIT_MEDIUM MB"
print_and_log "DEFAULT" "APP_MEM_RESERV_BIG: $APP_MEM_RESERV_BIG MB"
print_and_log "DEFAULT" "APP_MEM_LIMIT_BIG: $APP_MEM_LIMIT_BIG MB"
print_and_log "DEFAULT" "APP_MEM_RESERV_HUGE: $APP_MEM_RESERV_HUGE MB"
print_and_log "DEFAULT" "APP_MEM_LIMIT_HUGE: $APP_MEM_LIMIT_HUGE MB"
#read -r -p "Press Enter to continue"
fi
else
echo "Warning: Required commands not found. Leaving limits at default values."
fi
}
# Function to check if there are any updates available #
check_project_updates() {
# Get the current script version from the local .env file
SCRIPT_VERSION=$(grep -oP 'PROJECT_VERSION=\K[^#\r]+' "./${ENV_FILENAME}")
if [[ -z $SCRIPT_VERSION ]]; then
errorprint_and_log "Failed to get the script version from the local .env file."
return 1
fi
# Get the latest script version from the .env.template file on GitHub
LATEST_SCRIPT_VERSION=$(curl -fs "$PROJECT_URL/$ENV_TEMPLATE_FILENAME" | grep -oP 'PROJECT_VERSION=\K[^#\r]+')
if [[ -z $LATEST_SCRIPT_VERSION ]]; then
errorprint_and_log "Failed to get the latest script version from GitHub."
return 1
fi
# Split the versions into major, minor, and patch numbers
IFS='.' read -ra SCRIPT_VERSION_SPLIT <<< "$SCRIPT_VERSION"
IFS='.' read -ra LATEST_SCRIPT_VERSION_SPLIT <<< "$LATEST_SCRIPT_VERSION"
# Compare the versions and print a message if a newer version is available
for i in "${!SCRIPT_VERSION_SPLIT[@]}"; do
if (( ${SCRIPT_VERSION_SPLIT[i]} < ${LATEST_SCRIPT_VERSION_SPLIT[i]} )); then
print_and_log "YELLOW" "A newer version of the script is available. Please consider updating."
return 0 # Return here to exit the function as soon as a newer version is found
elif (( ${SCRIPT_VERSION_SPLIT[i]} > ${LATEST_SCRIPT_VERSION_SPLIT[i]} )); then
# If any part of the local version is greater, it's not an older version
return 0
fi
done
# If the loop completes without finding a newer version, you're up to date
print_and_log "BLUE" "Script is up to date."
}
# Function to detect OS
detect_os() {
toLog_ifDebug -l "[DEBUG]" -m "Detecting OS..."
if ! command -v uname -s &> /dev/null; then
toLog_ifDebug -l "[WARN]" -m "uname command not found, OS detection failed. OS type will be set to 'unknown'."
OS_TYPE="unknown"
else
OSStr="$(uname -s | tr '[:upper:]' '[:lower:]')" # Convert to lowercase
# Use a for loop to check if OSStr contains any known OS substring
for key in "${!os_map[@]}"; do
if [[ $OSStr == *"$key"* ]]; then
OS_TYPE="${os_map[$key]}"
break
else
OS_TYPE="unknown"
fi
done
fi
toLog_ifDebug -l "[DEBUG]" -m "OS type detected: $OS_TYPE"
}
# Function to detect OS architecture and set the relative Docker architecture
detect_architecture() {
toLog_ifDebug -l "[DEBUG]" -m "Detecting system architecture..."
if ! command -v uname -m &> /dev/null; then
toLog_ifDebug -l "[DEBUG]" -m "uname command not found, architecture detection failed. Architecture will be set to 'unknown'."
ARCH="unknown"
DKARCH="unknown"
else
archStr=$(uname -m | tr '[:upper:]' '[:lower:]') # Convert to lowercase
# Use a for loop to check if archStr contains any known architecture substring
for key in "${!arch_map[@]}"; do
if [[ $archStr == *"$key"* ]]; then
ARCH="${archStr}"
DKARCH="${arch_map[$key]}"
break
else
DKARCH="unknown"
fi
done
fi
toLog_ifDebug -l "[DEBUG]" -m "System architecture detected: $ARCH, Docker architecture has been set to $DKARCH"
}
# Function to check if dependencies packages are installed and install them if not #
fn_install_packages() {
toLog_ifDebug -l "[DEBUG]" -m "Checking if required packages are installed..."
REQUIRED_PACKAGES=("$@")
if [[ "$OS_TYPE" == "Linux" ]]; then
# Check which package manager is installed
if command -v apt &> /dev/null ; then
PKG_MANAGER="apt"
PKG_CHECK="dpkg -l"
PKG_INSTALL="sudo apt install -y"
elif command -v yum &> /dev/null ; then
PKG_MANAGER="yum"
PKG_CHECK="rpm -q"
PKG_INSTALL="sudo yum install -y"
elif command -v dnf &> /dev/null ; then
PKG_MANAGER="dnf"
PKG_CHECK="rpm -q"
PKG_INSTALL="sudo dnf install -y"
elif command -v pacman &> /dev/null ; then
PKG_MANAGER="pacman"
PKG_CHECK="pacman -Q"
PKG_INSTALL="sudo pacman -S --noconfirm"
elif command -v zypper &> /dev/null ; then
PKG_MANAGER="zypper"
PKG_CHECK="rpm -q"
PKG_INSTALL="sudo zypper install -y"
elif command -v apk &> /dev/null ; then
PKG_MANAGER="apk"
PKG_CHECK="apk info"
PKG_INSTALL="sudo apk add"
elif command -v emerge &> /dev/null ; then
PKG_MANAGER="emerge"
PKG_CHECK="qlist -I"
PKG_INSTALL="sudo emerge --ask n"
else
print_and_log "RED" "Your package manager has not been recognized by this script. Please install the following packages manually: ${REQUIRED_PACKAGES[*]}"
read -r -p "Press Enter to continue"
return
fi
toLog_ifDebug -l "[DEBUG]" -m "Detected package manager: $PKG_MANAGER"
# Install required packages
for package in "${REQUIRED_PACKAGES[@]}"
do
if ! $PKG_CHECK | grep -q "^ii $package"; then
print_and_log "DEFAULT" "$package is not installed. Trying to install now..."
if ! $PKG_INSTALL "${package}"; then
print_and_log "RED" "Failed to install $package. Please install it manually."
fi
else
print_and_log "DEFAULT" "$package is already installed."
fi
done
elif [[ "$OS_TYPE" == "MacOS" ]]; then
if ! command -v brew &> /dev/null; then
print_and_log "DEFAULT" "Homebrew is not installed. Trying to install now..."
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
fi
for package in "${REQUIRED_PACKAGES[@]}"
do
if ! brew list --versions "$package" > /dev/null; then
print_and_log "DEFAULT" "$package is not installed. Trying to install now..."
if ! brew install "$package"; then
print_and_log "RED" "Failed to install $package. Please install it manually."
fi
else
print_and_log "DEFAULT" "$package is already installed."
fi
done
elif [[ "$OS_TYPE" == "Windows" ]]; then
if ! command -v choco &> /dev/null; then
print_and_log "DEFAULT" "Chocolatey is not installed. Trying to install now..."
if ! powershell -Command "Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))"; then
print_and_log "RED" "Failed to install Chocolatey. Please install it manually."
fi
fi
for package in "${REQUIRED_PACKAGES[@]}"
do
if ! choco list --local-only --exact "$package" > /dev/null; then
print_and_log "DEFAULT" "$package is not installed. Trying to install now..."
if ! choco install "$package" -y; then
print_and_log "RED" "Failed to install $package. Please install it manually."
fi
else
print_and_log "DEFAULT" "$package is already installed."
fi
done
else
print_and_log "RED" "Your operating system has not been recognized or is not supported by this function. Please install the following packages manually: ${REQUIRED_PACKAGES[*]}"
read -r -p "Press Enter to continue"
return
fi
toLog_ifDebug -l "[DEBUG]" -m "Required packages installation completed."
}
## Multiarch emulation service installer function ##
fn_addDockerBinfmtSVC() {
toLog_ifDebug -l "[DEBUG]" -m "Installing multiarch emulation service..."
# Check if the service file exists if it does then check if it is enabled and if not enable it, if its enabled then check if the content is the same as the one in the script and if not overwrite it then start the service
toLog_ifDebug -l "[DEBUG]" -m "Checking if the service already exists..."
if [ -f "/etc/systemd/system/docker.binfmt.service" ]; then
# Compare the contents of the existing service file with the one in $FILES_DIR
toLog_ifDebug -l "[DEBUG]" -m "Service already exists, comparing contents..."
if ! cmp -s "/etc/systemd/system/docker.binfmt.service" "$FILES_DIR/docker.binfmt.service"; then
# The contents are different, overwrite the existing service file
toLog_ifDebug -l "[DEBUG]" -m "Service contents are different, overwriting the file of the existing service..."
if ! sudo cp "$FILES_DIR/docker.binfmt.service" /etc/systemd/system; then
fn_fail "Failed to copy service file. Please check your permissions and the file path."
fi
fi
# Check if the service is enabled
toLog_ifDebug -l "[DEBUG]" -m "Checking if the service is enabled..."
if [ -d "/etc/systemd/system" ]; then
# Systemd-based distributions
toLog_ifDebug -l "[DEBUG]" -m "Systemd-based distribution detected, checking if the service is enabled..."
if ! systemctl is-enabled --quiet docker.binfmt.service; then
# Enable the service
toLog_ifDebug -l "[DEBUG]" -m "Service is not enabled, enabling it..."
if ! sudo systemctl enable docker.binfmt.service; then
fn_fail "Failed to enable docker.binfmt.service. Please check your system config and try to enable the exixting service manually. Then run the script again."
fi
fi
fi
elif [ -f "/etc/init.d/docker.binfmt" ]; then
toLog_ifDebug -l "[DEBUG]" -m "SysV init-based distribution detected, checking if the service is enabled..."
# Compare the contents of the existing service file with the one in $FILES_DIR
toLog_ifDebug -l "[DEBUG]" -m "Service already exists, comparing contents..."
if ! cmp -s "/etc/init.d/docker.binfmt" "$FILES_DIR/docker.binfmt.service"; then
# The contents are different, overwrite the existing service file
toLog_ifDebug -l "[DEBUG]" -m "Service contents are different, overwriting the file of the existing service..."
if ! sudo cp "$FILES_DIR/docker.binfmt.service" /etc/init.d/docker.binfmt; then
fn_fail "Failed to copy service file. Please check your permissions and the file path."
fi
sudo chmod +x /etc/init.d/docker.binfmt
fi
# Check if the service is enabled
toLog_ifDebug -l "[DEBUG]" -m "Checking if the service is enabled..."
if [ -d "/etc/init.d" ]; then
# SysV init-based distributions
toLog_ifDebug -l "[DEBUG]" -m "SysV init-based distribution detected, checking if the service is enabled..."
if ! grep -q "docker.binfmt" /etc/rc.local; then
# Enable the service
toLog_ifDebug -l "[DEBUG]" -m "Service is not enabled, enabling it..."
sudo update-rc.d docker.binfmt defaults
fi
fi
else
# The service file does not exist, copy it to the appropriate location
toLog_ifDebug -l "[DEBUG]" -m "Service does not already exists, copying it to the appropriate location..."
if [ -d "/etc/systemd/system" ]; then
# Systemd-based distributions
toLog_ifDebug -l "[DEBUG]" -m "Systemd-based distribution detected, copying service file..."
sudo cp "$FILES_DIR/docker.binfmt.service" /etc/systemd/system
sudo systemctl enable docker.binfmt.service
toLog_ifDebug -l "[DEBUG]" -m "Service file copied and enabled."
elif [ -d "/etc/init.d" ]; then
# SysV init-based distributions
toLog_ifDebug -l "[DEBUG]" -m "SysV init-based distribution detected, copying service file..."
sudo cp "$FILES_DIR/docker.binfmt.service" /etc/init.d/docker.binfmt
sudo chmod +x /etc/init.d/docker.binfmt
sudo update-rc.d docker.binfmt defaults
toLog_ifDebug -l "[DEBUG]" -m "Service file copied and enabled."
else
# Fallback option (handle unsupported systems)
fn_fail "Warning: I can not find a supported init system. You will have to manually enable the binfmt service. Then restart the script."
fi
fi
# Start the service
toLog_ifDebug -l "[DEBUG]" -m "Starting the service..."
if [ -d "/etc/systemd/system" ]; then
# Systemd-based distributions
toLog_ifDebug -l "[DEBUG]" -m "Systemd-based distribution detected, starting the service..."
if ! sudo systemctl start docker.binfmt.service; then
fn_fail "Failed to start docker.binfmt.service. Please check your system config and try to start the exixting service manually. Then run the script again."
fi
toLog_ifDebug -l "[DEBUG]" -m "Service started."
elif [ -d "/etc/init.d" ]; then
# SysV init-based distributions
toLog_ifDebug -l "[DEBUG]" -m "SysV init-based distribution detected, starting the service..."
if ! sudo service docker.binfmt start; then
fn_fail "Failed to start docker.binfmt.service. Please check your system config and try to start the exixting service manually. Then run the script again."
fi
toLog_ifDebug -l "[DEBUG]" -m "Service started."
fi
}
### Sub-menu Functions ###
# Shows the liks of the apps
fn_showLinks() {
clear
toLog_ifDebug -l "[DEBUG]" -m "Showing apps links"
colorprint "GREEN" "Use CTRL+Click to open links or copy them:"
# reading from $APP_CONFIG_JSON_FILE show all the apps type that are the dictionary keys and then show the name and the link of each app in the dictionary
for app_type in $(jq -r 'keys[]' "$CONFIG_DIR/$APP_CONFIG_JSON_FILE"); do
colorprint "YELLOW" "---$app_type---"
for app in $(jq -r ".[\"$app_type\"][].name" "$CONFIG_DIR/$APP_CONFIG_JSON_FILE"); do
colorprint "DEFAULT" "$app"
colorprint "CYAN" "$(jq -r ".[\"$app_type\"][] | select(.name==\"$app\") | .link" "$CONFIG_DIR/$APP_CONFIG_JSON_FILE")"
done
done
read -r -p "Press Enter to go back to mainmenu"
toLog_ifDebug -l "[DEBUG]" -m "Links shown, going back to mainmenu"
}
## Docker checker and installer function ##
# Check if docker is installed and if not then it tries to install it automatically
fn_dockerInstall() {
toLog_ifDebug -l "[DEBUG]" -m "DockerInstall function started"
clear
colorprint "YELLOW" "This menu item will launch a script that will attempt to install Docker"
colorprint "YELLOW" "Use it only if you do not know how to perform the manual Docker installation described at https://docs.docker.com/get-docker/ as the automatic script in some rare cases and depending on the distros may fail to install Docker correctly."
while true; do
read -r -p "Do you wish to proceed with the Docker automatic installation Y/N? " yn
case $yn in
[Yy]* )
toLog_ifDebug -l "[DEBUG]" -m "User decided to install Docker through the script. Checking if Docker is already installed."
if docker --version >/dev/null 2>&1; then
toLog_ifDebug -l "[DEBUG]" -m "Docker is already installed. Asking user if he wants to continue with the installation anyway."
while true; do
colorprint "YELLOW" "It seems that Docker is already installed. Do you want to continue with the installation anyway? (Y/N)"
read -r yn
case $yn in
[Yy]* )
toLog_ifDebug -l "[DEBUG]" -m "User decided to continue with the Docker re-install anyway."
break
;;
[Nn]* )
toLog_ifDebug -l "[DEBUG]" -m "User decided to abort the Docker re-install."
read -r -p "Press Enter to go back to mainmenu"
sleep "$SLEEP_TIME"
return
;;
* )
colorprint "RED" "Please answer yes or no."
continue
;;
esac
done
fi
print_and_log "DEFAULT" "Proceeding with Docker installation. Please provide your sudo password if prompted."
if curl -fsSL https://get.docker.com -o "$SCRIPTS_DIR/get-docker.sh"; then
if sudo sh "$SCRIPTS_DIR/get-docker.sh"; then
print_and_log "GREEN" "Docker installed"
read -r -p "Press Enter to go back to mainmenu"
else
errorprint_and_log "Failed to install Docker automatically. Please try to install Docker manually by following the instructions on Docker website."
read -r -p "Press Enter to go back to mainmenu"
fi
else
errorprint_and_log "Failed to download the Docker installation script."
fi
break
;;
[Nn]* )
colorprint "BLUE" "Docker unattended installation canceled. Make sure you have Docker installed before proceeding with the other steps."
read -r -p "Press Enter to go back to mainmenu"
break
;;
* )
colorprint "RED" "Please answer yes or no."
;;
esac
done
}
## Notifications setup function ##
# This function will setup notifications about containers updates using shoutrrr
fn_setupNotifications() {
toLog_ifDebug -l "[DEBUG]" -m "SetupNotifications function started"
clear
while true; do
colorprint "YELLOW" "Do you wish to setup notifications about apps images updates (Yes to receive notifications and apply updates, No to just silently apply updates) Y/N?"
read -r yn
case $yn in
[Yy]* )
toLog_ifDebug -l "[DEBUG]" -m "User decided to setup notifications about apps images updates."
colorprint "YELLOW" "This step will setup notifications about containers updates using shoutrrr"
colorprint "DEFAULT" "The resulting SHOUTRRR_URL should have the format: <app>://<token>@<webhook>."
colorprint "DEFAULT" "Where <app> is one of the supported messaging apps on Shoutrrr (e.g. Discord), and <token> and <webhook> are specific to your messaging app."
colorprint "DEFAULT" "To obtain the SHOUTRRR_URL, create a new webhook for your messaging app and rearrange its URL to match the format above."
colorprint "DEFAULT" "For more details, visit https://containrrr.dev/shoutrrr/ and select your messaging app."
colorprint "DEFAULT" "Now a Discord notification setup example will be shown (Remember: you can also use a different supported app)."
read -r -p "Press enter to continue"
clear
colorprint "MAGENTA" "Create a new Discord server, go to server settings > integrations, and create a webhook."
colorprint "MAGENTA" "Your Discord Webhook-URL will look like this: https://discordapp.com/api/webhooks/YourWebhookid/YourToken."
colorprint "MAGENTA" "To obtain the SHOUTRRR_URL, rearrange it to look like this: discord://YourToken@YourWebhookid."
read -r -p "Press enter to proceed with the setup"
clear
while true; do
colorprint "YELLOW" "NOW INSERT BELOW THE LINK FOR NOTIFICATIONS using THE SAME FORMAT WRITTEN ABOVE e.g.: discord://yourToken@yourWebhookid"
read -r SHOUTRRR_URL
if [[ "$SHOUTRRR_URL" =~ ^[a-zA-Z]+:// ]]; then
# Replace the lines in the ${ENV_FILENAME} file and in the $DKCOM_FILENAME file
sed -i "s~# SHOUTRRR_URL=~SHOUTRRR_URL=~" ${ENV_FILENAME}
CURRENT_VALUE=$(grep -oP 'SHOUTRRR_URL=\K[^#\r]+' ${ENV_FILENAME})
sed -i "s~SHOUTRRR_URL=${CURRENT_VALUE}~SHOUTRRR_URL=$SHOUTRRR_URL~" ${ENV_FILENAME}
sed -i "s~# - WATCHTOWER_NOTIFICATIONS=shoutrrr~- WATCHTOWER_NOTIFICATIONS=shoutrrr~" "$DKCOM_FILENAME"
sed -i "s~# - WATCHTOWER_NOTIFICATION_URL~- WATCHTOWER_NOTIFICATION_URL~" "$DKCOM_FILENAME"
sed -i 's/NOTIFICATIONS_CONFIGURATION_STATUS=0/NOTIFICATIONS_CONFIGURATION_STATUS=1/' ${ENV_FILENAME}
colorprint "DEFAULT" "Notifications setup complete. If the link is correct, you will receive a notification for each update made on the app container images."
read -r -p "Press enter to continue."
break
else
colorprint "RED" "Invalid link format. Please make sure to use the correct format."
while true; do
colorprint "YELLOW" "Do you wish to try again or leave the notifications disabled and continue with the setup script? (Yes to try again, No to continue without notifications) Y/N?"
read -r yn
case $yn in
[Yy]* ) break;;
[Nn]* )
toLog_ifDebug -l "[DEBUG]" -m "User choose to not retry the notifications setup. Notifications wsetup will now return"
colorprint "BLUE" "Noted: all updates will be applied automatically and silently";
sleep "$SLEEP_TIME"
return;;
* ) colorprint "RED" "Please answer yes or no.";;
esac
done
fi
done
break;;
[Nn]* )
toLog_ifDebug -l "[DEBUG]" -m "User choose to skip notifications setup"
colorprint "BLUE" "Noted: all updates will be applied automatically and silently";
sleep "$SLEEP_TIME"
break;;
* )
colorprint "RED" "Please answer yes or no.";;
esac
done
clear
toLog_ifDebug -l "[DEBUG]" -m "SetupNotifications function ended"
}
fn_setupApp() {
toLog_ifDebug -l "[DEBUG]" -m "SetupApp function started"
local app_json=""
local dk_compose_filename="docker-compose.yaml"
while [[ "$#" -gt 0 ]]; do
case $1 in
--app-json)
app_json="$2"
shift
;;
--dk-compose-filename)
dk_compose_filename="$2"
shift
;;
*)
colorprint "RED" "Unknown parameter passed to fn_setupApp: $1"
;;
esac
shift
done
toLog_ifDebug -l "[DEBUG]" -m "SetupApp function parameters: app_json=$app_json, dk_compose_filename=$dk_compose_filename"
# Extract the necessary fields from the app json
toLog_ifDebug -l "[DEBUG]" -m "Extracting necessary fields from the passed app json"
local name
name=$(jq -r '.name' <<< "$app_json")
local link
link=$(jq -r '.link' <<< "$app_json")
local app_image
app_image=$(jq -r '.image' <<< "$app_json")
local flags_raw
flags_raw=$(jq -r 'keys[]' <<< "$(jq '.flags?' <<< "$app_json")")
# Load the flags thata are extracted by jq as a string in an array
local flags=()
while read -r line; do
flags+=("$line")
done <<< "$flags_raw"
local claimURLBase
claimURLBase=$(jq -r '.claimURLBase? // .link' <<< "$app_json") # The ? is to make the claimURLBase field optional if not present in the json it will be set to the link field
local CURRENT_APP
CURRENT_APP=$(echo "${name}" | tr '[:lower:]' '[:upper:]')
while true; do
# Check if the ${CURRENT_APP} is already enabled in the ${dk_compose_filename} file and if it is not (if there is a #ENABLE_$CURRENTAPP) then ask the user if they want to enable it
toLog_ifDebug -l "[DEBUG]" -m "Checking if the ${CURRENT_APP} app is already enabled in the ${dk_compose_filename} file"
if grep -q "#ENABLE_${CURRENT_APP}" "${dk_compose_filename}"; then
toLog_ifDebug -l "[DEBUG]" -m "${CURRENT_APP} is not enabled in the ${dk_compose_filename} file, asking the user if they want to enable it"
# Show the generic message before asking the user if they want to enable the app
colorprint "YELLOW" "PLEASE REGISTER ON THE PLATFORMS USING THE LINKS THAT WILL BE PROVIDED, YOU'LL THEN NEED TO ENTER SOME DATA BELOW:"
# Ask the user if they want to enable the ${CURRENT_APP}
colorprint "YELLOW" "Do you wish to enable and use ${CURRENT_APP}? (Y/N)"
read -r yn
case $yn in
[Yy]* )
toLog_ifDebug -l "[DEBUG]" -m "User decided to enable and use ${CURRENT_APP}"
colorprint "CYAN" "Go to ${CURRENT_APP} ${link} and register"
colorprint "GREEN" "Use CTRL+Click to open links or copy them:"
read -r -p "When you are done press Enter to continue"
toLog_ifDebug -l "[DEBUG]" -m "Enabling ${CURRENT_APP} app. The parameters received are: name=$name, link=$link, image=$app_image, flags=${flags[*]}, claimURLBase=$claimURLBase"
# Read the flags in the array and execute the relative logic using the case statement
for flag_name in "${flags[@]}"; do
# Extract flag details and all the parameters if they exist
local flag_details
flag_details=$(jq -r ".flags[\"$flag_name\"]?" <<< "$app_json")
toLog_ifDebug -l "[DEBUG]" -m "Result of flag_details reading: $flag_details"
if [[ "$flag_details" != "null" ]]; then
#load all the flags parameters keys in an array so then we cann iterate on them and access their values easily from the json
local flag_params_keys
flag_params_keys=$(jq -r "keys[]?" <<< "$flag_details")
toLog_ifDebug -l "[DEBUG]" -m "Result of flag_params_keys reading: $flag_params_keys"
else
toLog_ifDebug -l "[DEBUG]" -m "No flag details found for flag: $flag_name"
fi
case $flag_name in
--email)
toLog_ifDebug -l "[DEBUG]" -m "Starting email setup for ${CURRENT_APP} app"
while true; do
colorprint "GREEN" "Enter your ${CURRENT_APP} Email:"
read -r APP_EMAIL
if [[ "$APP_EMAIL" =~ ^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
sed -i "s/your${CURRENT_APP}Mail/$APP_EMAIL/" ${ENV_FILENAME}
break
else
colorprint "RED" "Invalid email address. Please try again."
fi
done
;;
--password)
toLog_ifDebug -l "[DEBUG]" -m "Starting password setup for ${CURRENT_APP} app"
while true; do
colorprint "DEFAULT" "Note: If you are using login with Google, remember to set also a password for your ${CURRENT_APP} account!"
colorprint "GREEN" "Enter your ${CURRENT_APP} Password:"
read -r APP_PASSWORD
if [[ -z "$APP_PASSWORD" ]]; then
colorprint "RED" "Password cannot be empty. Please try again."
else
sed -i "s/your${CURRENT_APP}Pw/$APP_PASSWORD/" ${ENV_FILENAME}
break
fi
done
;;
--apikey)
toLog_ifDebug -l "[DEBUG]" -m "Starting APIKey setup for ${CURRENT_APP} app"
colorprint "DEFAULT" "Find/Generate your APIKey inside your ${CURRENT_APP} dashboard/profile."
while true; do
colorprint "GREEN" "Enter your ${CURRENT_APP} APIKey:"
read -r APP_APIKEY
if [[ -z "$APP_APIKEY" ]]; then
colorprint "RED" "APIKey cannot be empty. Please try again."
else
sed -i "s^your${CURRENT_APP}APIKey^$APP_APIKEY^" ${ENV_FILENAME}
break
fi
done
;;
--userid)
toLog_ifDebug -l "[DEBUG]" -m "Starting UserID setup for ${CURRENT_APP} app"
colorprint "DEFAULT" "Find your UserID inside your ${CURRENT_APP} dashboard/profile."
while true; do
colorprint "GREEN" "Enter your ${CURRENT_APP} UserID:"
read -r APP_USERID
if [[ -z "$APP_USERID" ]]; then
colorprint "RED" "UserID cannot be empty. Please try again."
else
sed -i "s/your${CURRENT_APP}UserID/$APP_USERID/" ${ENV_FILENAME}
break
fi
done
;;
--uuid)
toLog_ifDebug -l "[DEBUG]" -m "Starting UUID setup for ${CURRENT_APP} app"
colorprint "DEFAULT" "Starting UUID generation/import for ${CURRENT_APP}"
# Read all the parameters for the uuid flag , if one of them is the case length then save it in a variable
if [[ -n "${flag_params_keys:-}" ]]; then
for flag_param_key in $flag_params_keys; do
toLog_ifDebug -l "[DEBUG]" -m "Reading flag parameter: $flag_param_key"
case $flag_param_key in
length)
toLog_ifDebug -l "[DEBUG]" -m "Reading flag parameter length"
flag_length_param=$(jq -r ".flags[\"$flag_name\"][\"$flag_param_key\"]?" <<< "$app_json")
toLog_ifDebug -l "[DEBUG]" -m "Result of flag_length_param reading: $flag_length_param"
;;
*)
toLog_ifDebug -l "[DEBUG]" -m "Unknown flag parameter: $flag_param_key"
;;
esac
done
else
toLog_ifDebug -l "[DEBUG]" -m "No flag parameters found for flag: $flag_name as flag_params_keys array is empty"
fi
# Check if the flag_length_param exists and if is a number (i.e., the desired length)
if [[ -n "${flag_length_param:-}" ]] && [[ "${flag_length_param:-}" =~ ^[0-9]+$ ]]; then
DESIRED_LENGTH="$flag_length_param"
toLog_ifDebug -l "[DEBUG]" -m "Desired length for UUID generation/import passed as argument of the uuid flag (read from json), its value is: $DESIRED_LENGTH"
else
# If no length is provided, ask the user
toLog_ifDebug -l "[DEBUG]" -m "No desired length for UUID generation/import passed as argument of the uuid flag, asking the user"
colorprint "GREEN" "Enter desired length for the UUID (default is 32, press Enter to use default):"
read -r DESIRED_LENGTH_INPUT
DESIRED_LENGTH=${DESIRED_LENGTH_INPUT:-32} # Defaulting to 32 if no input provided
fi
toLog_ifDebug -l "[DEBUG]" -m "Starting temporary UUID generation/import for ${CURRENT_APP} with desired length: $DESIRED_LENGTH. This will be overwritten if the user chooses to use an existing UUID."
local UUID=""
while [ ${#UUID} -lt "$DESIRED_LENGTH" ]; do
# Regenerate the salt for each iteration
SALT="${DEVICE_NAME}""${RANDOM}""${UUID}" # Incorporate the previously generated UUID part for added randomness
UUID_PART="$(echo -n "$SALT" | md5sum | cut -c1-32)"
UUID+="$UUID_PART"
done
# Cut or trail the generated UUID based on the desired length
UUID=${UUID:0:$DESIRED_LENGTH}
toLog_ifDebug -l "[DEBUG]" -m "Done, generated temporary UUID: $UUID"
while true; do
colorprint "YELLOW" "Do you want to use a previously registered uuid for ${CURRENT_APP}? (Y/N)"
read -r USE_EXISTING_UUID
case $USE_EXISTING_UUID in
[Yy]* )
while true; do
colorprint "GREEN" "Please enter the alphanumeric part of the existing uuid for ${CURRENT_APP}, it should be $DESIRED_LENGTH characters long."
colorprint "DEFAULT" "E.g. if existing registered node is sdk-node-b86301656baefekba8917349bdf0f3g4 then enter just b86301656baefekba8917349bdf0f3g4"
read -r EXISTING_UUID
if [[ ! "$EXISTING_UUID" =~ ^[a-f0-9]{$DESIRED_LENGTH}$ ]]; then
colorprint "RED" "Invalid UUID entered, it should be an alphanumeric string and $DESIRED_LENGTH characters long."
colorprint "DEFAULT" "Do you want to try again? (Y/N)"
read -r TRY_AGAIN
case $TRY_AGAIN in
[Yy]* ) continue ;;
[Nn]* ) break ;;
* ) continue ;;
esac
else
UUID="$EXISTING_UUID"
print_and_log "DEFAULT" "Using user provided existing UUID: $UUID"
break
fi