server-status-client/deploy-cn.sh

96 lines
2.4 KiB
Bash
Raw Permalink Normal View History

2024-10-05 22:50:41 +08:00
#!/bin/bash
# 部署脚本中国大陆可用版本
# check if sudo is used
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit 1
fi
# check install dir
install_dir="/opt"
2024-10-06 02:17:41 +08:00
echo -n "安装目录? (默认: $install_dir/server-status-client): " && read -r install_dir_input
2024-10-05 22:50:41 +08:00
if [ -n "$install_dir_input" ]; then
install_dir="$install_dir_input"
fi
# check server
2024-10-06 02:17:41 +08:00
echo -n "服务端地址? (必须): " && read -r server
2024-10-05 22:50:41 +08:00
if [ -z "$server" ]; then
2024-10-06 02:17:41 +08:00
echo "服务端地址是必须的"
2024-10-05 22:50:41 +08:00
exit 1
fi
# check token
2024-10-06 02:17:41 +08:00
echo -n "令牌? (必须或留空): " && read -r token
2024-10-05 22:50:41 +08:00
# check hostname
hostname=$(hostname)
2024-10-06 02:17:41 +08:00
echo -n "此主机名? (默认: $hostname): " && read -r hostname_input
2024-10-05 22:50:41 +08:00
if [ -n "$hostname_input" ]; then
hostname="$hostname_input"
fi
# labels
2024-10-06 02:17:41 +08:00
echo -n "标签们? (空格分隔): " && read -r labels_input
2024-10-05 22:50:41 +08:00
if [ -n "$labels_input" ]; then
labels="$labels_input"
fi
# location
2024-10-06 02:17:41 +08:00
echo -n "地理位置? (可选|自定义): " && read -r location_input
2024-10-05 22:50:41 +08:00
if [ -n "$location_input" ]; then
location="$location_input"
fi
repo2="https://git.liteyuki.icu/snowykami/server-status-client"
# try 1 if failed try 2
git clone "$repo2" "$install_dir/server-status-client"
2024-10-06 02:17:41 +08:00
cd "$install_dir/server-status-client" || { echo "克隆失败"; exit 1; }
2024-10-05 22:50:41 +08:00
# create venv
python3 -m venv venv
python_exe="./venv/bin/python"
# check if venv is created
if [ ! -f "$python_exe" ]; then
2024-10-06 02:17:41 +08:00
echo "创建虚拟环境失败"
2024-10-05 22:50:41 +08:00
exit 1
fi
2024-10-06 02:17:41 +08:00
echo "虚拟环境创建成功"
2024-10-05 22:50:41 +08:00
# install the required packages
2024-10-06 02:17:41 +08:00
echo "正在安装依赖包..."
2024-10-05 23:01:17 +08:00
$python_exe -m pip install pdm -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple
2024-10-05 22:50:41 +08:00
$python_exe -m pdm install
# create the systemd service
2024-10-06 02:17:41 +08:00
echo "正在创建服务..."
2024-10-05 22:50:41 +08:00
# generate random id
# shellcheck disable=SC2002
2024-10-05 23:08:40 +08:00
id=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
2024-10-05 22:50:41 +08:00
bash -c "cat <<EOF > /etc/systemd/system/server-status-client.service
[Unit]
Description=Server Status Client
After=network-online.target
[Service]
Type=simple
ExecStart=$install_dir/server-status-client/venv/bin/python main.py $server $token $id run -n $hostname --labels $labels --location $location
WorkingDirectory=$install_dir/server-status-client
Restart=on-failure
RestartSec=10
[Install]
WantedBy=multi-user.target
EOF"
# enable and start the service
systemctl enable server-status-client
systemctl start server-status-client
2024-10-06 02:17:41 +08:00
echo "安装完成,服务已启动"