#!/usr/bin/env python3 """Restart all youtube lanes on 3 hosts to load new youtube.py code.""" from __future__ import annotations import json import paramiko import sys import time if hasattr(sys.stdout, "reconfigure"): sys.stdout.reconfigure(encoding="utf-8", errors="backslashreplace") HOSTS = [ ("192.168.2.237", "eric", "4669805"), ("10.88.0.3", "eric", "Xiao4669805"), ("10.88.0.5", "eric", "4669805"), ] def run(ssh, cmd: str, timeout: int = 180) -> str: _, o, e = ssh.exec_command(cmd, timeout=timeout) return (o.read() + e.read()).decode("utf-8", "replace").strip() def fetch_dashboard(ssh, pw: str) -> dict | None: raw = run(ssh, "curl -sS -m 20 http://127.0.0.1:8001/hub/dashboard") try: return json.loads(raw) except json.JSONDecodeError: print(" dashboard unavailable, restarting live-console...") print(run(ssh, f"echo '{pw}' | sudo -S -p '' systemctl restart live-console.service", timeout=120)) time.sleep(6) raw = run(ssh, "curl -sS -m 20 http://127.0.0.1:8001/hub/dashboard") try: return json.loads(raw) except json.JSONDecodeError: print(" dashboard still unavailable:", raw[:200]) return None def youtube_rows(dash: dict) -> list[dict]: rows = [] for p in dash.get("live", {}).get("processes", []): name = str(p.get("pm2") or "").strip() if name.startswith("youtube"): rows.append(p) return rows def main() -> None: for host, user, pw in HOSTS: print("=" * 72) print(host) print("=" * 72) ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) try: ssh.connect(host, username=user, password=pw, timeout=25) except Exception as exc: print("SSH FAIL:", exc) continue rtmps = run(ssh, "pgrep -af ffmpeg 2>/dev/null | grep rtmps | grep -v pgrep || true") print("rtmps before:", len(rtmps.splitlines()) if rtmps else 0) dash = fetch_dashboard(ssh, pw) if dash is None: ssh.close() continue procs = youtube_rows(dash) print("youtube lanes:", len(procs)) for row in procs: print( f" {row.get('pm2')}: biz={row.get('business_status')} " f"push={row.get('is_pushing')} status={row.get('process_status')}" ) for row in procs: proc = str(row.get("pm2") or "") if not proc: continue out = run( ssh, "curl -sS -m 120 -X POST 'http://127.0.0.1:8001/restart' " "-H 'Content-Type: application/json' " f"-d '{{\"process\":\"{proc}\"}}'", timeout=150, ) print(f"restart {proc}: {out[:220]!r}") time.sleep(12) dash2 = fetch_dashboard(ssh, pw) if dash2: for row in youtube_rows(dash2): print( f" after {row.get('pm2')}: biz={row.get('business_status')} " f"push={row.get('is_pushing')} status={row.get('process_status')}" ) rtmps2 = run(ssh, "pgrep -af ffmpeg 2>/dev/null | grep rtmps | grep -v pgrep || true") print("rtmps after:", len(rtmps2.splitlines()) if rtmps2 else 0) ssh.close() print("DONE") if __name__ == "__main__": main()