nomadro/apps/admin/deploy/_push_audit_free_isolate.py
Eric 60606e53a7 Isolate WeChat audit books as always-free from paid unlocks.
Keep review-mode content off nomad VIP/purchase state, document the rule in README, and add the 82 API push script.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-22 20:04:22 +08:00

109 lines
3.5 KiB
Python

# -*- coding: utf-8 -*-
"""Push audit-book free + pay isolation patches → 82 nomadmini-api (backend only)."""
from __future__ import annotations
import io
import tarfile
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/domains/books/products.py", "domains/books/products.py"),
("apps/api/services/ebook_svc/domains/books/__init__.py", "domains/books/__init__.py"),
("apps/api/services/ebook_svc/routers/books.py", "routers/books.py"),
("apps/api/services/ebook_svc/routers/pay.py", "routers/pay.py"),
("apps/api/services/ebook_svc/routers/admin.py", "routers/admin.py"),
("apps/api/services/ebook_svc/services/store.py", "services/store.py"),
]
def rewrite(text: str) -> str:
return text.replace("ebook_svc.", "app.")
def pack() -> bytes:
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="api/" + dest)
info.size = len(data)
tar.addfile(info, io.BytesIO(data))
print("pack", dest, flush=True)
buf.seek(0)
blob = buf.read()
print("blob", len(blob), flush=True)
return blob
REMOTE_CMD = r"""
set -e
cd /tmp
rm -rf audit-free-api && mkdir audit-free-api
tar -xzf audit-free-api.tar.gz -C audit-free-api
B=/opt/nomadmini/backend/app
for f in domains/books/products.py domains/books/__init__.py routers/books.py routers/pay.py routers/admin.py services/store.py; do
sudo cp -a "$B/$f" "$B/$f.bak-audit-free" 2>/dev/null || true
sudo mkdir -p "$(dirname "$B/$f")"
sudo cp "audit-free-api/api/$f" "$B/$f"
echo copied $f
done
cd /opt/nomadmini/backend
.venv/bin/python - <<'PY'
from app.domains.books import is_audit_book_id, is_book_free
assert is_audit_book_id('audit')
assert is_audit_book_id('audit-v2')
assert not is_audit_book_id('nomad')
assert is_book_free({'id': 'audit', 'unlock_percent': 30})
print('audit_free_ok')
PY
sudo systemctl restart nomadmini-api
sleep 3
systemctl is-active nomadmini-api
curl -sk -o /dev/null -w 'health %{http_code}\n' https://api.nomadro.cn/health
curl -sk 'https://api.nomadro.cn/api/ebook/books/audit' | python3 -c 'import sys,json; d=json.load(sys.stdin); print("audit", d.get("id"), "free", d.get("is_free"), "pct", d.get("unlock_percent"), "fen", d.get("price_fen"))'
rm -rf /tmp/audit-free-api /tmp/audit-free-api.tar.gz
"""
def main() -> int:
blob = pack()
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/audit-free-api.tar.gz", "wb") as rf:
rf.write(blob)
sftp.close()
_, stdout, stderr = c.exec_command(REMOTE_CMD, timeout=180)
out = stdout.read().decode("utf-8", "replace")
err = stderr.read().decode("utf-8", "replace")
print(out)
if err.strip():
print("STDERR", err)
c.close()
if "audit_free_ok" not in out:
return 2
if "active" not in out:
return 3
return 0
if __name__ == "__main__":
raise SystemExit(main())