#!/usr/bin/env python3 """Deploy 游民社区 + MiroTalk(.cn) portal bits to 82.157.112.245 only.""" from __future__ import annotations import os import tarfile import tempfile import time from pathlib import Path import paramiko HOST = "82.157.112.245" USER = "ubuntu" PASS = "Xiao4669805" APP = "/var/www/nomadro.cn" ROOT = Path(__file__).resolve().parents[2] # Paths relative to repo root INCLUDE = [ "src/modules/social", "src/app/social", "src/modules/social/lib/seed.ts", # covered by tree "src/lib/remote-config.ts", "src/components/sections/hero.tsx", "src/app/layout.tsx", "src/app/hub.css", "src/app/admin/verticals/page.tsx", "data/remote-config.json", "next.config.ts", "tsconfig.json", "apps/api/app/main.py", "apps/api/services/social_svc", "apps/api/services/community_svc/config.py", "apps/api/services/community_svc/services/meetup_live.py", "deploy/social", "README.md", ] def connect() -> paramiko.SSHClient: 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, banner_timeout=120, ) t = c.get_transport() if t: t.set_keepalive(20) return c def run(c: paramiko.SSHClient, cmd: str, timeout: int = 1200) -> tuple[str, int]: _i, o, e = c.exec_command(cmd, timeout=timeout, get_pty=True) out = o.read().decode("utf-8", "ignore") code = o.channel.recv_exit_status() return out, code def pack() -> Path: tmp = Path(tempfile.mkdtemp()) / "social82.tgz" with tarfile.open(tmp, "w:gz") as tar: for rel in INCLUDE: p = ROOT / rel if not p.exists(): print("skip missing", rel) continue tar.add(p, arcname=rel) print("packed", tmp, "bytes", tmp.stat().st_size) return tmp def main() -> int: archive = pack() c = connect() print("connected") # upload sftp = c.open_sftp() sftp.put(str(archive), "/tmp/social82.tgz") sftp.close() print("uploaded") # extract into app (preserve remote-config backup) script = f""" set -euo pipefail cd {APP} sudo cp -a data/remote-config.json /tmp/remote-config.pre-social.json || true tar -xzf /tmp/social82.tgz # restore operational remote-config then merge social module key via python python3 - <<'PY' import json from pathlib import Path app = Path('{APP}') live = app / 'data' / 'remote-config.json' bak = Path('/tmp/remote-config.pre-social.json') incoming = json.loads(live.read_text(encoding='utf-8')) if live.exists() else {{}} base = json.loads(bak.read_text(encoding='utf-8')) if bak.exists() else {{}} # keep live ops flags; ensure modules.social exists from incoming mods = base.setdefault('modules', {{}}) if isinstance(base, dict) else {{}} inc_mods = incoming.get('modules') if isinstance(incoming, dict) else {{}} if isinstance(inc_mods, dict) and 'social' in inc_mods: mods['social'] = inc_mods['social'] elif 'social' not in mods: mods['social'] = {{'enabled': True, 'title': '游民社区', 'tabLabel': '社区'}} # prefer base (prod) as SoT with social injected if isinstance(base, dict): base['modules'] = mods live.write_text(json.dumps(base, ensure_ascii=False, indent=2) + '\\n', encoding='utf-8') print('remote-config merged social') else: print('kept incoming remote-config') PY # portal env: force MiroTalk on 82 .cn sudo mkdir -p /etc/nomadro if [ -f /etc/nomadro/portal.env ]; then sudo grep -q '^NEXT_PUBLIC_MIROTALK_URL=' /etc/nomadro/portal.env \\ && sudo sed -i 's|^NEXT_PUBLIC_MIROTALK_URL=.*|NEXT_PUBLIC_MIROTALK_URL=https://mirotalk.nomadro.cn|' /etc/nomadro/portal.env \\ || echo 'NEXT_PUBLIC_MIROTALK_URL=https://mirotalk.nomadro.cn' | sudo tee -a /etc/nomadro/portal.env >/dev/null else echo 'NEXT_PUBLIC_MIROTALK_URL=https://mirotalk.nomadro.cn' | sudo tee /etc/nomadro/portal.env >/dev/null fi grep MIROTALK /etc/nomadro/portal.env || true # .next often root-owned from prior deploys — clear then build as ubuntu sudo systemctl stop nomadro.service || true sudo chown -R ubuntu:ubuntu {APP} sudo rm -rf {APP}/.next export NODE_OPTIONS=--max-old-space-size=4096 /usr/local/bin/pnpm build test -f .next/BUILD_ID echo BUILD_ID=$(cat .next/BUILD_ID) sudo systemctl start nomadro.service sleep 5 systemctl is-active nomadro.service curl -sk -o /dev/null -w 'home %{{http_code}}\\n' https://nomadro.cn/ curl -sk -o /dev/null -w 'social %{{http_code}}\\n' https://nomadro.cn/social curl -sk -o /dev/null -w 'meetup %{{http_code}}\\n' https://nomadro.cn/social/meetup curl -sk -o /dev/null -w 'mirotalk_cn %{{http_code}} %{{time_total}}\\n' 'https://mirotalk.nomadro.cn/join?room=t&name=t¬ify=0' echo DONE """ print("building on 82 (may take several minutes)...") out, code = run(c, script, timeout=1500) print(out[-8000:] if len(out) > 8000 else out) c.close() try: archive.unlink() except OSError: pass return code if __name__ == "__main__": raise SystemExit(main())