nomadro/apps/admin/deploy/_diag_config_sync.py
eric 0e85d71386 Add Douyin BMI audit homepage with remote A/B switch.
Ship a health-management BMI home for review, keep the nomad hub as hub, and let admin switch platforms.tt.homeVariant live with track/ntfy; fix tt config fetch to api.nomadro.cn/api/config.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-03 02:32:26 -05:00

97 lines
3.7 KiB
Python

#!/usr/bin/env python3
"""Diagnose + fix: ensure admin write and public config share same file."""
from __future__ import annotations
import json
import sys
import time
import urllib.request
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST, USER, PASSWD = "82.157.112.245", "ubuntu", "Xiao4669805"
BASE = "https://admin.nomadro.cn"
CMD = r"""
set -e
echo '=== resolve path from API code ==='
cd /opt/nomadmini/backend && .venv/bin/python - <<'PY'
from app.services.tt_platform_config import read_tt_platform, resolve_config_file, write_tt_platform
print('file', resolve_config_file())
cfg = read_tt_platform()
print('read hv', cfg.get('homeVariant'))
print('read mods', {k:(v or {}).get('enabled') for k,v in (cfg.get('modules') or {}).items()})
PY
echo '=== public config ==='
curl -sk https://api.nomadro.cn/api/config | python3 - <<'PY'
import sys, json
d=json.load(sys.stdin)
print('source', d.get('source'))
tt=((d.get('config') or {}).get('platforms') or {}).get('tt') or {}
print('public hv', tt.get('homeVariant'))
print('public mods', {k:(v or {}).get('enabled') for k,v in (tt.get('modules') or {}).items()})
PY
"""
def http(method: str, path: str, token: str | None = None, body: dict | None = None):
data = None if body is None else json.dumps(body).encode()
headers = {"Content-Type": "application/json"}
if token:
headers["Authorization"] = f"Bearer {token}"
req = urllib.request.Request(BASE + path, data=data, headers=headers, method=method)
with urllib.request.urlopen(req, timeout=30) as resp:
return json.loads(resp.read().decode())
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(HOST, username=USER, password=PASSWD, timeout=40, allow_agent=False, look_for_keys=False)
_, stdout, _ = c.exec_command(CMD, timeout=90, get_pty=True)
out = ""
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
chunk = stdout.channel.recv(65536).decode("utf-8", "replace")
out += chunk
print(chunk, end="", flush=True)
time.sleep(0.05)
while stdout.channel.recv_ready():
chunk = stdout.channel.recv(65536).decode("utf-8", "replace")
out += chunk
print(chunk, end="", flush=True)
c.close()
login = http("POST", "/api/admin/login", body={"username": "admin", "password": "Xiao4669805"})
token = login.get("token") or login.get("access_token")
adm = http("GET", "/api/admin/douyin/config", token=token)
cfg = adm.get("config") or {}
print("admin hv", cfg.get("homeVariant"))
print("admin mods", {k: (v or {}).get("enabled") for k, v in (cfg.get("modules") or {}).items()})
# Force write hub then bmi roundtrip via admin API to prove public follows
print("=== roundtrip hub ===", flush=True)
http("PUT", "/api/admin/douyin/config", token=token, body={"homeVariant": "hub"})
time.sleep(1)
pub = json.loads(urllib.request.urlopen("https://api.nomadro.cn/api/config", timeout=20).read().decode())
tt = ((pub.get("config") or {}).get("platforms") or {}).get("tt") or {}
print("public after hub", tt.get("homeVariant"))
print("=== roundtrip bmi ===", flush=True)
http("PUT", "/api/admin/douyin/config", token=token, body={"homeVariant": "bmi"})
time.sleep(1)
pub2 = json.loads(urllib.request.urlopen("https://api.nomadro.cn/api/config", timeout=20).read().decode())
tt2 = ((pub2.get("config") or {}).get("platforms") or {}).get("tt") or {}
print("public after bmi", tt2.get("homeVariant"))
ok = tt.get("homeVariant") == "hub" and tt2.get("homeVariant") == "bmi"
print("SYNC_OK" if ok else "SYNC_FAIL")
return 0 if ok else 1
if __name__ == "__main__":
raise SystemExit(main())