forked from GhostWriters/DockSTARTer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.sh
More file actions
1338 lines (1287 loc) · 49.1 KB
/
Copy pathmain.sh
File metadata and controls
1338 lines (1287 loc) · 49.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
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
set -Eeuo pipefail
IFS=$'\n\t'
declare -rx APPLICATION_NAME='DockSTARTer'
declare -rx SOURCE_BRANCH='master'
declare -rx TARGET_BRANCH='main'
export LC_ALL=C
export PROMPT="CLI"
export MENU=false
# Script Information
# https://stackoverflow.com/questions/59895/get-the-source-directory-of-a-bash-script-from-within-the-script-itself/246128#246128
get_scriptname() {
# https://stackoverflow.com/questions/35006457/choosing-between-0-and-bash-source/35006505#35006505
local SOURCE=${BASH_SOURCE[0]:-$0}
while [[ -L ${SOURCE} ]]; do # resolve ${SOURCE} until the file is no longer a symlink
local DIR
DIR=$(cd -P "$(dirname "${SOURCE}")" > /dev/null 2>&1 && pwd)
SOURCE=$(readlink "${SOURCE}")
[[ ${SOURCE} != /* ]] && SOURCE="${DIR}/${SOURCE}" # if ${SOURCE} was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
echo "${SOURCE}"
}
SCRIPTPATH=$(cd -P "$(dirname "$(get_scriptname)")" > /dev/null 2>&1 && pwd)
readonly SCRIPTPATH
SCRIPTNAME="${SCRIPTPATH}/$(basename "$(get_scriptname)")"
readonly SCRIPTNAME
# System Information
ARCH=$(uname -m)
readonly ARCH
export ARCH
# Environment Information
readonly COMPOSE_FOLDER_NAME="compose"
export COMPOSE_FOLDER_NAME
readonly COMPOSE_FOLDER="${SCRIPTPATH}/${COMPOSE_FOLDER_NAME}"
export COMPOSE_FOLDER
readonly COMPOSE_OVERRIDE_NAME="docker-compose.override.yml"
export COMPOSE_OVERRIDE_NAME
readonly COMPOSE_OVERRIDE="${COMPOSE_FOLDER}/${COMPOSE_OVERRIDE_NAME}"
export COMPOSE_OVERRIDE
readonly COMPOSE_ENV="${COMPOSE_FOLDER}/.env"
export COMPOSE_ENV
readonly COMPOSE_ENV_DEFAULT_FILE="${COMPOSE_FOLDER}/.env.example"
export COMPOSE_ENV_DEFAULT_FILE
readonly APP_ENV_FOLDER_NAME="env_files"
export APP_ENV_FOLDER_NAME
readonly APP_ENV_FOLDER="${COMPOSE_FOLDER}/env_files"
export APP_ENV_FOLDER
readonly TEMPLATES_FOLDER="${COMPOSE_FOLDER}/.apps"
export TEMPLATES_FOLDER
readonly INSTANCES_FOLDER="${COMPOSE_FOLDER}/.instances"
export INSTANCES_FOLDER
readonly THEME_FOLDER="${SCRIPTPATH}/.themes"
export THEME_FOLDER
# User/Group Information
readonly DETECTED_PUID=${SUDO_UID:-$UID}
export DETECTED_PUID
DETECTED_UNAME=$(id -un "${DETECTED_PUID}" 2> /dev/null || true)
readonly DETECTED_UNAME
export DETECTED_UNAME
DETECTED_PGID=$(id -g "${DETECTED_PUID}" 2> /dev/null || true)
readonly DETECTED_PGID
export DETECTED_PGID
DETECTED_UGROUP=$(id -gn "${DETECTED_PUID}" 2> /dev/null || true)
readonly DETECTED_UGROUP
export DETECTED_UGROUP
DETECTED_HOMEDIR=$(eval echo "~${DETECTED_UNAME}" 2> /dev/null || true)
readonly DETECTED_HOMEDIR
export DETECTED_HOMEDIR
# Terminal Colors
declare -Agr B=( # Background
[B]=$(tput setab 4 2> /dev/null || echo -e "\e[44m") # Blue
[C]=$(tput setab 6 2> /dev/null || echo -e "\e[46m") # Cyan
[G]=$(tput setab 2 2> /dev/null || echo -e "\e[42m") # Green
[K]=$(tput setab 0 2> /dev/null || echo -e "\e[40m") # Black
[M]=$(tput setab 5 2> /dev/null || echo -e "\e[45m") # Magenta
[R]=$(tput setab 1 2> /dev/null || echo -e "\e[41m") # Red
[W]=$(tput setab 7 2> /dev/null || echo -e "\e[47m") # White
[Y]=$(tput setab 3 2> /dev/null || echo -e "\e[43m") # Yellow
)
declare -Agr F=( # Foreground
[B]=$(tput setaf 4 2> /dev/null || echo -e "\e[34m") # Blue
[C]=$(tput setaf 6 2> /dev/null || echo -e "\e[36m") # Cyan
[G]=$(tput setaf 2 2> /dev/null || echo -e "\e[32m") # Green
[K]=$(tput setaf 0 2> /dev/null || echo -e "\e[30m") # Black
[M]=$(tput setaf 5 2> /dev/null || echo -e "\e[35m") # Magenta
[R]=$(tput setaf 1 2> /dev/null || echo -e "\e[31m") # Red
[W]=$(tput setaf 7 2> /dev/null || echo -e "\e[37m") # White
[Y]=$(tput setaf 3 2> /dev/null || echo -e "\e[33m") # Yellow
)
NC=$(tput sgr0 2> /dev/null || echo -e "\e[0m")
readonly NC
BS=$(tput cup 1000 0 2> /dev/null || true) # Bottom of screen
readonly BS
export BS
DIALOG=$(command -v dialog) || true
export DIALOG
declare -rx MENU_INI_NAME='menu.ini'
declare -rx MENU_INI_FILE="${SCRIPTPATH}/${MENU_INI_NAME}"
declare -rx THEME_FILE_NAME='theme.ini'
declare -rx DIALOGRC_NAME='.dialogrc'
declare -rx DIALOGRC="${SCRIPTPATH}/${DIALOGRC_NAME}"
declare -rix DIALOGTIMEOUT=3
declare -rix DIALOG_OK=0
declare -rix DIALOG_CANCEL=1
declare -rix DIALOG_HELP=2
declare -rix DIALOG_EXTRA=3
declare -rix DIALOG_ITEM_HELP=4
declare -rix DIALOG_ERROR=254
declare -rix DIALOG_ESC=255
readonly -a DIALOG_BUTTONS=(
[DIALOG_OK]="OK"
[DIALOG_CANCEL]="CANCEL"
[DIALOG_HELP]="HELP"
[DIALOG_EXTRA]="EXTRA"
[DIALOG_ITEM_HELP]="ITEM_HELP"
[DIALOG_ERROR]="ERROR"
[DIALOG_ESC]="ESC"
)
export DIALOG_BUTTONS
declare -Ax DC=()
declare -x DIALOGOTS
# Log Functions
MKTEMP_LOG=$(mktemp) || echo -e "Failed to create temporary log file.\nFailing command: ${F[C]}mktemp"
readonly MKTEMP_LOG
echo "DockSTARTer Log" > "${MKTEMP_LOG}"
create_strip_log_colors_SEDSTRING() {
# Create the search string to strip ANSI colors
# String is saved after creation, so this is only done on the first call
local -a ANSICOLORS=("${F[@]}" "${B[@]}" "${NC}")
for index in "${!ANSICOLORS[@]}"; do
# Escape characters used by sed
ANSICOLORS[index]=$(printf '%s' "${ANSICOLORS[index]}" | sed -E 's/[]{}()[/{}\.''''$]/\\&/g')
done
printf '%s' "s/$(
IFS='|'
printf '%s' "${ANSICOLORS[*]}"
)//g"
}
strip_log_colors_SEDSTRING="$(create_strip_log_colors_SEDSTRING)"
readonly strip_log_colors_SEDSTRING
strip_log_colors() {
printf '%s' "$*" | sed -E "${strip_log_colors_SEDSTRING}"
}
log() {
local TOTERM=${1-}
local MESSAGE=${2-}
local STRIPPED_MESSAGE
STRIPPED_MESSAGE=$(strip_log_colors "${MESSAGE-}")
if [[ -n ${TOTERM} ]]; then
if [[ -t 2 ]]; then
# Stderr is not being redirected, output with color
echo -e "${MESSAGE-}" >&2
else
# Stderr is being redirected, output without colorr
echo -e "${STRIPPED_MESSAGE-}" >&2
fi
fi
# Output the message to the log file without color
echo -e "${STRIPPED_MESSAGE-}" >> "${MKTEMP_LOG}"
}
trace() { log "${TRACE-}" "${NC}$(date +"%F %T") ${F[B]}[TRACE ]${NC} $*${NC}"; }
debug() { log "${DEBUG-}" "${NC}$(date +"%F %T") ${F[B]}[DEBUG ]${NC} $*${NC}"; }
info() { log "${VERBOSE-}" "${NC}$(date +"%F %T") ${F[B]}[INFO ]${NC} $*${NC}"; }
notice() { log true "${NC}$(date +"%F %T") ${F[G]}[NOTICE]${NC} $*${NC}"; }
warn() { log true "${NC}$(date +"%F %T") ${F[Y]}[WARN ]${NC} $*${NC}"; }
error() { log true "${NC}$(date +"%F %T") ${F[R]}[ERROR ]${NC} $*${NC}"; }
fatal() {
log true "${NC}$(date +"%F %T") ${B[R]}${F[W]}[FATAL ]${NC} $*${NC}"
exit 1
}
# Check for supported CPU architecture
check_arch() {
if [[ ${ARCH} != "aarch64" ]] && [[ ${ARCH} != "x86_64" ]]; then
fatal "Unsupported architecture."
fi
}
# Check if the repo exists relative to the SCRIPTPATH
check_repo() {
if [[ -d ${SCRIPTPATH}/.git ]] && [[ -d ${SCRIPTPATH}/.scripts ]]; then
return
else
return 1
fi
}
# Check if running as root
check_root() {
if [[ ${DETECTED_PUID} == "0" ]] || [[ ${DETECTED_HOMEDIR} == "/root" ]]; then
fatal "Running as root is not supported. Please run as a standard user with sudo."
fi
}
# Check if running with sudo
check_sudo() {
if [[ ${EUID} -eq 0 ]]; then
fatal "Running with sudo is not supported. Commands requiring sudo will prompt automatically when required."
fi
}
# Script Runner Function
run_script() {
local SCRIPTSNAME=${1-}
shift
if [[ -f ${SCRIPTPATH}/.scripts/${SCRIPTSNAME}.sh ]]; then
# shellcheck source=/dev/null
source "${SCRIPTPATH}/.scripts/${SCRIPTSNAME}.sh"
${SCRIPTSNAME} "$@"
return
else
fatal "${SCRIPTPATH}/.scripts/${SCRIPTSNAME}.sh not found."
fi
}
# Take whitespace and newline delimited words and output a single line highlited list for dialog
highlighted_list() {
local List
List=$(xargs <<< "$*")
if [[ -n ${List-} ]]; then
echo "${DC["Subtitle"]}${List// /${DC[NC]} ${DC["Subtitle"]}}${DC[NC]}"
fi
}
_dialog_() {
local LeftBackTitle RightBackTitle
local CleanLeftBackTitle CleanRightBackTitle
CleanLeftBackTitle="${APPLICATION_NAME}"
LeftBackTitle="${DC[ApplicationName]}${APPLICATION_NAME}${DC[NC]}"
CleanRightBackTitle=''
RightBackTitle=''
if ds_update_available; then
CleanRightBackTitle="(Update Available)"
RightBackTitle="${DC[ApplicationUpdateBrackets]}(${DC[ApplicationUpdate]}Update Available${DC[ApplicationUpdateBrackets]})${DC[NC]}"
fi
if [[ ${APPLICATION_VERSION-} ]]; then
if [[ -n ${CleanRightBackTitle-} ]]; then
CleanRightBackTitle+=" "
RightBackTitle+="${DC[ApplicationVersionSpace]} "
fi
local CurrentVersion
CurrentVersion="$(ds_version)"
if [[ -z ${CurrentVersion} ]]; then
CurrentVersion="$(ds_branch) Unknown Version"
fi
CleanRightBackTitle+="[${CurrentVersion}]"
RightBackTitle+="${DC[ApplicationVersionBrackets]}[${DC[ApplicationVersion]}${CurrentVersion}${DC[ApplicationVersionBrackets]}]${DC[NC]}"
fi
local -i IndentLength
IndentLength=$((COLUMNS - ${#CleanLeftBackTitle} - ${#CleanRightBackTitle} - 2))
local Indent
Indent="$(printf %${IndentLength}s '')"
BackTitle="${LeftBackTitle}${Indent}${RightBackTitle}"
${DIALOG} --backtitle "${BackTitle}" "$@"
}
# Check to see if we should use a dialog box
use_dialog_box() {
[[ ${PROMPT:-CLI} == GUI && -t 1 && -t 2 ]]
}
# Pipe to Dialog Box Function
dialog_pipe() {
local Title=${1:-}
local SubTitle=${2:-}
local TimeOut=${3:-0}
_dialog_ \
--title "${DC["Title"]}${Title}" \
--timeout "${TimeOut}" \
--programbox "${DC["Subtitle"]}${SubTitle}" \
"$((LINES - DC["WindowRowsAdjust"]))" "$((COLUMNS - DC["WindowColsAdjust"]))" || true
echo -n "${BS}"
}
# Script Dialog Runner Function
run_script_dialog() {
local Title=${1:-}
local SubTitle=${2:-}
local TimeOut=${3:-0}
local SCRIPTSNAME=${4-}
shift 4
if use_dialog_box; then
# Using the GUI, pipe output to a dialog box
run_script "${SCRIPTSNAME}" "$@" |& dialog_pipe "${Title}" "${SubTitle}" "${TimeOut}"
return "${PIPESTATUS[0]}"
else
run_script "${SCRIPTSNAME}" "$@"
return
fi
}
# Command Dialog Runner Function
run_command_dialog() {
local Title=${1:-}
local SubTitle=${2:-}
local TimeOut=${3:-0}
local CommandName=${4-}
shift 4
if [[ -n ${CommandName-} ]]; then
if use_dialog_box; then
# Using the GUI, pipe output to a dialog box
"${CommandName}" "$@" |& dialog_pipe "${Title}" "${SubTitle}" "${TimeOut}"
return "${PIPESTATUS[0]}"
else
"${CommandName}" "$@"
return
fi
fi
}
dialog_message() {
local Title=${1:-}
local Message=${2:-}
local TimeOut=${3:-0}
_dialog_ \
--title "${Title}" \
--timeout "${TimeOut}" \
--msgbox "${Message}" \
"$((LINES - DC["WindowRowsAdjust"]))" "$((COLUMNS - DC["WindowColsAdjust"]))"
echo -n "${BS}"
}
dialog_error() {
local Title=${1:-}
local Message=${2:-}
local TimeOut=${3:-0}
dialog_message "${DC["TitleError"]}${Title}" "${Message}" "${TimeOut}"
}
dialog_warning() {
local Title=${1:-}
local Message=${2:-}
local TimeOut=${3:-0}
dialog_message "${DC["TitleWarning"]}${Title}" "${Message}" "${TimeOut}"
}
dialog_success() {
local Title=${1:-}
local Message=${2:-}
local TimeOut=${3:-0}
dialog_message "${DC["TitleSuccess"]}${Title}" "${Message}" "${TimeOut}"
}
ds_branch() {
pushd "${SCRIPTPATH}" &> /dev/null || fatal "Failed to change directory.\nFailing command: ${F[C]}pushd \"${SCRIPTPATH}\""
git fetch --quiet &> /dev/null || true
git symbolic-ref --short HEAD 2> /dev/null || true
popd &> /dev/null
}
ds_branch_exists() {
local CurrentBranch
CurrentBranch="$(ds_branch)"
local CheckBranch
CheckBranch=${1:-"${CurrentBranch}"}
pushd "${SCRIPTPATH}" &> /dev/null || fatal "Failed to change directory.\nFailing command: ${F[C]}pushd \"${SCRIPTPATH}\""
local -i result=0
git ls-remote --exit-code --heads origin "${CheckBranch}" &> /dev/null || result=$?
popd &> /dev/null
return ${result}
}
ds_version() {
local CheckBranch
CheckBranch=${1-}
local commitish Branch
if [[ -n ${CheckBranch-} ]]; then
commitish="origin/${CheckBranch}"
Branch="${CheckBranch}"
else
commitish='HEAD'
Branch="$(ds_branch)"
fi
pushd "${SCRIPTPATH}" &> /dev/null || fatal "Failed to change directory.\nFailing command: ${F[C]}pushd \"${SCRIPTPATH}\""
if [[ -z ${CheckBranch-} ]] || ds_branch_exists "${Branch}"; then
# Get the current tag. If no tag, use the commit instead.
local VersionString
VersionString="$(git describe --tags --exact-match "${commitish}" 2> /dev/null || true)"
if [[ -z ${VersionString-} ]]; then
VersionString="commit $(git rev-parse --short "${commitish}" 2> /dev/null || true)"
fi
VersionString="${Branch} ${VersionString}"
else
VersionString=''
fi
echo "${VersionString}"
popd &> /dev/null
}
ds_update_available() {
pushd "${SCRIPTPATH}" &> /dev/null || fatal "Failed to change directory.\nFailing command: ${F[C]}pushd \"${SCRIPTPATH}\""
git fetch --quiet &> /dev/null
local -i result=0
# shellcheck disable=SC2319 # This $? refers to a condition, not a command. Assign to a variable to avoid it being overwritten.
[[ $(git rev-parse HEAD 2> /dev/null) != $(git rev-parse '@{u}' 2> /dev/null) ]] || result=$?
popd &> /dev/null
return ${result}
}
switch_branch() {
local CurrentBranch
CurrentBranch="$(ds_branch)"
if [[ ${CurrentBranch} == "${SOURCE_BRANCH}" ]] && ds_branch_exists "${TARGET_BRANCH}"; then
FORCE=true
export FORCE
PROMPT="CLI"
notice "Automatically switching from ${APPLICATION_NAME} branch '${F[C]}${SOURCE_BRANCH}${NC}' to '${F[C]}${TARGET_BRANCH}${NC}'."
run_script 'update_self' "${TARGET_BRANCH}" bash "${SCRIPTNAME}" "$@"
exit
fi
}
declare -x APPLICATION_VERSION
if check_repo; then
APPLICATION_VERSION="$(ds_version)"
if [[ -z ${APPLICATION_VERSION} ]]; then
APPLICATION_VERSION="$(ds_branch) Unknown Version"
fi
else
APPLICATION_VERSION="Unknown Version"
fi
readonly APPLICATION_VERSION
usage() {
local APPLICATION_HEADING="${APPLICATION_NAME}"
if [[ ${APPLICATION_VERSION-} ]]; then
APPLICATION_HEADING+=" [${F[C]}${APPLICATION_VERSION}${NC}]"
fi
if ds_update_available; then
APPLICATION_HEADING+=" (${F[C]}Update Available${NC})"
fi
cat << EOF
Usage: ds [OPTION]
NOTE: ds shortcut is only available after the first run of
bash main.sh
${APPLICATION_HEADING}
This is the main ${APPLICATION_NAME} script.
For regular usage you can run without providing any options.
Any command that takes a variable name, the variable name can also be in the
form of app:var to refer to the variable in env_files/app.env. Some commands
that take app names can use the form app: to refer to the same file.
-a --add <app> [<app> ...]
Add the default .env variables for the app(s) specified
-c --compose <pull/up/down/stop/restart/update> [<app> ...]
Run docker compose commands. If no command is given, does an update.
Update is the same as a 'pull' followed by an 'up'
-c --compose <generate/merge>
Generates the docker-compose.yml file
-e --env
Update your .env file with new variables
--env-appvars <app> [<app> ...]
List all variable names for the app(s) specified
--env-appvars-lines <app> [<app> ...]
List all variables and values for the app(s) specified
--env-get <var> [<var> ...]
--env-get=<var>
Get the value of a <var>iable in .env (variable name is forced to UPPER CASE)
--env-get-line <var> [<var> ...]
--env-get-line=<var>
Get the line of a <var>iable in .env (variable name is forced to UPPER CASE)
--env-get-literal <var> [<var> ...]
--env-get-literal=<var>
Get the literal value (including quotes) of a <var>iable in .env (variable name is forced to UPPER CASE)
--env-get-lower <var> [<var> ...]
--env-get-lower=<var>
Get the value of a <var>iable in .env
--env-get-lower-line <var> [<var> ...]
--env-get-lower-line=<var>
Get the line of a <var>iable in .env
--env-get-lower-literal <var> [<var> ...]
--env-get-lower-literal=<var>
Get the literal value (including quotes) of a <var>iable in .env
--env-set <var>=<val>
--env-set=<var>,<val>
Set the <val>ue of a <var>iable in .env (variable name is forced to UPPER CASE)
--env-set-lower <var>=<val>
--env-set-lower=<var>,<val>
Set the <val>ue of a <var>iable in .env
-f --force
Force certain install/upgrade actions to run even if they would not be needed
-g --gui
Use dialog boxes
-l --list
List all apps
--list-added
List added apps
--list-builtin
List builtin apps
--list-depreciated
List depreciated apps
--list-enabled
List enabled apps
--list-disabled
List disabled apps
--list-nondepreciated
List depreciated apps
--list-referenced
List referenced apps (whether they are "built in" or not)
An app is considered "referenced" if there is a variable matching the app's name in the
global .env file, or there are any variables in the file env_files/<appname>.env
-h --help
Show this usage information
-i --install
Install/update all dependencies
-p --prune
Remove unused docker resources
-r --remove
Prompt to remove .env variables for all disabled apps
-r --remove <appname>
Prompt to remove the .env variables for the app specified
-s --status <appname>
Returns the enabled/disabled status for the app specified
--status-disable <appname>
Disable the app specified
--status-enable <appname>
Enable the app specified
-t --test <test_name>
Run tests to check the program
-T --theme <themename>
Applies the specified theme to the GUI
--theme-list
Lists the available themes
--theme-table
Lists the available themes in a table format
--theme-lines
--theme-no-lines
Turn the line drawing characters on or off in the GUI
--theme-borders
--theme-no-borders
Turn the borders on and off inthe GUI
--theme-shadow
--theme-no-shadow
Turn the shadow on or off in the GUI
--theme-scrollbar
--theme-no-scrollbar
Turn the scrollbar on or off in the GUI
-u --update
Update ${APPLICATION_NAME} to the latest stable commits
-u --update <branch>
Update ${APPLICATION_NAME} to the latest commits from the specified branch
-v --verbose
Verbose
-V --version
Display version information
-x --debug
Debug
EOF
}
# Cleanup Function
cleanup() {
local -ri EXIT_CODE=$?
trap - ERR EXIT SIGABRT SIGALRM SIGHUP SIGINT SIGQUIT SIGTERM
sudo sh -c "cat ${MKTEMP_LOG:-/dev/null} >> ${SCRIPTPATH}/dockstarter.log" || true
sudo rm -f "${MKTEMP_LOG-}" || true
sudo sh -c "echo \"$(tail -1000 "${SCRIPTPATH}/dockstarter.log")\" > ${SCRIPTPATH}/dockstarter.log" || true
sudo -E chmod +x "${SCRIPTNAME}" > /dev/null 2>&1 || true
if [[ ${CI-} == true ]] && [[ ${TRAVIS_SECURE_ENV_VARS-} == false ]]; then
echo "TRAVIS_SECURE_ENV_VARS is false for Pull Requests from remote branches. Please retry failed builds!"
fi
if [[ ${EXIT_CODE} -ne 0 ]]; then
echo "${APPLICATION_NAME} did not finish running successfully."
fi
exit ${EXIT_CODE}
}
trap 'cleanup' ERR EXIT SIGABRT SIGALRM SIGHUP SIGINT SIGQUIT SIGTERM
# Command Line Arguments
readonly ARGS=("$@")
cmdline() {
while getopts ":-:a:c:efghilpr:s:t:T:u:vV:x" OPTION; do
# support long options: https://stackoverflow.com/a/28466267/519360
if [ "$OPTION" = "-" ]; then # long option: reformulate OPTION and OPTARG
OPTION="${OPTARG}" # extract long option name
OPTARG=''
if [[ -n ${!OPTIND-} ]]; then
OPTARG="${!OPTIND}"
OPTIND=$((OPTIND + 1))
fi
fi
case ${OPTION} in
a | add)
if [[ -n ${OPTARG-} ]]; then
local MULTIOPT
MULTIOPT=("$OPTARG")
until [[ -z ${!OPTIND-} || ${!OPTIND} =~ ^-.* ]]; do
MULTIOPT+=("${!OPTIND}")
OPTIND=$((OPTIND + 1))
done
ADD=$(printf "%s " "${MULTIOPT[@]}" | xargs)
readonly ADD
else
error "${OPTION} requires an option."
exit 1
fi
;;
c | compose)
if [[ -n ${OPTARG-} ]]; then
case ${OPTARG} in
generate | merge) ;&
down | pull | stop | restart | update | up) ;&
"down "* | "pull "* | "stop "* | "restart "* | "update "* | "up "*)
local MULTIOPT
MULTIOPT=("$OPTARG")
until [[ -z ${!OPTIND-} || ${!OPTIND} =~ ^-.* ]]; do
MULTIOPT+=("${!OPTIND}")
OPTIND=$((OPTIND + 1))
done
COMPOSE=$(printf "%s " "${MULTIOPT[@]}" | xargs)
readonly COMPOSE
;;
*)
error "Invalid compose option ${OPTARG}."
exit 1
;;
esac
else
readonly COMPOSE=update
fi
;;
e | env)
readonly ENVMETHOD='env'
;;
env-appvars | env-appvars-lines)
readonly ENVMETHOD=${OPTION}
local MULTIOPT
MULTIOPT=("$OPTARG")
until [[ -z ${!OPTIND-} || ${!OPTIND} =~ ^-.* ]]; do
MULTIOPT+=("${!OPTIND}")
OPTIND=$((OPTIND + 1))
done
ENVAPP=$(printf "%s " "${MULTIOPT[@]}" | xargs)
readonly ENVAPP
;;
env-get=* | env-get-lower=* | env-get-line=* | env-get-lower-line=* | env-get-literal=* | env-get-lower-literal=*)
readonly ENVMETHOD=${OPTION%%=*}
readonly ENVARG=${OPTION#*=}
if [[ ${ENVMETHOD-} != "${ENVARG-}" ]]; then
readonly ENVVAR=${ENVARG}
fi
;;
env-set=* | env-set-lower=*)
readonly ENVMETHOD=${OPTION%%=*}
readonly ENVARG=${OPTION#*=}
if [[ ${ENVMETHOD-} != "${ENVARG-}" ]]; then
readonly ENVVAR=${ENVARG%%,*}
readonly ENVVAL=${ENVARG#*,}
fi
;;
env-get | env-get-lower | env-get-line | env-get-lower-line | env-get-literal | env-get-lower-literal)
readonly ENVMETHOD=${OPTION}
if [[ -z ${ENVVAR-} ]]; then
local MULTIOPT
MULTIOPT=("$OPTARG")
until [[ -z ${!OPTIND-} || ${!OPTIND} =~ ^-.* ]]; do
MULTIOPT+=("${!OPTIND}")
OPTIND=$((OPTIND + 1))
done
ENVVAR=$(printf "%s " "${MULTIOPT[@]}" | xargs)
readonly ENVVAR
fi
;;
env-set | env-set-lower)
readonly ENVMETHOD=${OPTION}
if [[ -z ${ENVVAR-} ]]; then
readonly ENVARG=${OPTARG}
readonly ENVVAR=${ENVARG%%=*}
readonly ENVVAL=${ENVARG#*=}
fi
;;
f | force)
readonly FORCE=true
export FORCE
;;
g | gui)
if [[ -n ${DIALOG-} ]]; then
PROMPT="GUI"
else
warn "The '--gui' option requires the dialog command to be installed."
warn "'dialog' command not found. Run 'ds -fiv' to install all dependencies."
warn "Coninuing without '--gui' option."
fi
;;
h | help)
usage
exit
;;
i | install)
readonly INSTALL=true
;;
l | list)
readonly LISTMETHOD='list'
readonly LIST=true
;;
list-*)
readonly LISTMETHOD=${OPTION}
;;
p | prune)
readonly PRUNE=true
;;
r | remove)
if [[ -n ${OPTARG-} ]]; then
local MULTIOPT
MULTIOPT=("$OPTARG")
until [[ -z ${!OPTIND-} || ${!OPTIND} =~ ^-.* ]]; do
MULTIOPT+=("${!OPTIND}")
OPTIND=$((OPTIND + 1))
done
REMOVE=$(printf "%s " "${MULTIOPT[@]}" | xargs)
readonly REMOVE
else
error "${OPTION} requires an option."
exit 1
fi
;;
status-*)
if [[ -n ${OPTARG-} ]]; then
readonly STATUSMETHOD=${OPTION}
local MULTIOPT
MULTIOPT=("$OPTARG")
until [[ -z ${!OPTIND-} || ${!OPTIND} =~ ^-.* ]]; do
MULTIOPT+=("${!OPTIND}")
OPTIND=$((OPTIND + 1))
done
STATUS=$(printf "%s " "${MULTIOPT[@]}" | xargs)
readonly STATUS
else
error "${OPTION} requires an option."
exit 1
fi
;;
s | status)
if [[ -n ${OPTARG-} ]]; then
readonly STATUSMETHOD='status'
local MULTIOPT
MULTIOPT=("$OPTARG")
until [[ -z ${!OPTIND-} || ${!OPTIND} =~ ^-.* ]]; do
MULTIOPT+=("${!OPTIND}")
OPTIND=$((OPTIND + 1))
done
STATUS=$(printf "%s " "${MULTIOPT[@]}" | xargs)
readonly STATUS
else
error "${OPTION} requires an option."
exit 1
fi
;;
t | test)
if [[ -n ${OPTARG-} ]]; then
readonly TEST=${OPTARG}
else
error "${OPTION} requires an option."
exit 1
fi
;;
T | theme)
readonly THEMEMETHOD='theme'
if [[ -n ${OPTARG-} ]]; then
readonly THEME="${OPTARG}"
OPTIND=$((OPTIND + 1))
fi
;;
theme-*)
readonly THEMEMETHOD=${OPTION}
;;
u | update)
UPDATE=true
if [[ -n ${OPTARG-} ]]; then
UPDATE="${OPTARG}"
fi
readonly UPDATE
;;
v | verbose)
readonly VERBOSE=1
;;
V | version)
VERSION=''
if [[ -n ${OPTARG-} && ${OPTARG:0:1} != '-' ]]; then
VERSION="${OPTARG}"
fi
readonly VERSION
;;
x | debug)
readonly DEBUG=1
set -x
;;
:)
case ${OPTARG} in
c)
readonly COMPOSE=update
;;
r)
readonly REMOVE=true
;;
T)
readonly THEMEMETHOD='theme'
;;
u)
readonly UPDATE=true
;;
V)
readonly VERSION=''
;;
*)
error "${OPTARG} requires an option."
exit 1
;;
esac
;;
*)
usage
exit
;;
esac
done
return
}
if check_repo; then
switch_branch "${ARGS[@]-}"
fi
cmdline "${ARGS[@]-}"
if [[ -n ${DEBUG-} ]] && [[ -n ${VERBOSE-} ]]; then
readonly TRACE=1
fi
# Test Runner Function
run_test() {
local SCRIPTSNAME=${1-}
shift
local TESTSNAME="test_${SCRIPTSNAME}"
if [[ -f ${SCRIPTPATH}/.scripts/${SCRIPTSNAME}.sh ]]; then
if grep -q -P "${TESTSNAME}" "${SCRIPTPATH}/.scripts/${SCRIPTSNAME}.sh"; then
notice "Testing ${SCRIPTSNAME}."
# shellcheck source=/dev/null
source "${SCRIPTPATH}/.scripts/${SCRIPTSNAME}.sh"
"${TESTSNAME}" "$@" || fatal "Failed to run ${TESTSNAME}."
notice "Completed testing ${TESTSNAME}."
else
fatal "Test function in ${SCRIPTPATH}/.scripts/${SCRIPTSNAME}.sh not found."
fi
else
fatal "${SCRIPTPATH}/.scripts/${SCRIPTSNAME}.sh not found."
fi
}
# Version Functions
# https://stackoverflow.com/questions/4023830/how-to-compare-two-strings-in-dot-separated-version-format-in-bash#comment92693604_4024263
vergte() { printf '%s\n%s' "${2}" "${1}" | sort -C -V; }
vergt() { ! vergte "${2}" "${1}"; }
verlte() { printf '%s\n%s' "${1}" "${2}" | sort -C -V; }
verlt() { ! verlte "${2}" "${1}"; }
# Github Token for CI
if [[ ${CI-} == true ]] && [[ ${TRAVIS_SECURE_ENV_VARS-} == true ]]; then
readonly GH_HEADER="Authorization: token ${GH_TOKEN}"
export GH_HEADER
fi
# Main Function
main() {
check_arch
# Terminal Check
if [[ -t 1 ]]; then
check_root
check_sudo
fi
# Repo Check
local DS_COMMAND
DS_COMMAND=$(command -v ds || true)
if [[ -L ${DS_COMMAND} ]]; then
local DS_SYMLINK
DS_SYMLINK=$(readlink -f "${DS_COMMAND}")
if [[ ${SCRIPTNAME} != "${DS_SYMLINK}" ]]; then
if check_repo; then
if run_script 'question_prompt' "${PROMPT:-CLI}" N "${APPLICATION_NAME} installation found at ${DS_SYMLINK} location. Would you like to run ${SCRIPTNAME} instead?"; then
run_script 'symlink_ds'
DS_COMMAND=$(command -v ds || true)
DS_SYMLINK=$(readlink -f "${DS_COMMAND}")
fi
fi
warn "Attempting to run ${APPLICATION_NAME} from ${DS_SYMLINK} location."
bash "${DS_SYMLINK}" -fvu
bash "${DS_SYMLINK}" -fvi
exec bash "${DS_SYMLINK}" "${ARGS[@]-}"
fi
else
if ! check_repo; then
warn "Attempting to clone ${APPLICATION_NAME} repo to ${DETECTED_HOMEDIR}/.docker location."
git clone https://github.com/GhostWriters/DockSTARTer "${DETECTED_HOMEDIR}/.docker" || fatal "Failed to clone ${APPLICATION_NAME} repo.\nFailing command: ${F[C]}git clone https://github.com/GhostWriters/DockSTARTer \"${DETECTED_HOMEDIR}/.docker\""
notice "Performing first run install."
exec bash "${DETECTED_HOMEDIR}/.docker/main.sh" "-fvi"
fi
fi
# Create Symlink
run_script 'symlink_ds'
local Branch
Branch="$(ds_branch)"
if ds_branch_exists "${Branch}"; then
if ds_update_available; then
notice "${APPLICATION_NAME} [${F[C]}${APPLICATION_VERSION}${NC}]"
notice "An update to ${APPLICATION_NAME} is available."
notice "Run '${F[C]}ds -u${NC}' to update to version ${F[C]}$(ds_version "${Branch}")${NC}."
else
info "${APPLICATION_NAME} [${F[C]}${APPLICATION_VERSION}${NC}]"
fi
else
local MainBranch="${TARGET_BRANCH}"
if ! ds_branch_exists "${MainBranch}"; then
MainBranch="${SOURCE_BRANCH}"
fi
warn "${APPLICATION_NAME} branch '${F[C]}${Branch}${NC}' appears to no longer exist."
warn "${APPLICATION_NAME} is currently on version ${F[C]}$(ds_version)${NC}."
if ! ds_branch_exists "${MainBranch}"; then
error "${APPLICATION_NAME} does not appear to have a '${F[C]}${TARGET_BRANCH}${NC}' or '${F[C]}${SOURCE_BRANCH}${NC}' branch."
else
warn "Run '${F[C]}ds -u ${MainBranch}${NC}' to update to the latest stable release ${F[C]}$(ds_version "${MainBranch}")${NC}."
fi
fi
# Apply the GUI theme
run_script 'apply_theme'
# Execute CLI Argument Functions
if [[ -n ${ADD-} ]]; then
run_script 'env_create'
run_script_dialog "Add Application" "$(highlighted_list "$(run_script 'app_nicename' "${ADD}")")" "" \
'appvars_create' "${ADD}"
run_script 'env_update'
exit
fi
if [[ -n ${COMPOSE-} ]]; then
case ${COMPOSE} in
generate | merge) ;&
down | pull | stop | restart | update | up) ;&
"down "* | "pull "* | "stop "* | "restart "* | "update "* | "up "*)
run_script 'docker_compose' "${COMPOSE}"
;;
*)
fatal "Invalid compose option."
;;
esac
exit
fi
if [[ -n ${ENVMETHOD-} ]]; then
case "${ENVMETHOD-}" in
env)
run_script_dialog "${DC["TitleSuccess"]}Creating environment variables for added apps" "Please be patient, this can take a while.\n${DC["CommandLine"]} ds --env" "" \
'appvars_create_all'
exit
;;
env-get)
if [[ ${ENVVAR-} != "" ]]; then
if use_dialog_box; then
for VarName in $(xargs -n1 <<< "${ENVVAR^^}"); do
run_script 'env_get' "${VarName}"
done |& dialog_pipe "Get Value of Variable" "$(highlighted_list "${ENVVAR^^}")" ""
else
for VarName in $(xargs -n1 <<< "${ENVVAR^^}"); do
run_script 'env_get' "${VarName}"
done
fi
else
echo "Invalid usage. Must be"
echo " --env-get with variable name ('--env-get VAR' or '--env-get VAR [VAR ...]')"
echo " Variable name will be forced to UPPER CASE"
fi
;;
env-get-lower)
if [[ ${ENVVAR-} != "" ]]; then
if use_dialog_box; then
for VarName in $(xargs -n1 <<< "${ENVVAR}"); do
run_script 'env_get' "${VarName}"
done |& dialog_pipe "Get Value of Variable" "$(highlighted_list "${ENVVAR}")" ""
else
for VarName in $(xargs -n1 <<< "${ENVVAR}"); do
run_script 'env_get' "${VarName}"
done
fi
else
echo "Invalid usage. Must be"
echo " --env-get-lower with variable name ('--env-get-lower=Var' or '--env-get-lower Var [Var ...]')"