#!/usr/bin/env python3 """Deploy normal-auto + exclusive sticky + remove slots UI 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 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" DY = "108.174.50.68" DYLIVE = "107.173.30.245" FILES = [ "backend/app/youtube_relay_pipeline.py", "frontend/src/components/YtAssignPanel.tsx", "frontend/src/components/YtRelayOpsHero.tsx", ] 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: upload + API restart + Next build ---------- 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, r""" set -euo pipefail install -m 0644 /tmp/youtube_relay_pipeline.py /opt/douyinWEB/backend/app/youtube_relay_pipeline.py install -m 0644 /tmp/YtAssignPanel.tsx /opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx install -m 0644 /tmp/YtRelayOpsHero.tsx /opt/douyinWEB/frontend/src/components/YtRelayOpsHero.tsx # syntax check cd /opt/douyinWEB/backend ./.venv/bin/python -c 'from app import youtube_relay_pipeline as p; print("import_ok", hasattr(p, "_claim_first_live_normal"))' systemctl restart douyinweb-api sleep 3 systemctl is-active douyinweb-api # build FE (dy has RAM) 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 # pack .next for dylive rm -f /tmp/dy_next_normal_auto.tgz tar -C /opt/douyinWEB/frontend -czf /tmp/dy_next_normal_auto.tgz .next ls -lh /tmp/dy_next_normal_auto.tgz # smoke 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 built chunk for new copy grep -Rnl '单路独占' /opt/douyinWEB/frontend/.next/static 2>/dev/null | head -3 || true grep -n '_claim_first_live_normal\|时间段排班 OFF' /opt/douyinWEB/backend/app/youtube_relay_pipeline.py | head -5 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}") # download tarball via sftp to local then upload to dylive (or pipe) print("=== transfer .next to local buffer ===", flush=True) sftp = dy.open_sftp() buf = io.BytesIO() sftp.getfo("/tmp/dy_next_normal_auto.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("/tmp/dy_next_normal_auto.tgz", "wb") as f: f.write(buf.getvalue()) sftp.close() code, _ = run( dl, r""" set -euo pipefail # snapshot running nekos — must stay (start only if we touch nothing about recreate) BEFORE=$(docker ps --filter name=douyinweb-neko --format '{{.Names}}' | sort | tr '\n' ' ') echo BEFORE_NEKO=$BEFORE install -m 0644 /tmp/youtube_relay_pipeline.py /opt/douyinWEB/backend/app/youtube_relay_pipeline.py install -m 0644 /tmp/YtAssignPanel.tsx /opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx install -m 0644 /tmp/YtRelayOpsHero.tsx /opt/douyinWEB/frontend/src/components/YtRelayOpsHero.tsx cd /opt/douyinWEB/backend ./.venv/bin/python -c 'from app import youtube_relay_pipeline as p; print("import_ok", hasattr(p, "_claim_first_live_normal"))' systemctl restart douyinweb-api sleep 3 systemctl is-active douyinweb-api # extract FE build — no npm build on 3.8G cd /opt/douyinWEB/frontend rm -rf .next.bak-normal-auto if [ -d .next ]; then mv .next .next.bak-normal-auto; fi tar -xzf /tmp/dy_next_normal_auto.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 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}") # public smoke import json import ssl import urllib.request from http.cookiejar import CookieJar CTX = ssl.create_default_context() def smoke(base: str): cj = CookieJar() op = urllib.request.build_opener( urllib.request.HTTPCookieProcessor(cj), 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 op.open(req, timeout=45) as r: print(base, "login", r.status) with op.open(base + "/youtube", timeout=45) as r: body = r.read().decode("utf-8", "replace") print(base, "youtube", r.status, "独占" in body or "Live Relay" in body or len(body)) print("\n=== public smoke ===", flush=True) smoke("https://dy.nomadro.live") smoke("https://dylive.nomadro.com") print("ALL_DONE")