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>
172 lines
4.7 KiB
Python
172 lines
4.7 KiB
Python
# -*- coding: utf-8 -*-
|
|
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]
|
|
VPS = "107.173.30.245"
|
|
PW = "4669805"
|
|
|
|
|
|
def ssh(port=4014, user="eric", password=PW):
|
|
last = None
|
|
for i in range(6):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
VPS,
|
|
port,
|
|
user,
|
|
password,
|
|
timeout=45,
|
|
banner_timeout=60,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
)
|
|
return c
|
|
except Exception as ex:
|
|
last = ex
|
|
print(f"ssh fail{i} {type(ex).__name__}", flush=True)
|
|
time.sleep(2)
|
|
raise last # type: ignore[misc]
|
|
|
|
|
|
def run(c, cmd, timeout=90):
|
|
_, o, e = c.exec_command(cmd, timeout=timeout)
|
|
return (o.read() + e.read()).decode("utf-8", "replace")
|
|
|
|
|
|
RECREATE = r"""
|
|
import os, sys
|
|
sys.path.insert(0, "/opt/douyinWEB/backend")
|
|
os.chdir("/opt/douyinWEB/backend")
|
|
os.environ["DY_NEKO_HTTP_BIND"] = "0.0.0.0"
|
|
os.environ["DY_NEKO_EDGE_UPSTREAM"] = "10.8.0.2"
|
|
os.environ["DY_NEKO_NAT1TO1"] = "107.173.30.245"
|
|
os.environ["DY_PUBLIC_BASE_URL"] = "https://dy.nomadro.com"
|
|
from app import youtube_neko
|
|
print("bind", youtube_neko._http_bind())
|
|
# force compose rewrite
|
|
p = youtube_neko._compose_path()
|
|
if p.exists():
|
|
p.unlink()
|
|
r = youtube_neko.ensure_capacity(1, start_instances={1}, reconcile_running=True)
|
|
print("ensure", r)
|
|
print("state", youtube_neko._container_state(1))
|
|
print("caddy", youtube_neko.sync_caddy_neko_snippet(4))
|
|
"""
|
|
|
|
g = ssh()
|
|
sftp = g.open_sftp()
|
|
sftp.put(str(ROOT / "backend/app/youtube_neko.py"), "/tmp/_up_youtube_neko.py")
|
|
sftp.put(str(ROOT / "backend/app/config.py"), "/tmp/_up_config.py")
|
|
with sftp.file("/tmp/_recreate_neko.py", "w") as f:
|
|
f.write(RECREATE)
|
|
sftp.close()
|
|
print("uploaded", flush=True)
|
|
|
|
print("STEP install", flush=True)
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{PW}' | sudo -S -p '' install -m 644 /tmp/_up_youtube_neko.py /opt/douyinWEB/backend/app/youtube_neko.py",
|
|
30,
|
|
),
|
|
flush=True,
|
|
)
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{PW}' | sudo -S -p '' install -m 644 /tmp/_up_config.py /opt/douyinWEB/backend/app/config.py",
|
|
30,
|
|
),
|
|
flush=True,
|
|
)
|
|
|
|
print("STEP env", flush=True)
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{PW}' | sudo -S -p '' bash -c "
|
|
"'grep -q DY_NEKO_HTTP_BIND /etc/systemd/system/douyinweb-api.service || "
|
|
"sed -i \"/DY_NEKO_NAT1TO1=/a Environment=DY_NEKO_HTTP_BIND=0.0.0.0\" /etc/systemd/system/douyinweb-api.service; "
|
|
"grep DY_NEKO /etc/systemd/system/douyinweb-api.service'",
|
|
30,
|
|
),
|
|
flush=True,
|
|
)
|
|
print(run(g, f"echo '{PW}' | sudo -S -p '' systemctl daemon-reload", 30), flush=True)
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{PW}' | sudo -S -p '' bash -c 'systemctl restart douyinweb-api >/tmp/api-restart.log 2>&1 & echo BG'",
|
|
20,
|
|
),
|
|
flush=True,
|
|
)
|
|
g.close()
|
|
|
|
for i in range(12):
|
|
time.sleep(5)
|
|
g = ssh()
|
|
st = run(g, "systemctl is-active douyinweb-api; curl -sS -m 3 -o /dev/null -w '%{http_code}' http://127.0.0.1:8014/api/health || echo fail", 20)
|
|
g.close()
|
|
print(f"api[{i}] {st.strip()}", flush=True)
|
|
if "active" in st and "200" in st:
|
|
break
|
|
|
|
print("STEP recreate", flush=True)
|
|
g = ssh()
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{PW}' | sudo -S -p '' "
|
|
"env DY_NEKO_HTTP_BIND=0.0.0.0 DY_NEKO_EDGE_UPSTREAM=10.8.0.2 "
|
|
"DY_NEKO_NAT1TO1=107.173.30.245 DY_PUBLIC_BASE_URL=https://dy.nomadro.com "
|
|
"/opt/douyinWEB/backend/.venv/bin/python /tmp/_recreate_neko.py",
|
|
300,
|
|
),
|
|
flush=True,
|
|
)
|
|
print(
|
|
run(
|
|
g,
|
|
f"echo '{PW}' | sudo -S -p '' bash -c "
|
|
"'docker ps --filter name=douyinweb-neko-1 --format \"{{.Status}} {{.Ports}}\"; "
|
|
"ss -lntp | grep 9200 || true'",
|
|
30,
|
|
),
|
|
flush=True,
|
|
)
|
|
print(
|
|
run(
|
|
g,
|
|
"curl -sS -m 5 -o /dev/null -w 'loop:%{http_code}\n' http://127.0.0.1:9200/; "
|
|
"curl -sS -m 5 -o /dev/null -w 'wg:%{http_code}\n' http://10.8.0.2:9200/",
|
|
20,
|
|
),
|
|
flush=True,
|
|
)
|
|
g.close()
|
|
|
|
v = ssh(22, "root", "Xiao4669805")
|
|
print(
|
|
run(
|
|
v,
|
|
"curl -sS -m 10 -o /dev/null -w 'vps_wg:%{http_code}\n' http://10.8.0.2:9200/; "
|
|
"curl -sS -m 10 -o /dev/null -w 'neko1:%{http_code}\n' https://dy.nomadro.com/neko/1/; "
|
|
"curl -sS -m 8 -o /dev/null -w 'network:%{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",
|
|
40,
|
|
),
|
|
flush=True,
|
|
)
|
|
v.close()
|
|
print("DONE", flush=True)
|