douyinWEB/scripts/_deploy_http_live_fix.py
2026-09-13 22:10:49 -05:00

91 lines
3.2 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Deploy http_live fix + youtube-svc /live resolve; verify 3 lives."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
FILES = [
("backend/app/youtube_live.py", "/opt/douyinWEB/backend/app/youtube_live.py"),
("youtube-svc/src/youtube.mjs", "/opt/douyinWEB/youtube-svc/src/youtube.mjs"),
]
VERIFY = r'''
import asyncio, os, sys
os.chdir("/opt/douyinWEB/backend"); sys.path.insert(0, ".")
from app import youtube_live
youtube_live._LAST_OAUTH_AT = 0.0
async def main():
m = await youtube_live.build_live_map(force=True)
live = [(k, v.get("source"), (v.get("title") or "")[:40], v.get("live_url","")[:60])
for k,v in (m.get("entries") or {}).items() if v.get("is_live") and str(k).startswith("assign:")]
print("live_count", m.get("live_count"), "source", m.get("source"))
print("assign_live", len(live))
for row in live:
print(" LIVE", row)
asyncio.run(main())
'''
def run(c, cmd, t=120):
_, o, e = c.exec_command(cmd, timeout=t)
o.channel.settimeout(t)
try:
return (o.read() + e.read()).decode("utf-8", "replace")
except Exception as ex:
return f"ERR {type(ex).__name__}: {ex}"
def deploy(host, port, user, pw, sudo=False):
print(f"\n===== {user}@{host}:{port} =====")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(host, port, user, pw, timeout=30, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
for rel, dst in FILES:
tmp = f"/tmp/_fix_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write((ROOT / rel).read_bytes())
if sudo:
print(run(c, f"echo '{pw}' | sudo -S -p '' bash -lc \"cat {tmp} > {dst}\"", 30).strip())
else:
# youtube-svc path may need mkdir
print(run(c, f"mkdir -p $(dirname {dst}); cat {tmp} > {dst} && echo OK {dst}", 20).strip())
with sftp.file("/tmp/_verify_http_live.py", "w") as f:
f.write(VERIFY)
sftp.close()
# restart api + yt svc
if sudo:
run(c, f"echo '{pw}' | sudo -S -p '' systemctl restart douyinweb-yt douyinweb-api", 50)
else:
run(c, "systemctl restart douyinweb-yt >/tmp/_yt.txt 2>&1 || true", 20)
run(c, "systemctl restart douyinweb-api >/tmp/_api.txt 2>&1 &", 10)
for i in range(12):
time.sleep(2)
h = run(c, "curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x").strip()
print(f" try{i} {h}")
if h == "200":
break
if i == 5 and not sudo:
run(c, "systemctl kill -s SIGKILL douyinweb-api; systemctl reset-failed douyinweb-api; systemctl start douyinweb-api", 30)
py = "/opt/douyinWEB/backend/.venv/bin/python /tmp/_verify_http_live.py"
if sudo:
py = f"echo '{pw}' | sudo -S -p '' {py}"
print(run(c, py, 200)[:2500])
c.close()
def main():
deploy("107.173.30.245", 22, "root", "Xiao4669805", False)
deploy("107.173.30.245", 4014, "eric", "4669805", True)
if __name__ == "__main__":
main()