#!/usr/bin/env python3 """从本机触发 VPS 部署:SSH + Git pull (eric) + build + pm2""" import paramiko import sys HOST = "107.173.30.245" SSH_USER = "root" SSH_PASS = "Xiao4669805" GIT_USER = "eric" GIT_PASS = "Xiao4669805" REMOTE = f"""set -e export DEBIAN_FRONTEND=noninteractive export GIT_USER={GIT_USER} export GIT_PASS={GIT_PASS} export NEXT_PUBLIC_SITE_URL=https://book.nomadro.com export NEXT_PUBLIC_SHARE_CAMPAIGN=nomad-code echo '=== [1/4] system deps ===' apt-get update -qq apt-get install -y -qq git curl ca-certificates python3 python3-pip python3-venv unzip wget >/dev/null 2>&1 || true echo '=== [2/4] git pull + build (server-side, ~3 min) ===' export GIT_PASS={GIT_PASS} if [ -f /var/www/ebook/deploy/install.sh ] && [ -d /var/www/ebook/.git ]; then bash /var/www/ebook/deploy/install.sh else rm -rf /var/www/ebook mkdir -p /var/www git clone -b dev1 https://{GIT_USER}:{GIT_PASS}@gitea.dsx2020.com/eric/ebook.git /var/www/ebook bash /var/www/ebook/deploy/install.sh fi echo '=== [3/4] caddy reverse proxy ===' mkdir -p /etc/caddy/sites cp /var/www/ebook/deploy/caddy-book.caddy /etc/caddy/sites/book.nomadro.com.caddy rm -f /etc/caddy/sites/book.caddy caddy validate --config /etc/caddy/Caddyfile systemctl reload caddy sleep 2 echo '=== [4/4] health checks ===' curl -sf -o /dev/null -w 'nextjs:%{{http_code}}\\n' http://127.0.0.1:3022/ || true curl -sf -o /dev/null -w 'fastapi:%{{http_code}}\\n' http://127.0.0.1:8010/api/health || true curl -sf -o /dev/null -w 'pocketbase:%{{http_code}}\\n' http://127.0.0.1:8090/api/health || true curl -sf -o /dev/null -w 'caddy:%{{http_code}}\\n' -H 'Host: book.nomadro.com' http://127.0.0.1/ || true pm2 status || true systemctl status ebook-api --no-pager -l || true systemctl status pocketbase --no-pager -l || true echo '=== DEPLOY DONE ===' """ def main(): if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8", errors="replace") client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) print(f"SSH -> {HOST} | Git -> eric@gitea (dev1)") client.connect(HOST, username=SSH_USER, password=SSH_PASS, timeout=30, banner_timeout=60) print("Connected. Deploying via git pull...") _, stdout, stderr = client.exec_command(REMOTE, get_pty=True) for line in iter(stdout.readline, ""): print(line, end="", flush=True) err = stderr.read().decode("utf-8", errors="replace") if err.strip(): print("STDERR:", err) code = stdout.channel.recv_exit_status() client.close() sys.exit(code) if __name__ == "__main__": main()