#!/usr/bin/env python3 """Deploy relay-log analytics UI + API to 10.88.0.8 and restart live-console.""" from __future__ import annotations import io import posixpath import sys import time from pathlib import Path import paramiko sys.stdout.reconfigure(encoding="utf-8", errors="replace") HOST, USER, PASS = "10.88.0.8", "eric", "4669805" REMOTE_BASE = "/opt/live/d2ypp" REPO = Path(__file__).resolve().parent.parent FILES = [ "src/youtube_relay_log.py", "src/hub_routes.py", "web.py", "web-console/app/youtube-relay-log/page.tsx", "web-console/components/live-product/LiveYoutubeRelayLogView.tsx", "web-console/components/live-product/LiveYoutubeRelayLogCharts.tsx", "tests/test_youtube_relay_log.py", ] def collect_files() -> list[str]: files = list(FILES) out_dir = REPO / "web-console" / "out" for path in out_dir.rglob("*"): if path.is_file(): files.append(str(path.relative_to(REPO)).replace("\\", "/")) return sorted(set(files)) def mkdir_p(sftp: paramiko.SFTPClient, remote_dir: str) -> None: remote_dir = remote_dir.rstrip("/") if not remote_dir or remote_dir == "/": return cur = "" for part in [p for p in remote_dir.split("/") if p]: cur = f"{cur}/{part}" if cur else f"/{part}" try: sftp.stat(cur) except OSError: try: sftp.mkdir(cur) except OSError: pass def put(sftp: paramiko.SFTPClient, local: Path, remote: str) -> None: mkdir_p(sftp, posixpath.dirname(remote)) if local.suffix.lower() in {".py", ".sh", ".tsx", ".ts", ".mjs", ".json", ".css", ".html", ".txt", ".js"}: data = local.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n") sftp.putfo(io.BytesIO(data), remote) else: sftp.put(str(local), 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 LANG=C.UTF-8; " + cmd, timeout=timeout, get_pty=True, ) out = (stdout.read() + stderr.read()).decode("utf-8", "replace") return stdout.channel.recv_exit_status(), out def main() -> int: files = collect_files() client = paramiko.SSHClient() client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) client.connect(HOST, username=USER, password=PASS, timeout=20) sftp = client.open_sftp() print(f"Uploading {len(files)} files to {REMOTE_BASE} ...") for idx, rel in enumerate(files, start=1): local = REPO / rel if not local.is_file(): print(f"SKIP missing: {rel}") continue put(sftp, local, f"{REMOTE_BASE}/{rel}") if idx <= 8 or idx == len(files) or idx % 50 == 0: print(f" {idx}/{len(files)} {rel}") sftp.close() steps = [ f"rm -rf {REMOTE_BASE}/src/__pycache__ 2>/dev/null; echo cache_cleared", ( f"cd {REMOTE_BASE} && python3 -m py_compile src/youtube_relay_log.py web.py src/hub_routes.py " f"&& python3 -m unittest tests.test_youtube_relay_log.YoutubeRelayLogTests.test_summarize_relay_sessions_by_anchor_and_hour -v " f"2>&1 | tail -20" ), f"echo {PASS!r} | sudo -S -p '' systemctl restart live-console.service; echo restarted", "sleep 8", "for i in 1 2 3 4 5 6 7 8 9 10 11 12; do " "curl -sf -m 4 http://127.0.0.1:8001/health >/dev/null 2>&1 && echo health_ok && break; " "sleep 3; done", "curl -sS -m 8 -o /dev/null -w 'relay_log_page=%{http_code}\\n' http://127.0.0.1:8001/youtube-relay-log", "curl -sS -m 12 'http://127.0.0.1:8001/hub/youtube/relay/sessions/stats?process=youtube&top_anchors=5' | head -c 1200; echo", ( "curl -sS -m 20 -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}' | head -c 400; echo" ), "curl -sS -m 12 'http://127.0.0.1:8001/hub/youtube/relay/sessions/stats?process=youtube&top_anchors=5' | head -c 1500; echo", "systemctl is-active live-console.service; journalctl -u live-console.service -n 12 --no-pager", ] for step in steps: code, out = run(client, step, timeout=240) print(f"\n--- exit={code} ---\n{out.strip()[:2000]}") client.close() return 0 if __name__ == "__main__": raise SystemExit(main())