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

99 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""Fix config perms, run hardware probe, restart Pro lanes on 10.88.0.4."""
from __future__ import annotations
import json
import time
from pathlib import Path
import paramiko
HOST, USER, PASS = "10.88.0.4", "eric", "4669805"
REMOTE = "/home/eric/d2ypp2-src"
LANES = ["youtube2__ch_mpwri36r_z5lm2", "youtube2__ch_mpyvqkc0_gj63k"]
ROOT = Path(__file__).resolve().parent.parent
UPLOADS = {
ROOT / "douyin_youtube_ffplay.py": f"{REMOTE}/douyin_youtube_ffplay.py",
ROOT / "scripts" / "hardware_probe.py": f"{REMOTE}/scripts/hardware_probe.py",
ROOT / "config" / "hardware.env.example": f"{REMOTE}/config/hardware.env.example",
ROOT / "youtube.py": f"{REMOTE}/youtube.py",
ROOT / "src" / "spider.py": f"{REMOTE}/src/spider.py",
ROOT / "src" / "recorder_config.py": f"{REMOTE}/src/recorder_config.py",
}
def run(c: paramiko.SSHClient, cmd: str, timeout: int = 90) -> str:
_, o, e = c.exec_command(cmd, timeout=timeout, get_pty=True)
return (o.read() + e.read()).decode("utf-8", "replace")
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, username=USER, password=PASS, timeout=20)
sftp = c.open_sftp()
for local, remote in UPLOADS.items():
sftp.put(str(local), remote)
print(f"uploaded {remote}")
sftp.close()
print(
run(
c,
f"echo '{PASS}' | sudo -S -p '' chown -R eric:eric {REMOTE}/config && "
f"chmod -R u+rwX {REMOTE}/config && echo perms_ok",
timeout=30,
)
)
probe_out = run(
c,
f"cd {REMOTE} && . .venv/bin/activate && "
"python scripts/hardware_probe.py --write --apply-youtube-defaults",
timeout=120,
)
try:
profile = json.loads(probe_out.strip().splitlines()[-1])
relay = profile.get("relay_strategy", {})
print(f"relay_tier={relay.get('tier')} video_mode={relay.get('video_mode')} policy={relay.get('dual_lane_policy')}")
applied = profile.get("youtube_defaults_applied") or []
if applied:
print(f"ini_updated={len(applied)}")
except (json.JSONDecodeError, IndexError):
print(probe_out[-2000:])
print("\n=== hardware.env ===")
print(run(c, f"grep LIVE_RELAY {REMOTE}/config/hardware.env 2>/dev/null || echo MISSING"))
for lane in LANES:
print(run(c, f"curl -sS -m 60 'http://127.0.0.1:8001/restart?process={lane}'")[:200])
time.sleep(4)
print("waiting 50s...")
time.sleep(50)
print(run(c, "pgrep -af 'ffmpeg.*rtmps' || echo none")[:3000])
raw = run(c, "curl -sS -m 15 'http://127.0.0.1:8001/process_monitor?family=youtube&light=1'")
try:
data = json.loads(raw)
for entry in data.get("entries", []):
if entry.get("pm2") in LANES:
print(
f"{entry['pm2']}: {entry.get('business_status')} "
f"speed={entry.get('ffmpeg_speed')} room={entry.get('source_room_id')}"
)
except json.JSONDecodeError:
print(raw[:1500])
for lane in LANES:
log = run(
c,
f"grep -E 'hardware relay|stream copy|YouTube Live profile|speed=' "
f"{REMOTE}/.pm2/logs/{lane}-out.log | tail -6",
)
print(f"\n=== {lane} ===\n{log[:1000]}")
c.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())