nomadro/deploy/work/deploy_us.py
Eric ffa456afdf Ship social join profile + SeaweedFS avatars, notify/coffee, and .com audience stack.
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>
2026-11-04 02:25:05 +08:00

235 lines
8.4 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Deploy Nomadro Work (Next UI + FastAPI work_svc) to nomadro.com US."""
from __future__ import annotations
import subprocess
import sys
import tempfile
import time
from pathlib import Path
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace", line_buffering=True)
ROOT = Path(__file__).resolve().parents[2]
US = ("108.174.50.68", "root", "Xiao4669805")
REMOTE = "/var/www/nomadro.cn"
REMOTE_TGZ = "/tmp/nomadro-work-us.tgz"
UNIT = """[Unit]
Description=Nomadro work API (boards + pads)
After=network.target
[Service]
Type=simple
WorkingDirectory=/var/www/nomadro.cn/apps/api
EnvironmentFile=-/etc/nomadro/portal.env
Environment=HOST=127.0.0.1
Environment=PORT=8021
Environment=WORK_ENABLED=true
Environment=WORK_DATA_DIR=/var/www/nomadro-data/work
Environment=PYTHONPATH=/var/www/nomadro.cn/apps/api:/var/www/nomadro.cn/apps/api/services
ExecStart=/var/www/nomadro.cn/apps/api/.venv-social/bin/python -m uvicorn work_main:app --host 127.0.0.1 --port 8021
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target
"""
def connect() -> paramiko.SSHClient:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
h, u, p = US
c.connect(h, 22, u, p, timeout=40, allow_agent=False, look_for_keys=False, banner_timeout=90)
t = c.get_transport()
if t:
t.set_keepalive(15)
return c
def sh(c: paramiko.SSHClient, cmd: str, timeout: int = 180) -> tuple[int, str]:
print(f"$ {cmd[:220].replace(chr(10), ' ')}", flush=True)
_, o, e = c.exec_command(cmd, timeout=timeout, get_pty=False)
out = o.read().decode("utf-8", "replace")
err = e.read().decode("utf-8", "replace")
code = o.channel.recv_exit_status()
text = out + (("\n" + err) if err.strip() else "")
if text.strip():
print(text[-6000:], flush=True)
print("exit", code, flush=True)
return code, text
def main() -> int:
nxt = ROOT / ".next"
if not (nxt / "BUILD_ID").is_file():
print("missing .next — run: pnpm exec next build")
return 1
build_id = (nxt / "BUILD_ID").read_text(encoding="utf-8").strip()
print("BUILD_ID", build_id, flush=True)
with tempfile.TemporaryDirectory() as td:
tgz = Path(td) / "bundle.tgz"
cmd = [
"tar",
"-czf",
str(tgz),
"--exclude=node_modules",
"--exclude=.next/cache",
"--exclude=.next/dev",
"--exclude=.next/types",
"--exclude=__pycache__",
"--exclude=*.pyc",
"-C",
str(ROOT),
".next",
"src",
"public",
"packages/pay",
"packages/pb",
"packages/sdk",
"next.config.ts",
"package.json",
"pnpm-lock.yaml",
"apps/api/work_main.py",
"apps/api/services/work_svc",
"apps/work/README.md",
]
print("packing…", flush=True)
subprocess.check_call(cmd)
size = tgz.stat().st_size
print(f"tarball {size / 1024 / 1024:.1f} MB", flush=True)
c = connect()
try:
print("upload…", flush=True)
sftp = c.open_sftp()
with open(tgz, "rb") as f, sftp.file(REMOTE_TGZ, "wb") as rf:
rf.set_pipelined(True)
sent = 0
while True:
chunk = f.read(512 * 1024)
if not chunk:
break
rf.write(chunk)
sent += len(chunk)
if sent % (8 * 1024 * 1024) < 512 * 1024:
print(f" {sent / 1024 / 1024:.1f}/{size / 1024 / 1024:.1f} MB", flush=True)
# unit file + nginx snippet (API must not go via Next Connection: upgrade)
snippet = (Path(__file__).resolve().parent / "nginx-work-api.conf").read_text(
encoding="utf-8"
)
with sftp.file("/tmp/nomadro-work.service", "w") as uf:
uf.write(UNIT)
with sftp.file("/tmp/nomadro-work-api.conf", "w") as nf:
nf.write(snippet)
sftp.close()
code, _ = sh(
c,
f"""
set -euo pipefail
cd {REMOTE}
cp -a data/remote-config.json /tmp/rc.preserve.json 2>/dev/null || true
mkdir -p /var/www/nomadro-data/work apps/api/services
systemctl kill -s9 nomadro.service 2>/dev/null || true
fuser -k 4003/tcp 2>/dev/null || true
sleep 1
rm -rf .next.bak
[ -d .next ] && mv .next .next.bak || true
# drop legacy Next JSON work routes if present (replaced by catch-all proxy)
rm -rf src/app/api/work/v1/boards src/app/api/work/v1/pads src/app/api/work/v1/projects 2>/dev/null || true
tar -xzf {REMOTE_TGZ} -C {REMOTE}
rm -f {REMOTE_TGZ}
test -f .next/BUILD_ID
test -f apps/api/work_main.py
test -d apps/api/services/work_svc
echo BUILD_ID=$(cat .next/BUILD_ID)
[ -f /tmp/rc.preserve.json ] && cp -a /tmp/rc.preserve.json data/remote-config.json || true
# portal env: point Next proxy at work_svc
mkdir -p /etc/nomadro
touch /etc/nomadro/portal.env
grep -q '^WORK_API_URL=' /etc/nomadro/portal.env 2>/dev/null \\
&& sed -i 's|^WORK_API_URL=.*|WORK_API_URL=http://127.0.0.1:8021|' /etc/nomadro/portal.env \\
|| echo 'WORK_API_URL=http://127.0.0.1:8021' >> /etc/nomadro/portal.env
grep -q '^WORK_ENABLED=' /etc/nomadro/portal.env 2>/dev/null \\
|| echo 'WORK_ENABLED=true' >> /etc/nomadro/portal.env
install -m 644 /tmp/nomadro-work.service /etc/systemd/system/nomadro-work.service
rm -f /tmp/nomadro-work.service
install -m 644 /tmp/nomadro-work-api.conf /etc/nginx/snippets/nomadro-work-api.conf
rm -f /tmp/nomadro-work-api.conf
NGCONF=/etc/nginx/sites-enabled/nomadro.com.conf
INCLUDE='include /etc/nginx/snippets/nomadro-work-api.conf;'
if [ -f "$NGCONF" ] && ! grep -q 'nomadro-work-api.conf' "$NGCONF"; then
if grep -q 'nomadro-work-mf.conf' "$NGCONF"; then
sed -i 's|include /etc/nginx/snippets/nomadro-work-mf.conf;|include /etc/nginx/snippets/nomadro-work-mf.conf;\\n '"$INCLUDE"'|g' "$NGCONF"
else
sed -i 's|location / {{| '"$INCLUDE"'\\n location / {{|g' "$NGCONF"
fi
fi
nginx -t && systemctl reload nginx
systemctl daemon-reload
systemctl enable nomadro-work
systemctl restart nomadro-work
sleep 2
systemctl is-active nomadro-work
curl -sS -m 5 http://127.0.0.1:8021/api/work/v1/health || true
export CI=true
export NODE_OPTIONS='--max-old-space-size=1536'
/usr/local/bin/pnpm install --prefer-offline || pnpm install --prefer-offline || true
systemctl reset-failed nomadro 2>/dev/null || true
systemctl start nomadro
sleep 6
systemctl is-active nomadro || true
""",
timeout=480,
)
if code != 0:
print("DEPLOY_FAIL", code)
return code
for i in range(12):
_, out = sh(
c,
"curl -sS -o /dev/null -w '%{http_code}' --max-time 8 http://127.0.0.1:4003/ || echo 000",
timeout=20,
)
http = out.strip().splitlines()[-1] if out.strip() else "000"
print("probe", i, http, flush=True)
if http in ("200", "301", "302", "307", "308"):
break
time.sleep(2)
sh(
c,
r"""
set +e
echo BUILD=$(cat /var/www/nomadro.cn/.next/BUILD_ID)
systemctl is-active nomadro nomadro-work
curl -sS -m 8 http://127.0.0.1:8021/health; echo
curl -sS -m 12 -o /dev/null -w 'home=%{http_code}\n' https://nomadro.com/
curl -sS -m 12 -o /dev/null -w 'work=%{http_code}\n' https://nomadro.com/social/work
curl -sS -m 12 -o /dev/null -w 'boards=%{http_code}\n' https://nomadro.com/social/work/boards
curl -sS -m 12 -o /dev/null -w 'pads=%{http_code}\n' https://nomadro.com/social/work/pads
curl -sS -m 12 -o /dev/null -w 'api_boards=%{http_code}\n' https://nomadro.com/api/work/v1/boards
curl -sS -m 12 -o /dev/null -w 'api_health=%{http_code}\n' https://nomadro.com/api/work/v1/health
curl -sS -m 12 https://nomadro.com/api/work/v1/boards | head -c 360; echo
curl -sS -m 12 https://nomadro.com/social/work | tr '\n' ' ' | grep -oE 'Nomadro Work|进入看板|Kanban|/social/work/boards|work\.(nomadro)' | head -15
""",
timeout=90,
)
print("DONE_US_WORK", flush=True)
return 0
finally:
c.close()
if __name__ == "__main__":
raise SystemExit(main())