#!/bin/bash
# MCSManager script for rainyun game server container, by CodeZhangBorui
mcsmanager_download_addr="https://awwa.cc/mcsm/linux"
package_name="mcsmanager_linux_release.tar.gz"
node="v16.20.2"
arch=$(uname -m)
container_root="/workspace"
mcsmanager_install_path="$container_root/mcsmanager"
node_install_path=""
web_port="<自定义 Web 端口>"
daemon_port="<自定义 Daemon 端口>"
printf "\033c"
echo_cyan() {
printf '\033[1;36m%b\033[0m\n' "$@"
}
echo_red() {
printf '\033[1;31m%b\033[0m\n' "$@"
}
echo_green() {
printf '\033[1;32m%b\033[0m\n' "$@"
}
echo_cyan_n() {
printf '\033[1;36m%b\033[0m' "$@"
}
echo_yellow() {
printf '\033[1;33m%b\033[0m\n' "$@"
}
Red_Error() {
echo '================================================='
printf '\033[1;31;40m%b\033[0m\n' "$@"
echo '================================================='
exit 1
}
# script info
echo_cyan "+----------------------------------------------------------------------
| 适用于雨云游戏云容器的 MCSManager 脚本 by CodeZhangBorui v2024.10.15
| 基于 MCSManager 官方 Linux 安装脚本改编
+----------------------------------------------------------------------
"
Env_Inspection() {
# Environmental inspection
if [[ "$arch" == x86_64 ]]; then
arch=x64
#echo "[-] x64 architecture detected"
elif [[ $arch == aarch64 ]]; then
arch=arm64
#echo "[-] 64-bit ARM architecture detected"
elif [[ $arch == arm ]]; then
arch=armv7l
#echo "[-] 32-bit ARM architecture detected"
elif [[ $arch == ppc64le ]]; then
arch=ppc64le
#echo "[-] IBM POWER architecture detected"
elif [[ $arch == s390x ]]; then
arch=s390x
#echo "[-] IBM LinuxONE architecture detected"
else
Red_Error "[x] Sorry, this architecture is not supported yet!\n[x]Please try to install manually: https://github.com/MCSManager/MCSManager#linux"
fi
echo_cyan "[-] Architecture: $arch"
node_install_path="$container_root/node-$node-linux-$arch"
}
Packages_Install() {
# Install related software
echo_cyan_n "[+] Installing dependent software (git, tar, wget)... "
# Use Tsinghua mirror source (Debian 12 Method)
cp /etc/apt/sources.list.d/debian.sources /etc/apt/sources.list.d/debian.sources.bak
cat <<EOF >/etc/apt/sources.list.d/debian.sources
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/debian
Suites: bookworm bookworm-updates bookworm-backports
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/debian-security
Suites: bookworm-security
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
EOF
# Install packages
apt update
apt install -y git tar wget
# Determine whether the relevant software is installed successfully
if [[ -x "$(command -v git)" && -x "$(command -v tar)" && -x "$(command -v wget)" ]]; then
echo_green "Success"
else
Red_Error "[x] Failed to find git, tar and wget, please install them manually!"
fi
}
Install_Node() {
echo_cyan_n "[+] Install Node.JS environment...\n"
rm -irf "$node_install_path"
cd $container_root || Red_Error "[x] Failed to enter $container_root"
rm -rf "node-$node-linux-$arch.tar.gz"
wget "https://registry.npmmirror.com/-/binary/node/$node/node-$node-linux-$arch.tar.gz" || Red_Error "[x] Failed to download node release"
tar -zxf "node-$node-linux-$arch.tar.gz" || Red_Error "[x] Failed to untar node"
rm -rf "node-$node-linux-$arch.tar.gz"
chmod -R 755 "$node_install_path"
if [[ -f "$node_install_path"/bin/node ]] && [[ "$("$node_install_path"/bin/node -v)" == "$node" ]]; then
echo_green "Success"
else
Red_Error "[x] Node installation failed!"
fi
echo
echo_yellow "=============== Node.JS Version ==============="
echo_yellow " node: $("$node_install_path"/bin/node -v)"
echo_yellow " npm: v$(env "$node_install_path"/bin/node "$node_install_path"/bin/npm -v)"
echo_yellow "=============== Node.JS Version ==============="
echo
sleep 3
}
Install_MCSManager() {
echo_cyan "[+] Install MCSManager..."
# stop service
service mcsm-daemon stop
service mcsm-web stop
# delete service
rm -rf /etc/init.d/mcsm-{daemon,web}
mkdir -p "${mcsmanager_install_path}" || Red_Error "[x] Failed to create ${mcsmanager_install_path}"
# cd $container_root/mcsmanager
cd "${mcsmanager_install_path}" || Red_Error "[x] Failed to enter ${mcsmanager_install_path}"
# download MCSManager release
wget "${mcsmanager_download_addr}" -O "${package_name}" || Red_Error "[x] Failed to download MCSManager"
tar -zxf ${package_name} -o || Red_Error "[x] Failed to untar ${package_name}"
rm -rf "${mcsmanager_install_path}/${package_name}"
# echo "[→] cd daemon"
cd "${mcsmanager_install_path}/daemon" || Red_Error "[x] Failed to enter ${mcsmanager_install_path}/daemon"
echo_cyan "[+] Install MCSManager-Daemon dependencies..."
env "$node_install_path"/bin/node "$node_install_path"/bin/npm install --registry=https://registry.npmmirror.com --production --no-fund --no-audit &>/dev/null || Red_Error "[x] Failed to npm install in ${mcsmanager_install_path}/daemon"
# echo "[←] cd .."
cd "${mcsmanager_install_path}/web" || Red_Error "[x] Failed to enter ${mcsmanager_install_path}/web"
echo_cyan "[+] Install MCSManager-Web dependencies..."
env "$node_install_path"/bin/node "$node_install_path"/bin/npm install --registry=https://registry.npmmirror.com --production --no-fund --no-audit &>/dev/null || Red_Error "[x] Failed to npm install in ${mcsmanager_install_path}/web"
echo
echo_yellow "=============== MCSManager ==============="
echo_green "Daemon: ${mcsmanager_install_path}/daemon"
echo_green "Web: ${mcsmanager_install_path}/web"
echo_yellow "=============== MCSManager ==============="
echo
echo_green "[+] MCSManager installation success!"
chmod -R 755 "$mcsmanager_install_path"
sleep 3
}
Create_Service() {
echo_cyan "[+] Create MCSManager service..."
# 创建 MCSManager Daemon 的 init 脚本
cat <<EOF >/etc/init.d/mcsm-daemon
#!/bin/bash
### BEGIN INIT INFO
# Provides: mcsm-daemon
# Required-Start: \$local_fs \$network
# Required-Stop: \$local_fs \$network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start MCSManager Daemon
### END INIT INFO
DAEMON_PATH="${mcsmanager_install_path}/daemon"
DAEMON_NAME="app.js"
DAEMON_BIN="${node_install_path}/bin/node"
PID_FILE="\$DAEMON_PATH/mcsm-daemon.pid"
start() {
echo "Starting MCSManager Daemon..."
cd "\$DAEMON_PATH" || exit 1
nohup "\$DAEMON_BIN" "\$DAEMON_NAME" > /dev/null 2>&1 &
echo \$! > "\$PID_FILE"
echo "MCSManager Daemon started with PID \$(cat "\$PID_FILE")."
}
stop() {
echo "Stopping MCSManager Daemon..."
if [ -f "\$PID_FILE" ]; then
PID=\$(cat "\$PID_FILE")
kill "\$PID" && echo "MCSManager Daemon stopped." || echo "Failed to stop MCSManager Daemon."
rm -f "\$PID_FILE"
else
echo "PID file not found. MCSManager Daemon may not be running."
fi
}
restart() {
stop
start
}
status() {
if [ -f "\$PID_FILE" ]; then
PID=\$(cat "\$PID_FILE")
if kill -0 "\$PID" 2>/dev/null; then
echo "MCSManager Daemon is running (PID \${PID})."
else
echo "MCSManager Daemon is not running, but PID file exists."
fi
else
echo "MCSManager Daemon is not running."
fi
}
case "\$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: \$0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
EOF
# 创建 MCSManager Web 的 init 脚本
cat <<EOF >/etc/init.d/mcsm-web
#!/bin/bash
### BEGIN INIT INFO
# Provides: mcsm-web
# Required-Start: \$local_fs \$network
# Required-Stop: \$local_fs \$network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start MCSManager Web
### END INIT INFO
DAEMON_PATH="${mcsmanager_install_path}/web"
DAEMON_NAME="app.js"
DAEMON_BIN="${node_install_path}/bin/node"
PID_FILE="\$DAEMON_PATH/mcsm-web.pid"
start() {
echo "Starting MCSManager Web..."
cd "\$DAEMON_PATH" || exit 1
nohup "\$DAEMON_BIN" "\$DAEMON_NAME" > /dev/null 2>&1 &
echo \$! > "\$PID_FILE"
echo "MCSManager Web started with PID \$(cat "\$PID_FILE")."
}
stop() {
echo "Stopping MCSManager Web..."
if [ -f "\$PID_FILE" ]; then
PID=\$(cat "\$PID_FILE")
kill "\$PID" && echo "MCSManager Web stopped." || echo "Failed to stop MCSManager Web."
rm -f "\$PID_FILE"
else
echo "PID file not found. MCSManager Web may not be running."
fi
}
restart() {
stop
start
}
status() {
if [ -f "\$PID_FILE" ]; then
PID=\$(cat "\$PID_FILE")
if kill -0 "\$PID" 2>/dev/null; then
echo "MCSManager Web is running (PID \${PID})."
else
echo "MCSManager Web is not running, but PID file exists."
fi
else
echo "MCSManager Web is not running."
fi
}
case "\$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage: \$0 {start|stop|restart|status}"
exit 1
;;
esac
exit 0
EOF
# 为服务脚本设置执行权限
chmod +x /etc/init.d/mcsm-{daemon,web}
echo_green "注册成功!"
sleep 2
printf "\n\n\n\n"
}
Update_Ports() {
echo
echo_yellow "=============== 创建端口映射 ==============="
echo_green " 请前往 https://app.rainyun.com"
echo_green " 并转到您的实例页面,创建端口映射:"
echo_cyan " Web 端口: 内 23333 -> 外 任意端口"
echo_cyan " Daemon 端口: 内 24444 -> 外 任意端口"
echo_green " 并在 MCSM 启动后,转到 MCSM 的【节点】页面"
echo_cyan " 设置 localhost 节点【远程节点端口】为您的外 Daemon 端口"
echo_cyan " 设置 localhost 节点【远程节点 IP 地址】为您的游戏云实例 IP(不能为域名)"
echo_yellow "=============== 创建端口映射 ==============="
echo
}
require_install=0
Env_Inspection
# Check if node is installed
if [[ -f "$node_install_path"/bin/node ]] && [[ "$("$node_install_path"/bin/node -v)" == "$node" ]]; then
echo
echo_yellow "=============== Node.JS Version ==============="
echo_yellow " node: $("$node_install_path"/bin/node -v)"
echo_yellow " npm: v$(env "$node_install_path"/bin/node "$node_install_path"/bin/npm -v)"
echo_yellow "=============== Node.JS Version ==============="
echo
else
echo_red "[x] Node has not been installed!"
require_install=1
fi
# Check if MCSManager directory exists
if [[ -d "$mcsmanager_install_path" ]]; then
echo
echo_yellow "=============== MCSManager ==============="
echo_green "Daemon: ${mcsmanager_install_path}/daemon"
echo_green "Web: ${mcsmanager_install_path}/web"
echo_yellow "=============== MCSManager ==============="
echo
else
echo_red "[x] MCSManager has not been installed!"
require_install=1
fi
if [[ $require_install -eq 1 ]]; then
Packages_Install
Install_Node
Install_MCSManager
fi
Create_Service
if [[ $require_install -eq 1 ]]; then
Update_Ports
fi
service mcsm-daemon start
service mcsm-web start
echo_yellow "=================================================================="
echo_green "欢迎使用 MCSManager !"
echo_yellow " "
echo_cyan_n "主控网页访问地址: "
echo_yellow "http://<Your IP>:$web_port (Browser)"
echo_cyan_n "被控守护进程地址: "
echo_yellow "ws://<Your IP>:$daemon_port (Cluster)"
echo_red "默认情况下,你必须开放 $web_port 和 $daemon_port 端口才能确保面板工作正常!"
echo_yellow " "
echo_cyan "面板开关指令:"
echo_cyan "service mcsm-daemon start"
echo_cyan "service mcsm-daemon stop"
echo_cyan "service mcsm-daemon restart"
echo_cyan "service mcsm-web start"
echo_cyan "service mcsm-web stop"
echo_cyan "service mcsm-web restart"
echo_yellow " "
echo_green "官方文档: https://docs.mcsmanager.com/"
echo_yellow "=================================================================="
echo
echo_yellow "=============== OK ==============="
echo_green " MCSManager 现在正在运行"
echo_green " 如您需要关闭游戏云实例,请使用 exit 退出当前终端"
echo_green " 请将【终端设置 > 关闭实例命令】改为 \"exit\""
echo_yellow "=============== OK ==============="
echo
cd $container_root
bash
echo_yellow "[+] 关闭 MCSManager 服务..."
service mcsm-daemon stop
service mcsm-web stop
exit 0
Bash展开
将 bash 脚本存入 “mcsm.sh
”,并修改权限为 755。
编辑 “启动脚本(可修改内容).sh” 为 `bash ./mcsm.sh` 即可。
这下就可以在一个 1 核 2G 的游戏云里同时跑 Velocity 和大厅了。