nomadro/apps/admin/deploy/deploy_douyin_admin.py
eric 3e5b7c2098 Monorepo super-optimize: src layout, workspace scripts, docs.
Collapse portal under src/, unify pnpm workspace/scripts, thin VIP topic re-exports, drop orphan vip scaffolds and one-shot scripts, ignore xhs/release, document root commands.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-01 00:34:04 +08:00

147 lines
5.2 KiB
Python

#!/usr/bin/env python3
"""Deploy Douyin custom admin panel to admin.nomadro.cn + restart API."""
from __future__ import annotations
import io
import tarfile
import time
from pathlib import Path
import paramiko
ROOT = Path(__file__).resolve().parents[1]
REPO = ROOT.parents[2] # nomadro
HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805"
REMOTE_SH = r"""
set -e
cd /tmp
rm -rf dy-unpack && mkdir dy-unpack
tar -xzf dy-admin-release.tar.gz -C dy-unpack
sudo rsync -a --delete dy-unpack/admin-dist/ /www/wwwroot/admin.nomadro.cn/
sudo chown -R www:www /www/wwwroot/admin.nomadro.cn
API_ROOT=$(systemctl show nomadmini-api -p WorkingDirectory --value 2>/dev/null || true)
if [ -z "$API_ROOT" ] || [ "$API_ROOT" = "/" ]; then
for d in /opt/nomadmini /www/wwwroot/nomadmini /opt/wemini /var/www/nomadmini /home/ubuntu/nomadmini; do
if [ -f "$d/backend/app/main.py" ] || [ -f "$d/app/main.py" ]; then API_ROOT=$d; break; fi
done
fi
echo API_ROOT=$API_ROOT
if [ -n "$API_ROOT" ]; then
if [ -d "$API_ROOT/backend/app" ]; then
B=$API_ROOT/backend/app
else
B=$API_ROOT/app
fi
sudo mkdir -p "$B/routers" "$B/services" "$API_ROOT/data" "$API_ROOT/backend/data" 2>/dev/null || true
sudo cp dy-unpack/backend/app/routers/douyin_admin.py "$B/routers/"
sudo cp dy-unpack/backend/app/services/tt_platform_config.py "$B/services/"
sudo cp dy-unpack/backend/app/main.py "$B/main.py"
sudo cp dy-unpack/backend/app/config.py "$B/config.py"
if [ -f dy-unpack/data/tt-default-book.json ]; then
sudo cp dy-unpack/data/tt-default-book.json "$API_ROOT/data/" 2>/dev/null || true
sudo cp dy-unpack/data/tt-default-book.json "$API_ROOT/backend/data/" 2>/dev/null || true
fi
fi
if [ -d /var/www/nomadro.cn/lib ] && [ -f dy-unpack/portal-lib/tt-default-book.json ]; then
sudo cp dy-unpack/portal-lib/tt-default-book.json /var/www/nomadro.cn/lib/tt-default-book.json
fi
if [ -f /var/www/nomadro.cn/data/remote-config.json ]; 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"))
plats = cfg.setdefault("platforms", {})
tt = plats.get("tt") or {}
need = not tt.get("modules")
if need:
seed = {}
sp = Path("/var/www/nomadro.cn/lib/tt-default-book.json")
if sp.exists():
seed = json.loads(sp.read_text(encoding="utf-8"))
plats["tt"] = {
"modules": {
"nav": {"enabled": False, "title": "出海导航", "tabLabel": "导航", "note": "抖音默认隐藏"},
"book": {"enabled": True, "title": "数字游民小册", "tabLabel": "小册", "note": "默认开"},
"community": {"enabled": False, "title": "旅居社群", "tabLabel": "社群", "note": "抖音默认隐藏"},
},
"bookContent": seed or {"title": "数字游民", "chapters": []},
}
p.write_text(json.dumps(cfg, ensure_ascii=False, indent=2), encoding="utf-8")
print("seeded platforms.tt")
else:
print("platforms.tt already present")
print("top modules.nav", (cfg.get("modules") or {}).get("nav", {}).get("enabled"))
PY
fi
sudo systemctl restart nomadmini-api || true
sleep 3
systemctl is-active nomadmini-api || true
curl -sk -o /dev/null -w "admin %{http_code}\n" https://admin.nomadro.cn/
code=$(curl -sk -o /dev/null -w "%{http_code}" https://admin.nomadro.cn/api/admin/douyin/meta || echo ERR)
echo "douyin_meta_http=$code"
rm -rf dy-unpack dy-admin-release.tar.gz
"""
def main() -> int:
buf = io.BytesIO()
with tarfile.open(fileobj=buf, mode="w:gz") as tar:
dist = ROOT / "dist"
for p in dist.rglob("*"):
if p.is_file():
tar.add(p, arcname="admin-dist/" + p.relative_to(dist).as_posix())
for rel in (
"backend/app/routers/douyin_admin.py",
"backend/app/services/tt_platform_config.py",
"backend/app/main.py",
"backend/app/config.py",
"data/tt-default-book.json",
):
p = ROOT / rel
if p.exists():
tar.add(p, arcname=rel)
seed = REPO / "src" / "lib" / "tt-default-book.json"
if seed.exists():
tar.add(seed, arcname="portal-lib/tt-default-book.json")
buf.seek(0)
print(f"pack {buf.getbuffer().nbytes} bytes", flush=True)
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
HOST,
username=USER,
password=PASS,
timeout=40,
allow_agent=False,
look_for_keys=False,
)
sftp = c.open_sftp()
with sftp.file("/tmp/dy-admin-release.tar.gz", "wb") as rf:
rf.write(buf.read())
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()
print(f"[exit {code}]", flush=True)
c.close()
return code
if __name__ == "__main__":
raise SystemExit(main())