#!/usr/bin/env python3 """Git-sync deploy: reset both VPS apps to origin/test2. Preserves monitor.db / pb_data / .venv / frontend/.next build inputs. Restarts api/web only — does NOT recreate Neko/Chrome. """ from __future__ import annotations import sys import time sys.stdout.reconfigure(encoding="utf-8", errors="replace") import paramiko REMOTE = "https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git" BRANCH = "test2" REPO = "/opt/douyinWEB" PASS = "Xiao4669805" # VPS-A: dylive.nomadro.com DYLIVE = ("107.173.30.245", 22, "root", PASS) # VPS-B: dy.nomadro.live (direct SSH; not FRP/physical) DY = ("108.174.50.68", 22, "root", PASS) def connect(host: str, port: int, user: str, password: str) -> paramiko.SSHClient: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect( host, port, user, password, timeout=60, banner_timeout=120, allow_agent=False, look_for_keys=False, ) return c def run(c: paramiko.SSHClient, cmd: str, timeout: int = 600, label: str = "") -> tuple[int, str]: tag = f"[{label}] " if label else "" print(f"\n{tag}$ {cmd[:220]}{'…' if len(cmd) > 220 else ''}", flush=True) _i, o, e = c.exec_command(cmd, timeout=timeout) code = o.channel.recv_exit_status() out = (o.read() + e.read()).decode("utf-8", "replace") if out.strip(): print(out[-8000:] if len(out) > 8000 else out, flush=True) print(f"{tag}→ exit={code}", flush=True) return code, out def git_sync(c: paramiko.SSHClient, *, label: str) -> int: ts = time.strftime("%Y%m%d_%H%M%S") run( c, f"bash -lc 'cd {REPO} && cp -a backend/data/monitor.db backend/data/monitor.db.bak.{ts} 2>/dev/null || true && " f"ls -la backend/data/monitor.db 2>/dev/null | head -1 || echo no_monitor_db'", timeout=60, label=label, ) code, _ = run( c, f"bash -lc 'set -e; cd {REPO}; " f"if [ ! -d .git ]; then git init -b {BRANCH}; fi; " f"git remote remove origin 2>/dev/null || true; " f"git remote add origin \"{REMOTE}\"; " f"git fetch origin {BRANCH}; " f"git clean -fd -e backend/data -e frontend/.next -e node_modules -e frontend/node_modules " f"-e backend/.venv -e .venv -e \"*.db\" -e \"*.db-*\" -e pb_data -e backend/pb_data; " f"git checkout -f -B {BRANCH} origin/{BRANCH}; " f"git reset --hard origin/{BRANCH}; " f"git log -1 --oneline; git status -sb'", timeout=300, label=label, ) return code def deps_and_restart(c: paramiko.SSHClient, *, label: str, public_health: str) -> int: run( c, f"bash -lc 'cd {REPO}/backend; " f"if [ -f requirements.txt ]; then " f" (test -x .venv/bin/pip && .venv/bin/pip install -r requirements.txt -q) || " f" (test -x ../.venv/bin/pip && ../.venv/bin/pip install -r requirements.txt -q) || true; " f"fi; " f".venv/bin/python -m py_compile app/main.py app/procutil.py app/host_metrics.py app/yt_revenue.py 2>/dev/null || true; " f"echo deps_ok'", timeout=300, label=label, ) run( c, "bash -lc 'systemctl restart douyinweb-api; " "systemctl restart douyinweb-pb douyinweb-yt 2>/dev/null || true; " "sleep 3; systemctl is-active douyinweb-api || true'", timeout=90, label=label, ) code, _ = run( c, f"bash -lc 'set -e; cd {REPO}/frontend; npm ci --prefer-offline 2>/dev/null || npm install; npm run build'", timeout=900, label=label, ) if code != 0: return code run( c, "bash -lc 'systemctl restart douyinweb-web; sleep 2; " "systemctl is-active douyinweb-api douyinweb-web; " "systemctl try-restart douyinweb-api-watch 2>/dev/null || true'", timeout=90, label=label, ) run( c, f"bash -lc 'curl -sS -m 12 -o /dev/null -w \"local_health=%{{http_code}} t=%{{time_total}}\\n\" " f"http://127.0.0.1:8014/api/health; " f"curl -sS -m 20 -o /dev/null -w \"public=%{{http_code}}\\n\" {public_health}; " f"cd {REPO}; git rev-parse --short HEAD; " f"test -f backend/data/monitor.db && echo monitor_db=ok'", timeout=60, label=label, ) return 0 def deploy_host( spec: tuple[str, int, str, str], *, label: str, public_health: str, ) -> int: host, port, user, pw = spec print(f"\n======== {label} {host}:{port} ========", flush=True) c = connect(host, port, user, pw) try: if git_sync(c, label=label) != 0: print(f"FATAL: {label} git sync failed", flush=True) return 1 if deps_and_restart(c, label=label, public_health=public_health) != 0: print(f"FATAL: {label} build/restart failed", flush=True) return 1 print(f"OK {label}", flush=True) return 0 finally: c.close() def main() -> int: rc = 0 if ( deploy_host( DYLIVE, label="dylive", public_health="https://dylive.nomadro.com/api/health", ) != 0 ): rc = 1 if ( deploy_host( DY, label="dy.live", public_health="https://dy.nomadro.live/api/health", ) != 0 ): rc = 1 print(f"\nDONE rc={rc}", flush=True) return rc if __name__ == "__main__": raise SystemExit(main())