forked from drunkleen/gatekeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install_script.sh
556 lines (485 loc) · 11.8 KB
/
install_script.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
#!/usr/bin/env bash
set -e
INSTALL_DIR="/opt"
if [ -z "$APP_NAME" ]; then
APP_NAME="gatekeeper"
fi
APP_DIR="$INSTALL_DIR/$APP_NAME"
DATA_DIR="/var/lib/$APP_NAME"
COMPOSE_FILE="$APP_DIR/docker-compose.yaml"
colorized_echo() {
local color=$1
local text=$2
case $color in
"red")
printf "\e[91m${text}\e[0m\n"
;;
"green")
printf "\e[92m${text}\e[0m\n"
;;
"yellow")
printf "\e[93m${text}\e[0m\n"
;;
"blue")
printf "\e[94m${text}\e[0m\n"
;;
"magenta")
printf "\e[95m${text}\e[0m\n"
;;
"cyan")
printf "\e[96m${text}\e[0m\n"
;;
*)
echo "${text}"
;;
esac
}
check_running_as_root() {
if [ "$(id -u)" != "0" ]; then
colorized_echo red "This command must be run as root."
exit 1
fi
}
# Detect the operating system
detect_os() {
if [ -f /etc/lsb-release ]; then
OS=$(lsb_release -si)
elif [ -f /etc/os-release ]; then
OS=$(awk -F= '/^NAME/{print $2}' /etc/os-release | tr -d '"')
elif [ -f /etc/redhat-release ]; then
OS=$(cat /etc/redhat-release | awk '{print $1}')
elif [ -f /etc/arch-release ]; then
OS="Arch"
else
colorized_echo red "Unsupported operating system"
exit 1
fi
}
detect_and_update_package_manager() {
colorized_echo blue "Updating package manager"
detect_os
if [[ "$OS" == "Ubuntu"* ]] || [[ "$OS" == "Ubuntu Linux"* ]] || [[ "$OS" == "Debian"* ]]; then
PKG_MANAGER="apt-get"
$PKG_MANAGER update
elif [[ "$OS" == "CentOS"* ]] || [[ "$OS" == "AlmaLinux"* ]]; then
PKG_MANAGER="yum"
$PKG_MANAGER update -y
$PKG_MANAGER install -y epel-release
elif [ "$OS" == "Fedora"* ] || [[ "$OS" == "Fedora Linux"* ]]; then
PKG_MANAGER="dnf"
$PKG_MANAGER update -y
elif [ "$OS" == "Arch" ]; then
PKG_MANAGER="pacman"
$PKG_MANAGER -Sy
else
colorized_echo red "Unsupported operating system"
exit 1
fi
}
detect_compose() {
if docker compose >/dev/null 2>&1; then
COMPOSE='docker compose'
elif docker-compose >/dev/null 2>&1; then
COMPOSE='docker-compose'
else
colorized_echo red "docker compose not found"
exit 1
fi
}
install_package() {
if [ -z $PKG_MANAGER ]; then
detect_and_update_package_manager
fi
PACKAGE=$1
colorized_echo blue "Installing $PACKAGE"
if [[ "$OS" == "Ubuntu"* ]] || [[ "$OS" == "Ubuntu Linux"* ]] || [[ "$OS" == "Debian"* ]]; then
$PKG_MANAGER -y install "$PACKAGE"
elif [[ "$OS" == "CentOS"* ]] || [[ "$OS" == "AlmaLinux"* ]]; then
$PKG_MANAGER install -y "$PACKAGE"
elif [[ "$OS" == "Fedora"* ]]; then
$PKG_MANAGER install -y "$PACKAGE"
elif [ "$OS" == "Arch" ]; then
$PKG_MANAGER -S --noconfirm "$PACKAGE"
else
colorized_echo red "Unsupported operating system"
exit 1
fi
}
install_docker() {
colorized_echo blue "Installing Docker"
curl -fsSL https://get.docker.com | sh
colorized_echo green "Docker installed successfully"
}
install_gatekeeper_script() {
FETCH_REPO="drunkleen/gatekeeper"
SCRIPT_URL="https://github.com/$FETCH_REPO/raw/master/install_script.sh"
colorized_echo blue "Installing Gate-Keeper script"
curl -sSL $SCRIPT_URL | install -m 755 /dev/stdin /usr/local/bin/gatekeeper
colorized_echo green "Gate-Keeper script installed successfully"
}
install_gatekeeper() {
FILES_URL_PREFIX="https://raw.githubusercontent.com/drunkleen/gatekeeper/master"
mkdir -p "$DATA_DIR"
mkdir -p "$APP_DIR"
colorized_echo blue "Fetching compose file"
curl -sL "$FILES_URL_PREFIX/docker-compose.yaml" -o "$APP_DIR/docker-compose.yaml"
colorized_echo green "File saved in $APP_DIR/docker-compose.yaml"
colorized_echo blue "Fetching .env file"
curl -sL "$FILES_URL_PREFIX/.env.example" -o "$APP_DIR/.env"
colorized_echo green "File saved in $APP_DIR/.env"
colorized_echo green "GateKeeper files downloaded successfully"
}
uninstall_gatekeeper_script() {
if [ -f "/usr/local/bin/gatekeeper" ]; then
colorized_echo yellow "Removing GateKeeper script"
rm "/usr/local/bin/gatekeeper"
fi
}
uninstall_gatekeeper() {
if [ -d "$APP_DIR" ]; then
colorized_echo yellow "Removing directory: $APP_DIR"
rm -r "$APP_DIR"
fi
}
uninstall_gatekeeper_docker_images() {
images=$(docker images | grep gatekeeper | awk '{print $3}')
if [ -n "$images" ]; then
colorized_echo yellow "Removing Docker images of GateKeeper"
for image in $images; do
if docker rmi "$image" >/dev/null 2>&1; then
colorized_echo yellow "Image $image removed"
fi
done
fi
}
uninstall_gatekeeper_data_files() {
if [ -d "$DATA_DIR" ]; then
colorized_echo yellow "Removing directory: $DATA_DIR"
rm -r "$DATA_DIR"
fi
}
up_gatekeeper() {
$COMPOSE -f $COMPOSE_FILE -p "$APP_NAME" up -d --remove-orphans
}
down_gatekeeper() {
$COMPOSE -f $COMPOSE_FILE -p "$APP_NAME" down
}
show_gatekeeper_logs() {
$COMPOSE -f $COMPOSE_FILE -p "$APP_NAME" logs
}
follow_gatekeeper_logs() {
$COMPOSE -f $COMPOSE_FILE -p "$APP_NAME" logs -f
}
update_gatekeeper_script() {
FETCH_REPO="drunkleen/gatekeeper"
SCRIPT_URL="https://github.com/$FETCH_REPO/raw/master/install_script.sh"
colorized_echo blue "Updating Gate-Keeper script"
curl -sSL $SCRIPT_URL | install -m 755 /dev/stdin /usr/local/bin/gatekeeper
colorized_echo green "Gate-Keeper script updated successfully"
}
update_gatekeeper() {
$COMPOSE -f $COMPOSE_FILE -p "$APP_NAME" pull
}
is_gatekeeper_installed() {
if [ -d $APP_DIR ]; then
return 0
else
return 1
fi
}
is_gatekeeper_up() {
if [ -z "$($COMPOSE -f $COMPOSE_FILE ps -q -a)" ]; then
return 1
else
return 0
fi
}
install_command() {
check_running_as_root
if is_gatekeeper_installed; then
colorized_echo red "GateKeeper is already installed at $APP_DIR"
read -p "Do you want to override the previous installation? (y/n) "
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
colorized_echo red "Aborted installation"
exit 1
fi
fi
detect_os
if ! command -v jq >/dev/null 2>&1; then
install_package jq
fi
if ! command -v curl >/dev/null 2>&1; then
install_package curl
fi
if ! command -v docker >/dev/null 2>&1; then
install_docker
fi
detect_compose
install_gatekeeper_script
install_gatekeeper
up_gatekeeper
follow_gatekeeper_logs
}
createadmin() {
docker exec -it $APP_NAME sh -c "cd /app/ && python ./cli.py createadmin"
}
uninstall_command() {
check_running_as_root
if ! is_gatekeeper_installed; then
colorized_echo red "GateKeeper not installed!"
exit 1
fi
read -p "Do you really want to uninstall GateKeeper? (y/n) "
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
colorized_echo red "Aborted"
exit 1
fi
detect_compose
if is_gatekeeper_up; then
down_gatekeeper
fi
uninstall_gatekeeper_script
uninstall_gatekeeper
uninstall_gatekeeper_docker_images
read -p "Do you want to remove GateKeeper data files too ($DATA_DIR)? (y/n) "
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
colorized_echo green "GateKeeper uninstalled successfully"
else
uninstall_gatekeeper_data_files
colorized_echo green "GateKeeper uninstalled successfully"
fi
}
up_command() {
help() {
colorized_echo red "Usage: GateKeeper up [options]"
echo ""
echo "OPTIONS:"
echo " -h, --help display this help message"
echo " -n, --no-logs do not follow logs after starting"
}
local no_logs=false
while [[ "$#" -gt 0 ]]; do
case "$1" in
-n | --no-logs)
no_logs=true
;;
-h | --help)
help
exit 0
;;
*)
echo "Error: Invalid option: $1" >&2
help
exit 0
;;
esac
shift
done
if ! is_gatekeeper_installed; then
colorized_echo red "GateKeeper not installed!"
exit 1
fi
detect_compose
if is_gatekeeper_up; then
colorized_echo red "GateKeeper already up"
exit 1
fi
up_gatekeeper
if [ "$no_logs" = false ]; then
follow_gatekeeper_logs
fi
}
down_command() {
if ! is_gatekeeper_installed; then
colorized_echo red "GateKeeper not installed!"
exit 1
fi
detect_compose
if ! is_gatekeeper_up; then
colorized_echo red "GateKeeper already down"
exit 1
fi
down_gatekeeper
}
restart_command() {
help() {
colorized_echo red "Usage: GateKeeper restart [options]"
echo
echo "OPTIONS:"
echo " -h, --help display this help message"
echo " -n, --no-logs do not follow logs after starting"
}
local no_logs=false
while [[ "$#" -gt 0 ]]; do
case "$1" in
-n | --no-logs)
no_logs=true
;;
-h | --help)
help
exit 0
;;
*)
echo "Error: Invalid option: $1" >&2
help
exit 0
;;
esac
shift
done
if ! is_gatekeeper_installed; then
colorized_echo red "GateKeeper not installed!"
exit 1
fi
detect_compose
down_gatekeeper
up_gatekeeper
if [ "$no_logs" = false ]; then
follow_gatekeeper_logs
fi
}
status_command() {
if ! is_gatekeeper_installed; then
echo -n "Status: "
colorized_echo red "Not Installed"
exit 1
fi
detect_compose
if ! is_gatekeeper_up; then
echo -n "Status: "
colorized_echo blue "Down"
exit 1
fi
echo -n "Status: "
colorized_echo green "Up"
json=$($COMPOSE -f $COMPOSE_FILE ps -a --format=json)
services=$(echo "$json" | jq -r 'if type == "array" then .[] else . end | .Service')
states=$(echo "$json" | jq -r 'if type == "array" then .[] else . end | .State')
for i in $(seq 0 $(expr $(echo $services | wc -w) - 1)); do
service=$(echo $services | cut -d' ' -f $(expr $i + 1))
state=$(echo $states | cut -d' ' -f $(expr $i + 1))
echo -n "- $service: "
if [ "$state" == "running" ]; then
colorized_echo green $state
else
colorized_echo red $state
fi
done
}
logs_command() {
help() {
colorized_echo red "Usage: GateKeeper logs [options]"
echo ""
echo "OPTIONS:"
echo " -h, --help display this help message"
echo " -n, --no-follow do not show follow logs"
}
local no_follow=false
while [[ "$#" -gt 0 ]]; do
case "$1" in
-n | --no-follow)
no_follow=true
;;
-h | --help)
help
exit 0
;;
*)
echo "Error: Invalid option: $1" >&2
help
exit 0
;;
esac
shift
done
if ! is_gatekeeper_installed; then
colorized_echo red "GateKeeper not installed!"
exit 1
fi
detect_compose
if ! is_gatekeeper_up; then
colorized_echo red "GateKeeper is not up."
exit 1
fi
if [ "$no_follow" = true ]; then
show_gatekeeper_logs
else
follow_gatekeeper_logs
fi
}
update_command() {
check_running_as_root
if ! is_gatekeeper_installed; then
colorized_echo red "GateKeeper not installed!"
exit 1
fi
detect_compose
update_gatekeeper_script
colorized_echo blue "Pulling latest version"
update_gatekeeper
colorized_echo blue "Restarting GateKeeper services"
down_gatekeeper
up_gatekeeper
colorized_echo blue "GateKeeper updated successfully"
}
usage() {
colorized_echo red "Usage: GateKeeper [command]"
echo
echo "Commands:"
echo " up Start services"
echo " down Stop services"
echo " restart Restart services"
echo " status Show status"
echo " logs Show logs"
echo " createadmin Creates an admin account"
echo " install Install GateKeeper"
echo " update Update latest version"
echo " uninstall Uninstall GateKeeper"
echo " install-script Install GateKeeper script"
echo
}
case "$1" in
up)
shift
up_command "$@"
;;
down)
shift
down_command "$@"
;;
restart)
shift
restart_command "$@"
;;
status)
shift
status_command "$@"
;;
logs)
shift
logs_command "$@"
;;
createadmin)
shift
createadmin "$@"
;;
install)
shift
install_command "$@"
;;
update)
shift
update_command "$@"
;;
uninstall)
shift
uninstall_command "$@"
;;
install-script)
shift
install_gatekeeper_script "$@"
;;
*)
usage
;;
esac