From f3e700f3f4089f9d3314bbfd44541fa5557b43a2 Mon Sep 17 00:00:00 2001 From: gouki Date: Sun, 2 Aug 2026 22:12:02 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=B8=80=E9=94=AE=E9=83=A8=E7=BD=B2?= =?UTF-8?q?=E8=84=9A=E6=9C=ACsetup.sh=EF=BC=88=E6=94=AF=E6=8C=81=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E5=88=B0=E6=96=B0=E6=9C=8D=E5=8A=A1=E5=99=A8=EF=BC=8C?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E7=94=9F=E6=88=90.env+=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E5=85=A8=E9=83=A8timer=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deploy/setup.sh | 92 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 deploy/setup.sh diff --git a/deploy/setup.sh b/deploy/setup.sh new file mode 100755 index 0000000..47c0e40 --- /dev/null +++ b/deploy/setup.sh @@ -0,0 +1,92 @@ +#!/usr/bin/env bash +# vps-manager 一键部署脚本(在新服务器上运行) +# +# 用法(在新服务器以 root 运行): +# sudo bash setup.sh +# 可选环境变量: +# GITEA_REPO 代码仓库地址(默认 git@gitea.neatcn.com:gouki/vps-manager.git) +# APP_DIR 部署目录(默认 /opt/vps-manager) +# +# 前置要求: +# 1. 新服务器已安装 tailscale 并连入 tailnet(用于内网访问) +# 2. 新服务器已配置可访问 Gitea 的 SSH deploy key(读权限), +# 或将 GITEA_REPO 改为 HTTPS + token 形式 +# +# 脚本会自动完成: +# 系统依赖 -> 克隆/更新代码 -> venv + Python 依赖 -> 生成 .env(含 MASTER_KEY) +# -> 配置 systemd 服务与全部 timer -> 启动服务 + +set -euo pipefail + +APP_DIR="${APP_DIR:-/opt/vps-manager}" +GITEA_REPO="${GITEA_REPO:-git@gitea.neatcn.com:gouki/vps-manager.git}" + +echo "==> [1/6] 安装系统依赖" +apt-get update -q +apt-get install -y -q python3 python3-venv git curl + +echo "==> [2/6] 克隆 / 更新代码到 $APP_DIR" +if [ ! -d "$APP_DIR/.git" ]; then + git clone "$GITEA_REPO" "$APP_DIR" +fi +cd "$APP_DIR" +git pull --ff-only + +echo "==> [3/6] 创建虚拟环境并安装 Python 依赖" +python3 -m venv .venv +.venv/bin/pip install -q --upgrade pip +.venv/bin/pip install -q -r requirements.txt + +echo "==> [4/6] 生成 .env(若不存在)" +if [ ! -f .env ]; then + MASTER_KEY="$(.venv/bin/python -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())")" + cat > .env < [5/6] 配置 systemd 服务与定时器" +cp deploy/vps-manager.service /etc/systemd/system/ +cp deploy/vps-manager-update.service deploy/vps-manager-update.timer /etc/systemd/system/ +cp deploy/vps-backup-assets.service deploy/vps-backup-assets.timer /etc/systemd/system/ +cp deploy/vps-backup-metrics.service deploy/vps-backup-metrics.timer /etc/systemd/system/ +cp deploy/vps-sync-ai.service deploy/vps-sync-ai.timer /etc/systemd/system/ +cp deploy/vps-renewal-check.service deploy/vps-renewal-check.timer /etc/systemd/system/ +systemctl daemon-reload +systemctl enable --now vps-manager +systemctl enable --now vps-manager-update.timer +systemctl enable --now vps-backup-assets.timer vps-backup-metrics.timer +systemctl enable --now vps-sync-ai.timer vps-renewal-check.timer + +echo "==> [6/6] 验证服务状态" +sleep 3 +if systemctl is-active --quiet vps-manager; then + echo " vps-manager 服务运行正常" +else + echo " 警告:服务未正常运行,请检查 journalctl -u vps-manager" +fi + +echo "" +echo "==========================================" +echo "部署完成!" +echo " 本地访问:http://127.0.0.1:8000" +echo " Tailscale 内网:http://<本机tailscale_ip>:8000" +echo " 如需 PWA/HTTPS:tailscale serve --bg --https=443 http://127.0.0.1:8000" +echo " 查看日志:journalctl -u vps-manager -f" +echo "=========================================="