From f688c037203b62f57c0f88c1c6dca891c99241f8 Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 30 Sep 2026 20:45:55 +0800 Subject: [PATCH] Preserve WeChat top-level remote-config on Douyin admin deploy. Backup live modules before git reset; only seed platforms.tt for Douyin. Co-authored-by: Cursor --- deploy/observability/_deploy_pre3_tt_admin.py | 185 ++++++++++++++++++ lib/remote-config.ts | 2 +- 2 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 deploy/observability/_deploy_pre3_tt_admin.py diff --git a/deploy/observability/_deploy_pre3_tt_admin.py b/deploy/observability/_deploy_pre3_tt_admin.py new file mode 100644 index 0000000..ae07b17 --- /dev/null +++ b/deploy/observability/_deploy_pre3_tt_admin.py @@ -0,0 +1,185 @@ +#!/usr/bin/env python3 +"""Deploy pre3 portal (.next + git) to 82.157.112.245.""" +from __future__ import annotations + +import os +import sys +import tarfile +import time + +import paramiko + +sys.stdout.reconfigure(encoding="utf-8", errors="replace") + +HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805" +APP = "/var/www/nomadro.cn" +ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", "..")) +NEXT = os.path.join(ROOT, ".next") +TAR = os.path.join(os.environ.get("TEMP", "/tmp"), "nomadro-next.tgz") +REMOTE = "/tmp/nomadro-next.tgz" +CHUNK = 256 * 1024 + + +def tar_filter(ti: tarfile.TarInfo): + p = ti.name.replace("\\", "/").split("/") + if len(p) > 1 and p[0] == ".next": + rest = p[1:] + if rest and rest[0] == "dev": + return None + if "cache" in rest and "turbopack" in rest: + return None + return ti + + +def connect(): + 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, + banner_timeout=120, + ) + t = c.get_transport() + if t: + t.set_keepalive(10) + return c + + +def run(c: paramiko.SSHClient, cmd: str, timeout: int = 300) -> int: + print(f"$ {cmd[:160]}", flush=True) + _, stdout, _ = c.exec_command(cmd, timeout=timeout, 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) + return code + + +def main() -> int: + if not os.path.isdir(NEXT): + print("missing .next — run pnpm build first") + return 1 + + print("packing .next ...", flush=True) + with tarfile.open(TAR, "w:gz") as tar: + tar.add(NEXT, arcname=".next", filter=tar_filter) + size = os.path.getsize(TAR) + print(f"packed {size / 1024 / 1024:.1f} MB", flush=True) + + c = connect() + run(c, "pkill -f 'next build' || true; pkill -f 'pnpm build' || true", 60) + + code = run( + c, + f""" +set -euo pipefail +cd {APP} +# 部署前备份线上 remote-config,避免冲掉微信/门户顶层 modules +if [ -f data/remote-config.json ]; then + cp -a data/remote-config.json data/remote-config.runtime.json +fi +git fetch origin pre3 +git checkout -B pre3 origin/pre3 +git reset --hard origin/pre3 +git log -1 --oneline +test -f data/remote-config.json +python3 - <<'PY' +import json +from pathlib import Path +p = Path('data/remote-config.json') +seed = json.loads(p.read_text(encoding='utf-8')) +bak = Path('data/remote-config.runtime.json') +live = json.loads(bak.read_text(encoding='utf-8')) if bak.exists() else seed +out = dict(live) +out['platforms'] = dict(live.get('platforms') or {{}}) +tt_seed = (seed.get('platforms') or {{}}).get('tt') or {{}} +tt_live = (live.get('platforms') or {{}}).get('tt') or {{}} +out['platforms']['tt'] = tt_live if (tt_live.get('bookContent') or tt_live.get('modules')) else tt_seed +if not out.get('modules'): + out['modules'] = seed.get('modules') +# 顶层 modules/flags/copy 一律保留 live(微信/门户);只保证 platforms.tt 存在 +text = json.dumps(out, ensure_ascii=False, indent=2) +p.write_text(text, encoding='utf-8') +bak.write_text(text, encoding='utf-8') +tt = out['platforms']['tt'] +mods = tt.get('modules') or {{}} +print('preserved top modules.nav', (out.get('modules') or {{}}).get('nav', {{}}).get('enabled')) +print('tt.modules', {{k: (v or {{}}).get('enabled') for k, v in mods.items()}}) +print('tt.chapters', len(((tt.get('bookContent') or {{}}).get('chapters') or []))) +PY +""", + 180, + ) + if code != 0: + c.close() + return code + + print("uploading ...", flush=True) + sftp = c.open_sftp() + with open(TAR, "rb") as f, sftp.file(REMOTE, "wb") as rf: + rf.set_pipelined(True) + sent = 0 + t0 = time.time() + while True: + data = f.read(CHUNK) + if not data: + break + rf.write(data) + sent += len(data) + if sent % (2 * 1024 * 1024) < CHUNK: + rate = sent / max(time.time() - t0, 0.1) / 1024 + print(f" {sent/1024/1024:.1f}/{size/1024/1024:.1f} MB ({rate:.0f} KB/s)", flush=True) + sftp.close() + print("uploaded ok", flush=True) + + code = run( + c, + f""" +set -euo pipefail +cd {APP} +rm -rf .next +tar -xzf {REMOTE} -C {APP} +rm -f {REMOTE} +test -f .next/BUILD_ID +echo BUILD_ID=$(cat .next/BUILD_ID) +sudo systemctl restart nomadro.service +sleep 5 +systemctl is-active nomadro.service +echo '--- smoke ---' +for p in /api/health /api/config /admin /admin/tt /admin/config; do + code=$(curl -s -o /dev/null -w '%{{http_code}}' -m 25 http://127.0.0.1:4003$p || echo ERR) + echo "local $p -> $code" +done +for p in /api/health /api/config /admin /admin/tt; do + code=$(curl -sk -o /dev/null -w '%{{http_code}}' -m 25 https://admin.nomadro.cn$p || echo ERR) + echo "admin $p -> $code" +done +curl -s http://127.0.0.1:4003/api/config > /tmp/cfg.json +python3 - <<'PY' +import json +from pathlib import Path +d = json.loads(Path('/tmp/cfg.json').read_text(encoding='utf-8')) +cfg = d.get('config') or d +tt = (cfg.get('platforms') or {{}}).get('tt') or {{}} +mods = tt.get('modules') or {{}} +print('api tt modules', {{k: (v or {{}}).get('enabled') for k, v in mods.items()}}) +print('api chapters', len(((tt.get('bookContent') or {{}}).get('chapters') or []))) +PY +echo DONE +""", + 240, + ) + c.close() + return code + + +if __name__ == "__main__": + raise SystemExit(main()) diff --git a/lib/remote-config.ts b/lib/remote-config.ts index 0e99ba3..d3442d9 100644 --- a/lib/remote-config.ts +++ b/lib/remote-config.ts @@ -68,7 +68,7 @@ export type NomadroRemoteConfig = { vipProductId: string; vipCta: string; }; - /** 分端配置;门户 / 微信仍用顶层 modules */ + /** 分端配置;仅抖音等使用。门户 / 微信仍只读顶层 modules,互不影响 */ platforms?: { tt?: TtPlatformConfig; };