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

117 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Build FE on dy, ship .next to dylive — no pkill."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(r"c:\Users\admin\Desktop\douyinWEB")
PASS = "Xiao4669805"
def connect(host):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(8):
try:
print(f"ssh {host} {i}", flush=True)
c.connect(host, 22, "root", PASS, timeout=30, allow_agent=False, look_for_keys=False)
return c
except Exception as e:
print(e, flush=True)
time.sleep(1 + i)
raise SystemExit("ssh_fail")
def run(c, cmd, timeout=700):
print(f"$ {cmd[:100]}...", flush=True)
_i, o, e = c.exec_command(cmd, timeout=timeout, get_pty=True)
chan = o.channel
t0 = time.time()
while True:
if chan.recv_ready():
sys.stdout.write(chan.recv(8192).decode("utf-8", "replace"))
sys.stdout.flush()
if chan.exit_status_ready():
while chan.recv_ready():
sys.stdout.write(chan.recv(8192).decode("utf-8", "replace"))
sys.stdout.flush()
break
if time.time() - t0 > timeout:
print("TIMEOUT", flush=True)
break
time.sleep(0.15)
code = chan.recv_exit_status()
print(f"\n[exit {code}]", flush=True)
return code
print("=== dy build ===", flush=True)
c = connect("108.174.50.68")
# ensure panel source present
sftp = c.open_sftp()
data = (ROOT / "frontend/src/components/YtRelayProStatusPanel.tsx").read_bytes()
with sftp.file("/opt/douyinWEB/frontend/src/components/YtRelayProStatusPanel.tsx", "wb") as f:
f.write(data)
sftp.close()
code = run(
c,
r"""
set -e
cd /opt/douyinWEB/frontend
grep -n '若 Live 开在远程' src/components/YtRelayProStatusPanel.tsx | head -3
export NODE_OPTIONS=--max-old-space-size=3072
npm run build
systemctl restart douyinweb-web
echo web=$(systemctl is-active douyinweb-web)
tar -C /opt/douyinWEB/frontend -czf /tmp/next-evidence.tgz .next
ls -lh /tmp/next-evidence.tgz
""",
timeout=700,
)
if code != 0:
raise SystemExit(f"build_fail {code}")
print("fetch...", flush=True)
sftp = c.open_sftp()
sftp.get("/tmp/next-evidence.tgz", str(ROOT / "scripts/_next-evidence.tgz"))
sftp.close()
c.close()
print("=== dylive apply ===", flush=True)
c = connect("107.173.30.245")
sftp = c.open_sftp()
local = ROOT / "scripts/_next-evidence.tgz"
last = [0.0]
def cb(sent, total):
now = time.time()
if now - last[0] >= 2 or sent == total:
print(f" upload {100*sent/total:.0f}%", flush=True)
last[0] = now
sftp.put(str(local), "/tmp/next-evidence.tgz", callback=cb)
# also panel src
with sftp.file("/opt/douyinWEB/frontend/src/components/YtRelayProStatusPanel.tsx", "wb") as f:
f.write(data)
sftp.close()
run(
c,
r"""
set -e
systemctl stop douyinweb-web || true
rm -rf /opt/douyinWEB/frontend/.next.bak_ev
mv /opt/douyinWEB/frontend/.next /opt/douyinWEB/frontend/.next.bak_ev 2>/dev/null || true
tar -C /opt/douyinWEB/frontend -xzf /tmp/next-evidence.tgz
systemctl start douyinweb-web
sleep 2
echo web=$(systemctl is-active douyinweb-web)
""",
timeout=120,
)
c.close()
print("FE_DONE", flush=True)