feat: add prefixed service initializer and packaged deployment

This commit is contained in:
shiran
2026-09-09 11:00:31 +08:00
parent 1e2366998e
commit 4da89b784e
9 changed files with 355 additions and 40 deletions
+33 -16
View File
@@ -1,11 +1,23 @@
#!/bin/bash
set -e
set -euo pipefail
APP_DIR="$(cd "$(dirname "$0")" && pwd)"
DEPLOY_DIR="$APP_DIR/deploy"
SERVICE_DIR="/etc/systemd/system"
LOG_DIR="$APP_DIR/logs"
SERVICES="server cli scheduler"
PREFIX_FILE="$APP_DIR/.service-prefix"
SERVICE_SUFFIXES=(server cli scheduler)
if [ ! -f "$PREFIX_FILE" ]; then
echo "缺少 $PREFIX_FILE,请先运行 initializer 完成初始化"
exit 1
fi
SERVICE_PREFIX="$(tr -d '\r\n' < "$PREFIX_FILE")"
if [[ ! "$SERVICE_PREFIX" =~ ^[A-Za-z0-9][A-Za-z0-9_.-]*$ ]]; then
echo "无效的服务前缀: $SERVICE_PREFIX"
exit 1
fi
if [ "$(id -u)" -ne 0 ]; then
echo "请使用 root 权限运行: sudo bash install.sh"
@@ -16,27 +28,32 @@ mkdir -p "$LOG_DIR"
echo "=== 安装 systemd 服务 ==="
for svc in $SERVICES; do
for svc in "${SERVICE_SUFFIXES[@]}"; do
src="$DEPLOY_DIR/${svc}.service"
service_name="${SERVICE_PREFIX}-${svc}"
if [ ! -f "$src" ]; then
echo "[${svc}] service 文件不存在: $src"
continue
exit 1
fi
sed "s|WorkingDirectory=/root|WorkingDirectory=$APP_DIR|g; \
s|ExecStart=/root/|ExecStart=$APP_DIR/|g; \
s|EnvironmentFile=-/root/.env|EnvironmentFile=-$APP_DIR/.env|g; \
s|/root/logs/|$LOG_DIR/|g" \
"$src" > "$SERVICE_DIR/${svc}.service"
escaped_app_dir="$(printf '%s' "$APP_DIR" | sed 's/[&|\\]/\\&/g')"
sed "s|/root|$escaped_app_dir|g" "$src" > "$SERVICE_DIR/${service_name}.service"
echo "[${svc}] 已安装到 $SERVICE_DIR/${svc}.service"
echo "[${service_name}] 已安装到 $SERVICE_DIR/${service_name}.service"
done
systemctl daemon-reload
for svc in $SERVICES; do
systemctl enable "$svc"
echo "[${svc}] 已设置开机自启"
for svc in "${SERVICE_SUFFIXES[@]}"; do
service_name="${SERVICE_PREFIX}-${svc}"
systemctl enable "$service_name"
echo "[${service_name}] 已设置开机自启"
done
for binary in server cli scheduler; do
if [ -f "$APP_DIR/$binary" ]; then
chmod +x "$APP_DIR/$binary"
fi
done
echo ""
@@ -45,6 +62,6 @@ echo "使用方式:"
echo " bash start.sh # 启动全部"
echo " bash stop.sh # 停止全部"
echo " bash restart.sh # 重启全部"
echo " systemctl status server # 查看主服务状态"
echo " systemctl status scheduler # 查看调度器状态"
echo " journalctl -u scheduler -f # 查看调度器实时日志"
echo " systemctl status ${SERVICE_PREFIX}-server # 查看主服务状态"
echo " systemctl status ${SERVICE_PREFIX}-scheduler # 查看调度器状态"
echo " journalctl -u ${SERVICE_PREFIX}-scheduler -f # 查看调度器实时日志"