#!/usr/bin/env python3 """Upgrade dylive.nomadro.com VPS /opt/douyinWEB to origin/test1. Preserves monitor.db.""" from __future__ import annotations import sys import time from pathlib import Path sys.stdout.reconfigure(encoding="utf-8", errors="replace") sys.path.insert(0, str(Path(__file__).resolve().parents[1])) import paramiko HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" REPO = "/opt/douyinWEB" REMOTE = "https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git" BRANCH = "test1" def run(c: paramiko.SSHClient, cmd: str, timeout: int = 600) -> tuple[int, str]: print(f"\n$ {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[-5000:] if len(out) > 5000 else out, flush=True) print(f"→ exit={code}", flush=True) return code, out def main() -> int: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, username=USER, password=PASSWORD, timeout=45, allow_agent=False, look_for_keys=False) try: ts = time.strftime("%Y%m%d_%H%M%S") run( c, f"set -e; cd {REPO}; " f"cp -a backend/data/monitor.db backend/data/monitor.db.bak.{ts} 2>/dev/null || true; " f"ls -la backend/data/monitor.db*", ) # Force sync to origin/test1; discard dirty VPS tree; keep backend/data + venv + node_modules code, _ = run( c, f"set -e; cd {REPO}; " f"git remote set-url origin '{REMOTE}'; " f"git fetch origin {BRANCH}; " f"git reset --hard HEAD; " 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-*'; " f"git checkout -f -B {BRANCH} origin/{BRANCH}; " f"git reset --hard 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-*'; " f"git log -3 --oneline; git status -sb", timeout=300, ) if code != 0: print("FATAL: git update failed", flush=True) return 1 # Python deps (best-effort) run( c, f"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) || " f" pip3 install -r requirements.txt -q || true; " f"fi; echo deps_done", timeout=300, ) run(c, "systemctl restart douyinweb-api douyinweb-pb douyinweb-yt 2>/dev/null || systemctl restart douyinweb-api; sleep 2; systemctl is-active douyinweb-api", timeout=60) # Frontend build code, _ = run( c, f"set -e; cd {REPO}/frontend; " f"npm ci --prefer-offline 2>/dev/null || npm install; " f"npm run build", timeout=900, ) if code != 0: print("FATAL: frontend build failed", flush=True) return 1 run(c, "systemctl restart douyinweb-web; sleep 2; systemctl is-active douyinweb-api douyinweb-web", timeout=60) # Verify health + public site run( c, "curl -sS -m 15 http://127.0.0.1:8014/api/health; echo; " "curl -sS -m 20 -o /dev/null -w 'dylive_http=%{http_code}\\n' https://dylive.nomadro.com/api/health; " "curl -sS -m 20 -o /dev/null -w 'dylive_home=%{http_code}\\n' https://dylive.nomadro.com/; " f"cd {REPO}; git rev-parse --short HEAD; " f"test -f backend/data/monitor.db && echo monitor_db=ok || echo monitor_db=MISSING", timeout=60, ) print("\nDONE dylive upgrade", flush=True) return 0 finally: c.close() if __name__ == "__main__": raise SystemExit(main())