#!/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 " 当前版本: v$(cat VERSION 2>/dev/null | tr -d '[:space:]' || echo dev) ($(git rev-parse --short HEAD 2>/dev/null || echo unknown))" 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 "=========================================="