This repository has been archived by the owner on Dec 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmewdeko-main-installer
executable file
·386 lines (331 loc) · 12.1 KB
/
mewdeko-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
#!/bin/bash
#
# The main installer for Linux.
#
# Comment Key:
# A.1. - Return to prevent further code execution.
# B.1. - Prevent the code from running if the option is disabled.
#
########################################################################################
#### [ Variables ]
## To be exported.
_SERVICE_NAME="mewdeko.service"
_SERVICE="/etc/systemd/system/$_SERVICE_NAME"
## Indicates which major version of .NET and Java (minimum) is required.
req_dotnet_version=6
req_min_java_version=11
#### End of [ Variables ]
########################################################################################
#### [ Functions ]
########
# Depending on the return/exit code from any of the executed scripts, perform the
# corresponding/appropriate actions.
#
# Arguments:
# $1 - required
# Return/exit code.
########
exit_code_actions() {
case "$1" in
3) return 0 ;;
*) exit "$1" ;;
esac
}
########
# Return whether or not 'ccze' is installed.
#
# Arguments:
# None
########
hash_ccze() {
hash ccze &>/dev/null && ccze_installed=true \
|| ccze_installed=false
}
########
# Provide the reason(s) for why one or more options are disabled.
#
# Arguments:
# None
########
disabled_reasons() {
echo "${_CYAN}Reasons for the disabled option:"
if (! hash dotnet \
|| ! hash java \
|| ! hash redis-server \
|| ! "$ccze_installed" \
|| [[ ${dotnet_version:-false} != "$req_dotnet_version" ]] \
|| [[ ${java_version:-false} < "$req_min_java_version" ]]) &>/dev/null; then
echo " One or more prerequisites are not installed"
echo " Use option 6 to install them all"
fi
if [[ -d Mewdeko ]]; then
if [[ ! -f Mewdeko/src/Mewdeko/credentials.json ]]; then
echo " The 'credentials.json' could not be found"
echo " Refer to the following link for help: https://blog.mewdeko.tech/credentials-guide/"
fi
else
echo " Mewdeko could not be found"
echo " Use option 1 to download Mewdeko"
fi
echo "$_NC"
}
########################################################################################
#### [[ Functions To Be Exported ]]
########
# Store the status of Mewdeko's service, inside of $_SERVICE_STATUS.
#
# Arguments:
# None
########
_GET_SERVICE_STATUS() {
_SERVICE_STATUS=$(systemctl is-active "$_SERVICE_NAME")
}
########
# Stop Mewdeko's service.
#
# Arguments:
# $1 - optional
# True when the function should output text indicating if the service has been
# stopped or is currently not running.
#
# Returns:
# 1 - If $_SERVICE_NAME is not currently running.
########
_STOP_SERVICE() {
if [[ $_SERVICE_STATUS = "active" ]]; then
echo "Stopping '$_SERVICE_NAME'..."
sudo systemctl stop "$_SERVICE_NAME" || {
echo "${_RED}Failed to stop '$_SERVICE_NAME'" >&2
echo "${_CYAN}You will need to restart '$_SERVICE_NAME' to apply any" \
"updates to Mewdeko${_NC}"
return 1
}
[[ $1 = true ]] && echo -e "\n${_GREEN}Mewdeko has been stopped${_NC}"
else
[[ $1 = true ]] && echo -e "\n${_CYAN}Mewdeko is not currently running${_NC}"
fi
}
########
# Display the logs from 'mewdeko.server' as they are created.
#
# Arguments:
# None
########
_FOLLOW_SERVICE_LOGS() {
(
trap 'exit 130' SIGINT
sudo journalctl --no-hostname -f -u "$_SERVICE_NAME" | ccze -A
)
}
########
# Output additional information to go along with the output of the function
# '_FOLLOW_SERVICE_LOGS'.
#
# Arguments:
# $1 - required
# Indicates if the function was called from one of the runner scripts or from
# within the main installer.
########
_WATCH_SERVICE_LOGS() {
if [[ $1 = "runner" ]]; then
echo "Displaying '$_SERVICE_NAME' startup logs, live..."
elif [[ $1 = "opt_five" ]]; then
echo "Watching '$_SERVICE_NAME' logs, live..."
else
_STDERR "INTERNAL ERROR: Invalid argument for '_WATCH_SERVICE_LOGS': $1" "4"
fi
echo "${_CYAN}To stop displaying the startup logs:"
echo "1) Press 'Ctrl' + 'C'${_NC}"
echo ""
_FOLLOW_SERVICE_LOGS
[[ $1 = "runner" ]] && echo -e "\nPlease check the logs above to make sure that" \
"there aren't any errors, and if there are, to resolve whatever issue is" \
"causing them"
read -rp "Press [Enter] to return to the installer menu"
}
#### End of [[ Functions To Be Exported ]]
########################################################################################
#### End of [ Functions ]
########################################################################################
#### [ Main ]
printf "%sWelcome to the Mewdeko installer menu\n\n" "$_CLRLN"
while true; do
####################################################################################
#### [[ Temporary Variables ]]
#### The variables below constantly get modified later in the while loop, and are
#### required 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 Mewdeko is running)"
## Option 1.
opt_one_dis=false
opt_one_text="1. Download Mewdeko"
## Option 2 & 3.
opt_two_and_three_dis=false
opt_two_text="2. Run Mewdeko in the background"
opt_three_text="3. Run Mewdeko in the background with auto restart"
## Option 5.
opt_five_dis=false
opt_five_text="5. Display '$_SERVICE_NAME' logs in follow mode"
#### End of [[ Temporary Variables ]]
####################################################################################
#### [[ Variable Checks ]]
#### The following variables re-check the status, existence, etc., of some service
#### or program, that has the possiblity 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 java &>/dev/null; then
java_version=$(javac -version | awk '{print $2}') # Version: x.x.x
java_version=${java_version//.*/} # Version: x
fi
#### End of [[ Variable Checks ]]
####################################################################################
#### [[ Main Continued ]]
_GET_SERVICE_STATUS
hash_ccze
## Disable option 1 if any of the following tools are not installed.
if (! hash dotnet \
|| ! hash java \
|| ! hash redis-server \
|| ! "$ccze_installed" \
|| [[ ${dotnet_version:-false} != "$req_dotnet_version" ]] \
|| [[ ${java_version:-false} < "$req_min_java_version" ]]) &>/dev/null; then
opt_one_dis=true
opt_one_text="${_GREY}${opt_one_text}${dis_option}${_NC}"
fi
## Disable options 2, 3, 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" || [[ ! -f Mewdeko/src/Mewdeko/credentials.json ]]; then
opt_two_and_three_dis=true
opt_two_text="${_GREY}${opt_two_text}${dis_option}${_NC}"
opt_three_text="${_GREY}${opt_three_text}${dis_option}${_NC}"
opt_five_dis=true
opt_five_text="${_GREY}${opt_five_text}${dis_opt_v2}${_NC}"
## Options 2 and 3 remain enabled, if 'MewdekoRun' exists.
elif [[ -f MewdekoRun ]]; then
## Option 5 remains enabled, if Mewdeko's service is running.
if [[ $_SERVICE_STATUS = "active" ]]; then
run_mode_status=" ${_GREEN}(Running in this mode)${_NC}"
## Disable option 5 if Mewdeko's service NOT running.
elif [[ $_SERVICE_STATUS = "inactive" ]]; then
opt_five_dis=true
opt_five_text="${_GREY}${opt_five_text}${dis_opt_v2}${_NC}"
run_mode_status=" ${_YELLOW}(Set up to run in this mode)${_NC}"
## Disable option 5.
else
opt_five_dis=true
opt_five_text="${_GREY}${opt_five_text}${dis_opt_v2}${_NC}"
run_mode_status=" ${_YELLOW}(Status unknown)${_NC}"
fi
## If Mewdeko is running in the background with auto restart...
if grep -q '_code_name_="MewdekoRunAR"' MewdekoRun; then
opt_three_text="${opt_three_text}${run_mode_status}"
## If Mewdeko is running in the background...
elif grep -q '_code_name_="MewdekoRun"' MewdekoRun; then
opt_two_text="${opt_two_text}${run_mode_status}"
fi
## Options 2 and 3 remained enabled, but option 5 becomes disabled.
else
opt_five_dis=true
opt_five_text="${_GREY}${opt_five_text}${dis_opt_v2}${_NC}"
fi
echo "$opt_one_text"
echo "$opt_two_text"
echo "$opt_three_text"
echo "4. Stop Mewdeko"
echo "$opt_five_text"
echo "6. Install prerequisites"
echo "7. Exit"
read -r choice
case "$choice" in
1)
## B.1.
if "$opt_one_dis"; then
clear -x
echo "${_RED}Option 1 is currently disabled${_NC}"
disabled_reasons
continue
fi
export _SERVICE
export -f _STOP_SERVICE
export _SERVICE_NAME
export _SERVICE_STATUS
_DOWNLOAD_SCRIPT "mewdeko-latest-installer" "true"
clear -x
./mewdeko-latest-installer || exit_code_actions "$?"
# TODO: Figure out way to kill previous execution of the installer, possibly
# an array of PIDs and the 'clean_up()' function. This way, cleaning
# up and exiting text doesn't print duplicates.
# TODO: Re-due comments...
# Execute the newly downloaded version of 'installer-prep', so that all
# changes are applied.
exec "$_INSTALLER_PREP"
;;
2|3)
## B.1.
if "$opt_two_and_three_dis"; then
clear -x
echo "${_RED}Option $choice is currently disabled${_NC}"
disabled_reasons
continue
fi
export _SERVICE
export _SERVICE_NAME
export _SERVICE_STATUS
export -f _WATCH_SERVICE_LOGS
export -f _FOLLOW_SERVICE_LOGS
_DOWNLOAD_SCRIPT "mewdeko-runner"
clear -x
# If option 2 was executed...
if [[ $choice = 2 ]]; then
export _CODENAME="MewdekoRun"
printf "We will now run Mewdeko in the background. "
# If option 3 was executed...
else
export _CODENAME="MewdekoRunAR"
printf "We will now run Mewdeko in the background with auto restart. "
fi
read -rp "Press [Enter] to begin."
./mewdeko-runner || exit_code_actions "$?"
clear -x
;;
4)
clear -x
read -rp "We will now stop Mewdeko. Press [Enter] to begin."
_STOP_SERVICE "true"
read -rp "Press [Enter] to return to the installer menu"
clear -x
;;
5)
clear -x
## B.1.
if "$opt_five_dis"; then
echo "${_RED}Option 5 is currently disabled${_NC}"
echo ""
continue
fi
_WATCH_SERVICE_LOGS "opt_five"
clear -x
;;
6)
_DOWNLOAD_SCRIPT "prereqs-installer"
clear -x
./prereqs-installer || exit_code_actions "$?"
clear -x
;;
7)
exit 0
;;
*)
clear -x
echo "${_RED}Invalid input: '$choice' is not a valid option${_NC}" >&2
echo ""
;;
esac
#### End of [[ Main Continued ]]
####################################################################################
done
#### End of [ Main ]
########################################################################################