Hard-separate admin.hackrobot.cn (VIP ops) from admin.nomadro.cn forever; add live_ops bus, overlay tags, and deploy helpers. Co-authored-by: Cursor <cursoragent@cursor.com>
352 lines
11 KiB
Python
352 lines
11 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Local-build then deploy VIP live ops to 129 (admin.hackrobot.cn + vip.hackrobot.cn)."""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import subprocess
|
|
import sys
|
|
import tarfile
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
HOST, USER, PASSWORD = "129.204.21.129", "ubuntu", "Xiao4669805"
|
|
ADMIN_DIST = ROOT / "apps" / "admin" / "dist"
|
|
ASSETS = ROOT / "apps" / "admin" / "deploy" / "_vip_live_assets"
|
|
API_ROOT = ROOT / "apps" / "api" / "services" / "ebook_svc"
|
|
|
|
API_FILES = [
|
|
(API_ROOT / "services" / "ops_live.py", "ops_live.py"),
|
|
(API_ROOT / "routers" / "live_ops.py", "live_ops.py"),
|
|
(API_ROOT / "services" / "store.py", "store.py"),
|
|
]
|
|
|
|
REMOTE_SH = r'''#!/bin/bash
|
|
set -euo pipefail
|
|
echo '=== free ==='
|
|
free -h | head -3
|
|
|
|
B=/opt/nomadmini/backend
|
|
if [ -d "$B/app" ]; then APP="$B/app"; else APP="$B"; fi
|
|
echo "APP=$APP"
|
|
sudo mkdir -p "$APP/services" "$APP/routers"
|
|
sudo cp /tmp/ops_live.py "$APP/services/ops_live.py"
|
|
sudo cp /tmp/live_ops.py "$APP/routers/live_ops.py"
|
|
sudo cp /tmp/store.py "$APP/services/store.py"
|
|
|
|
python3 /tmp/patch_main_live_ops.py
|
|
|
|
sudo systemctl restart nomadmini-api
|
|
sleep 2
|
|
systemctl is-active nomadmini-api
|
|
curl -sS -o /dev/null -w 'api_health:%{http_code}\n' --max-time 8 http://127.0.0.1:8010/health || true
|
|
curl -sS -o /dev/null -w 'vip_stream:%{http_code}\n' --max-time 8 http://127.0.0.1:8010/api/vip/live/stream || true
|
|
|
|
R=/www/wwwroot/admin.hackrobot.cn
|
|
sudo rm -rf "$R.new"
|
|
sudo mkdir -p "$R.new"
|
|
sudo tar -xzf /tmp/admin-hackrobot.tgz -C "$R.new"
|
|
sudo rm -rf "$R.old"
|
|
sudo mv "$R" "$R.old" 2>/dev/null || true
|
|
sudo mv "$R.new" "$R"
|
|
sudo rm -rf "$R.old"
|
|
|
|
sudo mkdir -p /www/wwwroot/nomadvip/public
|
|
sudo cp /tmp/vip-live.js /www/wwwroot/nomadvip/public/vip-live.js
|
|
sudo chmod 644 /www/wwwroot/nomadvip/public/vip-live.js
|
|
|
|
EXT=/www/server/panel/vhost/nginx/extension/vip.hackrobot.cn
|
|
sudo mkdir -p "$EXT"
|
|
sudo cp /tmp/vip-live-api.conf "$EXT/vip-live-api.conf"
|
|
sudo sed -i 's/\r$//' "$EXT/vip-live-api.conf"
|
|
|
|
AEXT=/www/server/panel/vhost/nginx/extension/admin.hackrobot.cn
|
|
sudo mkdir -p "$AEXT"
|
|
sudo cp /tmp/admin-live-sse.conf "$AEXT/admin-live-sse.conf"
|
|
sudo sed -i 's/\r$//' "$AEXT/admin-live-sse.conf"
|
|
|
|
# admin site did not include extension/*.conf — wire it before catch-all /api/
|
|
sudo python3 /tmp/patch_admin_nginx_include.py
|
|
# admin.hackrobot.cn /api/ops lives on VIP Next :4007 — do NOT point catch-all /api to 8010.
|
|
# Only /api/admin/live/* stays on 8010 via extension conf.
|
|
# sudo python3 /tmp/patch_admin_api_8010.py # DISABLED — breaks VIP 运营台
|
|
|
|
PROXY=$(sudo ls /www/server/panel/vhost/nginx/proxy/vip.hackrobot.cn/*.conf 2>/dev/null | head -1 || true)
|
|
if [ -n "$PROXY" ]; then
|
|
sudo python3 /tmp/patch_vip_proxy_inject.py "$PROXY"
|
|
else
|
|
echo 'WARN: vip proxy conf missing'
|
|
fi
|
|
|
|
sudo nginx -t
|
|
sudo nginx -s reload
|
|
sleep 1
|
|
|
|
echo '=== smoke ==='
|
|
curl -sk -o /dev/null -w 'admin:%{http_code}\n' --max-time 15 https://admin.hackrobot.cn/
|
|
curl -sk -o /dev/null -w 'admin_online:%{http_code}\n' --max-time 10 https://admin.hackrobot.cn/api/admin/live/online
|
|
curl -sk -o /dev/null -w 'vip:%{http_code}\n' --max-time 15 https://vip.hackrobot.cn/
|
|
curl -sk -o /dev/null -w 'vip-live.js:%{http_code}\n' --max-time 10 https://vip.hackrobot.cn/vip-live.js
|
|
curl -sk -o /dev/null -w 'presence:%{http_code}\n' -X POST https://vip.hackrobot.cn/api/vip/live/presence \
|
|
-H 'content-type: application/json' \
|
|
-d '{"visitorId":"deploy_smoke","nick":"smoke","page":"/","role":"guest"}'
|
|
curl -sk https://vip.hackrobot.cn/ | tr '\n' ' ' | grep -o 'vip-live.js' | head -1 || echo 'WARN: html inject missing'
|
|
free -h | head -3
|
|
echo DONE
|
|
'''
|
|
|
|
PATCH_MAIN = r'''# -*- coding: utf-8 -*-
|
|
from pathlib import Path
|
|
cands = [
|
|
Path("/opt/nomadmini/backend/app/main.py"),
|
|
Path("/opt/nomadmini/backend/main.py"),
|
|
]
|
|
insert = "\nfrom app.routers import live_ops\napp.include_router(live_ops.router)\n"
|
|
for p in cands:
|
|
if not p.is_file():
|
|
continue
|
|
t = p.read_text(encoding="utf-8")
|
|
if "live_ops" in t:
|
|
print("main already has live_ops", p)
|
|
break
|
|
if "include_router(audience_admin.router)" in t:
|
|
t = t.replace(
|
|
"app.include_router(audience_admin.router)",
|
|
"app.include_router(audience_admin.router)" + insert,
|
|
1,
|
|
)
|
|
p.write_text(t, encoding="utf-8")
|
|
print("patched", p)
|
|
break
|
|
t = t + insert
|
|
p.write_text(t, encoding="utf-8")
|
|
print("appended", p)
|
|
break
|
|
else:
|
|
print("WARN: main.py not found")
|
|
'''
|
|
|
|
PATCH_PROXY = r'''# -*- coding: utf-8 -*-
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
p = Path(sys.argv[1])
|
|
raw = p.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
|
|
t = raw.decode("utf-8")
|
|
if "vip-live.js" in t:
|
|
print("inject already present")
|
|
raise SystemExit(0)
|
|
old = '<script src="/vip-track.js" defer></script></body>'
|
|
new = '<script src="/vip-track.js" defer></script><script src="/vip-live.js" defer></script></body>'
|
|
if old in t:
|
|
t = t.replace(old, new, 1)
|
|
p.write_bytes(t.encode("utf-8"))
|
|
print("inject beside vip-track")
|
|
raise SystemExit(0)
|
|
if "sub_filter" not in t:
|
|
m = re.search(r"location \^~ /\s*\{", t)
|
|
if m:
|
|
insert = (
|
|
'\n proxy_set_header Accept-Encoding "";'
|
|
"\n sub_filter_types text/html;"
|
|
"\n sub_filter '</body>' '<script src=\"/vip-live.js\" defer></script></body>';"
|
|
"\n sub_filter_once on;\n"
|
|
)
|
|
t = t[: m.end()] + insert + t[m.end() :]
|
|
p.write_bytes(t.encode("utf-8"))
|
|
print("inject sub_filter added")
|
|
raise SystemExit(0)
|
|
print("WARN: could not inject")
|
|
'''
|
|
|
|
PATCH_ADMIN_INCLUDE = r'''# -*- coding: utf-8 -*-
|
|
from pathlib import Path
|
|
p = Path("/www/server/panel/vhost/nginx/admin.hackrobot.cn.conf")
|
|
raw = p.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
|
|
t = raw.decode("utf-8")
|
|
inc = "include /www/server/panel/vhost/nginx/extension/admin.hackrobot.cn/*.conf;"
|
|
if inc in t:
|
|
print("admin include already present")
|
|
else:
|
|
for needle in (
|
|
" # Ops API -> VIP next (4007)",
|
|
" # Ops API -> nomadmini (8010).",
|
|
):
|
|
if needle in t:
|
|
t = t.replace(
|
|
needle,
|
|
" # Live ops SSE/API (more specific than /api/)\n " + inc + "\n\n" + needle,
|
|
1,
|
|
)
|
|
print("admin include inserted")
|
|
break
|
|
else:
|
|
raise SystemExit("admin nginx include needle missing")
|
|
p.write_bytes(t.encode("utf-8"))
|
|
'''
|
|
|
|
PATCH_ADMIN_API_8010 = r'''# -*- coding: utf-8 -*-
|
|
from pathlib import Path
|
|
p = Path("/www/server/panel/vhost/nginx/admin.hackrobot.cn.conf")
|
|
raw = p.read_bytes().replace(b"\r\n", b"\n").replace(b"\r", b"\n")
|
|
t = raw.decode("utf-8")
|
|
if "proxy_pass http://127.0.0.1:8010;" in t and "location ^~ /api/" in t:
|
|
# still replace any leftover 4007 under /api/
|
|
if "proxy_pass http://127.0.0.1:4007;" in t:
|
|
t = t.replace("proxy_pass http://127.0.0.1:4007;", "proxy_pass http://127.0.0.1:8010;", 1)
|
|
print("replaced leftover 4007")
|
|
else:
|
|
print("admin api already 8010")
|
|
else:
|
|
if "proxy_pass http://127.0.0.1:4007;" not in t:
|
|
raise SystemExit("admin /api proxy_pass not found")
|
|
t = t.replace("proxy_pass http://127.0.0.1:4007;", "proxy_pass http://127.0.0.1:8010;", 1)
|
|
t = t.replace(
|
|
"proxy_set_header Host 127.0.0.1;",
|
|
"proxy_set_header Host $host;\n proxy_set_header Authorization $http_authorization;",
|
|
1,
|
|
)
|
|
t = t.replace("# Ops API -> VIP next (4007)", "# Ops API -> nomadmini (8010)", 1)
|
|
print("admin api patched to 8010")
|
|
p.write_bytes(t.encode("utf-8"))
|
|
'''
|
|
|
|
VIP_LIVE_API_CONF = """location ^~ /api/vip/live/ {
|
|
proxy_pass http://127.0.0.1:8010;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header Connection '';
|
|
proxy_buffering off;
|
|
proxy_cache off;
|
|
proxy_read_timeout 3600s;
|
|
chunked_transfer_encoding off;
|
|
}
|
|
location = /vip-live.js {
|
|
root /www/wwwroot/nomadvip/public;
|
|
add_header Cache-Control "public, max-age=60";
|
|
}
|
|
"""
|
|
|
|
ADMIN_LIVE_SSE_CONF = """location ^~ /api/admin/live/ {
|
|
proxy_pass http://127.0.0.1:8010;
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header Authorization $http_authorization;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
proxy_set_header Connection '';
|
|
proxy_buffering off;
|
|
proxy_cache off;
|
|
proxy_read_timeout 3600s;
|
|
chunked_transfer_encoding off;
|
|
}
|
|
"""
|
|
|
|
|
|
def to_app(text: str) -> str:
|
|
return text.replace("ebook_svc.", "app.")
|
|
|
|
|
|
def sh_local(cmd: list[str]) -> None:
|
|
print("$", " ".join(cmd), flush=True)
|
|
# Windows: pnpm is a .ps1 shim — run via shell so PATH resolves.
|
|
subprocess.check_call(" ".join(cmd) if sys.platform == "win32" else cmd, cwd=str(ROOT), shell=sys.platform == "win32")
|
|
|
|
|
|
def connect() -> paramiko.SSHClient:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
HOST,
|
|
22,
|
|
USER,
|
|
PASSWORD,
|
|
timeout=40,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
banner_timeout=60,
|
|
)
|
|
t = c.get_transport()
|
|
if t:
|
|
t.set_keepalive(20)
|
|
return c
|
|
|
|
|
|
def remote(c: paramiko.SSHClient, cmd: str, timeout: int = 240) -> str:
|
|
print(f"$ {cmd[:180].replace(chr(10), ' ')}", flush=True)
|
|
_, o, e = c.exec_command(cmd, timeout=timeout)
|
|
out = o.read().decode("utf-8", "replace")
|
|
err = e.read().decode("utf-8", "replace")
|
|
code = o.channel.recv_exit_status()
|
|
if out.strip():
|
|
print(out[-7000:], flush=True)
|
|
if err.strip():
|
|
print(err[-2000:], file=sys.stderr, flush=True)
|
|
if code:
|
|
raise RuntimeError(f"remote failed {code}")
|
|
return out
|
|
|
|
|
|
def put_text(sftp: paramiko.SFTPClient, remote_path: str, text: str) -> None:
|
|
# Always LF — CRLF breaks bash heredocs and nginx include files on Linux.
|
|
data = text.replace("\r\n", "\n").replace("\r", "\n").encode("utf-8")
|
|
with sftp.file(remote_path, "wb") as rf:
|
|
rf.write(data)
|
|
|
|
|
|
def main() -> int:
|
|
if not (ASSETS / "vip-live.js").is_file():
|
|
print("missing vip-live.js")
|
|
return 1
|
|
|
|
print("=== local build admin ===", flush=True)
|
|
sh_local(["pnpm", "build:admin"])
|
|
if not (ADMIN_DIST / "index.html").is_file():
|
|
print("admin dist missing")
|
|
return 1
|
|
|
|
buf = io.BytesIO()
|
|
with tarfile.open(fileobj=buf, mode="w:gz") as tar:
|
|
for p in ADMIN_DIST.rglob("*"):
|
|
if p.is_file():
|
|
tar.add(p, arcname=p.relative_to(ADMIN_DIST).as_posix())
|
|
admin_blob = buf.getvalue()
|
|
print("admin pack", len(admin_blob), flush=True)
|
|
|
|
c = connect()
|
|
try:
|
|
sftp = c.open_sftp()
|
|
with sftp.file("/tmp/admin-hackrobot.tgz", "wb") as f:
|
|
f.write(admin_blob)
|
|
sftp.put(str(ASSETS / "vip-live.js"), "/tmp/vip-live.js")
|
|
for src, name in API_FILES:
|
|
text = to_app(src.read_text(encoding="utf-8"))
|
|
if "ebook_svc." in text:
|
|
raise SystemExit(f"rewrite failed: {name}")
|
|
put_text(sftp, f"/tmp/{name}", text)
|
|
print("up api", name, flush=True)
|
|
put_text(sftp, "/tmp/remote_vip_live.sh", REMOTE_SH)
|
|
put_text(sftp, "/tmp/patch_main_live_ops.py", PATCH_MAIN)
|
|
put_text(sftp, "/tmp/patch_vip_proxy_inject.py", PATCH_PROXY)
|
|
put_text(sftp, "/tmp/patch_admin_nginx_include.py", PATCH_ADMIN_INCLUDE)
|
|
put_text(sftp, "/tmp/patch_admin_api_8010.py", PATCH_ADMIN_API_8010)
|
|
put_text(sftp, "/tmp/vip-live-api.conf", VIP_LIVE_API_CONF)
|
|
put_text(sftp, "/tmp/admin-live-sse.conf", ADMIN_LIVE_SSE_CONF)
|
|
sftp.close()
|
|
|
|
remote(c, "bash /tmp/remote_vip_live.sh", timeout=240)
|
|
finally:
|
|
c.close()
|
|
|
|
print("OK deploy_vip_live_129", flush=True)
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|