# -*- coding: utf-8 -*- """Switch server repo to dev branch without touching monitor.db or data files.""" import paramiko, time HOST = "107.173.30.245" USER = "root" PASSWORD = "Xiao4669805" REPO = "/opt/douyinWEB" DB_PATH = f"{REPO}/backend/data/monitor.db" def run(c, cmd, timeout=120): _s, out, err = c.exec_command(cmd, timeout=timeout) o = out.read().decode("utf-8", "replace").strip() e = err.read().decode("utf-8", "replace").strip() return o, e def main(): 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) # 1. backup monitor.db print("== backup DB ==") o, _ = run(c, f"cp -f {DB_PATH} /tmp/monitor.db.dev_switch_bak && echo ok") print(o) # 2. stop services (fire-and-forget) print("== stop services ==") c.exec_command("systemctl stop douyinweb-api douyinweb-web >/dev/null 2>&1 || true", timeout=5) time.sleep(4) # 3. resolve conflicts / hard reset to origin/main first print("== git fetch origin ==") o, e = run(c, f"cd {REPO} && git fetch origin 2>&1", timeout=60) print(o or e) # 4. hard reset working tree (keeps untracked files like monitor.db) print("== git reset --hard origin/main ==") o, e = run(c, f"cd {REPO} && git reset --hard origin/main 2>&1", timeout=60) print(o or e) # 5. checkout dev print("== git checkout dev ==") o, e = run(c, f"cd {REPO} && git checkout dev 2>&1", timeout=60) print(o or e) # 6. restore monitor.db (git checkout may have touched it) print("== restore DB ==") o, _ = run(c, f"cp -f /tmp/monitor.db.dev_switch_bak {DB_PATH} && echo ok") print(o) # 7. verify DB print("== DB counts ==") o, _ = 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])\"" )) print(o) # 8. restart API print("== restart API ==") c.exec_command("systemctl restart douyinweb-api >/dev/null 2>&1", timeout=5) time.sleep(6) o, _ = run(c, "systemctl is-active douyinweb-api") print("api:", o) # 9. rebuild frontend print("== npm run build (wait ~2min) ==") o, e = run(c, f"cd {REPO}/frontend && npm run build 2>&1 | tail -20", timeout=360) print(o.encode("ascii", "replace").decode()) # 10. restart web print("== restart web ==") c.exec_command("systemctl restart douyinweb-web >/dev/null 2>&1", timeout=5) time.sleep(4) o, _ = run(c, "systemctl is-active douyinweb-web") print("web:", o) # 11. show current branch o, _ = run(c, f"cd {REPO} && git branch --show-current") print("server branch:", o) c.close() print("DONE") if __name__ == "__main__": main()