forked from MCSManager/Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
434 lines (386 loc) · 14.2 KB
/
setup.sh
File metadata and controls
434 lines (386 loc) · 14.2 KB
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
#!/bin/bash
# shellcheck disable=SC2034,SC2312,SC1091
#########################################
### Variables
## Features
DEBUG=false;
## Paths
root_install_path="/opt/mcsmanager";
node_install_path="${root_install_path}/node/${node_version}";
web_install_path="${root_install_path}/web";
daemon_install_path="${root_install_path}/daemon";
tmp_path="/tmp/mcsmanager";
## URLs
mcsmanager_download_url="http://oss.duzuii.com/d/MCSManager/MCSManager/MCSManager-v10-linux.tar.gz";
mcsmanager_hash_url="";
node_download_url="https://nodejs.org/dist/${node_version}/node-${node_version}-linux-${arch}.tar.gz";
node_hash_url="https://nodejs.org/dist/${node_version}/SHASUMS256.txt";
## Versions
node_version="v16.20.2";
## Script
# DO NOT MODIFY
update=false;
### Functions
## Utils
# Logger
function print_log() {
local level=$1;
local message=$2;
case "$1" in
"DEBUG")
if ${DEBUG}; then
printf "\033[90m[ \033[96mDEBUG\033[0m \033[90m] \033[96m%s\033[0m\n" "$2";
fi
return 0;
;;
"INFO")
printf "\033[90m[ \033[92mINFO\033[0m \033[90m] \033[92m%s\033[0m\n" "$2";
return 0;
;;
"WARN")
printf "\033[90m[ \033[93mWARN\033[0m \033[90m] \033[93m%s\033[0m\n" "$2";
return 0;
;;
"ERROR")
printf "\033[90m[ \033[91mERROR\033[0m \033[90m] \033[91m%\033[0m\n" "$2";
return 0;
;;
"FATAL")
printf "\033[90m[ \033[41m\033[37mFATAL\033[0m \033[90m] \033[41m\033[37m%s\033[0m\n" "$2";
return 0;
;;
*)
printf "\033[90m[ \033[41m\033[37mFATAL\033[0m \033[90m] \033[41m\033[37m%s\033[0m\n" "Unable to recognize log level! Please check the script!"
exit 1;
esac
}
# Error handler
function error_handler() {
local err_line=$1;
print_log "FATAL" "An unexpected error occurred!";
print_log "DEBUG" "Error line: ${err_line}";
exit 1;
}
trap 'error_handler "$LINENO"' ERR;
# Cleaner
function cleaner(){
# Stop MCSManager service
print_log "INFO" "Stop the MCSManager service...";
sudo systemctl disable --now mcsm-{daemon,web}.service;
# Backup old MCSManager data
migration true;
# Cleanup old MCSManager
print_log "INFO" "Cleaning up old MCSManager...";
sudo rm -rf "${root_install_path}";
sudo rm -f /etc/systemd/system/mcsm-{daemon,web}.service;
return 0;
}
# Migration
function migration(){
local is_backup;
is_backup="$1";
print_log "DEBUG" "Backup: ${is_backup}";
if ${is_backup}; then
print_log "INFO" "Backing up MCSManager data...";
[[ -d "${web_install_path}/data" ]] && sudo mv -f "${web_install_path}/data" "${tmp_path}/data/web";
[[ -d "${daemon_install_path}/data" ]] && sudo mv -f "${daemon_install_path}/data" "${tmp_path}/data/daemon";
else
print_log "INFO" "Recovering MCSManager data...";
[[ -d "${tmp_path}/data/web" ]] && sudo mv -f "${tmp_path}/data/web" "${web_install_path}/data";
[[ -d "${tmp_path}/data/daemon" ]] && sudo mv -f "${tmp_path}/data/daemon" "${daemon_install_path}/daemon/data";
fi
return 0;
}
# Node dependency installer
function install_npm_packages() {
local install_path;
install_path="$1";
print_log "DEBUG" "Install path: ${install_path}";
if cd "${install_path}"; then
/usr/bin/env "${node_install_path}"/bin/node "${node_install_path}"/bin/npm install --production --no-fund --no-audit > npm_install_log
else
print_log "ERROR" "Failed to change directory to ${install_path}";
return 1;
fi
return 0;
}
# Service file creator
function create_service_file() {
local file_name=$1;
local service_name=$2;
local working_directory=$3;
print_log "DEBUG" "File name: ${file_name}";
print_log "DEBUG" "Service name: ${service_name}";
print_log "DEBUG" "Working directory: ${working_directory}";
# shellcheck disable=SC2250,SC2154
if ${DEBUG}; then
cat << EOF | tee "/etc/systemd/system/${file_name}";
[Unit]
Description=${service_name}
[Service]
WorkingDirectory=${working_directory}
ExecStart=${node_install_path}/bin/node app.js
ExecReload=/bin/kill -s QUIT $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Environment="PATH=${PATH}"
[Install]
WantedBy=multi-user.target
EOF
else
cat << EOF > "/etc/systemd/system/${file_name}";
[Unit]
Description=${service_name}
[Service]
WorkingDirectory=${working_directory}
ExecStart=${node_install_path}/bin/node app.js
ExecReload=/bin/kill -s QUIT $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
Environment="PATH=${PATH}"
[Install]
WantedBy=multi-user.target
EOF
fi
return 0;
}
# File downloader
function download_file(){
local download_url=$1;
local file_name=$2;
print_log "DEBUG" "Download URL: ${download_url}";
print_log "DEBUG" "File name: ${file_name}";
print_log "DEBUG" "File saved path: ${tmp_path}/${file_name}";
if ${DEBUG}; then
wget "${download_url}" -q --progress=bar:force -c --retry-connrefused -t 5 -v -O "${tmp_path}/${file_name}";
else
wget "${download_url}" -q --progress=bar:force -c --retry-connrefused -t 5 -O"${tmp_path}/${file_name}";
fi
return 0;
}
## Checks
# Arch check
function check_arch(){
local arch;
arch=$(uname -m);
print_log "DEBUG" "Original architecture: ${arch}";
case "${arch}" in
"x86_64")
arch="x64";
;;
"aarch64")
arch="arm64";
;;
"arm")
arch="armv7l";
;;
"ppc64le")
arch="ppc64le";
;;
"s390x")
arch="s390x";
;;
*)
print_log "ERROR" "Unsupported architecture!";
print_log "ERROR" "Please try to install manually: https://github.com/MCSManager/MCSManager#linux";
return 1;
;;
esac
print_log "DEBUG" "Converted architecture: ${arch}";
return 0;
}
# System check
function check_system(){
local os;
os=$(uname);
print_log "DEBUG" "System Type: ${os}";
if [[ "${os}" == "Linux" ]]; then
# shellcheck source=/etc/os-release
. /etc/os-release;
print_log "DEBUG" "System ID: ${ID}";
print_log "DEBUG" "System Name: ${PRETTY_NAME}";
if [[ ${ID} != "ubuntu" ]] && [[ ${ID} != "debian" ]] && [[ ${ID} != "centos" ]] && [[ ${ID} != "fedora" ]] && [[ ${ID} != "rocky" ]]; then
print_log "ERROR" "Unsupported system!";
print_log "ERROR" "Please try to install manually:https://github.com/MCSManager/MCSManager#linux";
return 1;
fi
else
print_log "ERROR" "Unsupported system!";
print_log "ERROR" "Please try to install manually:https://github.com/MCSManager/MCSManager#linux";
return 1;
fi
return 0;
}
# Dependency check and installation
function check_deps(){
print_log "DEBUG" "System ID: ${ID}";
print_log "DEBUG" "System Name: ${PRETTY_NAME}";
case "${ID}" in
"ubuntu" | "debian")
sudo apt-get update;
sudo apt-get install wget git tar -y;
;;
"centos" | "fedora" | "rocky")
sudo yum update;
sudo yum install wget git tar -y;
;;
*)
print_log "ERROR" "Unsupported system!";
print_log "ERROR" "Please try to install manually:https://github.com/MCSManager/MCSManager#linux";
return 1;
;;
esac
return 0;
}
## Init
function init(){
# Creating file directories
[[ -d ${root_install_path} ]] && mkdir "${root_install_path}";
[[ -d ${node_install_path} ]] && mkdir "${node_install_path}";
# Add user
[[ ${DEBUG} = true ]] && id "mcsmanager";
if ! id "mcsmanager" &>/dev/null; then
sudo useradd -r -s /bin/false -U mcsmanager;
fi
return 0;
}
## Install
# Node.js installer
function install_node() {
print_log "INFO" "Installing Node.js ${node_version} ...";
# Download Node.js
print_log "INFO" "Downloading Node.js file...";
download_file "${node_download_url}" "node.tar.gz";
download_file "${node_hash_url}" "node.sha256";
# Check Node.js integrity
print_log "INFO" "Checking Node.js file integrity...";
local offical_hash;
local file_hash;
offical_hash=$(grep "node-${node_version}-linux-${arch}.tar.gz" "${tmp_path}/node.sha256" | awk '{ print $1 }');
file_hash=$(sha256sum "${tmp_path}/node.tar.gz" | awk '{ print $1 }');
if [[ "${offical_hash}" != "${file_hash}" ]]; then
print_log "ERROR" "Node.js checksum failure!";
print_log "ERROR" "Expected: ${offical_hash}";
print_log "ERROR" "Actual: ${file_hash}";
return 1;
fi
# Install Node.js
print_log "INFO" "Installing Node.js...";
if ${DEBUG}; then
sudo tar -zxvf "${tmp_path}/node.tar.gz" -C "${node_install_path}" --strip-components=1;
else
sudo tar -zxf "${tmp_path}/node.tar.gz" -C "${node_install_path}" --strip-components=1;
fi
# Set permissions
print_log "INFO" "Setting permissions...";
sudo chown -R mcsmanager:mcsmanager "${node_install_path}";
sudo chmod -R 755 "${node_install_path}";
# Check Node.js installation
print_log "DEBUG" Node.js version: "$("${node_install_path}/bin/node" -v)";
print_log "DEBUG" NPM version: "$("${node_install_path}/bin/node ${node_install_path}/bin/npm" -v)";
if [[ -f "${node_install_path}"/bin/node ]] && [[ "$("${node_install_path}/bin/node" -v)" == "${node_version}" ]]; then
print_log "INFO" "Node.js ${node_version} installed successfully!";
else
print_log "ERROR" "Node.js installation failed!";
return 1;
fi
return 0;
}
# MCSManager installer
function install_mcsmanager() {
print_log "INFO" "Installing MCSManager ...";
# Download MCSManager
print_log "INFO" "Downloading MCSManager file...";
download_file "${mcsmanager_download_url}" "mcsmanager.tar.gz";
download_file "${mcsmanager_hash_url}" "mcsmanager.sha256";
# Check MCSManager integrity
print_log "INFO" "Checking MCSManager file integrity...";
local offical_hash;
local file_hash;
offical_hash=$(cat "${tmp_path}/mcsmanager.sha256");
file_hash=$(sha256sum "${tmp_path}/mcsmanager.tar.gz" | awk '{ print $1 }');
if [[ "${offical_hash}" != "${file_hash}" ]]; then
print_log "ERROR" "MCSManager checksum failure!"
print_log "ERROR" "Expected: ${offical_hash}";
print_log "ERROR" "Actual: ${file_hash}";
return 1;
fi
# Install MCSManager
print_log "INFO" "Installing MCSManager...";
if ${DEBUG}; then
tar -zxvf "${tmp_path}/mcsmanager.tar.gz" -C "${tmp_path}/mcsmanger";
else
tar -zxf "${tmp_path}/mcsmanager.tar.gz" -C "${tmp_path}/mcsmanger";
fi
sudo mv -f "${tmp_path}/mcsmanger/web" "${web_install_path}";
sudo mv -f "${tmp_path}/mcsmanger/daemon" "${daemon_install_path}";
# Install dependencies
print_log "INFO" "Installing MCSManager dependencies..."
install_npm_packages "${web_install_path}";
install_npm_packages "${daemon_install_path}";
# Set permissions
print_log "INFO" "Setting permissions..."
sudo chown -R mcsmanager:mcsmanager "${web_install_path}";
sudo chown -R mcsmanager:mcsmanager "${daemon_install_path}";
sudo chmod -R 755 "${web_install_path}";
sudo chmod -R 755 "${daemon_install_path}";
# Register MCSManager services
sudo bash -c "$(declare -f create_service_file); create_service_file 'mcsm-web.service' 'MCSManager Web' '${web_install_path}'";
sudo bash -c "$(declare -f create_service_file); create_service_file 'mcsm-daemon.service' 'MCSManager Daemon' '${daemon_install_path}'";
sudo systemctl daemon-reload;
return 0;
}
## Main
function main(){
local public_ip
local private_ip
public_ip=$(curl -s http://ipecho.net/plain);
private_ip=$(hostname -i | awk '{print $1}');
print_log "DEBUG" "Public IP: ${public_ip}";
print_log "DEBUG" "Private IP: ${private_ip}";
print_log "INFO" "+----------------------------------------------------------------------";
print_log "INFO" "| MCSManager Installer";
print_log "INFO" "+----------------------------------------------------------------------";
# Check if the installation directory exists
if [[ -d ${root_install_path} ]]; then
# Check if the installation method is docker
print_log "INFO" "Checking for Docker installation...";
if [[ -f "${root_install_path}/docker-compose.yml" ]]; then
print_log "WARN" "Docker installation detected!";
print_log "WARN" "Please use the Docker update method provided in the official documentation to update MCSManager!";
print_log "WARN" "Official Documentation: https://docs.mcsmanager.com/";
return 1;
fi
# Uninstall old mcsmanager (simultaneous data migration)
update=true;
cleaner;
fi
# Basic checks
check_arch;
check_system;
check_deps;
# Init environment
init;
# Install
install_node;
install_mcsmanager;
if ${update}; then
migration
sudo systemctl enable --now mcsm-{web,daemon}.service
fi
print_log "INFO" "+----------------------------------------------------------------------";
print_log "INFO" "| Installation is complete! Welcome to the MCSManager!!!";
print_log "INFO" "|";
print_log "INFO" "| HTTP Web Service: http://${public_ip}:23333 or http://${private_ip}:23333";
print_log "INFO" "| Daemon Address: ws://${public_ip}:24444 or ws://${private_ip}:24444";
print_log "INFO" "| You must expose ports 23333 and 24444 to use the service properly on the Internet.";
print_log "INFO" "|";
print_log "INFO" "| Usage:";
print_log "INFO" "| systemctl start mcsm-{daemon,web}.service";
print_log "INFO" "| systemctl stop mcsm-{daemon,web}.service";
print_log "INFO" "| systemctl restart mcsm-{daemon,web}.service";
print_log "INFO" "|";
print_log "INFO" "| Official Document: https://docs.mcsmanager.com/";
print_log "INFO" "+----------------------------------------------------------------------";
return 0;
}
### Entrypoint
main