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

144 lines
4.5 KiB
Python

#!/usr/bin/env python3
"""Finish perf deploy after API slow start; build FE; push dylive."""
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"
TGZ = "/tmp/dy_next_perf.tgz"
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("retry", host, i, e, flush=True)
time.sleep(3 + i)
raise SystemExit("ssh_fail")
def run(c, cmd: str, t: int = 1800) -> tuple[int, str]:
print(f"\n===== {cmd[:100].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[-14000:] if len(out) > 14000 else out, flush=True)
return code, out
print("=== DY finish ===", flush=True)
dy = connect("108.174.50.68")
code, _ = run(
dy,
rf"""
set +e
systemctl is-active douyinweb-api
journalctl -u douyinweb-api -n 40 --no-pager | tail -40
# wait for listen
for i in $(seq 1 30); do
code=$(curl -sS -m 3 -o /dev/null -w '%{{http_code}}' http://127.0.0.1:8014/api/health 2>/dev/null || echo 000)
echo wait_$i=$code
if [ "$code" = "200" ]; then break; fi
if [ "$i" = "5" ]; then systemctl restart douyinweb-api; fi
sleep 2
done
set -e
curl -sS -m 10 -o /dev/null -w 'health:%{{http_code}}\n' http://127.0.0.1:8014/api/health
cd /opt/douyinWEB/backend
./.venv/bin/python -c 'from app import pb_sync, youtube_relay_pipeline; print(pb_sync._PB_TIMEOUT_SEC, youtube_relay_pipeline.YoutubeRelayPipeline()._STATUS_CACHE_TTL_SEC)'
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)
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",
]
print("=== DYLIVE ===", flush=True)
dl = connect("107.173.30.245")
sftp = dl.open_sftp()
for rel in FILES:
raw = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
with sftp.file(f"/tmp/{Path(rel).name}", "wb") as f:
f.write(raw)
buf.seek(0)
with sftp.file(TGZ, "wb") as f:
f.write(buf.getvalue())
sftp.close()
code, _ = run(
dl,
rf"""
set +e
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
systemctl restart douyinweb-api
for i in $(seq 1 25); do
code=$(curl -sS -m 3 -o /dev/null -w '%{{http_code}}' http://127.0.0.1:8014/api/health 2>/dev/null || echo 000)
echo wait_$i=$code
[ "$code" = "200" ] && break
sleep 2
done
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)