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

91 lines
3.4 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Deploy assign→youtubei live-map fix; verify both hosts."""
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]
LOCAL = ROOT / "backend" / "app" / "youtube_live.py"
def run(c, cmd, t=90):
_, 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_one(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()
tmp = "/tmp/_youtube_live_assign_yt.py"
with sftp.file(tmp, "wb") as f:
f.write(LOCAL.read_bytes())
sftp.close()
dst = "/opt/douyinWEB/backend/app/youtube_live.py"
if sudo:
print(run(c, f"echo '{pw}' | sudo -S -p '' bash -lc \"cat {tmp} > {dst}\"", 30).strip())
else:
print(run(c, f"cat {tmp} > {dst} && echo OK", 20).strip())
print("marker", run(c, f"grep -c 'Assign cards: same public youtubei' {dst}", 10).strip())
if sudo:
run(c, f"echo '{pw}' | sudo -S -p '' systemctl restart douyinweb-api", 40)
else:
run(c, "systemctl restart douyinweb-api >/tmp/_rst.log 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", 10).strip()
a = run(c, "systemctl is-active douyinweb-api", 8).strip()
print(f" try{i} {a} {h}")
if h == "200":
break
if a == "deactivating" and i == 5:
run(c, "systemctl kill -s SIGKILL douyinweb-api; systemctl reset-failed douyinweb-api; systemctl start douyinweb-api", 30)
# verify live-map
verify = r'''
import asyncio, os, sys
os.chdir("/opt/douyinWEB/backend"); sys.path.insert(0,".")
from app import youtube_live, yt_assign_db
async def main():
gs=[g for g in yt_assign_db.list_groups(include_urls=False) if g.get("enabled") and (g.get("youtube_channel_id") or g.get("channel_pk"))]
print("assign_cards", len(gs))
for g in gs[:12]:
print(" ", g.get("name"), g.get("youtube_channel_id"))
m=await youtube_live.build_live_map(force=True)
live=[(k,v.get("source"),(v.get("title") or "")[:36]) 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"), "svc_error", (m.get("svc_error") or "")[:80])
print("assign_live", len(live))
for row in live:
print(" ", row)
asyncio.run(main())
'''
sftp = c.open_sftp()
with sftp.file("/tmp/_verify_assign_yt.py", "w") as f:
f.write(verify)
sftp.close()
py = "/opt/douyinWEB/backend/.venv/bin/python /tmp/_verify_assign_yt.py"
if sudo:
py = f"echo '{pw}' | sudo -S -p '' {py}"
print(run(c, py, 150)[:2500])
c.close()
def main():
deploy_one("107.173.30.245", 22, "root", "Xiao4669805", False)
deploy_one("107.173.30.245", 4014, "eric", "4669805", True)
if __name__ == "__main__":
main()