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

165 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""Finish receiving-first panel deploy with live prints (no silent wait)."""
from __future__ import annotations
import hashlib
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"
FILES = [
("backend/app/douyin.py", "c1a660fcf8f8"), # expected may drift; we recompute
("frontend/src/components/YtRelayProStatusPanel.tsx", ""),
]
def connect(host):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
last = None
for i in range(10):
try:
print(f"ssh {host} try {i}...", flush=True)
c.connect(host, 22, "root", PASS, timeout=30, allow_agent=False, look_for_keys=False, banner_timeout=90)
print(f"ssh {host} ok", flush=True)
return c
except Exception as e:
last = e
print(f"ssh fail: {e}", flush=True)
time.sleep(1 + i)
raise SystemExit(f"ssh_fail {host}: {last}")
def run_stream(c, cmd, timeout=900):
print(f"$ {cmd[:120]}...", flush=True)
_i, o, e = c.exec_command(cmd, timeout=timeout, get_pty=True)
chan = o.channel
start = time.time()
buf = b""
while True:
if chan.recv_ready():
chunk = chan.recv(4096)
if not chunk:
break
buf += chunk
sys.stdout.write(chunk.decode("utf-8", "replace"))
sys.stdout.flush()
if chan.recv_stderr_ready():
chunk = chan.recv_stderr(4096)
sys.stdout.write(chunk.decode("utf-8", "replace"))
sys.stdout.flush()
if chan.exit_status_ready():
while chan.recv_ready():
chunk = chan.recv(4096)
buf += chunk
sys.stdout.write(chunk.decode("utf-8", "replace"))
sys.stdout.flush()
break
if time.time() - start > timeout:
print("TIMEOUT", flush=True)
break
time.sleep(0.2)
code = chan.recv_exit_status()
print(f"\n[exit {code}]", flush=True)
return code
def put(c, local: Path, remote: str):
print(f"put {local.name} -> {remote}", flush=True)
sftp = c.open_sftp()
with sftp.file(remote, "wb") as f:
f.write(local.read_bytes())
sftp.close()
# hashes
hashes = {}
for rel, _ in FILES:
p = ROOT / rel.replace("/", "\\")
h = hashlib.sha256(p.read_bytes()).hexdigest()[:12]
hashes[rel] = h
print(f"local {h} {rel}", flush=True)
# --- DY ---
print("\n=== DY ===", flush=True)
c = connect("108.174.50.68")
# kill stuck npm if any from previous deploy
run_stream(
c,
"pkill -f 'npm run build' 2>/dev/null || true; pkill -f 'next build' 2>/dev/null || true; echo cleaned; "
"ps aux | grep -E 'npm|next' | grep -v grep | head -5 || true",
timeout=30,
)
for rel, _ in FILES:
put(c, ROOT / rel.replace("/", "\\"), f"/opt/douyinWEB/{rel}")
code = run_stream(
c,
r"""
set -e
cd /opt/douyinWEB/backend
./.venv/bin/python -c "from app.douyin import pick_stream_candidates; r={'stream_url':{'flv_pull_url':{'ORIGIN':'http://x/a_uhd.flv','HD1':'http://x/a_hd.flv'}}}; assert 'a_hd' in pick_stream_candidates(r)[0]; print('non_uhd_ok')"
grep -n 'YouTube 接流中\|推流接流' /opt/douyinWEB/frontend/src/components/YtRelayProStatusPanel.tsx | head -8
systemctl restart douyinweb-api
echo api=$(systemctl is-active douyinweb-api)
cd /opt/douyinWEB/frontend
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-recv-fix.tgz .next
tar -C /opt/douyinWEB -czf /tmp/src-recv-fix.tgz backend/app/douyin.py frontend/src/components/YtRelayProStatusPanel.tsx
ls -lh /tmp/next-recv-fix.tgz /tmp/src-recv-fix.tgz
""",
timeout=700,
)
if code != 0:
raise SystemExit(f"dy_build_failed {code}")
print("fetch tarballs...", flush=True)
sftp = c.open_sftp()
sftp.get("/tmp/next-recv-fix.tgz", str(ROOT / "scripts/_next-recv-fix.tgz"))
sftp.get("/tmp/src-recv-fix.tgz", str(ROOT / "scripts/_src-recv-fix.tgz"))
sftp.close()
c.close()
print("dy done", flush=True)
# --- DYLIVE ---
print("\n=== DYLIVE ===", flush=True)
c = connect("107.173.30.245")
put(c, ROOT / "scripts/_src-recv-fix.tgz", "/tmp/src-recv-fix.tgz")
put(c, ROOT / "scripts/_next-recv-fix.tgz", "/tmp/next-recv-fix.tgz")
run_stream(
c,
r"""
set -e
tar -C /opt/douyinWEB -xzf /tmp/src-recv-fix.tgz
systemctl restart douyinweb-api
systemctl stop douyinweb-web || true
rm -rf /opt/douyinWEB/frontend/.next.bak_recv
mv /opt/douyinWEB/frontend/.next /opt/douyinWEB/frontend/.next.bak_recv 2>/dev/null || true
tar -C /opt/douyinWEB/frontend -xzf /tmp/next-recv-fix.tgz
systemctl start douyinweb-web
sleep 2
echo api=$(systemctl is-active douyinweb-api) web=$(systemctl is-active douyinweb-web)
python3 - <<'PY'
import hashlib, pathlib
for rel in ['backend/app/douyin.py','frontend/src/components/YtRelayProStatusPanel.tsx']:
p=pathlib.Path('/opt/douyinWEB')/rel
print(hashlib.sha256(p.read_bytes()).hexdigest()[:12], rel)
PY
grep -n 'YouTube 接流中\|推流接流' /opt/douyinWEB/frontend/src/components/YtRelayProStatusPanel.tsx | head -5
""",
timeout=120,
)
c.close()
print("\nALL_DONE", flush=True)
for rel, h in hashes.items():
print(f"shipped {h} {rel}", flush=True)