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

107 lines
3.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Deploy batch2: busy split, quota UI, neko CDP watch, live align, yt-long gap."""
from __future__ import annotations
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"
HOSTS = [("dy", "108.174.50.68"), ("dylive", "107.173.30.245")]
FILES = [
("frontend/src/components/YtAssignPanel.tsx", "/opt/douyinWEB/frontend/src/components/YtAssignPanel.tsx"),
("frontend/src/app/shorts/page.tsx", "/opt/douyinWEB/frontend/src/app/shorts/page.tsx"),
("backend/app/shorts_yt_quota.py", "/opt/douyinWEB/backend/app/shorts_yt_quota.py"),
("backend/app/shorts_pipeline.py", "/opt/douyinWEB/backend/app/shorts_pipeline.py"),
("backend/app/yt_long_db.py", "/opt/douyinWEB/backend/app/yt_long_db.py"),
("scripts/linux/neko_health_watch.sh", "/opt/douyinWEB/scripts/linux/neko_health_watch.sh"),
]
REMOTE = r'''#!/bin/bash
set -euo pipefail
# Install watch script to common locations if present (no edge service touch).
for dst in /opt/douyinWEB/scripts/linux/neko_health_watch.sh /usr/local/bin/neko_health_watch.sh; do
if [ -f "$dst" ] || [ -d "$(dirname "$dst")" ]; then
install -m 755 /tmp/_neko_health_watch.sh "$dst" 2>/dev/null || cp -a /tmp/_neko_health_watch.sh "$dst"
echo "watch_installed $dst"
fi
done
# Restart only douyinweb-neko-watch if unit exists — never sing-box/frpp/wg.
if systemctl list-unit-files 2>/dev/null | grep -q '^douyinweb-neko-watch'; then
systemctl restart douyinweb-neko-watch.service || true
systemctl is-active douyinweb-neko-watch.service || true
fi
cd /opt/douyinWEB/backend
.venv/bin/python - <<'PY'
from app.shorts_yt_quota import quota_budgets_by_profile
from app import db
rows=quota_budgets_by_profile(db.get_settings_map())
for r in rows:
assert not (r.get("blocked") and r.get("exhausted_hint")), r
print("quota_split_ok", [(r["id"], r["exhausted_hint"], r["blocked"]) for r in rows])
PY
systemctl restart douyinweb-api
for i in $(seq 1 18); do
c=$(curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x)
echo health_$i=$c
[ "$c" = "200" ] && break
sleep 2
done
cd /opt/douyinWEB/frontend && npm run build 2>&1 | tail -20
systemctl restart douyinweb-web
sleep 2
systemctl is-active douyinweb-api douyinweb-web
'''
def connect(host: str):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(10):
try:
c.connect(host, 22, "root", PASS, timeout=40, allow_agent=False, look_for_keys=False, banner_timeout=90)
return c
except Exception as e:
print(f"ssh_retry {host} {i}: {e}", flush=True)
time.sleep(2 + i)
raise SystemExit(f"ssh_fail {host}")
def deploy(label: str, host: str):
print(f"\n===== {label} {host} =====", flush=True)
c = connect(host)
sftp = c.open_sftp()
for rel, dst in FILES:
data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
tmp = f"/tmp/_b2_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write(data)
if rel.endswith("neko_health_watch.sh"):
with sftp.file("/tmp/_neko_health_watch.sh", "wb") as f:
f.write(data)
_, o, e = c.exec_command(
f"mkdir -p $(dirname {dst}); cat {tmp} > {dst} && echo OK {dst}",
timeout=30,
)
print((o.read() + e.read()).decode()[:180], flush=True)
with sftp.file("/tmp/_deploy_batch2.sh", "w") as f:
f.write(REMOTE.replace("\r\n", "\n"))
sftp.close()
_, o, e = c.exec_command("bash /tmp/_deploy_batch2.sh", timeout=700)
print((o.read() + e.read()).decode("utf-8", "replace")[-5000:], flush=True)
c.close()
def main():
for label, host in HOSTS:
deploy(label, host)
if __name__ == "__main__":
main()