diff --git a/deploy/social/README.md b/deploy/social/README.md index f3579f4..24c052f 100644 --- a/deploy/social/README.md +++ b/deploy/social/README.md @@ -38,21 +38,16 @@ pnpm dev 生产铁律:[`https://nomadro.cn`](https://nomadro.cn/) / MiroTalk / API / Centrifugo **全部在 `82.157.112.245`**,进房用 `mirotalk.nomadro.cn`。 ```bash -# 0) Centrifugo 实时层(本机下好 .tmp_cf/centrifugo.tgz 再跑;82 访问不了 GitHub) -# curl -L -o .tmp_cf/centrifugo.tgz https://github.com/centrifugal/centrifugo/releases/download/v5.4.9/centrifugo_5.4.9_linux_amd64.tar.gz -python deploy/social/centrifugo/deploy_82.py -# → systemd centrifugo :8008;nginx https://nomadro.cn/rt/;密钥 /etc/nomadro/centrifugo.env -# → social API:systemd nomadro-social :8020;GET /api/social/v1/realtime/token +# 推荐(2C/4G):本地构建再上传 .next,避免机上 next build OOM +pnpm build +python deploy/social/deploy_local_build_82.py -# 1) 钉死 MiroTalk 在 82(.cn) -python deploy/social/mirotalk/pin_82_only.py -python deploy/social/mirotalk/apply_nomadro_whitelabel.py -python deploy/social/mirotalk/apply_zh_ui.py -python deploy/social/mirotalk/apply_zh_deep.py -python deploy/social/mirotalk/apply_speed.py +# 备选:机上低内存构建(堆 1536 + swap + nice;勿并发 SSH) +# python deploy/social/deploy_82.py -# 2) 同步游民社区门户代码到 /var/www/nomadro.cn 并 build -python deploy/social/deploy_82.py +# Centrifugo / MiroTalk 仅首次或变更时跑 +# python deploy/social/centrifugo/deploy_82.py +# python deploy/social/mirotalk/pin_82_only.py ``` 环境变量(可选): diff --git a/deploy/social/deploy_local_build_82.py b/deploy/social/deploy_local_build_82.py new file mode 100644 index 0000000..3443838 --- /dev/null +++ b/deploy/social/deploy_local_build_82.py @@ -0,0 +1,141 @@ +#!/usr/bin/env python3 +"""Build portal locally, upload .next (+ social src) to 2C/4G box — no remote next build. + +Usage (from repo root, after `pnpm build`): + python deploy/social/deploy_local_build_82.py +""" +from __future__ import annotations + +import os +import tarfile +import tempfile +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] + +SRC_INCLUDE = [ + "src/modules/social", + "src/app/social", + "src/lib/remote-config.ts", + "package.json", + "pnpm-lock.yaml", + "README.md", + "deploy/social", +] + + +def connect() -> paramiko.SSHClient: + c = paramiko.SSHClient() + c.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + c.connect( + HOST, + username=USER, + password=PASS, + timeout=30, + banner_timeout=60, + auth_timeout=30, + allow_agent=False, + look_for_keys=False, + ) + t = c.get_transport() + if t: + t.set_keepalive(20) + return c + + +def run(c: paramiko.SSHClient, cmd: str, timeout: int = 600) -> tuple[str, int]: + _i, o, e = c.exec_command(cmd, timeout=timeout, get_pty=True) + out = o.read().decode("utf-8", "ignore") + return out, o.channel.recv_exit_status() + + +def pack() -> Path: + nxt = ROOT / ".next" + if not (nxt / "BUILD_ID").is_file(): + raise SystemExit("missing .next/BUILD_ID — run `pnpm build` locally first") + tmp = Path(tempfile.mkdtemp()) / "portal_local.tgz" + skip_dir = {"__pycache__", "node_modules", ".git", "cache", "dev", "diagnostics"} + skip_name = {"lock", "trace", "turbopack"} + + def keep(info: tarfile.TarInfo): + parts = Path(info.name.replace("\\", "/")).parts + if any(p in skip_dir for p in parts): + return None + if Path(info.name).name in skip_name: + return None + return info + + with tarfile.open(tmp, "w:gz") as tar: + tar.add(nxt, arcname=".next", filter=keep) + for rel in SRC_INCLUDE: + p = ROOT / rel + if p.exists(): + tar.add(p, arcname=rel, filter=keep) + print("packed", tmp, tmp.stat().st_size, "bytes", flush=True) + return tmp + + +def main() -> int: + archive = pack() + print("connecting...", flush=True) + c = connect() + print("connected", flush=True) + sftp = c.open_sftp() + remote = "/tmp/portal_local.tgz" + print("uploading...", flush=True) + sftp.put(str(archive), remote) + sftp.close() + print("uploaded", flush=True) + + script = f""" +set -euo pipefail +APP='{APP}' +sudo systemctl stop nomadro.service || true +sudo pkill -f 'next build' || true +sudo pkill -f 'pnpm build' || true +sudo chown -R ubuntu:ubuntu "$APP" +cd "$APP" +# backup last good build +if [ -f .next/BUILD_ID ]; then + rm -rf /tmp/nomadro.next.bak + mv .next /tmp/nomadro.next.bak +fi +tar -xzf /tmp/portal_local.tgz --no-same-owner +test -f .next/BUILD_ID +echo BUILD_ID=$(cat .next/BUILD_ID) +# ensure runtime deps present (centrifuge etc) — no next build +export CI=true +/usr/local/bin/pnpm install --prefer-offline +sudo systemctl reset-failed nomadro.service || true +sudo systemctl start nomadro.service +sleep 5 +systemctl is-active nomadro.service +curl -sS -m 12 -o /dev/null -w 'local=%{{http_code}}\\n' http://127.0.0.1:4003/ || true +curl -sS -m 12 -o /dev/null -w 'home=%{{http_code}}\\n' https://nomadro.cn/ || true +curl -sS -m 12 -o /dev/null -w 'social=%{{http_code}}\\n' https://nomadro.cn/social || true +free -h | head -2 +echo DONE_LOCAL_DEPLOY +""" + # Prefer no-PTY: systemd stop under PTY can kill the SSH session on 2C/4G + _i, o, e = c.exec_command(script, timeout=900, get_pty=False) + out = o.read().decode("utf-8", "ignore") + err = e.read().decode("utf-8", "ignore") + code = o.channel.recv_exit_status() + text = out + (("\n" + err) if err.strip() else "") + print(text[-8000:] if len(text) > 8000 else text) + c.close() + try: + archive.unlink() + except OSError: + pass + return code if code else (0 if "DONE_LOCAL_DEPLOY" in text else 1) + + +if __name__ == "__main__": + raise SystemExit(main())