Rust_ServerStatus/one-touch.sh

90 lines
2.4 KiB
Bash

#!/bin/bash
# set rust_serverstatus
set -ex
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# step 0: prepare
OS_ARCH=${1}
WORKSPACE=${2}
passwd=${3}
uid=${4}
server_address=${5}
user_name=${6}
test_or_not=${7}
mkdir -p ${WORKSPACE}
cd ${WORKSPACE}
latest_version=$(curl -m 10 -sL "https://api.github.com/repos/zdz/ServerStatus-Rust/releases/latest" | grep "tag_name" | head -n 1 | awk -F ":" '{print $2}' | sed 's/\"//g;s/,//g;s/ //g')
# step 1: 下载和解压客户端
wget --no-check-certificate -qO "client-${OS_ARCH}-unknown-linux-musl.zip" "https://github.com/zdz/ServerStatus-Rust/releases/download/${latest_version}/client-${OS_ARCH}-unknown-linux-musl.zip"
unzip -o "client-${OS_ARCH}-unknown-linux-musl.zip"
rm "client-${OS_ARCH}-unknown-linux-musl.zip"
# client systemd service
cat > /etc/systemd/system/stat_client.service <<EOF
[Unit]
Description=ServerStatus-Rust Client
After=network.target
[Service]
User=${user_name}
Group=${user_name}
Environment="RUST_BACKTRACE=1"
WorkingDirectory=${WORKSPACE}
# EnvironmentFile=~/myserve/serverstatus/.env
ExecStart=${WORKSPACE}/stat_client -a "${server_address}" -u ${uid} -p ${passwd}
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/stat_client.service
# journalctl -u stat_client -f -n 100
EOF
systemctl daemon-reload
systemctl enable --now stat_client
systemctl status stat_client
# 判断是否部署服务端,不部署就直接退出
if [[ -z ${test_or_not} ]]; then
exit 1
fi
# 下载和解压服务端
wget --no-check-certificate -qO "server-${OS_ARCH}-unknown-linux-musl.zip" "https://github.com/zdz/ServerStatus-Rust/releases/download/${latest_version}/server-${OS_ARCH}-unknown-linux-musl.zip"
unzip -o "server-${OS_ARCH}-unknown-linux-musl.zip"
rm "server-${OS_ARCH}-unknown-linux-musl.zip"
# server systemd service
cat > /etc/systemd/system/stat_server.service <<EOF
[Unit]
Description=ServerStatus-Rust Server
After=network.target
[Service]
User=${user_name}
Group=${user_name}
Environment="RUST_BACKTRACE=1"
WorkingDirectory=${WORKSPACE}
ExecStart=${WORKSPACE}/stat_server -c ${WORKSPACE}/config.toml
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
# /etc/systemd/system/stat_server.service
EOF
systemctl daemon-reload
systemctl enable --now stat_server
systemctl status stat_server