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

209 lines
6.4 KiB
Python

#!/usr/bin/env python3
"""Deploy normal-mode overview UI (YtAssignPanel) to dy then dylive.
Build Next on dy (more RAM), copy .next tarball to dylive. Never recreate Neko.
"""
from __future__ import annotations
import io
import json
import ssl
import sys
import time
import urllib.request
from http.cookiejar import CookieJar
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
PASS = "Xiao4669805"
DY = "108.174.50.68"
DYLIVE = "107.173.30.245"
FILES = [
"frontend/src/components/YtAssignPanel.tsx",
]
MARKER = "普通频道 · 在播抖音"
TGZ = "/tmp/dy_next_normal_overview.tgz"
def connect(host: str, retries: int = 12):
last = None
for i in range(retries):
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=300,
)
return c
except Exception as e:
last = e
print(f"ssh_retry {host} {i}: {e}", flush=True)
time.sleep(3 + i)
raise SystemExit(f"ssh_fail {host}: {last}")
def run(c, cmd: str, t: int = 1200) -> tuple[int, str]:
print(f"\n===== {cmd[:120].replace(chr(10), ' ')} =====", flush=True)
_i, o, e = c.exec_command(cmd, timeout=t)
out = (o.read() + e.read()).decode("utf-8", "replace")
code = o.channel.recv_exit_status()
print(out[-16000:] if len(out) > 16000 else out, flush=True)
return code, out
def put_text(sftp, local: Path, remote: str) -> None:
raw = local.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
with sftp.file(remote, "wb") as f:
f.write(raw)
for rel in FILES:
assert (ROOT / rel).is_file(), rel
# ---------- dy ----------
print("=== DY deploy ===", flush=True)
dy = connect(DY)
run(
dy,
"docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort; "
"systemctl is-active douyinweb-api douyinweb-web; free -h | head -2",
)
sftp = dy.open_sftp()
for rel in FILES:
put_text(sftp, ROOT / rel, f"/tmp/{Path(rel).name}")
sftp.close()
code, _ = run(
dy,
rf"""
set -euo pipefail
install -m 0644 /tmp/YtAssignPanel.tsx /opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx
grep -n '{MARKER}' /opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx | head -3
cd /opt/douyinWEB/frontend
NODE_OPTIONS='--max-old-space-size=1536' npm run build
systemctl restart douyinweb-web
sleep 2
systemctl is-active douyinweb-web
rm -f {TGZ}
tar -C /opt/douyinWEB/frontend -czf {TGZ} .next
ls -lh {TGZ}
CS=$(mktemp)
curl -sS -m 20 -c "$CS" -b "$CS" -X POST http://127.0.0.1:8014/api/auth/login \
-H 'Content-Type: application/json' \
-d '{{"username":"hackrobot","password":"Xiao4669805@"}}' | head -c 120; echo
curl -sS -m 15 -b "$CS" -o /dev/null -w 'youtube:%{{http_code}}\n' http://127.0.0.1:4015/youtube
grep -Rnl '{MARKER}' /opt/douyinWEB/frontend/.next/static 2>/dev/null | head -3 || true
docker ps --filter name=douyinweb-neko --format '{{{{.Names}}}} {{{{.Status}}}}' | sort
echo DY_DONE
""",
t=1800,
)
if code != 0:
dy.close()
raise SystemExit(f"dy deploy failed: {code}")
print("=== transfer .next to local buffer ===", flush=True)
sftp = dy.open_sftp()
buf = io.BytesIO()
sftp.getfo(TGZ, buf)
sftp.close()
dy.close()
print(f"tarball bytes={buf.tell()}", flush=True)
# ---------- dylive ----------
print("=== DYLIVE deploy ===", flush=True)
dl = connect(DYLIVE)
run(
dl,
"docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort; "
"systemctl is-active douyinweb-api douyinweb-web; free -h | head -2",
)
sftp = dl.open_sftp()
for rel in FILES:
put_text(sftp, ROOT / rel, f"/tmp/{Path(rel).name}")
buf.seek(0)
with sftp.file(TGZ, "wb") as f:
f.write(buf.getvalue())
sftp.close()
code, _ = run(
dl,
rf"""
set -euo pipefail
BEFORE=$(docker ps --filter name=douyinweb-neko --format '{{{{.Names}}}}' | sort | tr '\n' ' ')
echo BEFORE_NEKO=$BEFORE
install -m 0644 /tmp/YtAssignPanel.tsx /opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx
grep -n '{MARKER}' /opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx | head -3
cd /opt/douyinWEB/frontend
rm -rf .next.bak-normal-overview
if [ -d .next ]; then mv .next .next.bak-normal-overview; fi
tar -xzf {TGZ}
test -d .next
systemctl restart douyinweb-web
sleep 2
systemctl is-active douyinweb-web
CS=$(mktemp)
curl -sS -m 20 -c "$CS" -b "$CS" -X POST http://127.0.0.1:8014/api/auth/login \
-H 'Content-Type: application/json' \
-d '{{"username":"hackrobot","password":"Xiao4669805@"}}' | head -c 120; echo
curl -sS -m 15 -b "$CS" -o /dev/null -w 'youtube:%{{http_code}}\n' http://127.0.0.1:4015/youtube
grep -Rnl '{MARKER}' /opt/douyinWEB/frontend/.next/static 2>/dev/null | head -3 || true
AFTER=$(docker ps --filter name=douyinweb-neko --format '{{{{.Names}}}}' | sort | tr '\n' ' ')
echo AFTER_NEKO=$AFTER
docker ps --filter name=douyinweb-neko --format '{{{{.Names}}}} {{{{.Status}}}}' | sort
free -h | head -2
echo DYLIVE_DONE
""",
t=300,
)
dl.close()
if code != 0:
raise SystemExit(f"dylive deploy failed: {code}")
CTX = ssl.create_default_context()
def smoke(base: str) -> None:
jar = CookieJar()
opener = urllib.request.build_opener(
urllib.request.HTTPCookieProcessor(jar),
urllib.request.HTTPSHandler(context=CTX),
)
req = urllib.request.Request(
f"{base}/api/auth/login",
data=json.dumps({"username": "hackrobot", "password": "Xiao4669805@"}).encode(),
headers={"Content-Type": "application/json"},
method="POST",
)
with opener.open(req, timeout=25) as r:
print(base, "login", r.status, flush=True)
with opener.open(urllib.request.Request(f"{base}/youtube", method="GET"), timeout=25) as r:
body = r.read(200000).decode("utf-8", "replace")
hit = MARKER in body or "normalLiveRooms" in body
print(base, "youtube", r.status, "marker" if MARKER in body else ("chunk?" if hit else "no_marker_in_html"), flush=True)
print("=== public smoke ===", flush=True)
for url in ("https://dy.nomadro.live", "https://dylive.nomadro.com"):
try:
smoke(url)
except Exception as e:
print(url, "SMOKE_ERR", e, flush=True)
print("ALL_DONE", flush=True)