Document test9 tag 2026-11-02 for upload; POST config pull + WS, fixed dark home, no debug bar. Co-authored-by: Cursor <cursoragent@cursor.com>
165 lines
5.3 KiB
Python
165 lines
5.3 KiB
Python
# -*- coding: utf-8 -*-
|
||
"""Deploy config SSE/WebSocket realtime to api.nomadro.cn (nomadmini-api)."""
|
||
from __future__ import annotations
|
||
|
||
import io
|
||
import json
|
||
import tarfile
|
||
import time
|
||
import urllib.request
|
||
from pathlib import Path
|
||
|
||
import paramiko
|
||
|
||
REPO = Path(__file__).resolve().parents[3]
|
||
HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805"
|
||
|
||
FILES = [
|
||
(
|
||
"apps/api/services/ebook_svc/services/config_live.py",
|
||
"services/config_live.py",
|
||
),
|
||
(
|
||
"apps/api/services/ebook_svc/routers/public_config.py",
|
||
"routers/public_config.py",
|
||
),
|
||
(
|
||
"apps/api/services/ebook_svc/services/weapp_switches.py",
|
||
"services/weapp_switches.py",
|
||
),
|
||
]
|
||
|
||
|
||
def rewrite(text: str) -> str:
|
||
return text.replace("ebook_svc.", "app.")
|
||
|
||
|
||
def main() -> int:
|
||
buf = io.BytesIO()
|
||
with tarfile.open(fileobj=buf, mode="w:gz") as tar:
|
||
for rel, dest in FILES:
|
||
src = REPO / rel
|
||
text = rewrite(src.read_text(encoding="utf-8"))
|
||
if "ebook_svc." in text:
|
||
raise SystemExit(f"rewrite failed {rel}")
|
||
data = text.encode("utf-8")
|
||
info = tarfile.TarInfo(name=dest)
|
||
info.size = len(data)
|
||
tar.addfile(info, io.BytesIO(data))
|
||
blob = buf.getvalue()
|
||
print("pack", len(blob), flush=True)
|
||
|
||
c = paramiko.SSHClient()
|
||
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
||
c.connect(HOST, username=USER, password=PASS, timeout=60, allow_agent=False, look_for_keys=False)
|
||
sftp = c.open_sftp()
|
||
with sftp.file("/tmp/config-rt.tar.gz", "wb") as rf:
|
||
rf.write(blob)
|
||
sftp.close()
|
||
|
||
cmd = r"""
|
||
set -e
|
||
cd /tmp
|
||
rm -rf config-rt && mkdir config-rt
|
||
tar -xzf config-rt.tar.gz -C config-rt
|
||
B=/opt/nomadmini/backend/app
|
||
TS=$(date -u +%Y%m%dT%H%M%SZ)
|
||
sudo mkdir -p "$B/services" "$B/routers"
|
||
for f in services/config_live.py routers/public_config.py services/weapp_switches.py; do
|
||
if [ -f "$B/$f" ]; then sudo cp "$B/$f" "$B/$f.bak-$TS"; fi
|
||
sudo cp "config-rt/$f" "$B/$f"
|
||
echo copied $f
|
||
done
|
||
|
||
# nginx websocket:写到 /tmp 再 sudo 覆盖
|
||
sudo python3 - <<'PY'
|
||
from pathlib import Path
|
||
import shutil
|
||
src = Path("/www/server/panel/vhost/nginx/api.nomadro.cn.conf")
|
||
if not src.is_file():
|
||
print("nginx_ws_skip_no_conf")
|
||
raise SystemExit(0)
|
||
text = src.read_text(encoding="utf-8")
|
||
if "location /api/config/ws" in text:
|
||
print("nginx_ws_ok")
|
||
raise SystemExit(0)
|
||
upstream = "http://127.0.0.1:8000"
|
||
for line in text.splitlines():
|
||
s = line.strip()
|
||
if s.startswith("proxy_pass ") and "127.0.0.1" in s:
|
||
upstream = s.split("proxy_pass", 1)[1].strip().rstrip(";")
|
||
break
|
||
snippet = (
|
||
"\n location /api/config/ws {\n"
|
||
f" proxy_pass {upstream};\n"
|
||
" proxy_http_version 1.1;\n"
|
||
" proxy_set_header Upgrade $http_upgrade;\n"
|
||
' proxy_set_header Connection "upgrade";\n'
|
||
" proxy_set_header Host $host;\n"
|
||
" proxy_set_header X-Real-IP $remote_addr;\n"
|
||
" proxy_read_timeout 3600s;\n"
|
||
" proxy_send_timeout 3600s;\n"
|
||
" }\n"
|
||
" location /api/ebook/config/ws {\n"
|
||
f" proxy_pass {upstream};\n"
|
||
" proxy_http_version 1.1;\n"
|
||
" proxy_set_header Upgrade $http_upgrade;\n"
|
||
' proxy_set_header Connection "upgrade";\n'
|
||
" proxy_set_header Host $host;\n"
|
||
" proxy_set_header X-Real-IP $remote_addr;\n"
|
||
" proxy_read_timeout 3600s;\n"
|
||
" proxy_send_timeout 3600s;\n"
|
||
" }\n"
|
||
)
|
||
idx = text.find("location /")
|
||
if idx < 0:
|
||
idx = text.rfind("}")
|
||
if idx <= 0:
|
||
print("nginx_ws_skip")
|
||
raise SystemExit(0)
|
||
out = text[:idx] + snippet + "\n" + text[idx:]
|
||
bak = src.with_suffix(src.suffix + ".bak-config-ws")
|
||
shutil.copy2(src, bak)
|
||
src.write_text(out, encoding="utf-8")
|
||
print("nginx_ws_patched", upstream)
|
||
PY
|
||
sudo nginx -t && sudo nginx -s reload || true
|
||
|
||
cd /opt/nomadmini/backend
|
||
.venv/bin/python - <<'PY'
|
||
from app.services import config_live
|
||
from app.routers import public_config
|
||
print("import_ok", config_live.read_config_meta())
|
||
PY
|
||
|
||
sudo systemctl restart nomadmini-api
|
||
sleep 4
|
||
systemctl is-active nomadmini-api
|
||
curl -sk -o /dev/null -w "health %{http_code}\n" https://api.nomadro.cn/health
|
||
curl -sk -o /dev/null -w "config %{http_code}\n" https://api.nomadro.cn/api/config
|
||
curl -sk -o /dev/null -w "meta %{http_code}\n" https://api.nomadro.cn/api/config/meta
|
||
# SSE hello (2s)
|
||
timeout 3 curl -sk -N -H 'Accept: text/event-stream' https://api.nomadro.cn/api/config/stream | head -c 400 || true
|
||
echo
|
||
rm -rf /tmp/config-rt /tmp/config-rt.tar.gz
|
||
"""
|
||
_, stdout, _ = c.exec_command(cmd, timeout=180, get_pty=True)
|
||
while not stdout.channel.exit_status_ready():
|
||
if stdout.channel.recv_ready():
|
||
print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True)
|
||
time.sleep(0.05)
|
||
while stdout.channel.recv_ready():
|
||
print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True)
|
||
code = stdout.channel.recv_exit_status()
|
||
c.close()
|
||
|
||
# local meta check
|
||
meta = json.loads(urllib.request.urlopen("https://api.nomadro.cn/api/config/meta", timeout=15).read())
|
||
print("META", meta, flush=True)
|
||
print(f"[exit {code}] config realtime deployed", flush=True)
|
||
return code
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|