Pay-then-profile join desk with live card, S3 upload fallback, hydration-safe drafts; document on test9 README. Co-authored-by: Cursor <cursoragent@cursor.com>
168 lines
5.5 KiB
Python
168 lines
5.5 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",
|
|
"apps/api/services/social_svc",
|
|
"apps/api/social_main.py",
|
|
"apps/api/requirements.txt",
|
|
"public/maplibre",
|
|
]
|
|
|
|
|
|
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
|
|
find "$APP/apps/api/services/social_svc" -type d -name '__pycache__' -prune -exec rm -rf {{}} + 2>/dev/null || true
|
|
tar -xzf /tmp/portal_local.tgz --no-same-owner
|
|
test -f .next/BUILD_ID
|
|
echo BUILD_ID=$(cat .next/BUILD_ID)
|
|
# MUST install while nomadro is stopped — live next + pnpm rewrite = ENOENT / 500
|
|
export CI=true
|
|
export NODE_OPTIONS='--max-old-space-size=1024'
|
|
/usr/local/bin/pnpm install --prefer-offline
|
|
# sanity: next runtime files must exist
|
|
node -e "require('next/package.json')"
|
|
test -e node_modules/next/dist/compiled/cookie/index.js \\
|
|
|| test -e node_modules/.pnpm/next@*/node_modules/next/dist/compiled/cookie/index.js \\
|
|
|| {{ echo 'next cookie missing after install'; exit 4; }}
|
|
# social API deps (upload + geo)
|
|
if [ -x "$APP/apps/api/.venv-social/bin/pip" ]; then
|
|
"$APP/apps/api/.venv-social/bin/pip" install -q 'python-multipart' 'h3' 'httpx' || true
|
|
fi
|
|
sudo systemctl reset-failed nomadro.service || true
|
|
sudo systemctl start nomadro.service
|
|
sudo systemctl restart nomadro-social.service || true
|
|
sleep 8
|
|
systemctl is-active nomadro.service
|
|
# social may briefly report activating after restart
|
|
for _ in 1 2 3 4 5 6; do
|
|
systemctl is-active nomadro-social.service && break
|
|
sleep 2
|
|
done
|
|
systemctl is-active nomadro-social.service || true
|
|
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
|
|
curl -sS -m 12 -o /dev/null -w 'map=%{{http_code}}\\n' https://nomadro.cn/social/map || true
|
|
curl -sS -m 12 -o /dev/null -w 'places=%{{http_code}}\\n' https://nomadro.cn/social/places || true
|
|
curl -sS -m 12 -o /dev/null -w 'health=%{{http_code}}\\n' https://nomadro.cn/api/social/v1/health || true
|
|
curl -sS -m 12 -o /dev/null -w 'map_cities=%{{http_code}}\\n' https://nomadro.cn/api/social/v1/map/cities || 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())
|