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

103 lines
3.6 KiB
Python

# -*- coding: utf-8 -*-
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"
FILES = [
"backend/app/wireguard_hub.py",
"backend/app/main.py",
"frontend/src/app/network/page.tsx",
]
def ssh():
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(VPS, 4014, "eric", PW, timeout=40, banner_timeout=80, allow_agent=False, look_for_keys=False)
return c
def run(c, cmd, timeout=180):
_, o, e = c.exec_command(cmd, timeout=timeout)
return (o.read() + e.read()).decode("utf-8", "replace")
def main():
g = ssh()
sftp = g.open_sftp()
installs = []
for rel in FILES:
data = (ROOT / rel).read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
tmp = f"/tmp/_up_{Path(rel).name}"
with sftp.file(tmp, "wb") as f:
f.write(data)
installs.append(f"install -m 644 {tmp} /opt/douyinWEB/{rel}")
print("put", rel, flush=True)
fe = b"""#!/bin/bash
cd /opt/douyinWEB/frontend || exit 1
export DY_INTERNAL_API_URL=http://127.0.0.1:8014
export DY_PUBLIC_BASE_URL=https://dy.nomadro.com
npm run build >/tmp/fe-wgfix.log 2>&1
echo EXIT:$? >>/tmp/fe-wgfix.log
"""
with sftp.file("/tmp/_fe_wgfix.sh", "wb") as f:
f.write(fe.replace(b"\r\n", b"\n"))
sftp.close()
print(
run(
g,
f"echo '{PW}' | sudo -S -p '' bash -c '"
+ "; ".join(installs)
+ "; rm -f /opt/douyinWEB/backend/data/wireguard/imports/wg-test-overlay.conf "
"/opt/douyinWEB/backend/data/wireguard/imports/wg-test-overlay.summary.json; "
"systemctl restart douyinweb-api; sleep 2; systemctl is-active douyinweb-api; "
"systemctl stop douyinweb-web || true; chmod 755 /tmp/_fe_wgfix.sh; rm -f /tmp/fe-wgfix.log; "
"nohup /bin/bash /tmp/_fe_wgfix.sh >/dev/null 2>&1 & echo BUILD'",
),
flush=True,
)
g.close()
for i in range(40):
time.sleep(12)
g = ssh()
st = run(g, "grep '^EXIT:' /tmp/fe-wgfix.log 2>/dev/null | tail -1")
g.close()
print(f"fe[{i}] {st.strip()}", flush=True)
if "EXIT:0" in st:
break
if "EXIT:" in st:
g = ssh()
print(run(g, "tail -40 /tmp/fe-wgfix.log"), flush=True)
g.close()
raise SystemExit("fe fail")
else:
raise SystemExit("timeout")
g = ssh()
print(run(g, f"echo '{PW}' | sudo -S -p '' systemctl start douyinweb-web; sleep 2; systemctl is-active douyinweb-web"), flush=True)
g.close()
with httpx.Client(base_url="https://dy.nomadro.com", timeout=40, follow_redirects=True) as client:
client.post("/api/auth/login", json={"username": "hackrobot", "password": "Xiao4669805@"})
net = client.get("/api/network").json()
wg = net.get("wireguard") or {}
print("active_config", wg.get("active_config_name"), flush=True)
print("system", [c.get("name") for c in (wg.get("system_configs") or [])], flush=True)
print("repo", [c.get("name") for c in (wg.get("repo_configs") or [])], flush=True)
name = wg.get("active_config_name") or "wg-dy"
cfg = client.get(f"/api/network/wireguard/config/{name}")
print("load", cfg.status_code, "bytes", len((cfg.json() or {}).get("content") or ""), flush=True)
print("DONE", flush=True)
return 0
if __name__ == "__main__":
raise SystemExit(main())