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

123 lines
4.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Deploy remaining live-map / studio / ops-neko / youtube-svc / FE to both hosts."""
from __future__ import annotations
import sys
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
LOCAL = Path(__file__).resolve().parents[1]
FILES = [
"backend/app/db.py",
"backend/app/host_profile.py",
"backend/app/models.py",
"backend/app/ops_neko.py",
"backend/app/ops_neko_db.py",
"backend/app/ops_neko_sync.py",
"backend/app/session_auth.py",
"backend/app/youtube_live.py",
"backend/app/youtube_neko.py",
"backend/app/yt_studio_db.py",
"backend/neko/cdp_studio_scrape.py",
"backend/neko/live-ops-extension/background.js",
"backend/neko/live-ops-extension/content.js",
"youtube-svc/src/youtube.mjs",
"frontend/src/app/neko/page.tsx",
"frontend/src/components/OpsNekoHubCredentialsCard.tsx",
"frontend/src/components/YtRelayOpsHero.tsx",
"frontend/src/components/YtRelayProStatusPanel.tsx",
"frontend/src/lib/ytRelayStatus.ts",
]
def put(c: paramiko.SSHClient, root: str, sudo: str | None = None) -> None:
sftp = c.open_sftp()
for rel in FILES:
tmp = f"/tmp/_cont_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write((LOCAL / rel).read_bytes())
dst = f"{root}/{rel}".replace("\\", "/")
parent = str(Path(dst).parent).replace("\\", "/")
if sudo:
cmd = (
f"echo '{sudo}' | sudo -S -p '' bash -lc "
f"\"mkdir -p '{parent}'; install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}\""
)
else:
cmd = f"mkdir -p '{parent}'; install -m 644 {tmp} {dst}; sed -i 's/\\r$//' {dst}"
c.exec_command(cmd, timeout=40)[1].read()
print("ok", rel, flush=True)
sftp.close()
def build_restart(c: paramiko.SSHClient, sudo: str | None = None) -> None:
inner = (
"cd /opt/douyinWEB/frontend && npm run build >/tmp/fe_cont.log 2>&1 "
"&& echo FE_OK || (tail -30 /tmp/fe_cont.log; echo FE_FAIL); "
"systemctl restart douyinweb-yt douyinweb-api douyinweb-web; "
"sleep 3; "
"systemctl is-active douyinweb-yt; systemctl is-active douyinweb-api; systemctl is-active douyinweb-web; "
"for i in 1 2 3 4 5 6 7 8; do "
" code=$(curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x); "
" echo health=$code; "
" [ \"$code\" = 200 ] && break; sleep 2; "
"done"
)
cmd = f"echo '{sudo}' | sudo -S -p '' bash -lc {repr(inner)}" if sudo else inner
_, o, e = c.exec_command(cmd, timeout=900)
print((o.read() + e.read()).decode("utf-8", "replace"), flush=True)
def verify(c: paramiko.SSHClient, sudo: str | None = None) -> None:
script = r'''
import json, urllib.request, http.cookiejar, time
for i in range(10):
try:
print("health", urllib.request.urlopen("http://127.0.0.1:8014/api/health", timeout=3).status)
break
except Exception as e:
print("wait", i, type(e).__name__); time.sleep(2)
cj=http.cookiejar.CookieJar(); op=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
req=urllib.request.Request("http://127.0.0.1:8014/api/auth/login", data=json.dumps({"username":"hackrobot","password":"Xiao4669805@"}).encode(), headers={"Content-Type":"application/json"}, method="POST")
print("login", op.open(req, timeout=15).status)
p=json.loads(op.open("http://127.0.0.1:8014/api/youtube/live-map?force=1", timeout=150).read())
print("live_count", p.get("live_count"), "source", p.get("source"))
ex=json.loads(op.open("http://127.0.0.1:8014/api/youtube/assign/timeslot-exclude", timeout=20).read())
print("exclude", ex.get("ok"), "count", ex.get("count"), "effective", ex.get("effective_room_count"))
'''
if sudo:
sftp = c.open_sftp()
with sftp.file("/tmp/_cont_v.py", "w") as f:
f.write(script)
sftp.close()
cmd = f"echo '{sudo}' | sudo -S -p '' /opt/douyinWEB/backend/.venv/bin/python /tmp/_cont_v.py"
_, o, e = c.exec_command(cmd, timeout=200)
else:
_, o, e = c.exec_command("/opt/douyinWEB/backend/.venv/bin/python -", timeout=200)
o.channel.sendall(script.encode())
o.channel.shutdown_write()
print((o.read() + e.read()).decode("utf-8", "replace")[:2500], flush=True)
def main() -> None:
for port, user, pwd, label, sudo in [
(4014, "eric", "4669805", "PHYS", "4669805"),
(22, "root", "Xiao4669805", "VPS", None),
]:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect("107.173.30.245", port, user, pwd, timeout=40, allow_agent=False, look_for_keys=False)
print(f"===== {label} =====", flush=True)
put(c, "/opt/douyinWEB", sudo)
build_restart(c, sudo)
verify(c, sudo)
c.close()
print("DONE", flush=True)
if __name__ == "__main__":
main()