#!/usr/bin/env python3 """Deploy YouTube relay log feature to 10.88.0.3 and restart live-console.""" from __future__ import annotations import io import posixpath import sys import time from pathlib import Path import paramiko HOST, USER, PASS = "10.88.0.3", "eric", "Xiao4669805" REMOTE_BASE = "/opt/live/d2ypp" REPO = Path(__file__).resolve().parent.parent def collect_files() -> list[str]: files = [ "src/youtube_relay_log.py", "src/youtube_ingest_auth.py", "src/hub_routes.py", "web.py", "douyin_youtube_ffplay.py", "web-console/app/youtube-relay-log/page.tsx", "web-console/components/live-product/LiveYoutubeRelayLogView.tsx", "web-console/components/live-product/LiveYoutubeUnmannedView.tsx", "web-console/components/live-product/LiveControlApp.tsx", ] for path in (REPO / "src").rglob("*.py"): files.append(str(path.relative_to(REPO)).replace("\\", "/")) 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"}: 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 = 120) -> tuple[int, str]: _, stdout, stderr = client.exec_command( "export LC_ALL=C.UTF-8 LANG=C.UTF-8; " + cmd, timeout=timeout, ) 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 <= 5 or idx == len(files) or idx % 40 == 0: print(f" {idx}/{len(files)} {rel}") sftp.close() steps = [ f"rm -rf {REMOTE_BASE}/src/__pycache__ 2>/dev/null; echo cache_cleared", ( f"python3 -m py_compile {REMOTE_BASE}/src/youtube_relay_log.py " f"{REMOTE_BASE}/web.py {REMOTE_BASE}/src/hub_routes.py; echo syntax_ok" ), f"echo {PASS!r} | sudo -S -p '' systemctl restart live-console.service; echo restarted", "sleep 10", "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 8 'http://127.0.0.1:8001/hub/youtube/relay/sessions?limit=1' " "| head -c 500; echo" ), ( "curl -sS -m 8 -X POST 'http://127.0.0.1:8001/hub/youtube/relay/sessions/import-log' " "-H 'Content-Type: application/json' -d '{\"process\":\"youtube\"}' | head -c 500; echo" ), "systemctl is-active live-console.service 2>/dev/null; journalctl -u live-console.service -n 15 --no-pager 2>/dev/null", ] for step in steps: code, out = run(client, step, timeout=180) try: print(f"\n--- exit={code} ---\n{out.strip()[:1200]}") except UnicodeEncodeError: sys.stdout.buffer.write(f"\n--- exit={code} ---\n".encode()) sys.stdout.buffer.write(out.strip()[:1200].encode("utf-8", errors="replace") + b"\n") client.close() print("\nDeploy complete.") return 0 if __name__ == "__main__": raise SystemExit(main())