-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
480 lines (422 loc) · 13 KB
/
install.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
#!/usr/bin/env bash
# shellcheck disable=SC1090,SC2031,SC2034,SC2154,SC2155,SC2181,SC2260
# ==================================================================
# install.sh
# ==================================================================
# Swarm Cookbook Installer
#
# File: install.sh
# Author: Ragdata
# Date: 17/09/2023
# License: MIT License
# Copyright: Copyright © 2023 Darren (Ragdata) Poulton
# ==================================================================
# PREFLIGHT
# ==================================================================
# if script is called with 'debug' as an argument, then set debug mode
if [[ "${1,,}" == "debug" ]] || [[ "$DEBUG" == 1 ]]; then shift; export DEBUG=1; set -- "${@}"; set -axeET; else export DEBUG=0; set -aeET; fi
# if script is called with 'verbose' as an argument, then unset verbose mode
if [[ "${1,,}" == "verbose" ]] || [[ "$LOG_VERBOSE" == 0 ]]; then shift; export LOG_VERBOSE=0; else export LOG_VERBOSE=1; fi
# ==================================================================
# VARIABLES
# ==================================================================
# define REPO
if [[ -z "$REPO" ]]; then export REPO="$(dirname "$(realpath "${BASH_SOURCE[0]}")")"; fi
# define SOURCE_DIRS
export SOURCE_DIRS=("$REPO/src/apps" "$REPO/install")
# define USERNAME
export USERNAME="$(whoami)"
# ==================================================================
# HELPER FUNCTIONS
# ==================================================================
if [[ "${SHELL##*/}" == 'bash' ]]; then
# ------------------------------------------------------------------
# checkFunc
# ------------------------------------------------------------------
checkFunc() { if typeset -f "$1" > /dev/null; then return 0; else return 1; fi; }
# ------------------------------------------------------------------
# checkPkg
# ------------------------------------------------------------------
checkPkg()
{
local pkg="${1:-}"
local name="${2:-}"
if [[ -z "$pkg" ]] || [[ -z "$name" ]]; then echo "Missing Argument(s)!"; exit 1; fi
if loadSource "$pkg" -d; then
log::file "Found '$name'"
else
loadSource "$pkg" -i
if [ "$?" -ne 0 ]; then log::file "Failed installing '$name' - exiting ..."; exit 1; fi
fi
}
# ------------------------------------------------------------------
# configGET
# ------------------------------------------------------------------
configGET()
{
local value
local key="${1:-}"
local field="${2:-}"
value="$(redis-cli HGET "$key" "$field")"
printf '%s' "$value"
}
# ------------------------------------------------------------------
# configSET
# ------------------------------------------------------------------
configSET()
{
local key="${1:-}"
local field="${2:-}"
local value="${3:-}"
if grep -q "$field" "$SWARMDIR"/.node; then
sed -i "/^${field}.*/c\\${field}=${value}" "$SWARMDIR"/.node
else
echo "${field}=${value}" >> "$SWARMDIR"/.node
fi
redis-cli HSET "$key" "$field" "$value" > /dev/null
}
# ------------------------------------------------------------------
# getPassword
# ------------------------------------------------------------------
getPassword()
{
local len="${1:-16}"
local NUM_REGEX CAP_REGEX SML_REGEX SYM_REGEX
local passwd=""
NUM_REGEX='^.*[0-9]+.*$'
CAP_REGEX='^.*[A-Z]+.*$'
SML_REGEX='^.*[a-z]+.*$'
SYM_REGEX='^[A-Za-z0-9]+[@#%_+=][A-Za-z0-9]+$'
while [[ ! $passwd =~ $NUM_REGEX ]] && [[ ! $passwd =~ $CAP_REGEX ]] && [[ ! $passwd =~ $SML_REGEX ]] && [[ ! $passwd =~ $SYM_REGEX ]]
do
passwd=$(tr </dev/urandom -dc 'A-Za-z0-9@#%_+=' | head -c "$len")
done
echo "$passwd"
}
# ------------------------------------------------------------------
# getRepo
# ------------------------------------------------------------------
getRepo()
{
if [[ -n "$2" ]]; then git clone [email protected]:"${1:-}" "${2:-}";
else git clone [email protected]:"${1:-}"; fi
}
# ------------------------------------------------------------------
# loadLib
# ------------------------------------------------------------------
loadLib()
{
local file="${1:-}"
if [[ "${1:0:1}" == "/" ]] && [[ -f "$file" ]]; then
source "$file"
elif [[ -f /usr/local/lib/"$file" ]]; then
source /usr/local/lib/"$file"
elif [[ -f "$REPO"/src/lib/"$file" ]]; then
source "$REPO"/src/lib/"$file"
else
echo "Library File '$file' Not Found!"
exit 1
fi
}
# ------------------------------------------------------------------
# loadSource
# ------------------------------------------------------------------
loadSource()
{
local app="${1:-}"
local options dir fileName fullPath filePath pathName
local -A FILEOPTS
if [[ -z "$app" ]]; then echo "Missing Argument!"; exit 1; fi
shift
fileName="${app##*/}"
fileName="${fileName%%.*}"
FILEOPTS[help]=0
FILEOPTS[requires]=0
FILEOPTS[installed]=0
FILEOPTS[install]=0
FILEOPTS[config]=0
FILEOPTS[remove]=0
FILEOPTS[test]=0
options=$(getopt -o "hRcdirt" -a -- "$@")
eval set -- "$options"
while true
do
case "$1" in
-h) FILEOPTS[help]=1;;
-R) FILEOPTS[requires]=1;;
-c) FILEOPTS[config]=1;;
-d) FILEOPTS[installed]=1;;
-i) FILEOPTS[install]=1;;
-r) FILEOPTS[remove]=1;;
-t) FILEOPTS[test]=1;;
--)
shift
break
;;
*)
echo "loadSource :: Invalid Option '$1'"
exit 1
;;
esac
shift
done
if [[ -f "$app" ]]; then
fullPath="$app"
else
if [[ $DEBUG -eq 1 ]]; then log::file "Finding '$app'"; fi
if [[ ! "$app" = *.* ]]; then thisFile="$app".sh; else thisFile="$app"; fi
for dir in "${SOURCE_DIRS[@]}"
do
if [[ $DEBUG -eq 1 ]]; then log::file "Searching '$dir'"; fi
if [[ -f "$dir/$thisFile" ]]; then fullPath="$dir/$thisFile"; break; fi
done
fi
if [[ -z "$fullPath" ]] || [[ ! -f "$fullPath" ]]; then log::file "File '$app' Not Found!"; exit 1; fi
source "$fullPath"
# HELP
if [[ "${FILEOPTS[help]}" -eq 1 ]]; then
log::file "Displaying '$fileName' help"
eval "$fileName::help $*"
return $?
fi
# REQUIRES
if [[ "${FILEOPTS[requires]}" -eq 1 ]]; then
log::file "Checking '$fileName' requirements"
eval "$fileName::requires $*"
return $?
fi
# INSTALLED
if [[ "${FILEOPTS[installed]}" -eq 1 ]]; then
log::file "Checking if '$fileName' installed"
eval "$fileName::installed $*"
return $?
fi
# INSTALL & REMOVE
if [[ "${FILEOPTS[install]}" -eq 1 ]]; then
log::file "Installing '$fileName'"
eval "$fileName::install $*"
return $?
elif [[ "${FILEOPTS[remove]}" -eq 1 ]]; then
log::file "Uninstalling '$fileName'"
eval "$fileName::remove $*"
return $?
fi
# CONFIGURE
if [[ "${FILEOPTS[config]}" -eq 1 ]]; then
log::file "Configuring '$fileName'"
eval "$fileName::config $*"
return $?
fi
# TEST
if [[ "${FILEOPTS[test]}" -eq 1 ]]; then
log::file "Testing '$fileName'"
eval "$fileName::test $*"
return $?
fi
}
# ------------------------------------------------------------------
# log::exit
# ------------------------------------------------------------------
log::exit()
{
local redis=0
if [[ "$1" == "-r" ]]; then redis=1; shift; fi
if [[ $redis -eq 0 ]]; then
local msg="${1:-}"
local code="${2:1}"
if [[ -f "$logFile" ]]; then log::file "$msg"; fi
elif [[ $redis -eq 1 ]]; then
local key="${1:-}"
local val="${2:-}"
local code="${3:1}"
if [[ -n "$logFile" ]]; then log::redis "$key" "$val"; fi
fi
# ALWAYS ECHO ERROR MESSAGE ONLY ONCE
if [[ "$LOG_VERBOSE" -ne 1 ]]; then echo "$msg"; fi
exit "$code"
}
# ------------------------------------------------------------------
# log::init
# ------------------------------------------------------------------
log::init()
{
local file="${1:-}"
if [[ ! -d "$LOGDIR" ]]; then sudo mkdir -p "$LOGDIR"; fi
if [[ -z "$file" ]]; then file=ZSH-"$(logTime)"; fi
export logFile="$LOGDIR/$file"
sudo touch "$logFile"
log::file "LogFile Initialized"
}
# ------------------------------------------------------------------
# log::file
# ------------------------------------------------------------------
log::file()
{
local silent=0
if [[ "$1" == "-s" ]]; then silent=1; shift; fi
local msg="${1:-}" timestamp
timestamp="$(logStamp)"
[[ ! -f "$logFile" ]] && { echo "LogFile '$logFile' Not Found!"; exit 1; }
if [[ "$LOG_VERBOSE" -eq 1 ]] && [[ "$silent" -eq 0 ]]; then echo "$msg"; fi
echo "$timestamp :: $USERNAME - $msg" | sudo tee -a "$logFile" > /dev/null
}
# ------------------------------------------------------------------
# log::redis
# ------------------------------------------------------------------
log::redis()
{
local silent=0
if [[ "$1" == "-s" ]]; then silent=1; shift; fi
local key="${1:-}" timestamp
local val="${2:-}"
timestamp="$(logStamp)"
[[ -z "$REDISCLI_AUTH" ]] && { echo "Redis Authentication Missing!"; exit 1; }
if [[ "$LOG_VERBOSE" -eq 1 ]] && [[ "$silent" -eq 0 ]]; then echo "$msg"; fi
redis-cli "$logFile" "$key" "$val" > /dev/null
}
# ------------------------------------------------------------------
# logStamp
# ------------------------------------------------------------------
logStamp()
{
local dtg="$(date -u +'%Y-%m-%dT%H:%M:%S.%3N%z')";
printf '%s' "$dtg"
}
# ------------------------------------------------------------------
# logTime
# ------------------------------------------------------------------
logTime()
{
local dtg="$(date -u +'%y%m%dT%H%M%S.%3N')"
printf '%s' "$dtg"
}
# ------------------------------------------------------------------
# redis::passGET
# ------------------------------------------------------------------
redis::passGET() { sudo sed -n -e '/^requirepass.*/p' /etc/redis/redis.conf | awk '{print $2}'; }
else
# ------------------------------------------------------------------
# loadLib
# ------------------------------------------------------------------
loadLib()
{
local file="${1:-}"
if [[ "${1:0:1}" == "/" ]] && [[ -f "$file" ]]; then
source "$file"
elif [[ -f /usr/local/lib/"$file" ]]; then
source /usr/local/lib/"$file"
elif [[ -f "$REPO"/src/lib/"$file" ]]; then
source "$REPO"/src/lib/"$file"
else
echo "Library File '$file' Not Found!"
exit 1
fi
}
fi
# ==================================================================
# DEPENDENCIES
# ==================================================================
if [[ "${SHELL##*/}" == 'zsh' ]]; then
if ! grep -q 'function' <<< "$(type historyStats 2> /dev/null)"; then loadLib common.zsh; fi
fi
# create .env, if not exists
if [[ ! -f "$REPO"/.env ]]; then cp "$REPO"/.env.dist "$REPO"/.env; fi
# set file ownership
chown "$USERNAME":"$USERNAME" "$REPO"/.env
# load .env
source "$REPO"/.env;
# initialize log
if [[ "${1,,}" == "logfile" ]]; then export logFile="${2:-}"; shift 2; else log::init INSTALL-"$(logTime)"; fi
# ==================================================================
# INSTALL FUNCTIONS
# ==================================================================
# ------------------------------------------------------------------
# install::init
# ------------------------------------------------------------------
install::init()
{
if [[ -f "$HOME/.local/.node-init" ]]; then return 0; fi
echo
echo "=================================================================="
echo "SYSTEM INITIALIZATION"
echo "=================================================================="
echo
checkPkg "jq" "JQ"
checkPkg "redis" "Redis"
loadSource redis -c
checkPkg "dialog" "Dialog"
if [[ ! -d "$HOME/.local" ]]; then mkdir -p "$HOME/.local"; fi
touch "$HOME/.local/.node-init"
echo
echo "SYSTEM INITIALIZATION - DONE!"
echo "=================================================================="
echo
}
# ------------------------------------------------------------------
# install::report
# ------------------------------------------------------------------
install::report()
{
local resp
[[ -z "$logFile" ]] && { echo "No logFile passed for reporting!"; exit 1; }
echo "The previous operation was logged and details are available to view"
echo "Please select from the following options:"
echo
echo -e "\t (V)iew Logs"
echo -e "\t (R)eboot"
echo -e "\t (Q)uit"
echo
read -rs -n 1 resp
resp="${resp,,}"
case "$resp" in
v)
echo
echo "===================================================================="
echo "CONTENT OF LOGFILE '$logFile'"
echo "===================================================================="
echo
sudo cat "$logFile"
echo
echo "DONE!"
echo
exit 0
;;
r)
reboot
;;
q)
exit 0
;;
esac
}
# ==================================================================
# MAIN
# ==================================================================
clear
#checkShell
#checkRoot
install::init
if [[ "$#" == 0 ]]; then set -- "all"; fi
while [[ "$#" -gt 0 ]]
do
case "$1" in
zsh)
loadSource zsh -i "${@:2}"
;;
configZSH|zshConfig)
loadSource zsh -c
;;
rmZSH|zshRemove)
loadSource zsh -r
;;
zsh-p10k)
loadSource zsh-p10k -i
;;
esac
shift
done
if [[ -n "$ZSH_RESTART" ]]; then
exec zsh
else
install::report
fi