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

122 lines
3.6 KiB
Python

#!/usr/bin/env python3
"""Deploy stream-key visible/copy UI+API to dy + dylive. Never touch Neko."""
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]
PASS = "Xiao4669805"
HOSTS = [
("dy", "108.174.50.68"),
("dylive", "107.173.30.245"),
]
FILES = [
"backend/app/yt_assign_db.py",
"backend/app/yt_stream_key_db.py",
"backend/app/youtube_relay_status.py",
"backend/app/main.py",
"frontend/src/lib/ytRelayStatus.ts",
"frontend/src/components/YtAssignPanel.tsx",
"frontend/src/components/YtRelayProStatusPanel.tsx",
"frontend/src/app/youtube/keys/page.tsx",
"frontend/src/app/youtube-relay-log/page.tsx",
]
def connect(host: str):
last = None
for i in range(10):
try:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
host,
22,
"root",
PASS,
timeout=60,
allow_agent=False,
look_for_keys=False,
banner_timeout=240,
)
return c
except Exception as e:
last = e
print(f"ssh_retry {i} {host}: {e}", flush=True)
time.sleep(3 + i)
raise SystemExit(f"ssh fail {host}: {last}")
def run(c, cmd: str, t: int = 900) -> tuple[int, str]:
_i, o, e = c.exec_command(cmd, timeout=t)
out = (o.read() + e.read()).decode("utf-8", "replace")
return o.channel.recv_exit_status(), out
for label, host in HOSTS:
print(f"\n===== {label} {host} =====", flush=True)
c = connect(host)
_, before = run(c, "docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort")
print("BEFORE\n" + before, flush=True)
sftp = c.open_sftp()
for rel in FILES:
local = ROOT / rel
assert local.is_file(), local
remote = f"/opt/douyinWEB/{rel}"
run(c, f"mkdir -p {'/'.join(remote.split('/')[:-1])}")
print("put", rel, flush=True)
sftp.put(str(local), remote)
sftp.close()
code, out = run(
c,
r"""
set -euo pipefail
cd /opt/douyinWEB/backend
./.venv/bin/python - <<'PY'
from app import yt_assign_db
gs = yt_assign_db.list_groups(include_urls=False, include_secret=True)
print('groups', len(gs), 'with_key_field', sum(1 for g in gs if 'stream_key' in g and g.get('stream_key')))
PY
systemctl restart douyinweb-api
sleep 4
systemctl is-active douyinweb-api
cd /opt/douyinWEB/frontend
export NODE_OPTIONS=--max-old-space-size=2048
if [ -d node_modules ]; then
npm run build
else
echo 'WARN skip build: no node_modules'
fi
systemctl restart douyinweb-web
sleep 3
systemctl is-active douyinweb-web
echo '=== neko after ==='
docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort
""",
t=900,
)
print(out[-7000:] if len(out) > 7000 else out, flush=True)
if code != 0:
print(f"WARN exit {code}", flush=True)
before_up = {ln.split()[0] for ln in before.splitlines() if " Up " in ln and "neko" in ln}
_, after = run(c, "docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort")
after_up = {ln.split()[0] for ln in after.splitlines() if " Up " in ln and "neko" in ln}
lost = before_up - after_up
print("AFTER\n" + after, flush=True)
if lost:
print("CRITICAL neko lost", lost, flush=True)
raise SystemExit(2)
print(f"neko_ok {label}", sorted(after_up), flush=True)
c.close()
print("\nALL_DONE", flush=True)