#!/usr/bin/env python3 """Deploy relay log import fix and re-import on host03.""" from __future__ import annotations import io import json import posixpath import sys from pathlib import Path import paramiko HOST, USER, PASS = "10.88.0.3", "eric", "Xiao4669805" BASE = "/opt/live/d2ypp" REPO = Path(__file__).resolve().parent.parent FILES = [ "src/youtube_relay_log.py", "src/hub_routes.py", "web.py", "web-console/components/live-product/LiveYoutubeRelayLogView.tsx", "web-console/components/live-product/LiveYoutubeUnmannedView.tsx", ] def put(sftp: paramiko.SFTPClient, local: Path, remote: str) -> None: parent = posixpath.dirname(remote) parts = [p for p in parent.split("/") if p] cur = "" for part in parts: cur = f"{cur}/{part}" if cur else f"/{part}" try: sftp.stat(cur) except OSError: try: sftp.mkdir(cur) except OSError: pass data = local.read_bytes().replace(b"\r\n", b"\n") sftp.putfo(io.BytesIO(data), remote) def run(client: paramiko.SSHClient, cmd: str, timeout: int = 180) -> tuple[int, str]: _, stdout, stderr = client.exec_command("export LC_ALL=C.UTF-8; " + cmd, timeout=timeout) out = (stdout.read() + stderr.read()).decode("utf-8", "replace") return stdout.channel.recv_exit_status(), out def safe_print(text: str) -> None: try: print(text) except UnicodeEncodeError: sys.stdout.buffer.write(text.encode("utf-8", errors="replace") + b"\n") def main() -> int: client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(HOST, username=USER, password=PASS, timeout=20) sftp = client.open_sftp() for rel in FILES: put(sftp, REPO / rel, f"{BASE}/{rel}") safe_print(f"uploaded {rel}") sftp.close() steps = [ f"python3 -m py_compile {BASE}/src/youtube_relay_log.py {BASE}/web.py", f"echo {PASS!r} | sudo -S -p '' systemctl restart live-console.service", "sleep 10", "curl -sf http://127.0.0.1:8001/health", ( "curl -sS -X POST http://127.0.0.1:8001/hub/youtube/relay/sessions/import-log " "-H 'Content-Type: application/json' " "-d '{\"process\":\"youtube\",\"replace_imported\":true}'" ), "curl -sS 'http://127.0.0.1:8001/hub/youtube/relay/sessions?process=youtube&limit=20'", ] for step in steps: code, out = run(client, step) safe_print(f"\n--- exit={code} ---\n{out[:5000]}") if step.endswith("limit=20'"): try: data = json.loads(out) for row in data.get("sessions", []): safe_print( f" {row.get('anchor_name')} | {row.get('source_url')} | " f"start={row.get('started_at')} dur={row.get('duration_seconds')}s | " f"{row.get('stream_title')}" ) except json.JSONDecodeError: pass client.close() return 0 if __name__ == "__main__": raise SystemExit(main())