# -*- coding: utf-8 -*- """Force reset server to latest git HEAD, restart services, rebuild frontend.""" import paramiko import time import sys HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" REPO = "/opt/douyinWEB" DB_PATH = f"{REPO}/backend/data/monitor.db" def run(c: paramiko.SSHClient, cmd: str, timeout: int = 300) -> tuple[str, str]: _stdin, stdout, stderr = c.exec_command(cmd, timeout=timeout) out = stdout.read().decode("utf-8", "replace").strip() err = stderr.read().decode("utf-8", "replace").strip() return out, err def main() -> None: c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(HOST, 22, USER, PASSWORD, timeout=30, allow_agent=False, look_for_keys=False) print("== backup monitor.db ==") out, _ = run(c, f"cp -f {DB_PATH} /tmp/monitor.db.bak_reset_$(date +%s) 2>/dev/null; echo ok", timeout=30) print(out) print("== stop services ==") # fire-and-forget; don't wait for stdout to avoid pipe timeout c.exec_command("systemctl stop douyinweb-api douyinweb-web >/dev/null 2>&1 || true", timeout=5) time.sleep(4) # Resolve conflicts: mark all UU files resolved by taking 'theirs' (remote HEAD) print("== git fetch + reset to origin/main ==") out, err = run(c, f"cd {REPO} && git fetch origin 2>&1", timeout=60) print(out) # Hard reset to remote HEAD (keeps untracked files like monitor.db) out, err = run(c, f"cd {REPO} && git reset --hard origin/main 2>&1", timeout=60) print(out) if err: print("ERR:", err[:300]) print("== git status ==") out, _ = run(c, f"cd {REPO} && git status --short 2>&1", timeout=30) print(out[:400]) # Restore monitor.db from backup (git reset may have clobbered it from git history) print("== restore monitor.db ==") out, _ = run(c, f"cp -f /tmp/monitor.db.bak_reset_$(ls -t /tmp/monitor.db.bak_reset_* 2>/dev/null | head -1 | xargs basename 2>/dev/null | sed 's/monitor.db.bak_reset_//') {DB_PATH} 2>/dev/null; echo done", timeout=30) # simpler: just find latest backup out, _ = run(c, "ls -t /tmp/monitor.db.bak_reset_* 2>/dev/null | head -1", timeout=15) backup = out.strip() if backup: print(f"restoring from {backup}") out, err = run(c, f"cp -f '{backup}' {DB_PATH}; echo ok", timeout=30) print(out) else: print("no backup found, leaving monitor.db as-is") # Verify db out, _ = run(c, ( "python3 -c \"" f"import sqlite3; conn=sqlite3.connect('{DB_PATH}'); " "print('blacklist', conn.execute('select count(*) from blacklist').fetchone()[0]); " "print('rooms', conn.execute('select count(*) from rooms').fetchone()[0]); " "print('shorts_channels', conn.execute('select count(*) from shorts_channels').fetchone()[0])\"" ), timeout=30) print("DB counts:", out) # Restart API print("== restart API ==") c.exec_command("systemctl restart douyinweb-api >/dev/null 2>&1", timeout=10) time.sleep(6) out, _ = run(c, "systemctl is-active douyinweb-api", timeout=15) print("api:", out) # Rebuild frontend print("== rebuild frontend (wait ~2min) ==") out, err = run(c, f"cd {REPO}/frontend && npm run build 2>&1 | tail -20", timeout=360) # write to file to avoid encoding issues with open("/tmp/build_out.txt", "w", encoding="utf-8", errors="replace") as f: f.write(out) print(out.encode("ascii", "replace").decode()) if err: print("ERR:", err[:200].encode("ascii", "replace").decode()) # Restart web print("== restart web ==") c.exec_command("systemctl restart douyinweb-web >/dev/null 2>&1", timeout=10) time.sleep(4) out, _ = run(c, "systemctl is-active douyinweb-web", timeout=15) print("web:", out) c.close() print("ALL DONE") if __name__ == "__main__": main()