# -*- coding: utf-8 -*- import paramiko HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" DB_PATH = "/opt/douyinWEB/backend/data/monitor.db" STASH_REF = "stash@{1}" def ssh(c: paramiko.SSHClient, cmd: str, timeout: int = 120) -> 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("== stop services ==") out, err = ssh( c, "systemctl stop douyinweb-api douyinweb-web douyinweb-pb >/dev/null 2>&1 || true", timeout=120, ) if err: print("stop err:", err[:200]) print("== backup current monitor.db ==") out, err = ssh( c, "cp -f /opt/douyinWEB/backend/data/monitor.db /tmp/monitor.db.backup_current_$(date +%s) 2>/dev/null || true", timeout=60, ) if err: print("backup err:", err[:200]) print(f"== git stash apply --index {STASH_REF} ==") out, err = ssh( c, f"cd /opt/douyinWEB && git stash apply --index {STASH_REF} >/dev/null 2>&1 || git stash apply {STASH_REF} >/dev/null 2>&1 || true", timeout=240, ) if out: print(out) if err: print("apply err:", err[:300]) print("== restart services ==") out, err = ssh( c, "systemctl restart douyinweb-api douyinweb-web douyinweb-pb >/dev/null 2>&1 || true; sleep 3", timeout=240, ) if out: print(out) if err: print("restart err:", err[:200]) print("== verify counts ==") verify_cmd = ( "python3 -c \"import sqlite3, json; " f"conn=sqlite3.connect('{DB_PATH}'); " "tables=['blacklist','rooms','sessions','events','channel_groups','shorts_channels','shorts_jobs']; " "out={}; " "for t in tables: " " exists=conn.execute(\"SELECT 1 FROM sqlite_master WHERE type='table' AND name=?\", (t,)).fetchone(); " " out[t]=int(conn.execute('SELECT count(*) FROM ' + t).fetchone()[0]) if exists else None; " "print(json.dumps(out, ensure_ascii=False));\"" ) out, err = ssh(c, verify_cmd, timeout=60) print(out) if err: print("verify err:", err[:200]) c.close() print("DONE") if __name__ == "__main__": main()