sh2/d2ypp2/tools/fix_host04_frontend.py
2026-06-14 10:41:48 -05:00

113 lines
4.1 KiB
Python

#!/usr/bin/env python3
from __future__ import annotations
import io
import json
import posixpath
from pathlib import Path
import paramiko
HOST, USER, PASS = "10.88.0.4", "eric", "4669805"
BASE = "/home/eric/d2ypp2-src"
REPO = Path(__file__).resolve().parent.parent
OUT = REPO / "web-console" / "out"
def run(ssh: paramiko.SSHClient, cmd: str, timeout: int = 120) -> str:
_, stdout, stderr = ssh.exec_command(f"export LC_ALL=C.UTF-8; {cmd}", timeout=timeout)
return (stdout.read() + stderr.read()).decode("utf-8", "replace").strip()
def mkdir_p(sftp: paramiko.SFTPClient, remote_dir: str) -> None:
remote_dir = remote_dir.replace("\\", "/").rstrip("/")
parts = [p for p in remote_dir.split("/") if p]
cur = ""
for part in parts:
cur = f"{cur}/{part}" if cur else f"/{part}"
try:
sftp.stat(cur)
except OSError:
sftp.mkdir(cur)
def put_tree(sftp: paramiko.SFTPClient, local_dir: Path, remote_dir: str) -> int:
count = 0
for path in sorted(local_dir.rglob("*")):
if not path.is_file():
continue
rel = path.relative_to(local_dir).as_posix()
remote = posixpath.join(remote_dir, rel)
mkdir_p(sftp, posixpath.dirname(remote))
sftp.put(str(path), remote)
count += 1
return count
def main() -> int:
if not (OUT / "index.html").is_file():
raise SystemExit("missing local web-console/out; run pnpm build first")
local_page = next(OUT.glob("_next/static/chunks/app/page-*.js"), None)
local_index = (OUT / "index.html").read_text(encoding="utf-8", errors="replace")
print("local page bundle:", local_page.name if local_page else "missing")
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("uploading web-console/out ...")
uploaded = put_tree(sftp, OUT, f"{BASE}/web-console/out")
print(f"uploaded {uploaded} files")
backend_files = [
"src/process_runtime_status.py",
"src/douyin_source_display.py",
"src/youtube_quality_guard.py",
"web-console/components/live-product/LiveYoutubeUnmannedView.tsx",
"web-console/components/live-product/LiveProcessLiveLogCard.tsx",
"web-console/lib/ffmpegMetrics.ts",
"web-console/lib/youtubeConfigUtils.ts",
]
for rel in backend_files:
local = REPO / rel
remote = f"{BASE}/{rel.replace(chr(92), '/')}"
mkdir_p(sftp, posixpath.dirname(remote))
if local.suffix.lower() in {".py", ".ts", ".tsx"}:
sftp.putfo(io.BytesIO(local.read_bytes().replace(b"\r\n", b"\n")), remote)
else:
sftp.put(str(local), remote)
sftp.close()
print(run(client, f"grep -o 'page-[a-z0-9]*\\.js' {BASE}/web-console/out/index.html | head -1"))
print(run(client, f"wc -c {BASE}/web-console/out/_next/static/chunks/app/page-*.js"))
print("restarting live-console ...")
print(run(client, f"echo '{PASS}' | sudo -S -p '' systemctl restart live-console.service", timeout=60))
print(run(client, "sleep 4; curl -sf -m 8 http://127.0.0.1:8001/health; echo"))
raw = run(client, "curl -sS -m 12 'http://127.0.0.1:8001/process_monitor?family=youtube'")
try:
data = json.loads(raw)
entries = data.get("entries") or []
print(f"process_monitor entries={len(entries)}")
for row in entries:
print(
f" {row.get('pm2')}: business={row.get('business_status')} pushing={row.get('is_pushing')} "
f"label={row.get('label')} display={row.get('source_display') or row.get('source_title') or '-'}"
)
except json.JSONDecodeError:
print("process_monitor raw:", raw[:1500])
served = run(client, "curl -sS -m 8 http://127.0.0.1/ | grep -o 'page-[a-z0-9]*\\.js' | head -1")
print("served page bundle:", served)
print("local index references page-9ff3a078ec6e16cb.js:", "page-9ff3a078ec6e16cb.js" in local_index)
client.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())