#!/usr/bin/env python3 """Deploy project-wide perf fixes to dy then 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, DYLIVE = "108.174.50.68", "107.173.30.245" TGZ = "/tmp/dy_next_perf.tgz" FILES = [ "backend/app/youtube_relay_pipeline.py", "backend/app/pb_sync.py", "backend/app/youtube_neko.py", "backend/app/main.py", "frontend/src/lib/api.js", "frontend/src/lib/douyinLive.ts", "frontend/src/components/YtAssignPanel.tsx", "frontend/src/app/page.tsx", ] def connect(host: str): c = paramiko.SSHClient() c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) for i in range(12): try: c.connect(host, 22, "root", PASS, timeout=60, allow_agent=False, look_for_keys=False, banner_timeout=300) return c except Exception as e: print(f"retry {host} {i}: {e}", flush=True) time.sleep(3 + i) raise SystemExit(f"ssh_fail {host}") def run(c, cmd: str, t: int = 1800) -> tuple[int, str]: print(f"\n===== {cmd[:90].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[-12000:] if len(out) > 12000 else out, flush=True) return code, out def put(c, local: Path, remote_name: str) -> None: raw = local.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n") sftp = c.open_sftp() with sftp.file(f"/tmp/{remote_name}", "wb") as f: f.write(raw) sftp.close() for rel in FILES: assert (ROOT / rel).is_file(), rel print("=== DY ===", flush=True) dy = connect(DY) for rel in FILES: put(dy, ROOT / rel, Path(rel).name) code, _ = run( dy, rf""" set -euo pipefail install -m 0644 /tmp/youtube_relay_pipeline.py /opt/douyinWEB/backend/app/youtube_relay_pipeline.py install -m 0644 /tmp/pb_sync.py /opt/douyinWEB/backend/app/pb_sync.py install -m 0644 /tmp/youtube_neko.py /opt/douyinWEB/backend/app/youtube_neko.py install -m 0644 /tmp/main.py /opt/douyinWEB/backend/app/main.py install -m 0644 /tmp/api.js /opt/douyinWEB/frontend/src/lib/api.js install -m 0644 /tmp/douyinLive.ts /opt/douyinWEB/frontend/src/lib/douyinLive.ts install -m 0644 /tmp/YtAssignPanel.tsx /opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx install -m 0644 /tmp/page.tsx /opt/douyinWEB/frontend/src/app/page.tsx cd /opt/douyinWEB/backend ./.venv/bin/python - <<'PY' from app import pb_sync, youtube_relay_pipeline assert pb_sync._PB_TIMEOUT_SEC == 6.0 assert youtube_relay_pipeline.YoutubeRelayPipeline()._STATUS_CACHE_TTL_SEC == 5.0 print('backend_ok') PY # revive API if D-state systemctl restart douyinweb-api sleep 4 curl -sS -m 10 -o /dev/null -w 'health:%{{http_code}}\n' http://127.0.0.1:8014/api/health cd /opt/douyinWEB/frontend NODE_OPTIONS='--max-old-space-size=1536' npm run build systemctl restart douyinweb-web sleep 2 rm -f {TGZ} tar -C /opt/douyinWEB/frontend -czf {TGZ} .next ls -lh {TGZ} curl -sS -m 10 -o /dev/null -w 'home:%{{http_code}}\n' http://127.0.0.1:4015/ echo DY_DONE """, ) if code != 0: dy.close() raise SystemExit(f"dy fail {code}") sftp = dy.open_sftp() buf = io.BytesIO() sftp.getfo(TGZ, buf) sftp.close() dy.close() print(f"tarball={buf.tell()}", flush=True) print("=== DYLIVE ===", flush=True) dl = connect(DYLIVE) for rel in FILES: put(dl, ROOT / rel, Path(rel).name) buf.seek(0) sftp = dl.open_sftp() 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=$BEFORE install -m 0644 /tmp/youtube_relay_pipeline.py /opt/douyinWEB/backend/app/youtube_relay_pipeline.py install -m 0644 /tmp/pb_sync.py /opt/douyinWEB/backend/app/pb_sync.py install -m 0644 /tmp/youtube_neko.py /opt/douyinWEB/backend/app/youtube_neko.py install -m 0644 /tmp/main.py /opt/douyinWEB/backend/app/main.py install -m 0644 /tmp/api.js /opt/douyinWEB/frontend/src/lib/api.js install -m 0644 /tmp/douyinLive.ts /opt/douyinWEB/frontend/src/lib/douyinLive.ts install -m 0644 /tmp/YtAssignPanel.tsx /opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx install -m 0644 /tmp/page.tsx /opt/douyinWEB/frontend/src/app/page.tsx cd /opt/douyinWEB/backend ./.venv/bin/python -c 'from app import pb_sync; assert pb_sync._PB_TIMEOUT_SEC==6.0; print("ok")' systemctl restart douyinweb-api sleep 4 curl -sS -m 10 -o /dev/null -w 'health:%{{http_code}}\n' http://127.0.0.1:8014/api/health cd /opt/douyinWEB/frontend rm -rf .next.bak-perf [ -d .next ] && mv .next .next.bak-perf || true tar -xzf {TGZ} systemctl restart douyinweb-web sleep 2 curl -sS -m 10 -o /dev/null -w 'home:%{{http_code}}\n' http://127.0.0.1:4015/ AFTER=$(docker ps --filter name=douyinweb-neko --format '{{{{.Names}}}}' | sort | tr '\n' ' ') echo AFTER=$AFTER free -m | head -2 echo DYLIVE_DONE """, t=300, ) dl.close() if code != 0: raise SystemExit(f"dylive fail {code}") print("ALL_DONE", flush=True)