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

76 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Deploy ffmpeg PID matching fix to 10.88.0.4."""
from __future__ import annotations
import io
import json
import time
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
FILES = [
REPO / "src" / "web_process_backend.py",
REPO / "douyin_youtube_ffplay.py",
]
def put(sftp: paramiko.SFTPClient, local: Path, remote: str) -> None:
sftp.putfo(io.BytesIO(local.read_bytes().replace(b"\r\n", b"\n")), remote)
def ssh_run(client: paramiko.SSHClient, cmd: str, sudo: bool = False) -> str:
_, stdout, stderr = client.exec_command(f"export LC_ALL=C.UTF-8; {cmd}", timeout=180, get_pty=sudo)
if sudo:
stdout.channel.send(f"{PASS}\n")
return (stdout.read() + stderr.read()).decode("utf-8", "replace").strip()
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 local in FILES:
remote = f"{BASE}/{local.relative_to(REPO).as_posix()}"
put(sftp, local, remote)
print("uploaded", remote)
sftp.close()
print(ssh_run(client, "sudo -S -p '' systemctl restart live-console.service", sudo=True))
for _ in range(15):
if '"status":"ok"' in ssh_run(client, "curl -sf -m 4 http://127.0.0.1:8001/health; echo"):
print("health ok")
break
time.sleep(2)
for proc in ("youtube2__ch_mpwri36r_z5lm2", "youtube2__ch_mpyvqkc0_gj63k"):
print(proc, ssh_run(
client,
"curl -sS -m 60 -X POST 'http://127.0.0.1:8001/restart' "
"-H 'Content-Type: application/json' "
f"-d '{{\"process\":\"{proc}\"}}'",
)[:300])
print("waiting for ffmpeg ...")
time.sleep(20)
print(ssh_run(client, "pgrep -af 'ffmpeg.*rtmps' | grep -v pgrep"))
raw = ssh_run(client, "curl -sS -m 12 'http://127.0.0.1:8001/process_monitor?family=youtube'")
data = json.loads(raw)
for row in data.get("entries") or []:
if str(row.get("pm2", "")).startswith("youtube2__"):
line = (
f"{row.get('pm2')} business={row.get('business_status')} "
f"pushing={row.get('is_pushing')} label={row.get('label')}"
)
print(line.encode("utf-8", "replace").decode("utf-8", "replace"))
client.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())