douyinWEB/scripts/_deploy_yt_revenue.py
eric fbdc3fc0db Harden dual-VPS API against live+encode CPU storms; document and ship YPP revenue.
Keep /api/health instant, isolate ffmpeg cgroups, defer clip encodes while live push runs, and align dy.live systemd with dylive. README covers the hang root cause and /yt-revenue.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-16 20:50:45 -05:00

137 lines
5.2 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy yt-revenue tab + API to dy + dylive."""
from __future__ import annotations
import time
from pathlib import Path
import paramiko
ROOT = Path(__file__).resolve().parents[1]
JUMP = "107.173.30.245"
DY = "108.174.50.68"
PASS = "Xiao4669805"
BACKEND = [
"backend/app/yt_revenue.py",
"backend/app/shorts_oauth.py",
"backend/app/main.py",
]
FRONTEND = [
"frontend/src/lib/shellNav.ts",
"frontend/src/app/yt-revenue/page.tsx",
]
def put_files(sftp, remote_prefix: str = "/tmp") -> None:
for rel in BACKEND + FRONTEND:
data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n")
name = Path(rel).name
# disambiguate page.tsx
if rel.endswith("yt-revenue/page.tsx"):
name = "yt_revenue_page.tsx"
with sftp.file(f"{remote_prefix}/_rev_{name}", "wb") as f:
f.write(data)
REMOTE_DY = r'''
set -e
cp -a /tmp/_rev_yt_revenue.py /opt/douyinWEB/backend/app/yt_revenue.py
cp -a /tmp/_rev_shorts_oauth.py /opt/douyinWEB/backend/app/shorts_oauth.py
cp -a /tmp/_rev_main.py /opt/douyinWEB/backend/app/main.py
mkdir -p /opt/douyinWEB/frontend/src/app/yt-revenue
cp -a /tmp/_rev_shellNav.ts /opt/douyinWEB/frontend/src/lib/shellNav.ts
cp -a /tmp/_rev_yt_revenue_page.tsx /opt/douyinWEB/frontend/src/app/yt-revenue/page.tsx
systemctl restart douyinweb-api
for i in $(seq 1 20); do
c=$(curl -sS -m 3 -o /dev/null -w "%{http_code}" http://127.0.0.1:8014/api/health || echo x)
echo health:$c
[ "$c" = "200" ] && break
sleep 2
done
# rebuild next if script exists
if [ -x /opt/douyinWEB/frontend/node_modules/.bin/next ] || [ -f /opt/douyinWEB/frontend/package.json ]; then
cd /opt/douyinWEB/frontend
if systemctl list-unit-files | grep -q douyinweb-web; then
npm run build >/tmp/_yt_rev_fe_build.log 2>&1 || true
systemctl restart douyinweb-web || true
elif systemctl list-unit-files | grep -q douyinweb-next; then
npm run build >/tmp/_yt_rev_fe_build.log 2>&1 || true
systemctl restart douyinweb-next || true
else
# try pm2 / generic
npm run build >/tmp/_yt_rev_fe_build.log 2>&1 || true
systemctl restart douyinweb-frontend 2>/dev/null || true
fi
tail -5 /tmp/_yt_rev_fe_build.log 2>/dev/null || true
fi
curl -sk -m 8 -o /dev/null -w "pub_api:%{http_code}\n" https://dy.nomadro.live/api/health
curl -sk -m 8 -o /dev/null -w "rev_page:%{http_code}\n" https://dy.nomadro.live/yt-revenue
curl -sS -m 20 "http://127.0.0.1:8014/api/yt-revenue/summary?days=7" | head -c 400; echo
echo DONE
'''
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(JUMP, 22, "root", PASS, timeout=40, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
put_files(sftp)
with sftp.file("/tmp/_rev_dy.sh", "w") as f:
f.write(REMOTE_DY)
sftp.close()
print("===== dy =====", flush=True)
chan = c.get_transport().open_session()
chan.exec_command(
f"SSHPASS={PASS} sshpass -e scp -o StrictHostKeyChecking=no "
"/tmp/_rev_yt_revenue.py /tmp/_rev_shorts_oauth.py /tmp/_rev_main.py "
"/tmp/_rev_shellNav.ts /tmp/_rev_yt_revenue_page.tsx /tmp/_rev_dy.sh "
f"root@{DY}:/tmp/; "
f"SSHPASS={PASS} sshpass -e ssh -o StrictHostKeyChecking=no root@{DY} "
"'bash /tmp/_rev_dy.sh > /tmp/_rev_dy.out 2>&1; echo EXIT:$? >> /tmp/_rev_dy.out'"
)
out = ""
for i in range(180):
time.sleep(3)
_, o, _ = c.exec_command(
f"SSHPASS={PASS} sshpass -e ssh -o StrictHostKeyChecking=no root@{DY} "
"'tail -c 20000 /tmp/_rev_dy.out 2>/dev/null'",
timeout=25,
)
out = o.read().decode("utf-8", "replace")
if "DONE" in out or "EXIT:" in out:
print(out[-8000:])
break
if i % 5 == 0:
print("poll", i, out[-140:].replace("\n", " | "), flush=True)
else:
print("TIMEOUT\n", out[-5000:])
print("===== dylive =====", flush=True)
_, o, e = c.exec_command(
"cp -a /tmp/_rev_yt_revenue.py /opt/douyinWEB/backend/app/yt_revenue.py; "
"cp -a /tmp/_rev_shorts_oauth.py /opt/douyinWEB/backend/app/shorts_oauth.py; "
"cp -a /tmp/_rev_main.py /opt/douyinWEB/backend/app/main.py; "
"mkdir -p /opt/douyinWEB/frontend/src/app/yt-revenue; "
"cp -a /tmp/_rev_shellNav.ts /opt/douyinWEB/frontend/src/lib/shellNav.ts; "
"cp -a /tmp/_rev_yt_revenue_page.tsx /opt/douyinWEB/frontend/src/app/yt-revenue/page.tsx; "
"systemctl restart douyinweb-api; "
"for i in 1 2 3 4 5 6 7 8 9 10 12 14 16; do "
"c=$(curl -sS -m 3 -o /dev/null -w %{http_code} http://127.0.0.1:8014/api/health || echo x); "
"echo health:$c; [ \"$c\" = 200 ] && break; sleep 2; done; "
"cd /opt/douyinWEB/frontend && npm run build >/tmp/_yt_rev_fe_build.log 2>&1 || true; "
"systemctl restart douyinweb-web 2>/dev/null || systemctl restart douyinweb-next 2>/dev/null || true; "
"tail -3 /tmp/_yt_rev_fe_build.log; "
"curl -sS -m 20 'http://127.0.0.1:8014/api/yt-revenue/summary?days=7' | head -c 300; echo",
timeout=600,
)
print((o.read() + e.read()).decode("utf-8", "replace")[-6000:])
c.close()
return 0
if __name__ == "__main__":
raise SystemExit(main())