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>
115 lines
4.1 KiB
Python
115 lines
4.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""Deploy weapp switches API + admin SPA."""
|
|
from __future__ import annotations
|
|
|
|
import subprocess
|
|
import tarfile
|
|
import time
|
|
from io import BytesIO
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
REPO = ROOT.parents[1]
|
|
HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805"
|
|
|
|
API_FILES = [
|
|
(REPO / "apps/api/services/ebook_svc/services/weapp_switches.py", "services/weapp_switches.py"),
|
|
(REPO / "apps/api/services/ebook_svc/routers/weapp_switches_admin.py", "routers/weapp_switches_admin.py"),
|
|
(REPO / "apps/api/services/ebook_svc/services/store.py", "services/store.py"),
|
|
(REPO / "apps/api/services/ebook_svc/main.py", "main.py"),
|
|
]
|
|
|
|
|
|
def to_app(src: Path) -> bytes:
|
|
return src.read_text(encoding="utf-8").replace("ebook_svc.", "app.").encode("utf-8")
|
|
|
|
|
|
def main() -> int:
|
|
print("vite build…", flush=True)
|
|
r = subprocess.run(
|
|
["pnpm", "exec", "vite", "build"],
|
|
cwd=ROOT,
|
|
shell=True,
|
|
capture_output=True,
|
|
encoding="utf-8",
|
|
errors="replace",
|
|
)
|
|
if r.returncode != 0:
|
|
print(r.stdout)
|
|
print(r.stderr)
|
|
return r.returncode
|
|
|
|
dist = ROOT / "dist"
|
|
buf = BytesIO()
|
|
with tarfile.open(fileobj=buf, mode="w:gz") as tar:
|
|
for p in dist.rglob("*"):
|
|
if p.is_file():
|
|
tar.add(p, arcname="admin-dist/" + p.relative_to(dist).as_posix())
|
|
for src, rel in API_FILES:
|
|
data = to_app(src)
|
|
info = tarfile.TarInfo(name="api/" + rel.replace("/", "__"))
|
|
info.size = len(data)
|
|
tar.addfile(info, BytesIO(data))
|
|
buf.seek(0)
|
|
print("pack", buf.getbuffer().nbytes, 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/weapp-switches.tar.gz", "wb") as rf:
|
|
rf.write(buf.read())
|
|
sftp.close()
|
|
|
|
cmd = r"""
|
|
set -e
|
|
cd /tmp
|
|
rm -rf sw-unpack && mkdir sw-unpack
|
|
tar -xzf weapp-switches.tar.gz -C sw-unpack
|
|
sudo rsync -a --delete sw-unpack/admin-dist/ /www/wwwroot/admin.nomadro.cn/
|
|
sudo chown -R www:www /www/wwwroot/admin.nomadro.cn
|
|
B=/opt/nomadmini/backend/app
|
|
sudo mkdir -p "$B/services" "$B/routers"
|
|
sudo cp sw-unpack/api/services__weapp_switches.py "$B/services/weapp_switches.py"
|
|
sudo cp sw-unpack/api/routers__weapp_switches_admin.py "$B/routers/weapp_switches_admin.py"
|
|
sudo cp sw-unpack/api/services__store.py "$B/services/store.py"
|
|
sudo cp sw-unpack/api/main.py "$B/../main.py" 2>/dev/null || sudo cp sw-unpack/api/main.py /opt/nomadmini/backend/main.py
|
|
# main may live as app package entry
|
|
if [ -f /opt/nomadmini/backend/app/main.py ]; then
|
|
sudo cp sw-unpack/api/main.py /opt/nomadmini/backend/app/main.py
|
|
elif [ -f /opt/nomadmini/backend/main.py ]; then
|
|
sudo cp sw-unpack/api/main.py /opt/nomadmini/backend/main.py
|
|
fi
|
|
cd /opt/nomadmini/backend
|
|
.venv/bin/python - <<'PY'
|
|
from app.services.weapp_switches import read_weapp_switches
|
|
from app.routers import weapp_switches_admin
|
|
from app.services.store import store
|
|
assert hasattr(store, "ensure_book")
|
|
print("ok", "switches", list(read_weapp_switches().get("modules", {}).keys())[:3])
|
|
PY
|
|
sudo systemctl restart nomadmini-api
|
|
sleep 3
|
|
systemctl is-active nomadmini-api
|
|
curl -sk -o /dev/null -w "admin %{http_code}\n" https://admin.nomadro.cn/wechat/switches
|
|
curl -sk -o /dev/null -w "api %{http_code}\n" https://api.nomadro.cn/health
|
|
rm -rf sw-unpack weapp-switches.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()
|
|
print(f"[exit {code}]", flush=True)
|
|
c.close()
|
|
return code
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|