douyinWEB/scripts/_deploy_neko_vps_opt.py
eric d409e416da Ship physical dual-edge stack, Studio CDP live metrics, and ops tooling.
Capture Neko/WG/network hubs, Studio viewer charts, physical deploy docs, and one-shot scripts so VPS vs physical can be toggled later without losing WIP.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-04 22:42:40 -05:00

126 lines
4.2 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy optimized Neko (US VPS / sh2-aligned) to dy.nomadro.live."""
from __future__ import annotations
import time
from pathlib import Path
import paramiko
HOST = "108.174.50.68"
ROOT = Path(__file__).resolve().parents[1]
APP = "/opt/douyinWEB"
FILES = [
"backend/app/youtube_neko.py",
"backend/app/config.py",
"frontend/src/components/YtRelayProStatusPanel.tsx",
]
REMOTE = r"""#!/usr/bin/env bash
set -euo pipefail
source /etc/douyinweb.env 2>/dev/null || true
export DY_PUBLIC_BASE_URL="${PUBLIC_BASE:-https://dy.nomadro.live}"
export DY_NEKO_NAT1TO1="${DY_NEKO_NAT1TO1:-108.174.50.68}"
# docker-compose binary
if ! command -v docker-compose >/dev/null 2>&1 && ! docker compose version >/dev/null 2>&1; then
curl -fsSL https://github.com/docker/compose/releases/download/v2.29.7/docker-compose-linux-x86_64 \
-o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
fi
# WebRTC firewall (HTTP stays behind nginx)
if command -v ufw >/dev/null 2>&1; then
ufw allow 52300:52331/tcp || true
ufw allow 52300:52331/udp || true
ufw allow 52000:52098/udp || true
ufw allow 52101:52199/udp || true
ufw allow 52201:52299/udp || true
fi
chmod -R 777 /opt/douyinWEB/backend/data/neko/profiles 2>/dev/null || true
# Remove old path-prefix containers so compose recreates cleanly
docker rm -f douyinweb-neko-1 douyinweb-neko-2 2>/dev/null || true
cd /opt/douyinWEB/backend
.venv/bin/python - <<'PY'
from app.db import init_db
from app import yt_assign_db, youtube_neko
init_db()
groups = yt_assign_db.list_groups(include_urls=False)
print("groups", len(groups), "neko_ids", [g.get("neko_instance_id") for g in groups])
# Force re-render by deleting compose if present
cp = youtube_neko._compose_path()
if cp.is_file():
cp.unlink()
r = youtube_neko.sync_for_groups(groups)
print("sync", r)
print("nat1to1", youtube_neko._detect_public_ip())
# Start enabled ones; also start #1 for smoke test if any group exists
for g in groups:
nid = int(g.get("neko_instance_id") or 0)
if nid < 1:
continue
if g.get("neko_enabled") or nid == 1:
print("start", nid, youtube_neko.instance_action(nid, "start"))
for i in range(1, (r.get("desired") or 0) + 1):
s = youtube_neko._container_state(i)
print("state", i, s.get("status"), "ready", s.get("ready"), "ports", s.get("http_port"), s.get("webrtc_mux"), s.get("udp_range"))
PY
nginx -t && systemctl reload nginx
docker ps -a --filter name=douyinweb-neko --format 'table {{.Names}}\t{{.Status}}\t{{.Ports}}'
echo "=== curl checks ==="
curl -sS -o /dev/null -w 'local_root=%{http_code}\n' http://127.0.0.1:9200/ || true
curl -sS -o /dev/null -w 'nginx_neko=%{http_code}\n' -k https://127.0.0.1/neko/1/ -H 'Host: dy.nomadro.live' || true
echo "=== build frontend ==="
cd /opt/douyinWEB/frontend && npm run build
systemctl restart douyinweb-api douyinweb-web
sleep 4
systemctl is-active douyinweb-api douyinweb-web
echo DONE
"""
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, 22, "root", "Xiao4669805", timeout=45, allow_agent=False, look_for_keys=False)
sftp = c.open_sftp()
for rel in FILES:
local = ROOT / rel
remote = f"{APP}/{rel.replace(chr(92), '/')}"
print(f"upload {rel}")
sftp.put(str(local), remote)
with sftp.file("/tmp/deploy_neko_vps.sh", "w") as f:
f.write(REMOTE)
sftp.chmod("/tmp/deploy_neko_vps.sh", 0o755)
sftp.close()
chan = c.get_transport().open_session()
chan.settimeout(1200)
chan.get_pty(width=200, height=50)
chan.exec_command("bash /tmp/deploy_neko_vps.sh")
while True:
if chan.recv_ready():
print(chan.recv(65535).decode("utf-8", "replace"), end="", flush=True)
if chan.recv_stderr_ready():
print(chan.recv_stderr(65535).decode("utf-8", "replace"), end="", flush=True)
if chan.exit_status_ready() and not chan.recv_ready() and not chan.recv_stderr_ready():
break
time.sleep(0.3)
while chan.recv_ready():
print(chan.recv(65535).decode("utf-8", "replace"), end="", flush=True)
code = chan.recv_exit_status()
c.close()
print(f"\nexit {code}")
return code
if __name__ == "__main__":
raise SystemExit(main())