forked from MCSManager/Script
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_cn.sh
More file actions
1358 lines (1156 loc) · 39 KB
/
setup_cn.sh
File metadata and controls
1358 lines (1156 loc) · 39 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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# MCSManager官方安装脚本.
# 这个脚本将会把MCSManager服务端和节点服务端更新/安装至最新发布版本.
# ------------------------------------------------------------------------------
# 受支持的Linux:
# 此脚本支持以下Linux发行版:
# - Ubuntu: 18.04, 20.04, 22.04, 24.04
# - Debian: 10, 11, 12, 13
# - CentOS: 7, 8 Stream, 9 Stream, 10 Stream
# - RHEL: 7, 8, 9, 10
# - Arch Linux: Support planned (TBD)
# ------------------------------------------------------------------------------
# Target installation directory (can be overridden with --install-dir)
install_dir="/opt/mcsmanager"
# Primary download URL bas. Full package URL = download_base_url + package_name
download_base_url="https://cdn.imlazy.ink:233/files/"
# Fallback download URL (can also be a local directory or mirror)
download_fallback_url="https://github.com/MCSManager/MCSManager/releases/latest/download/mcsmanager_linux_release.tar.gz"
# Name of the release package to download/detect
package_name="mcsmanager_linux_release.tar.gz"
# Node.js version to be installed
# Keep the leading "v"
node_version="v20.12.2"
# Node download base URL - primary
node_download_url_base="https://nodejs.org/dist/"
# Node download URL - fallback.
# This is the URL points directly to the file, not the base. This can also be a local absolute path.
# Only supports https:// or http:// for web locations.
node_download_fallback=""
# Node.js installation path (defaults to the MCSManager installation path. Can be overridden with --node-install-dir)
node_install_dir="$install_dir"
# Temp dir for file extraction
tmp_dir="/tmp"
# Bypass installed user permission check, override by --force-permission
force_permission=false
# --------------- Global Variables ---------------#
# DO NOT MODIFY #
# Component installation options.
# For fresh installs, both daemon and web components are installed by default.
# For updates, behavior depends on detected existing components.
# Can be overridden with --install daemon/web/all
install_daemon=true
install_web=true
# Install MCSM as (default: root).
# To install as a general user (e.g., "mcsm"), use the --user option: --user mcsm
# To ensure compatibility, only user mcsm is supported.
install_user="root"
# Installed user, for permission check
web_installed=false
daemon_installed=false
web_installed_user=""
daemon_installed_user=""
# Service file locations
# the final dir = systemd_file + {web/daemon} + ".service"
systemd_file="/etc/systemd/system/mcsm-"
# Optional: Override the default installation source file.
# If --install-source is specified, the installer will use the provided
# "mcsmanager_linux_release.tar.gz" file instead of downloading it.
# Only support local absolute path.
install_source_path=""
# temp path for extracted file(s)
install_tmp_dir="/opt/mcsmanager/mcsm_abcd"
# dir name for data dir backup
# e.g. /opt/mcsmanager/daemon/data -> /opt/mcsmanager/data_bak_data
# only valid for when during an update
backup_prefix="data_bak_"
# System architecture (detected automatically)
arch=""
version=""
distro=""
# Supported OS versions (map-style structure)
# Format: supported_os["distro_name"]="version1 version2 version3 ..."
declare -A supported_os
supported_os["Ubuntu"]="18 20 22 24"
supported_os["Debian"]="10 11 12 13"
supported_os["CentOS"]="7 8 8-stream 9-stream 10-stream"
supported_os["RHEL"]="7 8 9 10"
supported_os["Arch"]="rolling"
# Required system commands for installation
# These will be checked before logic process
required_commands=(
chmod
chown
wget
tar
stat
useradd
usermod
date
)
# Node.js related sections
# Enable strict version checking (exact match)
# enabled -> strict requriement for defined node version
# false -> newer version allowed
# Older version is NEVER allowed
strict_node_version_check=true
# Will be set based on actual node status
install_node=true
# Remove leading "v" from defined version
required_node_ver="${node_version#v}"
# Holds absolute path for node & npm
node_bin_path=""
npm_bin_path=""
# Hold Node.js arch name, e.g. x86_64 -> x64
node_arch=""
# Hold Node.js intallation path, e.g. ${node_install_dir}/node-${node_version}-linux-${arch}
node_path=""
# For installation result
daemon_key=""
daemon_port=""
web_port=""
daemon_key_config_subpath="data/Config/global.json"
web_port_config_subpath="data/SystemConfig/config.json"
# Terminal color & style related
# Default to false, auto check later
SUPPORTS_COLOR=false
SUPPORTS_STYLE=false
# Declare ANSI reset
RESET="\033[0m"
# Foreground colors
declare -A FG_COLORS=(
[black]="\033[0;30m"
[red]="\033[0;31m"
[green]="\033[0;32m"
[yellow]="\033[0;33m"
[blue]="\033[0;34m"
[magenta]="\033[0;35m"
[cyan]="\033[0;36m"
[white]="\033[0;37m"
)
# Font styles
declare -A STYLES=(
[bold]="\033[1m"
[underline]="\033[4m"
[italic]="\033[3m" # Often ignored
[clear_line]="\r\033[2K"
[strikethrough]="\033[9m"
)
### Helper Functions
# Execution wrapper, avoid unexpected crashes.
safe_run() {
local func="$1"
local err_msg="$2"
shift 2
if ! "$func" "$@"; then
echo "Error: $err_msg"
exit 1
fi
}
# Function to ensure the script is run as root
check_root() {
# Using Bash's built-in EUID variable
if [ -n "$EUID" ]; then
if [ "$EUID" -ne 0 ]; then
cprint red "错误: 这个脚本只能运行在root或sudo模式下,请尝试切换用户或者使用sudo."
exit 1
fi
else
# Fallback to using id -u if EUID is unavailable (e.g., non-Bash shell or misconfigured environment)
if [ "$(id -u)" -ne 0 ]; then
cprint red "错误: 这个脚本只能运行在root或sudo模式下,请尝试切换用户或者使用sudo."
exit 1
fi
fi
}
# Function to check whether current terminal support color & style
detect_terminal_capabilities() {
SUPPORTS_COLOR=false
SUPPORTS_STYLE=false
if [ -t 1 ] && command -v tput >/dev/null 2>&1; then
if [ "$(tput colors)" -ge 8 ]; then
SUPPORTS_COLOR=true
fi
if tput bold >/dev/null 2>&1 && tput smul >/dev/null 2>&1; then
SUPPORTS_STYLE=true
fi
fi
if [ "$SUPPORTS_COLOR" = true ]; then
cprint green "[OK] 这个终端支持彩色输出."
else
cprint yellow "注:终端不支持彩色输出。不格式化继续."
fi
if [ "$SUPPORTS_STYLE" = true ]; then
cprint green "[OK] 终端支持粗体和下划线格式."
else
cprint yellow "注意:终端不支持高级文本样式."
fi
}
# Check whether daemon or web is installed
is_component_installed() {
local component_name="$1"
local component_path="${install_dir}/${component_name}"
if [[ -d "$component_path" ]]; then
cprint green "组件 '$component_name' 已经被安装在 $component_path"
# Set corresponding global variable
if [[ "$component_name" == "daemon" ]]; then
daemon_installed=true
elif [[ "$component_name" == "web" ]]; then
web_installed=true
fi
return 0
else
cprint yellow "组件 '$component_name' 未被安装"
# Set corresponding global variable
if [[ "$component_name" == "daemon" ]]; then
daemon_installed=false
elif [[ "$component_name" == "web" ]]; then
web_installed=false
fi
return 1
fi
}
check_component_permission() {
local component="$1"
local service_file="${systemd_file}${component}.service"
if [[ ! -f "$service_file" ]]; then
cprint yellow "找不到服务文件: $service_file"
return 0 # nothing changed
fi
# Extract the User= line if it exists
local user_line
user_line=$(grep -E '^User=' "$service_file" 2>/dev/null | head -1)
local user
if [[ -z "$user_line" ]]; then
user="root" # default if no User= is defined
else
user="${user_line#User=}"
fi
# Validate user
if [[ "$user" != "root" && "$user" != "mcsm" ]]; then
cprint red bold "不支持的用户 '$user' 在 $service_file. 使用 'root' 或 'mcsm'."
exit 1
fi
# Assign to appropriate global
if [[ "$component" == "web" ]]; then
web_installed_user="$user"
elif [[ "$component" == "daemon" ]]; then
daemon_installed_user="$user"
fi
cprint cyan "已删除 $component 以用户身份安装: $user"
return 0
}
parse_args() {
local explicit_install_flag=false
while [[ $# -gt 0 ]]; do
case "$1" in
--install-dir)
if [[ -n "$2" ]]; then
install_dir="$2"
shift 2
else
echo "错误:--install-dir需要一个路径参数."
exit 1
fi
;;
--node-install-dir)
if [[ -n "$2" ]]; then
node_install_dir="$2"
shift 2
else
echo "错误:--node-install-dir需要一个路径参数."
exit 1
fi
;;
--install)
explicit_install_flag=true
if [[ -n "$2" && "$2" != --* ]]; then
case "$2" in
daemon)
install_daemon=true
is_component_installed "daemon"
install_web=false
check_component_permission "daemon"
;;
web)
install_daemon=false
is_component_installed "web"
install_web=true
check_component_permission "web"
;;
all)
install_daemon=true
install_web=true
is_component_installed "daemon"
is_component_installed "web"
check_component_permission "daemon"
check_component_permission "web"
;;
*)
echo "错误:--install的值无效。期望‘daemon’, ‘web’或‘all’."
echo "Usage: --install daemon|web|all"
exit 1
;;
esac
shift 2
else
echo "错误:提供了--install标志,但没有值。请指定:daemon、web或all."
echo "使用方法: --install daemon|web|all"
exit 1
fi
;;
--user)
if [[ -n "$2" ]]; then
case "$2" in
root)
install_user="root"
;;
mcsm)
install_user="mcsm"
;;
*)
echo "错误:无效用户 '$2'. 只有 'root' 和 'mcsm' 受支持."
echo "使用方法: --user root|mcsm"
exit 1
;;
esac
shift 2
else
echo "错误:--user需要一个值 (root 或 mcsm)."
exit 1
fi
;;
--install-source)
if [[ -n "$2" ]]; then
install_source_path="$2"
shift 2
else
echo "错误:--install-source需要文件路径."
exit 1
fi
;;
--force-permission)
force_permission=true
shift
;;
*)
echo "错误:未知参数t: $1"
exit 1
;;
esac
done
# Auto-detect branch: only run if --install was not explicitly passed
if [[ "$explicit_install_flag" == false ]]; then
daemon_installed=false
web_installed=false
if is_component_installed "daemon"; then
daemon_installed=true
check_component_permission "daemon"
fi
if is_component_installed "web"; then
web_installed=true
check_component_permission "web"
fi
# When only one component installed, we wanted to process that one only.
if [[ "$daemon_installed" == true && "$web_installed" == false ]]; then
install_daemon=true
install_web=false
elif [[ "$daemon_installed" == false && "$web_installed" == true ]]; then
install_daemon=false
install_web=true
else
install_daemon=true
install_web=true
fi
fi
}
# Get Distribution & Architecture Info
detect_os_info() {
distro="Unknown"
version="Unknown"
arch=$(uname -m)
# Try primary source
if [ -f /etc/os-release ]; then
. /etc/os-release
distro_id="${ID,,}"
version_id="${VERSION_ID,,}"
case "$distro_id" in
ubuntu)
distro="Ubuntu"
version="$version_id"
;;
debian)
distro="Debian"
version="$version_id"
;;
centos)
distro="CentOS"
version="$version_id"
;;
rhel*)
distro="RHEL"
version="$version_id"
;;
arch)
distro="Arch"
version="rolling"
;;
*)
distro="${ID:-Unknown}"
version="$version_id"
;;
esac
fi
# Fallbacks for missing or invalid version
if [[ -z "$version" || "$version" == "unknown" || "$version" == "" ]]; then
if [ -f /etc/issue ]; then
version_guess=$(grep -oP '[0-9]+(\.[0-9]+)*' /etc/issue | head -1)
if [[ -n "$version_guess" ]]; then
version="$version_guess"
fi
fi
fi
# Normalize version: keep only major version
version_full="$version"
cprint cyan "检测到操作系统: $distro $version_full"
cprint cyan "检测到架构: $arch"
}
# Check if all required commands are available
check_required_commands() {
local missing=0
for cmd in "${required_commands[@]}"; do
if ! command -v "$cmd" >/dev/null 2>&1; then
echo "错误:必需的命令 '$cmd' 在PATH中不可用."
missing=1
fi
done
if [ "$missing" -ne 0 ]; then
echo "缺少一个或多个必需的命令。请安装后再试."
return 1
fi
cprint green "所有必需的命令都可用."
return 0
}
# Print with specified color and style, fallback to RESET if not supported.
# Supported colors*: black|red|green|yellow|blue|magenta|cyan|white
# Supported styles*: bold|underline|italic|clear_line|strikethrough
# *Note: some style may not necessarily work on all terminals.
# Example usage:
# cprint green bold "Installation completed successfully."
# cprint red underline "Failed to detect required command: wget"
# cprint yellow "Warning: Disk space is low."
# cprint underline "Failed to detect required command: wget"
# cprint bold green underline"Installation completed successfully."
cprint() {
local color=""
local text=""
local styles=""
local disable_prefix=false
local disable_newline=false
while [[ $# -gt 1 ]]; do
case "$1" in
black|red|green|yellow|blue|magenta|cyan|white)
color="$1"
;;
bold|underline|italic|clear_line|strikethrough)
styles+="${STYLES[$1]}"
;;
noprefix)
disable_prefix=true
;;
nonl)
disable_newline=true
;;
esac
shift
done
text="$1"
local prefix_text=""
if [[ "$disable_prefix" != true ]]; then
local timestamp="[$(date +%H:%M:%S)]"
local label="[MCSM Installer]"
prefix_text="${FG_COLORS[white]}$timestamp $label${RESET} "
fi
local prefix=""
if [[ -n "$color" && "$SUPPORTS_COLOR" = true ]]; then
prefix+="${FG_COLORS[$color]}"
fi
if [[ "$SUPPORTS_STYLE" = true || "$styles" == *"${STYLES[clear_line]}"* ]]; then
prefix="$styles$prefix"
fi
if [[ "$disable_newline" == true ]]; then
printf "%b%b%s%b" "$prefix_text" "$prefix" "$text" "$RESET"
else
printf "%b%b%s%b\n" "$prefix_text" "$prefix" "$text" "$RESET"
fi
}
# Permission check before proceed with installation
permission_barrier() {
if [[ "$web_installed" == false && "$daemon_installed" == false ]]; then
cprint cyan "当前没有安装组件-跳过权限检查."
return 0
fi
for component in web daemon; do
local is_installed_var="${component}_installed"
local installed_user_var="${component}_installed_user"
if [[ "${!is_installed_var}" == true ]]; then
local installed_user="${!installed_user_var}"
# Step 0: Ensure installed user is detected
if [[ -z "$installed_user" ]]; then
cprint red bold "检测到 '$component' 已安装,但无法从其systemd服务文件确定用户."
cprint red "这可能表示自定义或不支持的服务文件设置."
cprint red "拒绝执行以避免潜在的冲突."
exit 1
fi
# Step 1: User match check with optional force override
if [[ "$installed_user" != "$install_user" ]]; then
if [[ "$force_permission" == true ]]; then
cprint yellow bold "权限不匹配 '$component':"
cprint yellow "以用户身份安装: $installed_user"
cprint yellow "目标安装用户: $install_user"
cprint yellow "用户不匹配,但设置了--force-permission。继续和更新权限…"
sleep 3
else
cprint red bold "权限不匹配 '$component':"
cprint red "以用户身份安装: $installed_user"
cprint red "目标安装用户: $install_user"
cprint red "用户不匹配,但设置了--force-permission。继续和更新权限..."
exit 1
fi
else
cprint green bold "权限检查已通过: '$installed_user' 匹配目标用户."
fi
fi
done
# Step 2: Directory ownership check
local dir_owner
dir_owner=$(stat -c '%U' "$install_dir" 2>/dev/null)
if [[ -z "$dir_owner" ]]; then
cprint red bold "无法确定安装目录的所有者: $install_dir"
exit 1
fi
if [[ "$dir_owner" != "$install_user" ]]; then
if [[ "$force_permission" == true ]]; then
cprint yellow bold "安装目录所有权不匹配:"
cprint yellow " 目录: $install_dir"
cprint yellow " 归: $dir_owner"
cprint yellow " 预期: $install_user"
cprint yellow " --force-permission设置。尽管不匹配,但继续."
sleep 3
else
cprint red bold "安装目录所有权不匹配:"
cprint red " 目录: $install_dir"
cprint red " 归: $dir_owner"
cprint red " 预期: $install_user"
exit 1
fi
else
cprint green bold "安装目录所有权检查通过: '$install_dir' is owned by '$install_user'."
fi
cprint green bold "验证了权限和所有权。继续."
return 0
}
# Map OS arch with actual Node.js Arch name
# This function should be placed after var arch has been assigned a valid value.
resolve_node_arch() {
case "$arch" in
x86_64)
node_arch="x64"
;;
aarch64)
node_arch="arm64"
;;
armv7l)
node_arch="armv7l"
;;
*)
cprint red bold "Node.js不支持的架构: $arch"
return 1
;;
esac
# Assign node_path based on resolved arch and current version/install dir
node_path="${node_install_dir}/node-${node_version}-linux-${node_arch}"
cprint cyan "解析了Node.js架构: $node_arch"
cprint cyan "Node.js安装路径: $node_path"
}
# Check if Node.js at PATH is valid.
# This function check Node.js version + NPM (if Node.js valid)
verify_node_at_path() {
local node_path="$1"
node_bin_path="$node_path/bin/node"
npm_bin_path="$node_path/bin/npm"
# Node binary missing
if [ ! -x "$node_bin_path" ]; then
return 1
fi
local installed_ver
installed_ver="$("$node_bin_path" -v 2>/dev/null | sed 's/^v//')"
if [[ -z "$installed_ver" ]]; then
return 1
fi
if [ "$strict_node_version_check" = true ]; then
if [[ "$installed_ver" != "$required_node_ver" ]]; then
return 3
fi
else
local cmp
cmp=$(printf "%s\n%s\n" "$required_node_ver" "$installed_ver" | sort -V | head -1)
if [[ "$cmp" != "$required_node_ver" ]]; then
return 2
fi
fi
# Check if npm exists and works using node (not $PATH/npm)
if [ ! -x "$npm_bin_path" ]; then
return 4
fi
# Use node to run npm.js directly, in case env is broken
local npm_version
npm_version="$("$node_bin_path" "$npm_bin_path" --version 2>/dev/null)"
if [[ -z "$npm_version" ]]; then
return 4
fi
return 0
}
# Node.js pre-check. check if we need to install Node.js before installer run.
# Use postcheck_node_after_install() to check after install.
check_node_installed() {
verify_node_at_path "$node_path"
local result=$?
case $result in
0)
cprint green bold "Node.js和npm在 $node_path (版本 $required_node_ver 或兼容)"
install_node=false
;;
1)
cprint yellow bold "Node.js二进制文件未找到或无法使用 $node_path"
install_node=true
;;
2)
cprint red bold "Node.js版本 $node_path 太老. 要求: >= $required_node_ver"
install_node=true
;;
3)
cprint red bold "Node.js版本不匹配。要求: $required_node_ver, 发现了其他的Node.js版本."
install_node=true
;;
4)
cprint red bold "Node.js存在,但npm缺失或损坏."
install_node=true
;;
*)
cprint red bold "Node验证中出现意外错误."
install_node=true
;;
esac
}
# Node.js post-check. check if Node.js is valid after install.
postcheck_node_after_install() {
verify_node_at_path "$node_path"
if [[ $? -ne 0 ]]; then
cprint red bold "Node.js安装失败或无效 $node_path"
return 1
else
cprint green bold "Node.js的安装和运行在 $node_path"
return 0
fi
}
# Install Node.js and check
install_node() {
local archive_name="node-${node_version}-linux-${node_arch}.tar.xz"
local target_dir="${node_install_dir}/node-${node_version}-linux-${node_arch}"
local archive_path="${node_install_dir}/${archive_name}"
local download_url="${node_download_url_base}${node_version}/${archive_name}"
local fallback="$node_download_fallback"
cprint cyan bold "安装Node.js $node_version 架构: $node_arch"
mkdir -p "$node_install_dir" || {
cprint red bold "创建Node安装目录失败: $node_install_dir"
return 1
}
# Download
cprint cyan "下载Node.js: $download_url"
if ! wget --progress=bar:force -O "$archive_path" "$download_url"; then
cprint yellow "主下载失败。尝试备用下载……"
if [[ -n "$fallback" ]]; then
if [[ "$fallback" =~ ^https?:// ]]; then
cprint cyan "从备用URL下载: $fallback"
if ! wget --progress=bar:force -O "$archive_path" "$fallback"; then
cprint red bold "备用下载失败: $fallback"
return 1
fi
elif [ -f "$fallback" ]; then
cprint cyan "从本地备份进行复制: $fallback"
cp "$fallback" "$archive_path" || {
cprint red bold "复制备用Node.js存档失败 $fallback"
return 1
}
else
cprint red bold "无效的备用路径: $fallback"
return 1
fi
else
cprint red bold "没有配置备用源。不能继续进行."
return 1
fi
fi
# Extract archive
cprint cyan "提取Node.js存档..."
if ! tar -xf "$archive_path" -C "$node_install_dir"; then
cprint red bold "提取Node.js文件失败."
return 1
fi
chmod -R a+rx "$target_dir" || {
cprint red bold "在Node.js文件上设置执行权限失败."
return 1
}
verify_node_at_path "$target_dir"
local result=$?
if [[ $result -ne 0 ]]; then
cprint red bold "Node.js安装验证失败."
return 1
fi
cprint cyan "清理文件……"
rm -f "$archive_path"
cprint green bold "Node.js $node_version 安装成功 $target_dir"
# Save resolved binary paths to global variables
node_bin_path="${target_dir}/bin/node"
npm_bin_path="${target_dir}/bin/npm"
cprint green "Node.js 二进制文件: $node_bin_path"
cprint green "npm 二进制文件: $npm_bin_path"
return 0
}
# Function to download MCSM package. fetch from primary URL first, then fallback URL.
# This function only put extracted file(s) into install_dir, it does not perform the actual update.
download_mcsm() {
local archive_name="$package_name"
local archive_path="${tmp_dir}/${archive_name}"
local primary_url="${download_base_url}${archive_name}"
local fallback="$download_fallback_url"
cprint cyan bold "下载MCSManager安装包…"
# Step 1: Try downloading from primary URL
if ! wget --progress=bar:force -O "$archive_path" "$primary_url"; then
cprint yellow "主下载失败。尝试备用资源…"
if [[ -z "$fallback" ]]; then
cprint red bold "没有指定备用URL或路径."
return 1
fi
if [[ "$fallback" =~ ^https?:// ]]; then
if ! wget --progress=bar:force -O "$archive_path" "$fallback"; then
cprint red bold "备用下载失败 $fallback"
return 1
fi
elif [[ -f "$fallback" ]]; then
cp "$fallback" "$archive_path" || {
cprint red bold "未能复制备用文件 $fallback"
return 1
}
else
cprint red bold "备用路径无效: $fallback"
return 1
fi
fi
# Step 2: Generate extract directory
local suffix
suffix=$(tr -dc 'a-z0-9' </dev/urandom | head -c 4)
local extracted_tmp_path="${tmp_dir}/mcsm_${suffix}"
if [[ -e "$extracted_tmp_path" ]]; then
cprint red bold "临时提取路径已存在: $extracted_tmp_path"
return 1
fi
mkdir -p "$extracted_tmp_path" || {
cprint red bold "创建临时解压目录失败: $extracted_tmp_path"
return 1
}
cprint cyan "将存档提取到 $extracted_tmp_path..."
if ! tar -xzf "$archive_path" -C "$extracted_tmp_path"; then
cprint red bold "提取存档失败."
rm -rf "$extracted_tmp_path"
return 1
fi
rm -f "$archive_path"
# Step 3: Move the entire extracted directory to install_dir
install_tmp_dir="${install_dir}/mcsm_${suffix}"
if [[ -e "$install_tmp_dir" ]]; then
cprint red bold "安装目标已经存在 $install_tmp_dir"
cprint red " 请在继续之前删除或重命名它."
return 1
fi
mv "$extracted_tmp_path" "$install_tmp_dir" || {
cprint red bold "未能将解压缩的文件移动到 $install_tmp_dir"
return 1
}
cprint green bold "MCSManager源提取并移动到: $install_tmp_dir"
return 0
}
# Prepare user if needed
prepare_user() {
if [[ "$install_user" == "root" ]]; then
cprint cyan "安装用户是'root' -跳过用户创建."
return 0
fi
# Check if user already exists
if id "$install_user" &>/dev/null; then
cprint green "用户 '$install_user' 已经存在."
else
cprint cyan "创建系统用户: $install_user (无登录,无密码)..."
if ! useradd --system --home "$install_dir" --shell /usr/sbin/nologin "$install_user"; then
cprint red bold "创建用户失败: $install_user"
exit 1
fi
cprint green "用户 '$install_user' 已创建."
fi
# Docker integration
if command -v docker &>/dev/null; then
cprint cyan "Docker已被安装 -检查组分配…"
if getent group docker &>/dev/null; then
if id -nG "$install_user" | grep -qw docker; then
cprint green "用户 '$install_user' 已经在“docker”组中."
else
cprint cyan "添加用户 '$install_user' 到 'docker' 组..."
if usermod -aG docker "$install_user"; then
cprint green "授予的Docker组访问权限 '$install_user'."
else
cprint red "未能添加 '$install_user' 给“Docker”组。这个用户可能无法使用Docker."
fi
fi
else
cprint red "安装了Docker,但没有找到Docker组。跳过组分配."
fi
else
cprint yellow "未安装Docker -跳过Docker组配置."
fi
return 0
}
# Function to stop MCSM services if they exist
stop_mcsm_services() {
cprint yellow bold "试图停止mcsm-web和mcsm-daemon服务..."
# Attempt to stop mcsm-web
cprint blue "正在停止 mcsm-web..."
if systemctl stop mcsm-web; then
cprint green "mcsm-web 已停止."
else
cprint red bold "警告:未能停止mcsm-web(可能不存在或已停止)."
fi
# Attempt to stop mcsm-daemon
cprint blue "正在停止 mcsm-daemon..."
if systemctl stop mcsm-daemon; then
cprint green "mcsm-daemon 已停止."
else
cprint red bold "警告:未能停止mcsm-daemon(可能不存在或已停止)."
fi
}
# Prepare file & permissions before install.
mcsm_install_prepare() {
# Stop service if existed
stop_mcsm_services
if [[ ! -d "$install_tmp_dir" ]]; then
cprint red bold "临时安装目录不存在: $install_tmp_dir"
exit 1
fi
cprint cyan "改变所有权 $install_tmp_dir 到用户 '$install_user'..."
chown -R "$install_user":"$install_user" "$install_tmp_dir" || {
cprint red bold "所有权变更失败 $install_tmp_dir"
cleanup_install_tmp
exit 1
}
# Normalize install_dir to ensure it ends with a slash
[[ "${install_dir}" != */ ]] && install_dir="${install_dir}/"
if [[ "$web_installed" == false && "$daemon_installed" == false ]]; then