-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.sh
361 lines (338 loc) · 12 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
#!/usr/bin/env bash
set -Eeuo pipefail
IFS=$'\n\t'
# Usage Information
usage() {
cat << EOF
Usage: sudo dsac [OPTION]
NOTE: dsac shortcut is only available after the first run of
sudo bash ~/.dsac/main.sh
This is the main DockSTARTer App Config script.
For regular usage you can run without providing any options.
-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
exit
}
# 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=$?
sudo sh -c "cat ${MKTEMP_LOG:-/dev/null} >> ${SCRIPTPATH}/dockstarterappconfig.log" || true
sudo rm -f "${MKTEMP_LOG}" || true
sudo -E chmod +x "${SCRIPTNAME}" > /dev/null 2>&1 || true
if repo_exists; then
info "Setting executable permission on ${SCRIPTNAME}"
sudo -E chmod +x "${SCRIPTNAME}" > /dev/null 2>&1 || fatal "dsac must be executable."
fi
if [[ ${CI:-} == true ]] && [[ ${TRAVIS_SECURE_ENV_VARS:-} == false ]]; then
warn "TRAVIS_SECURE_ENV_VARS is false for Pull Requests from remote branches. Please retry failed builds!"
fi
if [[ ${EXIT_CODE} -ne 0 ]]; then
error "DockSTARTer App Config did not finish running successfully."
fi
exit ${EXIT_CODE}
trap - ERR EXIT SIGABRT SIGALRM SIGHUP SIGINT SIGQUIT SIGTERM
}
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)
--debug) LOCAL_ARGS="${LOCAL_ARGS:-}-x " ;;
--force) LOCAL_ARGS="${LOCAL_ARGS:-}-f " ;;
--help) LOCAL_ARGS="${LOCAL_ARGS:-}-h " ;;
--install) LOCAL_ARGS="${LOCAL_ARGS:-}-i " ;;
--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 ":dfhit:u:vx" OPTION; do
case ${OPTION} in
d)
readonly DEBUG=1
;;
f)
readonly FORCE=true
;;
h)
usage
exit
;;
i)
readonly INSTALL=true
;;
t)
readonly TEST=${OPTARG}
;;
u)
readonly UPDATE=${OPTARG}
;;
v)
readonly VERBOSE=1
;;
x)
readonly DEBUG=1
set -x
;;
:)
case ${OPTARG} in
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
echo "Skipping TRACE"
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 "Failed to create temporary log file."
readonly MKTEMP_LOG
echo "DockSTARTer App Config 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
}
# User/Group Information
readonly DETECTED_PUID=${SUDO_UID:-$UID}
DETECTED_UNAME=$(id -un "${DETECTED_PUID}" 2> /dev/null || true)
readonly 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
# DS Information
DETECTED_DSDIR=$(eval echo "~${DETECTED_UNAME}/.docker" 2> /dev/null || true)
readonly DETECTED_DSDIR
export DETECTED_DSDIR
# DSAC Information
DETECTED_DSACDIR=$(eval echo "~${DETECTED_UNAME}/.dsac" 2> /dev/null || true)
readonly DETECTED_DSACDIR
# Repo Exists Function
repo_exists() {
if [[ -d ${SCRIPTPATH}/.git ]] && [[ -d ${SCRIPTPATH}/.scripts ]]; then
return
else
return 1
fi
}
# Root Check Function
root_check() {
if [[ ${DETECTED_PUID} == "0" ]] || [[ ${DETECTED_HOMEDIR} == "/root" ]]; then
fatal "Running as root is not supported. Please run as a standard user with sudo."
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} "$@"
else
fatal "${SCRIPTPATH}/.scripts/${SCRIPTSNAME}.sh not found."
fi
}
# Test Runner Function
run_test() {
local TESTSNAME=${1:-}
shift
if [[ -f ${SCRIPTPATH}/.scripts/${TESTSNAME}.sh ]]; then
if grep -q "test_${TESTSNAME}" "${SCRIPTPATH}/.scripts/${TESTSNAME}.sh"; then
notice "Testing ${TESTSNAME}."
# shellcheck source=/dev/null
source "${SCRIPTPATH}/.scripts/${TESTSNAME}.sh"
eval "test_${TESTSNAME}" "$@" || fatal "Failed to run ${TESTSNAME}."
notice "Completed testing ${TESTSNAME}."
else
fatal "Test function in ${SCRIPTPATH}/.scripts/${TESTSNAME}.sh not found."
fi
else
fatal "${SCRIPTPATH}/.scripts/${TESTSNAME}.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 Travis CI
if [[ ${CI:-} == true ]] && [[ ${TRAVIS_SECURE_ENV_VARS:-} == true ]]; then
readonly GH_HEADER="Authorization: token ${GH_TOKEN}"
export GH_HEADER
fi
# Main Function
main() {
# Arch Check
ARCH=$(uname -m)
readonly ARCH
if [[ ${ARCH} != "aarch64" ]] && [[ ${ARCH} != "armv7l" ]] && [[ ${ARCH} != "x86_64" ]]; then
fatal "Unsupported architecture."
fi
# Terminal Check
if [[ -t 1 ]]; then
root_check
fi
# Repo Check
local PROMPT
if [[ ${FORCE:-} == true ]]; then
PROMPT="FORCE"
fi
local DSAC_COMMAND
DSAC_COMMAND=$(command -v dsac || true)
if [[ -L ${DSAC_COMMAND} ]]; then
local DSAC_SYMLINK
DSAC_SYMLINK=$(readlink -f "${DSAC_COMMAND}")
if [[ ${SCRIPTNAME} != "${DSAC_SYMLINK}" ]]; then
if repo_exists; then
if run_script 'question_prompt' "${PROMPT:-CLI}" N "DockSTARTer App Config installation found at ${DSAC_SYMLINK} location. Would you like to run ${SCRIPTNAME} instead?"; then
run_script 'symlink_dsac'
DSAC_COMMAND=$(command -v dsac || true)
DSAC_SYMLINK=$(readlink -f "${DSAC_COMMAND}")
fi
fi
warn "Attempting to run DockSTARTer App Config from ${DSAC_SYMLINK} location."
sudo -E bash "${DSAC_SYMLINK}" -vu
sudo -E bash "${DSAC_SYMLINK}" -vi
exec sudo -E bash "${DSAC_SYMLINK}" "${ARGS[@]:-}"
fi
else
if ! repo_exists; then
warn "Attempting to clone DockSTARTer App Config repo to ${DETECTED_DSACDIR} location."
# Anti Sudo Check
if [[ ${EUID} -eq 0 ]]; then
fatal "Using sudo during cloning on first run is not supported."
fi
git clone https://github.com/GhostWriters/DSAC "${DETECTED_DSACDIR}" || fatal "Failed to clone DockSTARTer App Config repo to ${DETECTED_DSACDIR} location."
notice "Performing first run install."
exec sudo -E bash "${DETECTED_DSACDIR}/main.sh" "-vi"
fi
fi
# Sudo Check
if [[ ${EUID} -ne 0 ]]; then
exec sudo -E bash "${SCRIPTNAME}" "${ARGS[@]:-}"
fi
# Create Symlink
run_script 'symlink_dsac'
# Execute CLI Argument Functions
if [[ -n ${INSTALL:-} ]]; then
run_script 'run_install'
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'
notice "DockStarter App Config has finished."
}
main