#!/usr/bin/env python3 # -*- coding: utf-8 -*- from __future__ import annotations import sys import time from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") ROOT = Path(__file__).resolve().parents[1] FILES = ["backend/app/youtube_live.py", "backend/app/youtube_neko.py"] def run(c, cmd, t=60): _, o, e = c.exec_command(cmd, timeout=t) o.channel.settimeout(t) try: return (o.read() + e.read()).decode("utf-8", "replace") except Exception as ex: return f"ERR {type(ex).__name__}: {ex}" def upload_and_apply(host, port, user, pw, sudo=False): print(f"\n===== {user}@{host}:{port} =====") c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) c.connect(host, port, user, pw, timeout=30, allow_agent=False, look_for_keys=False) sftp = c.open_sftp() for rel in FILES: tmp = f"/tmp/_rf_{Path(rel).name}" with sftp.file(tmp, "wb") as f: f.write((ROOT / rel).read_bytes()) dst = f"/opt/douyinWEB/{rel}" if sudo: print(run(c, f"echo '{pw}' | sudo -S -p '' bash -lc \"cat {tmp} > {dst}\"", 30).strip()) else: print(run(c, f"cat {tmp} > {dst} && echo OK {dst}", 20).strip()) sftp.close() # restart non-blocking then poll if sudo: run(c, f"echo '{pw}' | sudo -S -p '' systemctl restart douyinweb-api", 45) else: # fire and forget run(c, "systemctl restart douyinweb-api >/tmp/_api_restart.log 2>&1 &", 10) for i in range(15): time.sleep(2) h = run(c, "curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x", 10).strip() a = run(c, "systemctl is-active douyinweb-api", 10).strip() print(f" try{i} active={a} health={h}") if h == "200": break print( "marker", run(c, "grep -c studio_receiving /opt/douyinWEB/backend/app/youtube_live.py", 10).strip(), ) print( "yt-svc", run( c, "systemctl is-active youtube-svc 2>/dev/null; curl -sS -m 2 http://127.0.0.1:8099/health 2>/dev/null | head -c 80; echo; ss -lntp | grep -E '8099|8089|9100' | head -5", 15, ).strip(), ) c.close() def main(): upload_and_apply("107.173.30.245", 22, "root", "Xiao4669805", sudo=False) upload_and_apply("107.173.30.245", 4014, "eric", "4669805", sudo=True) if __name__ == "__main__": main()