nomadro/deploy/observability/_remote_build_restart.py
eric c90670a1f7 Harden portal deploy path: chunked .next upload and observability docs.
Avoid remote pnpm build overload; document health/ntfy/Kuma anchors for miniprogram smoke.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-29 01:18:12 -05:00

71 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""Build portal on production server (when Windows→server SFTP is too slow)."""
from __future__ import annotations
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
HOST, USER, PASS = "82.157.112.245", "ubuntu", "Xiao4669805"
APP = "/var/www/nomadro.cn"
CMD = r"""
set -euo pipefail
cd /var/www/nomadro.cn
git log -1 --oneline
export CI=1
pnpm build
test -f .next/BUILD_ID
echo BUILD_ID=$(cat .next/BUILD_ID)
sudo systemctl restart nomadro.service
sleep 4
systemctl is-active nomadro.service
echo '--- local ---'
for p in / /api/health /api/config /nav /book /book/read /community /vip/ebook/read /admin; do
code=$(curl -s -o /dev/null -w '%{http_code}' -m 25 http://127.0.0.1:4003$p || echo ERR)
echo "$p -> $code"
done
echo '--- public ---'
for p in / /api/health /api/config /book/read /vip/ebook/read; do
code=$(curl -sk -o /dev/null -w '%{http_code}' -m 25 https://nomadro.cn$p || echo ERR)
echo "https://nomadro.cn$p -> $code"
done
if [ -x /usr/local/bin/notify-ntfy ]; then
/usr/local/bin/notify-ntfy "Nomadro deploy" "portal built on server $(date -u +%Y-%m-%dT%H:%MZ)" default "rocket,nomadro" || true
fi
echo DONE
"""
def main() -> int:
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
c.connect(
HOST,
username=USER,
password=PASS,
timeout=30,
allow_agent=False,
look_for_keys=False,
banner_timeout=60,
)
print("starting remote build...", flush=True)
_, stdout, _ = c.exec_command(CMD, timeout=1200, get_pty=True)
while not stdout.channel.exit_status_ready():
if stdout.channel.recv_ready():
print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True)
time.sleep(0.1)
while stdout.channel.recv_ready():
print(stdout.channel.recv(65536).decode("utf-8", "replace"), end="", flush=True)
code = stdout.channel.recv_exit_status()
print(f"\n[exit {code}]", flush=True)
c.close()
return code
if __name__ == "__main__":
raise SystemExit(main())