-
Notifications
You must be signed in to change notification settings - Fork 1
/
nadeko-main-installer
executable file
·459 lines (404 loc) · 14.7 KB
/
nadeko-main-installer
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
#!/bin/bash
#
# The main installer for Linux. This script is responsible for displaying the menu
# options and executing the necessary scripts to install, run, and manage NadekoBot.
#
# Comment Key:
# - A.1.: Return to prevent further code execution.
# - B.1.: Prevent the code from running if the option is disabled.
#
########################################################################################
####[ Variables ]#######################################################################
export E_SERVICE_NAME="nadeko.service"
export E_SERVICE="/etc/systemd/system/$E_SERVICE_NAME"
export E_CURRENT_CREDS="nadekobot/output/creds.yml"
export E_YT_DLP_PATH="$HOME/.local/bin/yt-dlp"
readonly C_REQ_DOTNET_VERSION=8
####[ Functions ]#######################################################################
####
# Depending on the return value or exit code from any of the executed scripts, perform
# the corresponding actions.
#
# Custom Exit Codes:
# - 3: Related to NadekoBot daemon service.
# - 4: Unsupported OS/Distro.
# - 5: A problem occurred finalizing an installation or the backup of files.
# - 50: Arbitrary exit code telling the main installer to continue running.
#
# PARAMETERS:
# - $1: exit_code (Required)
exit_code_actions() {
local exit_code="$1"
## We don't specify any output for SIGINT because it's handled by 'installer-prep'.
## As a note, SIGHUP and SIGTERM don't propagate to parent processes, so we also
## specify the output for those signals in 'installer-prep'.
case "$exit_code" in
3|4|5|50) return 0 ;;
129) echo -e "\n${E_WARNING}Hangup signal detected (SIGHUP)" ;;
130) ;;
143) echo -e "\n${E_WARNING}Termination signal detected (SIGTERM)" ;;
esac
exit "$exit_code"
}
####
# Check if the 'token' in 'creds.yml' is set.
#
# RETURNS:
# - 0: If the 'token' is not set.
# - 1: If the 'token' is set.
is_token_set() {
if [[ ! -f $E_CURRENT_CREDS ]]; then
return 0
elif grep -Eq '^token: '\'\''' "$E_CURRENT_CREDS"; then
return 1
else
return 0
fi
}
####
# Provide the reason(s) for why one or more options are disabled.
#
# PARAMETERS:
# - $1: option_number (Required)
disabled_reasons() {
local option_number="$1"
echo "${E_NOTE}Reason option '$option_number' is disabled:"
case "$option_number" in
1)
echo "${E_NOTE} One or more prerequisites are not installed"
echo "${E_NOTE} Use option 6 to install them all"
echo ""
;;
2|3)
if [[ ! -d nadekobot ]]; then
echo "${E_NOTE} NadekoBot could not be found"
echo "${E_NOTE} Use option 1 to download NadekoBot"
echo ""
elif [[ ! -f nadekobot/output/creds.yml ]]; then
echo "${E_NOTE} The 'creds.yml' could not be found"
echo "${E_NOTE} Refer to the following guide for help:" \
"https://nadekobot.readthedocs.io/en/latest/creds-guide/"
echo ""
elif ! is_token_set; then
echo "${E_NOTE} The 'token' in 'creds.yml' is not set"
echo "${E_NOTE} Refer to the following guide for help:" \
"https://nadekobot.readthedocs.io/en/latest/creds-guide/"
echo ""
else
echo "${E_NOTE} Unknown reason"
echo ""
fi
;;
4|5)
echo "${E_NOTE} NadekoBot is not currently running"
echo "${E_NOTE} Use option 2 or 3 to start NadekoBot"
echo ""
;;
7)
echo "${E_NOTE} NadekoBot could not be found"
echo "${E_NOTE} Use option 1 to download NadekoBot"
echo ""
;;
esac
}
###
### [ Functions To Be Exported ]
###
####
# Retrieve that status of NadekoBot's service.
#
# NEW GLOBALS:
# - E_SERVICE_STATUS: The status of NadekoBot's service.
E_GET_SERVICE_STATUS() {
E_SERVICE_STATUS=$(systemctl is-active "$E_SERVICE_NAME")
}
####
# Stop NadekoBot's service.
#
# PARAMETERS:
# - $1: output_text (Optional, Default: false)
# - If the function should output text indicating if the service has been stopped
# or is currently not running.
# - Acceptable values:
# - true
# - false
E_STOP_SERVICE() {
local output_text="${1:-false}"
if [[ $E_SERVICE_STATUS == "active" ]]; then
echo "${E_INFO}Stopping '$E_SERVICE_NAME'..."
sudo systemctl stop "$E_SERVICE_NAME" \
|| E_STDERR "Failed to stop '$E_SERVICE_NAME'" "" \
"${E_NOTE}You will need to restart '$E_SERVICE_NAME' to apply any updates to NadekoBot"
[[ $output_text == true ]] \
&& echo -e "\n${E_SUCCESS}NadekoBot has been stopped"
else
[[ $output_text == true ]] \
&& echo -e "\n${E_NOTE}NadekoBot is not currently running"
fi
}
####
# Display the logs from 'nadeko.service' as they are created.
E_FOLLOW_SERVICE_LOGS() {
(
trap 'echo -e "\n"; exit 130' SIGINT
sudo journalctl --no-hostname -f -u "$E_SERVICE_NAME" | ccze -A
)
}
####
# Output additional information to go along with the output of 'E_FOLLOW_SERVICE_LOGS'.
#
# PARAMETERS:
# - $1: log_type (Required)
# - Indicates if the function was called from one of the runner scripts or from
# within the main installer.
# - Acceptable values:
# - runner: Called from one of the runner scripts.
# - opt_five: Called from the main installer.
E_WATCH_SERVICE_LOGS() {
local log_type="$1"
if [[ $log_type == "runner" ]]; then
echo "${E_INFO}Displaying '$E_SERVICE_NAME' startup logs, live..."
elif [[ $log_type == "opt_five" ]]; then
echo "${E_INFO}Watching '$E_SERVICE_NAME' logs, live..."
else
E_STDERR "INTERNAL ERROR: Invalid argument for 'E_WATCH_SERVICE_LOGS': $1" \
"4"
fi
echo "${E_NOTE}To stop displaying the startup logs:"
echo "${E_NOTE} 1) Press 'Ctrl' + 'C'"
echo ""
E_FOLLOW_SERVICE_LOGS
[[ $1 == "runner" ]] \
&& echo "${E_NOTE}Please check the logs above to make sure that there" \
"aren't any errors. If there are, resolve whatever issue is causing them."
read -rp "${E_NOTE}Press [Enter] to return to the installer menu"
}
####[ Trapping Logic ]##################################################################
trap 'exit_code_actions "129"' SIGHUP
trap 'exit_code_actions "143"' SIGTERM
####[ Main ]############################################################################
cd "$E_ROOT_DIR" || E_STDERR "Failed to change working directory to '$E_ROOT_DIR'" "1"
printf "%sWelcome to the NadekoBot installer menu\n\n" "$E_CLRLN"
while true; do
###
### [ Temporary Variables ]
###
### The variables below constantly get modified within the while loop, and need to
### be reset everytime the loop starts back at the top.
###
## Disabled option text.
dis_option=" (Execute option to display the reason it's disabled)"
dis_opt_v2=" (Disabled until NadekoBot is running)"
## Option 1.
opt_one_dis=false
opt_one_text="1. Download NadekoBot"
## Option 2 & 3.
opt_two_and_three_dis=false
opt_two_text="2. Run NadekoBot in the background"
opt_three_text="3. Run NadekoBot in the background with auto restart"
## Option 4.
opt_four_dis=false
opt_four_text="4. Stop NadekoBot"
## Option 5.
opt_five_dis=false
opt_five_text="5. Display '$E_SERVICE_NAME' logs in follow mode"
## Option 7.
opt_seven_dis=false
opt_seven_text="7. Back up important files"
###
### [ Variable Checks ]
###
### The following variables re-check the status, existence, etc., of some service
### or program, that has the possibility of changing every time the while loop runs.
###
if hash dotnet &>/dev/null; then
dotnet_version=$(dotnet --version) # Version: x.x.x
dotnet_version=${dotnet_version//.*/} # Version: x
fi
if hash ccze &>/dev/null; then
ccze_installed=true
else
ccze_installed=false
fi
if [[ -f "$E_YT_DLP_PATH" ]]; then
yt_dlp_installed=true
else
yt_dlp_installed=false
fi
E_GET_SERVICE_STATUS
###
### [ Main Continued ]
###
## Disable option 1 if any of the following tools are not installed.
if (! hash dotnet \
|| ! hash redis-server \
|| ! hash python3 \
|| ! "$ccze_installed" \
|| ! "$yt_dlp_installed" \
|| [[ ${dotnet_version:-false} != "$C_REQ_DOTNET_VERSION" ]]) &>/dev/null
then
opt_one_dis=true
opt_one_text="${E_GREY}${opt_one_text}${dis_option}${E_NC}"
fi
## Disable options 2, 3, 4, and 5 if any of the tools in the previous if statement
## are not installed, or none of the specified directories/files could be found.
if [[ $opt_one_dis == true || ! -f $E_CURRENT_CREDS ]] || ! is_token_set; then
opt_two_and_three_dis=true
opt_two_text="${E_GREY}${opt_two_text}${dis_option}${E_NC}"
opt_three_text="${E_GREY}${opt_three_text}${dis_option}${E_NC}"
opt_four_dis=true
opt_four_text="${E_GREY}${opt_four_text}${dis_opt_v2}${E_NC}"
opt_five_dis=true
opt_five_text="${E_GREY}${opt_five_text}${dis_opt_v2}${E_NC}"
if [[ ! -d nadekobot ]]; then
opt_seven_dis=true
opt_seven_text="${E_GREY}${opt_seven_text}${dis_option}${E_NC}"
fi
## Options 2 and 3 remain enabled, if 'NadekoRun' exists.
elif [[ -f NadekoRun ]]; then
## Option 4 and 5 remain enabled, if NadekoBot's service is running.
if [[ $E_SERVICE_STATUS == "active" ]]; then
run_mode_status=" ${E_GREEN}(Running in this mode)${E_NC}"
## Disable option 4 and 5 if NadekoBot's service NOT running.
elif [[ $E_SERVICE_STATUS == "inactive" ]]; then
opt_four_dis=true
opt_four_text="${E_GREY}${opt_four_text}${dis_opt_v2}${E_NC}"
opt_five_dis=true
opt_five_text="${E_GREY}${opt_five_text}${dis_opt_v2}${E_NC}"
run_mode_status=" ${E_YELLOW}(Set up to run in this mode)${E_NC}"
## Disable options 4 and 5.
else
opt_four_dis=true
opt_four_text="${E_GREY}${opt_four_text}${dis_opt_v2}${E_NC}"
opt_five_dis=true
opt_five_text="${E_GREY}${opt_five_text}${dis_opt_v2}${E_NC}"
run_mode_status=" ${E_YELLOW}(Status unknown)${E_NC}"
fi
## If NadekoBot is running in the background with auto restart...
if grep -q '_code_name_="NadekoRunAR"' NadekoRun; then
opt_three_text="${opt_three_text}${run_mode_status}"
## If NadekoBot is running in the background...
elif grep -q '_code_name_="NadekoRun"' NadekoRun; then
opt_two_text="${opt_two_text}${run_mode_status}"
fi
## Options 2 and 3 remained enabled, but options 4 and 5 becomes disabled.
else
opt_four_dis=true
opt_four_text="${E_GREY}${opt_four_text}${dis_opt_v2}${E_NC}"
opt_five_dis=true
opt_five_text="${E_GREY}${opt_five_text}${dis_opt_v2}${E_NC}"
fi
echo "$opt_one_text"
echo "$opt_two_text"
echo "$opt_three_text"
echo "$opt_four_text"
echo "$opt_five_text"
echo "6. Install prerequisites"
echo "$opt_seven_text"
echo "8. Exit"
read -r choice
case "$choice" in
1)
## B.1.
if "$opt_one_dis"; then
clear -x
echo "${E_ERROR}Option 1 is currently disabled" >&2
disabled_reasons 1
continue
fi
export -f E_STOP_SERVICE
export E_SERVICE_STATUS
E_DOWNLOAD_SCRIPT "nadeko-latest-installer" "true"
clear -x
./nadeko-latest-installer || exit_code_actions "$?"
clear -x
;;
2|3)
## B.1.
if "$opt_two_and_three_dis"; then
clear -x
echo "${E_ERROR}Option $choice is currently disabled" >&2
disabled_reasons "$choice"
continue
fi
export E_SERVICE_STATUS
export -f E_WATCH_SERVICE_LOGS
export -f E_FOLLOW_SERVICE_LOGS
E_DOWNLOAD_SCRIPT "nadeko-runner"
clear -x
if [[ $choice == 2 ]]; then
export E_CODENAME="NadekoRun"
printf "%sWe will now run NadekoBot in the background. " "$E_NOTE"
else
export E_CODENAME="NadekoRunAR"
echo -n "${E_NOTE}We will now run NadekoBot in the background" \
"with auto restart. "
fi
(
trap 'exit 1' SIGINT
read -rp "Press [Enter] to begin."
) || {
echo ""
echo -e "\n${E_WARNING}User interrupt detected (SIGINT)"
read -rp "${E_NOTE}Press [Enter] to return to the main menu"
clear -x
continue
}
./nadeko-runner || exit_code_actions "$?"
clear -x
;;
4)
## B.1.
if "$opt_four_dis"; then
clear -x
echo "${E_ERROR}Option 4 is currently disabled" >&2
disabled_reasons 4
continue
fi
clear -x
read -rp "${E_NOTE}We will now stop NadekoBot. Press [Enter] to begin."
E_STOP_SERVICE "true"
read -rp "${E_NOTE}Press [Enter] to return to the installer menu"
clear -x
;;
5)
## B.1.
if "$opt_five_dis"; then
clear -x
echo "${E_ERROR}Option 5 is currently disabled" >&2
disabled_reasons 5
continue
fi
clear -x
E_WATCH_SERVICE_LOGS "opt_five"
clear -x
;;
6)
E_DOWNLOAD_SCRIPT "prereqs-installer"
clear -x
./prereqs-installer || exit_code_actions "$?"
clear -x
;;
7)
## B.1.
if "$opt_seven_dis"; then
clear -x
echo "${E_ERROR}Option 7 is currently disabled" >&2
disabled_reasons 7
continue
fi
E_DOWNLOAD_SCRIPT "file-backup"
clear -x
./file-backup || exit_code_actions "$?"
clear -x
;;
8)
exit_code_actions 0
;;
*)
clear -x
echo "${E_ERROR}Invalid input: '$choice' is not a valid option" >&2
echo ""
;;
esac
done