Keep Douyin homeVariant BMI router intact while restoring admin join-group controls, TLS-safe config fetch, and ntfy channel labels. Co-authored-by: Cursor <cursoragent@cursor.com>
195 lines
6.5 KiB
Python
195 lines
6.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Deploy join-group admin UI + API to 82.157.112.245 (admin.nomadro.cn / nomadmini-api)."""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import re
|
|
import tarfile
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
ROOT = Path(__file__).resolve().parents[1] # apps/admin
|
|
REPO = ROOT.parents[1] # nomadro
|
|
HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805"
|
|
|
|
EBOOK = REPO / "apps" / "api" / "services" / "ebook_svc"
|
|
|
|
|
|
def to_app_pkg(text: str) -> str:
|
|
return text.replace("ebook_svc.", "app.")
|
|
|
|
|
|
REMOTE_SH = r"""
|
|
set -euo pipefail
|
|
cd /tmp
|
|
rm -rf jg-unpack && mkdir jg-unpack
|
|
tar -xzf jg-release.tar.gz -C jg-unpack
|
|
|
|
# --- admin SPA ---
|
|
if [ -d jg-unpack/admin-dist ]; then
|
|
sudo rsync -a --delete jg-unpack/admin-dist/ /www/wwwroot/admin.nomadro.cn/
|
|
sudo chown -R www:www /www/wwwroot/admin.nomadro.cn
|
|
echo admin_dist_synced
|
|
fi
|
|
|
|
# --- API files ---
|
|
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
|
|
echo API_ROOT=$API_ROOT
|
|
B=$API_ROOT/app
|
|
if [ ! -d "$B" ]; then B=$API_ROOT/backend/app; fi
|
|
sudo mkdir -p "$B/routers" "$B/services"
|
|
|
|
sudo cp jg-unpack/backend/routers/join_group_admin.py "$B/routers/"
|
|
sudo cp jg-unpack/backend/services/join_group_config.py "$B/services/"
|
|
|
|
# patch main.py to include join_group_admin (idempotent)
|
|
MAIN="$B/main.py"
|
|
sudo python3 <<PY
|
|
from pathlib import Path
|
|
p = Path("$MAIN")
|
|
t = p.read_text(encoding="utf-8")
|
|
changed = False
|
|
if "join_group_admin" not in t:
|
|
t2 = t.replace(
|
|
"from app.routers import admin, auth, books, douyin_admin, pay, track",
|
|
"from app.routers import admin, auth, books, douyin_admin, join_group_admin, pay, track",
|
|
)
|
|
if t2 == t:
|
|
t2 = t.replace(
|
|
"from app.routers import admin, auth, books, douyin_admin, pay, track\n",
|
|
"from app.routers import admin, auth, books, douyin_admin, join_group_admin, pay, track\n",
|
|
)
|
|
t = t2
|
|
if "join_group_admin" not in t:
|
|
raise SystemExit("failed to patch import in main.py")
|
|
changed = True
|
|
if "include_router(join_group_admin.router)" not in t:
|
|
needle = "app.include_router(douyin_admin.router)"
|
|
if needle not in t:
|
|
raise SystemExit("douyin_admin include missing")
|
|
t = t.replace(
|
|
needle,
|
|
needle + "\napp.include_router(join_group_admin.router)",
|
|
1,
|
|
)
|
|
changed = True
|
|
if changed:
|
|
p.write_text(t, encoding="utf-8")
|
|
print("main.py patched")
|
|
else:
|
|
print("main.py already ok")
|
|
PY
|
|
|
|
# additive flags on portal remote-config (never wipe platforms/modules)
|
|
CFG=/var/www/nomadro.cn/data/remote-config.json
|
|
if [ -f "$CFG" ]; then
|
|
sudo python3 <<'PY'
|
|
import json
|
|
from pathlib import Path
|
|
p = Path("/var/www/nomadro.cn/data/remote-config.json")
|
|
cfg = json.loads(p.read_text(encoding="utf-8"))
|
|
flags = cfg.setdefault("flags", {})
|
|
copy = cfg.setdefault("copy", {})
|
|
changed = False
|
|
defaults = {
|
|
"joinGroupWechat": True,
|
|
"joinGroupDouyin": False,
|
|
"socialFollowAuto": False,
|
|
}
|
|
for k, v in defaults.items():
|
|
if k not in flags:
|
|
flags[k] = v
|
|
changed = True
|
|
copy_defs = {
|
|
"wechatOaName": "异度世界",
|
|
"joinGroupDouyinHint": "打开抖音搜群「数字游民」或扫码进群",
|
|
"joinGroupDouyinQr": "",
|
|
}
|
|
for k, v in copy_defs.items():
|
|
if k not in copy:
|
|
copy[k] = v
|
|
changed = True
|
|
if changed:
|
|
p.write_text(json.dumps(cfg, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
print("remote-config flags seeded (additive)")
|
|
else:
|
|
print("remote-config flags already present")
|
|
print("flags=", {k: flags.get(k) for k in list(defaults)})
|
|
PY
|
|
fi
|
|
|
|
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/
|
|
# login + join-group get
|
|
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)
|
|
echo "token_len=${#TOK}"
|
|
if [ -n "$TOK" ]; then
|
|
curl -sk -H "Authorization: Bearer $TOK" https://api.nomadro.cn/api/admin/join-group | python3 -c "import sys,json; d=json.load(sys.stdin); print('join-group', d.get('ok'), d.get('flags'))"
|
|
fi
|
|
curl -sk https://nomadro.cn/api/config | python3 -c "import sys,json; f=json.load(sys.stdin).get('config',{}).get('flags',{}); print('portal flags', {k:f.get(k) for k in ('joinGroupWechat','joinGroupDouyin','socialFollowAuto')})"
|
|
rm -rf jg-unpack jg-release.tar.gz
|
|
echo DONE
|
|
"""
|
|
|
|
|
|
def main() -> int:
|
|
dist = ROOT / "dist"
|
|
if not (dist / "index.html").exists():
|
|
print("missing apps/admin/dist — run pnpm build first")
|
|
return 1
|
|
|
|
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())
|
|
|
|
router_src = (EBOOK / "routers" / "join_group_admin.py").read_text(encoding="utf-8")
|
|
svc_src = (EBOOK / "services" / "join_group_config.py").read_text(encoding="utf-8")
|
|
for name, text in (
|
|
("backend/routers/join_group_admin.py", to_app_pkg(router_src)),
|
|
("backend/services/join_group_config.py", to_app_pkg(svc_src)),
|
|
):
|
|
data = text.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/jg-release.tar.gz", "wb") as rf:
|
|
rf.write(payload)
|
|
sftp.close()
|
|
print("uploaded", flush=True)
|
|
|
|
_, stdout, _ = c.exec_command(REMOTE_SH, 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()
|
|
print(f"[exit {code}]", flush=True)
|
|
return code
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|