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>
136 lines
4.5 KiB
Python
136 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
"""Deploy public /api/config on dyapi/api (valid TLS) for Douyin 真机."""
|
|
from __future__ import annotations
|
|
|
|
import io
|
|
import tarfile
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
REPO = Path(__file__).resolve().parents[3]
|
|
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 pc-unpack && mkdir pc-unpack
|
|
tar -xzf pc-release.tar.gz -C pc-unpack
|
|
|
|
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"
|
|
|
|
sudo cp pc-unpack/backend/routers/public_config.py "$B/routers/"
|
|
|
|
MAIN="$B/main.py"
|
|
sudo python3 <<PY
|
|
from pathlib import Path
|
|
p = Path("$MAIN")
|
|
t = p.read_text(encoding="utf-8")
|
|
changed = False
|
|
if "public_config" not in t:
|
|
for old in (
|
|
"from app.routers import admin, auth, books, douyin_admin, join_group_admin, pay, track",
|
|
"from app.routers import admin, auth, books, douyin_admin, join_group_admin, pay, track\n",
|
|
"from app.routers import admin, auth, books, douyin_admin, pay, track",
|
|
):
|
|
if old.strip() in t.replace("\r",""):
|
|
pass
|
|
if "join_group_admin" in t and "public_config" not in t:
|
|
t2 = t.replace(
|
|
"from app.routers import admin, auth, books, douyin_admin, join_group_admin, pay, track",
|
|
"from app.routers import admin, auth, books, douyin_admin, join_group_admin, pay, public_config, track",
|
|
)
|
|
if t2 == t:
|
|
t2 = t.replace(
|
|
"from app.routers import admin, auth, books, douyin_admin, pay, track",
|
|
"from app.routers import admin, auth, books, douyin_admin, pay, public_config, track",
|
|
)
|
|
t = t2
|
|
changed = True
|
|
if "public_config" not in t:
|
|
raise SystemExit("failed to patch import")
|
|
if "include_router(public_config.router)" not in t:
|
|
for needle in (
|
|
"app.include_router(join_group_admin.router)",
|
|
"app.include_router(douyin_admin.router)",
|
|
"app.include_router(admin.router)",
|
|
):
|
|
if needle in t:
|
|
t = t.replace(needle, needle + "\napp.include_router(public_config.router)", 1)
|
|
changed = True
|
|
break
|
|
else:
|
|
raise SystemExit("no include needle")
|
|
if changed:
|
|
p.write_text(t, encoding="utf-8")
|
|
print("main.py patched")
|
|
else:
|
|
print("main.py already ok")
|
|
PY
|
|
|
|
sudo systemctl restart nomadmini-api
|
|
sleep 3
|
|
systemctl is-active nomadmini-api
|
|
echo "--- smoke ---"
|
|
for u in \
|
|
https://dyapi.nomadro.cn/api/config \
|
|
https://api.nomadro.cn/api/config \
|
|
https://dyapi.nomadro.cn/api/config/meta \
|
|
https://api.nomadro.cn/api/config/meta
|
|
do
|
|
code=$(curl -s -o /tmp/pcbody.json -w '%{http_code}' -m 15 "$u" || echo ERR)
|
|
echo "$code $u"
|
|
python3 -c "import json; d=json.load(open('/tmp/pcbody.json')); c=d.get('config',d); f=(c.get('flags') or {}) if isinstance(c,dict) else {}; print(' ver', c.get('version') if isinstance(c,dict) else d.get('version'), 'wx', f.get('joinGroupWechat'), 'dy', f.get('joinGroupDouyin'))" 2>/dev/null || head -c 120 /tmp/pcbody.json; echo
|
|
done
|
|
rm -rf pc-unpack pc-release.tar.gz
|
|
"""
|
|
|
|
|
|
def main() -> None:
|
|
buf = io.BytesIO()
|
|
with tarfile.open(fileobj=buf, mode="w:gz") as tar:
|
|
src = (EBOOK / "routers" / "public_config.py").read_text(encoding="utf-8")
|
|
data = to_app_pkg(src).encode("utf-8")
|
|
info = tarfile.TarInfo(name="backend/routers/public_config.py")
|
|
info.size = len(data)
|
|
tar.addfile(info, io.BytesIO(data))
|
|
payload = buf.getvalue()
|
|
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(HOST, username=USER, password=PASS, timeout=30)
|
|
sftp = c.open_sftp()
|
|
with sftp.file("/tmp/pc-release.tar.gz", "wb") as f:
|
|
f.write(payload)
|
|
with sftp.file("/tmp/pc-deploy.sh", "w") as f:
|
|
f.write(REMOTE_SH)
|
|
sftp.close()
|
|
|
|
print("uploaded, running remote…")
|
|
_, stdout, stderr = c.exec_command("bash /tmp/pc-deploy.sh", timeout=120)
|
|
out = stdout.read().decode("utf-8", errors="replace")
|
|
err = stderr.read().decode("utf-8", errors="replace")
|
|
print(out)
|
|
if err.strip():
|
|
print("STDERR:", err)
|
|
c.close()
|
|
print("done", time.strftime("%H:%M:%S"))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|