#!/usr/bin/env python3 """Push-synced deploy: git reset to origin/test1 on dylive VPS + physical guest. Preserves monitor.db. Restarts api/web only — does NOT recreate Neko/Chrome. """ from __future__ import annotations import sys import time from pathlib import Path sys.stdout.reconfigure(encoding="utf-8", errors="replace") import paramiko REMOTE = "https://eric:Xiao4669805@gitea.dsx2020.com/eric/douyinWEB.git" BRANCH = "test1" REPO = "/opt/douyinWEB" # dylive colocated on overseas VPS DYLIVE = ("107.173.30.245", 22, "root", "Xiao4669805") # physical app guest via FRP PHYSICAL = ("107.173.30.245", 4014, "eric", "4669805") GUEST_SUDO = "4669805" 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[:200]}{'…' if len(cmd) > 200 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[-6000:] if len(out) > 6000 else out, flush=True) print(f"{tag}→ exit={code}", flush=True) return code, out def git_sync(c: paramiko.SSHClient, *, sudo_prefix: str, label: str) -> int: ts = time.strftime("%Y%m%d_%H%M%S") # backup db run( c, f"{sudo_prefix}bash -lc 'cd {REPO} && cp -a backend/data/monitor.db backend/data/monitor.db.bak.{ts} 2>/dev/null || true && ls -la backend/data/monitor.db | head -1'", timeout=60, label=label, ) code, _ = run( c, f"{sudo_prefix}bash -lc 'set -e; cd {REPO}; " # Tarball physical installs may lack .git — bootstrap tracking without wiping data/. 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 reset --hard HEAD 2>/dev/null || true; " 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 log -1 --oneline; git status -sb'", timeout=300, label=label, ) return code def deps_and_restart(c: paramiko.SSHClient, *, sudo_prefix: str, label: str, public_health: str) -> int: run( c, f"{sudo_prefix}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; echo deps_ok'", timeout=300, label=label, ) run( c, f"{sudo_prefix}bash -lc 'systemctl restart douyinweb-api douyinweb-pb douyinweb-yt 2>/dev/null || systemctl restart douyinweb-api; sleep 2; systemctl is-active douyinweb-api || true'", timeout=90, label=label, ) code, _ = run( c, f"{sudo_prefix}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, f"{sudo_prefix}bash -lc 'systemctl restart douyinweb-web; sleep 2; systemctl is-active douyinweb-api douyinweb-web'", timeout=90, label=label, ) run( c, f"bash -lc 'curl -sS -m 15 http://127.0.0.1:8014/api/health | head -c 400; echo; " 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, *, label: str, public_health: str, use_sudo: bool) -> int: host, port, user, pw = spec print(f"\n======== {label} {host}:{port} ========", flush=True) c = connect(host, port, user, pw) try: if use_sudo: sudo_prefix = f"echo '{GUEST_SUDO}' | sudo -S -p '' " else: sudo_prefix = "" if git_sync(c, sudo_prefix=sudo_prefix, label=label) != 0: print(f"FATAL: {label} git sync failed", flush=True) return 1 if deps_and_restart(c, sudo_prefix=sudo_prefix, 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", use_sudo=False) != 0: rc = 1 if deploy_host(PHYSICAL, label="physical", public_health="https://dy.nomadro.com/api/health", use_sudo=True) != 0: rc = 1 print(f"\nDONE rc={rc}", flush=True) return rc if __name__ == "__main__": raise SystemExit(main())