#!/usr/bin/env python3 """Deploy relay-slot fix to 10.88.0.3 and 10.88.0.5, restart youtube lanes + live-console.""" from __future__ import annotations import io import json import paramiko import time from pathlib import Path ROOT = Path(__file__).resolve().parent.parent FILES = [ "src/youtube_relay_slot.py", "youtube.py", "douyin_youtube_ffplay.py", "src/hub_routes.py", ] HOSTS = [ ("10.88.0.3", "eric", "Xiao4669805", "/opt/live/d2ypp"), ("10.88.0.5", "eric", "4669805", "/opt/live/d2ypp"), ] def put_lf(sftp, local: Path, remote: str) -> None: data = local.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n") sftp.putfo(io.BytesIO(data), remote) def run(ssh, cmd, timeout=180): _, o, e = ssh.exec_command(cmd, timeout=timeout) return (o.read() + e.read()).decode("utf-8", "replace").strip() def youtube_procs(ssh) -> list[str]: raw = run(ssh, "curl -sS -m 15 http://127.0.0.1:8001/hub/dashboard") try: dash = json.loads(raw) except json.JSONDecodeError: return [] out = [] for p in dash.get("live", {}).get("processes", []): name = str(p.get("pm2") or "").strip() if name.startswith("youtube2__") or name == "youtube": out.append(name) return out for host, user, pw, base in HOSTS: print(f"\n{'='*72}\n{host} {base}\n{'='*72}") ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect(host, username=user, password=pw, timeout=25) print("before old guard:", run(ssh, f"grep -n 'len(recording) > 0' {base}/youtube.py | head -1 || echo fixed")) sftp = ssh.open_sftp() install_cmds = [] for rel in FILES: local = ROOT / rel tmp = f"/tmp/_relay_slot_{rel.replace('/', '_')}" dest = f"{base}/{rel.replace(chr(92), '/')}" put_lf(sftp, local, tmp) install_cmds.append(f"install -m 0644 {tmp} {dest}") print("staged", rel) sftp.close() print(run(ssh, f"echo '{pw}' | sudo -S -p '' bash -lc \"{' && '.join(install_cmds)}\"")) print("after markers:") print(" relay_slot:", run(ssh, f"test -f {base}/src/youtube_relay_slot.py && echo present")) print(" old guard:", run(ssh, f"grep -c 'len(recording) > 0' {base}/youtube.py || true")) print(" stream filter:", run(ssh, f"grep -c '_is_probable_stream_input' {base}/douyin_youtube_ffplay.py || true")) print("\nrestart live-console ...") print(run(ssh, f"echo '{pw}' | sudo -S -p '' systemctl restart live-console.service", timeout=120)) time.sleep(4) procs = youtube_procs(ssh) print("restart lanes:", procs) for proc in procs: if proc == "youtube": continue out = run( ssh, f"curl -sS -m 120 -X POST 'http://127.0.0.1:8001/restart' " f"-H 'Content-Type: application/json' " f"-d '{{\"process\":\"{proc}\"}}'", timeout=150, ) print(f" {proc}: {out[:220]}") time.sleep(10) print("\nverify ffmpeg:", run(ssh, "pgrep -af 'ffmpeg.*rtmps' | grep -v pgrep | head -2 || echo none")) dash = json.loads(run(ssh, "curl -sS -m 15 http://127.0.0.1:8001/hub/dashboard")) for p in dash.get("live", {}).get("processes", []): if "youtube" in str(p.get("pm2", "")).lower(): note = str(p.get("business_note") or "") print( f" {p.get('pm2')}: biz={p.get('business_status')} pushing={p.get('is_pushing')} " f"note={note[:70]}" ) ssh.close() print("\nDONE")