-
-
Notifications
You must be signed in to change notification settings - Fork 231
/
main.sh
executable file
·529 lines (503 loc) · 17.9 KB
/
main.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
#!/usr/bin/env bash
set -Eeuo pipefail
IFS=$'\n\t'
export LC_ALL=C
usage() {
cat << EOF
Usage: ds [OPTION]
NOTE: ds shortcut is only available after the first run of
bash main.sh
This is the main DockSTARTer script.
For regular usage you can run without providing any options.
-a --add <appname>
add the default .env variables for the app specified
-c --compose
run docker compose up with confirmation prompt
-c --compose <up/down/restart/pull>
run docker compose commands without confirmation prompts
-e --env
update your .env file with new variables
--env-get=<var>
get the value of a <var>iable in .env (variable name is forced to UPPER CASE)
--env-get-lower=<var>
get the value of a <var>iable in .env
--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>
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
-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
-t --test <test_name>
run tests to check the program
-u --update
update DockSTARTer to the latest stable commits
-u --update <branch>
update DockSTARTer to the latest commits from the specified branch
-v --verbose
verbose
-x --debug
debug
EOF
}
# 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
# 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 "DockSTARTer 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() {
# http://www.kfirlavi.com/blog/2012/11/14/defensive-bash-programming/
# http://kirk.webfinish.com/2009/10/bash-shell-script-to-use-getopts-with-gnu-style-long-positional-parameters/
local ARG=
local LOCAL_ARGS
for ARG; do
local DELIM=""
case "${ARG}" in
#translate --gnu-long-options to -g (short options)
--add) LOCAL_ARGS="${LOCAL_ARGS-}-a " ;;
--compose) LOCAL_ARGS="${LOCAL_ARGS-}-c " ;;
--debug) LOCAL_ARGS="${LOCAL_ARGS-}-x " ;;
--env) LOCAL_ARGS="${LOCAL_ARGS-}-e " ;;
--env-*)
readonly ENVMETHOD=${ARG%%=*}
readonly ENVARG=${ARG#*=}
if [[ ${ENVMETHOD-} == "${ENVARG-}" ]]; then
echo "Invalid usage. Must be on of the following:"
echo " --env-set with variable name ('--env-set=VAR,VAL') and value"
echo " --env-get with variable name ('--env-get=VAR')"
echo " --env-set-lower with variable name ('--env-set-lower=Var,VAL') and value"
echo " --env-get-lower with variable name ('--env-get-lower=Var')"
exit
else
readonly ENVVAR=${ENVARG%%,*}
readonly ENVVAL=${ENVARG#*,}
fi
;;
--force) LOCAL_ARGS="${LOCAL_ARGS-}-f " ;;
--help) LOCAL_ARGS="${LOCAL_ARGS-}-h " ;;
--install) LOCAL_ARGS="${LOCAL_ARGS-}-i " ;;
--prune) LOCAL_ARGS="${LOCAL_ARGS-}-p " ;;
--remove) LOCAL_ARGS="${LOCAL_ARGS-}-r " ;;
--test) LOCAL_ARGS="${LOCAL_ARGS-}-t " ;;
--update) LOCAL_ARGS="${LOCAL_ARGS-}-u " ;;
--verbose) LOCAL_ARGS="${LOCAL_ARGS-}-v " ;;
#pass through anything else
*)
[[ ${ARG:0:1} == "-" ]] || DELIM='"'
LOCAL_ARGS="${LOCAL_ARGS-}${DELIM}${ARG}${DELIM} "
;;
esac
done
#Reset the positional parameters to the short options
eval set -- "${LOCAL_ARGS-}"
while getopts ":a:c:efghipr:t:u:vx" OPTION; do
case ${OPTION} in
a)
readonly ADD=${OPTARG}
;;
c)
case ${OPTARG} in
down | generate | merge | pull* | restart* | up*)
local MULTIOPT
MULTIOPT=("$OPTARG")
until [[ $(eval "echo \${$OPTIND}" 2> /dev/null) =~ ^-.* ]] || [[ -z $(eval "echo \${$OPTIND}" 2> /dev/null) ]]; do
MULTIOPT+=("$(eval "echo \${$OPTIND}")")
OPTIND=$((OPTIND + 1))
done
COMPOSE=$(printf "%s " "${MULTIOPT[@]}" | xargs)
readonly COMPOSE
;;
*)
echo "Invalid compose option."
exit 1
;;
esac
;;
e)
readonly ENV=true
;;
f)
readonly FORCE=true
;;
h)
usage
exit
;;
i)
readonly INSTALL=true
;;
p)
readonly PRUNE=true
;;
r)
readonly REMOVE=${OPTARG}
;;
t)
readonly TEST=${OPTARG}
;;
u)
readonly UPDATE=${OPTARG}
;;
v)
readonly VERBOSE=1
;;
x)
readonly DEBUG=1
set -x
;;
:)
case ${OPTARG} in
c)
readonly COMPOSE=up
;;
r)
readonly REMOVE=true
;;
u)
readonly UPDATE=true
;;
*)
echo "${OPTARG} requires an option."
exit 1
;;
esac
;;
*)
usage
exit
;;
esac
done
return
}
cmdline "${ARGS[@]-}"
if [[ -n ${DEBUG-} ]] && [[ -n ${VERBOSE-} ]]; then
readonly TRACE=1
fi
# 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
# 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}"
log() {
local TOTERM=${1-}
local MESSAGE=${2-}
echo -e "${MESSAGE-}" | (
if [[ -n ${TOTERM} ]]; then
tee -a "${MKTEMP_LOG}" >&2
else
cat >> "${MKTEMP_LOG}" 2>&1
fi
)
}
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
}
# System Information
ARCH=$(uname -m)
readonly ARCH
export ARCH
# Environment Information
readonly COMPOSE_ENV="${SCRIPTPATH}/compose/.env"
export COMPOSE_ENV
# 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
# 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} "$@" < /dev/null
else
fatal "${SCRIPTPATH}/.scripts/${SCRIPTSNAME}.sh not found."
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}" "$@" < /dev/null || 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 PROMPT
if [[ ${FORCE-} == true ]]; then
PROMPT="FORCE"
fi
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 "DockSTARTer 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 DockSTARTer from ${DS_SYMLINK} location."
bash "${DS_SYMLINK}" -vu
bash "${DS_SYMLINK}" -vi
exec bash "${DS_SYMLINK}" "${ARGS[@]-}"
fi
else
if ! check_repo; then
warn "Attempting to clone DockSTARTer repo to ${DETECTED_HOMEDIR}/.docker location."
git clone https://github.com/GhostWriters/DockSTARTer "${DETECTED_HOMEDIR}/.docker" || fatal "Failed to clone DockSTARTer 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" "-vi"
fi
fi
# Create Symlink
run_script 'symlink_ds'
# Execute CLI Argument Functions
if [[ -n ${ADD-} ]]; then
run_script 'appvars_create' "${ADD}"
run_script 'env_update'
exit
fi
if [[ -n ${COMPOSE-} ]]; then
case ${COMPOSE} in
down)
run_script 'docker_compose' "${COMPOSE}"
;;
generate | merge)
run_script 'yml_merge'
;;
pull* | restart* | up*)
run_script 'yml_merge'
run_script 'docker_compose' "${COMPOSE}"
;;
*)
fatal "Invalid compose option."
;;
esac
exit
fi
if [[ -n ${ENV-} ]]; then
run_script 'appvars_create_all'
run_script 'env_update'
exit
fi
if [[ -n ${ENVMETHOD-} ]]; then
case "${ENVMETHOD-}" in
--env-get)
if [[ ${ENVVAR-} != "" ]]; then
run_script 'env_get' "${ENVVAR^^}"
else
echo "Invalid usage. Must be"
echo " --env-get with variable name ('--env-get=VAR')"
echo " Variable name will be forced to UPPER CASE"
fi
;;
--env-set)
if [[ ${ENVVAR-} != "" ]] && [[ ${ENVVAL-} != "" ]]; then
run_script 'env_set' "${ENVVAR^^}" "${ENVVAL}"
else
echo "Invalid usage. Must be"
echo " --env-set with variable name and value ('--env-set=VAR,VAL')"
echo " Variable name will be forced to UPPER CASE"
fi
;;
--env-get-lower)
if [[ ${ENVVAR-} != "" ]]; then
run_script 'env_get' "${ENVVAR}"
else
echo "Invalid usage. Must be"
echo " --env-get-lower with variable name ('--env-get-lower=Var')"
echo " Variable name can be Mixed Case"
fi
;;
--env-set-lower)
if [[ ${ENVVAR-} != "" ]] && [[ ${ENVVAL-} != "" ]]; then
run_script 'env_set' "${ENVVAR}" "${ENVVAL}"
else
echo "Invalid usage. Must be"
echo " --env-set-lower with variable name and value ('--env-set-lower=Var,VAL')"
echo " Variable name can be Mixed Case"
fi
;;
*)
echo "Invalid option: '${ENVMETHOD-}'"
;;
esac
exit
fi
if [[ -n ${INSTALL-} ]]; then
run_script 'run_install'
exit
fi
if [[ -n ${PRUNE-} ]]; then
run_script 'docker_prune'
exit
fi
if [[ -n ${REMOVE-} ]]; then
if [[ ${REMOVE} == true ]]; then
run_script 'appvars_purge_all'
run_script 'env_update'
else
run_script 'appvars_purge' "${REMOVE}"
run_script 'env_update'
fi
exit
fi
if [[ -n ${TEST-} ]]; then
run_test "${TEST}"
exit
fi
if [[ -n ${UPDATE-} ]]; then
if [[ ${UPDATE} == true ]]; then
run_script 'update_self'
else
run_script 'update_self' "${UPDATE}"
fi
exit
fi
# Run Menus
PROMPT="GUI"
run_script 'menu_main'
}
main