douyinWEB/scripts/_deploy_wg_shellcrash.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

154 lines
5.3 KiB
Python

# -*- coding: utf-8 -*-
"""Deploy WireGuard/ShellCrash hub to physical guest + rebuild network page."""
from __future__ import annotations
import sys
import time
from pathlib import Path
import httpx
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
ROOT = Path(__file__).resolve().parents[1]
VPS = "107.173.30.245"
PW = "4669805"
def ssh(port=4014, user="eric", password=PW):
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(6):
try:
c.connect(VPS, port, user, password, timeout=45, banner_timeout=90, allow_agent=False, look_for_keys=False)
return c
except Exception as ex:
print("ssh", i, type(ex).__name__, flush=True)
time.sleep(2)
raise RuntimeError("ssh")
def run(c, cmd, timeout=120):
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
FILES = [
"backend/app/wireguard_hub.py",
"backend/app/shellcrash_hub.py",
"backend/app/network_hub.py",
"backend/app/main.py",
"frontend/src/app/network/page.tsx",
]
g = ssh()
sftp = g.open_sftp()
for rel in FILES:
sftp.put(str(ROOT / rel), f"/tmp/_up_{Path(rel).name}")
print("put", rel, flush=True)
sftp.close()
print(
run(
g,
f"echo '{PW}' | sudo -S -p '' bash -c '"
"install -m 644 /tmp/_up_wireguard_hub.py /opt/douyinWEB/backend/app/wireguard_hub.py; "
"install -m 644 /tmp/_up_shellcrash_hub.py /opt/douyinWEB/backend/app/shellcrash_hub.py; "
"install -m 644 /tmp/_up_network_hub.py /opt/douyinWEB/backend/app/network_hub.py; "
"install -m 644 /tmp/_up_main.py /opt/douyinWEB/backend/app/main.py; "
"mkdir -p /opt/douyinWEB/frontend/src/app/network; "
"install -m 644 /tmp/_up_page.tsx /opt/douyinWEB/frontend/src/app/network/page.tsx; "
"cd /opt/douyinWEB/backend && .venv/bin/python -m py_compile app/wireguard_hub.py app/shellcrash_hub.py app/network_hub.py; "
"echo COMPILE_OK; "
"systemctl kill -s SIGKILL douyinweb-api || true; sleep 1; "
"systemctl reset-failed douyinweb-api || true; "
"systemctl start douyinweb-api; sleep 2; systemctl is-active douyinweb-api"
"'",
60,
),
flush=True,
)
for i in range(10):
time.sleep(2)
code = run(g, "curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo x")
print("health", code.strip(), flush=True)
if "200" in code:
break
# FE build background
print(
run(
g,
f"echo '{PW}' | sudo -S -p '' bash -c '"
"cd /opt/douyinWEB/frontend; "
"export DY_INTERNAL_API_URL=http://127.0.0.1:8014 DY_PUBLIC_BASE_URL=https://dy.nomadro.com; "
"nohup bash -c \"npm run build >/tmp/fe-net.log 2>&1; echo EXIT:$? >>/tmp/fe-net.log\" >/dev/null 2>&1 & "
"echo BUILD_STARTED'"
),
flush=True,
)
g.close()
print("=== API smoke ===", flush=True)
with httpx.Client(base_url="https://dy.nomadro.com", timeout=30.0) as client:
r = client.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"})
print("login", r.status_code, flush=True)
net = client.get("/api/network")
print("network", net.status_code, flush=True)
if net.status_code == 200:
j = net.json()
wg = j.get("wireguard") or {}
sc = j.get("shellcrash") or {}
print(
"wg_active",
wg.get("active"),
"unit",
wg.get("unit_wg_dy"),
"ifaces",
[i.get("name") for i in (wg.get("interfaces") or [])],
flush=True,
)
print("sc_running", sc.get("running"), "unit", sc.get("unit"), "mixed", sc.get("mixed_port"), flush=True)
print("recs", (wg.get("recommendations") or [])[:2], flush=True)
wg_st = client.get("/api/network/wireguard/status")
print("wg_status", wg_st.status_code, flush=True)
sc_st = client.get("/api/network/shellcrash/status")
print("sc_status", sc_st.status_code, flush=True)
# safe status actions
print("wg_iface_status", client.post("/api/network/wireguard/wg-dy/status", json={}).status_code, flush=True)
print("sc_status_post", client.post("/api/network/shellcrash/status", json={}).status_code, flush=True)
# wait FE a bit
for i in range(24):
time.sleep(10)
g = ssh()
tail = run(g, "tail -n 2 /tmp/fe-net.log 2>/dev/null; grep -q '^EXIT:' /tmp/fe-net.log && echo DONE || echo RUN")
g.close()
print(f"fe[{i}] {tail.strip()[-200:]}", flush=True)
if "DONE" in tail:
if "EXIT:0" not in tail:
g = ssh()
print(run(g, "grep '^EXIT:' /tmp/fe-net.log; tail -n 30 /tmp/fe-net.log"), flush=True)
g.close()
raise SystemExit("fe build failed")
break
else:
raise SystemExit("fe timeout")
g = ssh()
print(run(g, f"echo '{PW}' | sudo -S -p '' systemctl restart douyinweb-web; sleep 2; systemctl is-active douyinweb-web"), flush=True)
g.close()
v = ssh(22, "root", "Xiao4669805")
print(
run(
v,
"curl -sS -m 8 -o /dev/null -w 'network_page:%{http_code}\\n' https://dy.nomadro.com/network; "
"curl -sS -m 8 -o /dev/null -w 'dylive:%{http_code}\\n' https://dylive.nomadro.com/api/health",
),
flush=True,
)
v.close()
print("DONE", flush=True)