Restructure /wechat nav (VIP submenu for ebooks/course/shop/memo), rebuild ops pages with shared wx UI, and wire vip-library remote content end-to-end with README paths. Co-authored-by: Cursor <cursoragent@cursor.com>
227 lines
8.9 KiB
Python
227 lines
8.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Deploy VIP formal content library (ebooks/courses) + Admin VIP Library UI.
|
|
|
|
Additive: does not wipe remote-config / orders / platforms.tt.
|
|
Seeds vip-library.json only when missing (from package seed).
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import subprocess
|
|
import tarfile
|
|
import time
|
|
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"
|
|
EBOOK = REPO / "apps" / "api" / "services" / "ebook_svc"
|
|
SEED = EBOOK / "data" / "vip_library_seed.json"
|
|
|
|
|
|
def to_app_pkg(text: str) -> str:
|
|
return text.replace("ebook_svc.", "app.")
|
|
|
|
|
|
def main() -> int:
|
|
# refresh seed from H5 SoT
|
|
print("exporting vip_library_seed.json…", flush=True)
|
|
er = subprocess.run(
|
|
[
|
|
"node",
|
|
"--experimental-strip-types",
|
|
"--experimental-transform-types",
|
|
str(ROOT / "deploy" / "_export_vip_library_seed.mts"),
|
|
],
|
|
cwd=REPO,
|
|
capture_output=True,
|
|
text=True,
|
|
encoding="utf-8",
|
|
errors="replace",
|
|
)
|
|
print((er.stdout or "")[-400:], flush=True)
|
|
if er.returncode != 0:
|
|
print(er.stderr[-800:], flush=True)
|
|
return er.returncode or 1
|
|
if not SEED.is_file():
|
|
print("missing seed", SEED)
|
|
return 1
|
|
|
|
print("building admin…", flush=True)
|
|
r = subprocess.run(
|
|
["pnpm", "exec", "vite", "build"],
|
|
cwd=ROOT,
|
|
shell=True,
|
|
capture_output=True,
|
|
text=True,
|
|
encoding="utf-8",
|
|
errors="replace",
|
|
)
|
|
log = ((r.stdout or "") + (r.stderr or ""))[-800:]
|
|
print(log.encode("ascii", "replace").decode("ascii"), flush=True)
|
|
if r.returncode != 0:
|
|
return r.returncode or 1
|
|
|
|
dist = ROOT / "dist"
|
|
buf = io.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())
|
|
files = (
|
|
("backend/routers/vip_admin.py", EBOOK / "routers" / "vip_admin.py"),
|
|
("backend/routers/vip_library_admin.py", EBOOK / "routers" / "vip_library_admin.py"),
|
|
("backend/routers/vip_library_public.py", EBOOK / "routers" / "vip_library_public.py"),
|
|
("backend/services/vip_config.py", EBOOK / "services" / "vip_config.py"),
|
|
("backend/services/vip_content.py", EBOOK / "services" / "vip_content.py"),
|
|
("backend/services/vip_library.py", EBOOK / "services" / "vip_library.py"),
|
|
("backend/data/vip_library_seed.json", SEED),
|
|
)
|
|
for name, src in files:
|
|
data = src.read_bytes()
|
|
if name.endswith(".py"):
|
|
data = to_app_pkg(data.decode("utf-8")).encode("utf-8")
|
|
info = tarfile.TarInfo(name=name)
|
|
info.size = len(data)
|
|
tar.addfile(info, io.BytesIO(data))
|
|
|
|
payload = buf.getvalue()
|
|
print(f"pack {len(payload)/1024:.1f} KB", 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/vip-library-release.tar.gz", "wb") as rf:
|
|
rf.write(payload)
|
|
with sftp.file("/tmp/vip-library-deploy.sh", "w") as sf:
|
|
sf.write(_REMOTE_CLEAN)
|
|
sftp.close()
|
|
print("uploaded", flush=True)
|
|
|
|
_, stdout, _ = c.exec_command("bash /tmp/vip-library-deploy.sh", timeout=300, 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()
|
|
print(f"[exit {code}]", flush=True)
|
|
return code
|
|
|
|
|
|
_REMOTE_CLEAN = r"""#!/bin/bash
|
|
set -euo pipefail
|
|
cd /tmp
|
|
rm -rf vip-lib-unpack && mkdir vip-lib-unpack
|
|
tar -xzf vip-library-release.tar.gz -C vip-lib-unpack
|
|
|
|
if [ -d vip-lib-unpack/admin-dist ]; then
|
|
sudo rsync -a --delete vip-lib-unpack/admin-dist/ /www/wwwroot/admin.nomadro.cn/
|
|
sudo chown -R www:www /www/wwwroot/admin.nomadro.cn
|
|
echo admin_dist_synced
|
|
fi
|
|
|
|
API_ROOT=$(systemctl show nomadmini-api -p WorkingDirectory --value 2>/dev/null || true)
|
|
if [ -z "$API_ROOT" ] || [ "$API_ROOT" = "/" ]; then API_ROOT=/opt/nomadmini/backend; fi
|
|
B=$API_ROOT/app
|
|
[ -d "$B" ] || B=$API_ROOT/backend/app
|
|
echo API_ROOT=$API_ROOT B=$B
|
|
sudo mkdir -p "$B/routers" "$B/services" "$B/data"
|
|
|
|
sudo cp vip-lib-unpack/backend/routers/vip_admin.py "$B/routers/"
|
|
sudo cp vip-lib-unpack/backend/routers/vip_library_admin.py "$B/routers/"
|
|
sudo cp vip-lib-unpack/backend/routers/vip_library_public.py "$B/routers/"
|
|
sudo cp vip-lib-unpack/backend/services/vip_config.py "$B/services/"
|
|
sudo cp vip-lib-unpack/backend/services/vip_content.py "$B/services/"
|
|
sudo cp vip-lib-unpack/backend/services/vip_library.py "$B/services/"
|
|
sudo cp vip-lib-unpack/backend/data/vip_library_seed.json "$B/data/"
|
|
|
|
for DIR in /var/www/nomadro.cn/data /www/wwwroot/nomadro.cn/data; do
|
|
if [ -d "$DIR" ]; then
|
|
if [ ! -f "$DIR/vip-library.json" ]; then
|
|
sudo cp vip-lib-unpack/backend/data/vip_library_seed.json "$DIR/vip-library.json"
|
|
sudo chown ubuntu:ubuntu "$DIR/vip-library.json" 2>/dev/null || sudo chown www:www "$DIR/vip-library.json" 2>/dev/null || true
|
|
echo "seeded $DIR/vip-library.json"
|
|
else
|
|
echo "keep existing $DIR/vip-library.json"
|
|
fi
|
|
fi
|
|
done
|
|
|
|
MAIN="$B/main.py"
|
|
sudo python3 - <<PY
|
|
from pathlib import Path
|
|
import ast, re
|
|
p = Path("$MAIN")
|
|
t = p.read_text(encoding="utf-8")
|
|
changed = False
|
|
m = re.search(r"from app\.routers import \((.*?)\)\n", t, re.S)
|
|
if m:
|
|
names = []
|
|
for part in m.group(1).replace("\n", ",").split(","):
|
|
n = part.strip()
|
|
if n and n not in names:
|
|
names.append(n)
|
|
for req in ("vip_admin", "vip_library_admin", "vip_library_public"):
|
|
if req not in names:
|
|
names.append(req)
|
|
changed = True
|
|
block = "from app.routers import (\n" + ",\n".join(f" {n}" for n in names) + "\n)\n"
|
|
t = t[: m.start()] + block + t[m.end() :]
|
|
elif "vip_library_admin" not in t:
|
|
# single-line import → parenthesize and append
|
|
m2 = re.search(r"from app\.routers import ([^\n]+)\n", t)
|
|
if not m2:
|
|
raise SystemExit("cannot find routers import")
|
|
names = [x.strip() for x in m2.group(1).split(",") if x.strip()]
|
|
for req in ("vip_admin", "vip_library_admin", "vip_library_public"):
|
|
if req not in names:
|
|
names.append(req)
|
|
block = "from app.routers import (\n" + ",\n".join(f" {n}" for n in names) + "\n)\n"
|
|
t = t[: m2.start()] + block + t[m2.end() :]
|
|
changed = True
|
|
if "include_router(vip_library_admin.router)" not in t:
|
|
if "include_router(vip_admin.router)" in t:
|
|
t = t.replace(
|
|
"app.include_router(vip_admin.router)",
|
|
"app.include_router(vip_admin.router)\napp.include_router(vip_library_admin.router)\napp.include_router(vip_library_public.router)",
|
|
1,
|
|
)
|
|
changed = True
|
|
else:
|
|
raise SystemExit("vip_admin router missing")
|
|
ast.parse(t)
|
|
if changed:
|
|
p.write_text(t, encoding="utf-8")
|
|
print("main.py patched for vip_library")
|
|
else:
|
|
print("main.py already has vip_library")
|
|
PY
|
|
|
|
sudo systemctl restart nomadmini-api
|
|
sleep 4
|
|
systemctl is-active nomadmini-api
|
|
echo "--- smoke ---"
|
|
curl -sk -o /dev/null -w "admin %{http_code}\n" https://admin.nomadro.cn/
|
|
curl -sk -o /dev/null -w "vip_library_page %{http_code}\n" https://admin.nomadro.cn/wechat/vip/library
|
|
curl -sk -o /dev/null -w "public_course %{http_code}\n" https://api.nomadro.cn/api/vip/course
|
|
curl -sk https://api.nomadro.cn/api/vip/library/meta | python3 -c "import sys,json; d=json.load(sys.stdin); print('meta', d.get('ok'), d.get('ebookIds'), d.get('courseLessons'), d.get('hasFile'))" || true
|
|
TOK=$(curl -sk -X POST https://api.nomadro.cn/api/admin/login -H 'Content-Type: application/json' -d '{"username":"admin","password":"Xiao4669805"}' | python3 -c "import sys,json; print(json.load(sys.stdin).get('token',''))" || true)
|
|
if [ -n "$TOK" ]; then
|
|
curl -sk -H "Authorization: Bearer $TOK" https://api.nomadro.cn/api/admin/vip/library/meta | python3 -c "import sys,json; d=json.load(sys.stdin); print('admin_meta', d.get('ok'), d.get('ebookIds'), d.get('courseLessons'))"
|
|
curl -sk -H "Authorization: Bearer $TOK" https://api.nomadro.cn/api/admin/vip | python3 -c "import sys,json; d=json.load(sys.stdin); print('vip_price', d.get('vipPriceFen'), d.get('is_free'))"
|
|
fi
|
|
curl -sk https://api.nomadro.cn/api/config | python3 -c "import sys,json; d=json.load(sys.stdin); c=d.get('config') or d; print('tt_ok', 'tt' in (c.get('platforms') or {}))"
|
|
rm -rf vip-lib-unpack vip-library-release.tar.gz
|
|
echo DONE
|
|
"""
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|