nomadro/deploy/social/deploy_local_build_82.py
Eric 8edd9a0733 Prefer local Next build upload for 2C/4G portal deploys.
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-02 00:04:24 +08:00

142 lines
4.0 KiB
Python

#!/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())