nomadro/scripts/deploy_portal_upgrade_com.py
Eric 308974bf5e Harden platform upgrade: ops admin, live config, shared service proxy, and smoke deploys.
Ship Operations console polish, PocketBase social fixtures, and portal join-leads without local DBs or probe scripts.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 01:20:55 +08:00

75 lines
3.1 KiB
Python

"""Deploy the locally built portal only; retain a rollback and verify public HTTPS."""
import hashlib
import json
import runpy
import tarfile
import tempfile
from pathlib import Path
deployment = runpy.run_path(str(Path(__file__).with_name('deploy_platform_upgrade_com.py')))
ROOT, PORTAL, RELEASE = (deployment[key] for key in ['ROOT', 'PORTAL', 'RELEASE'])
run = deployment['run']
backup = deployment['BACKUP']
stage = deployment['STAGE']
client = runpy.run_path(str(ROOT / 'apps/admin/deploy/_upload_next_us.py'))['connect']()
try:
with tempfile.TemporaryDirectory() as temporary:
package = Path(temporary) / 'portal.tgz'
with tarfile.open(package, 'w:gz') as archive:
def include(info):
if any(info.name == f'.next/{part}' or info.name.startswith(f'.next/{part}/') for part in ['cache', 'dev']):
return None
return None if info.name.endswith('/lock') else info
archive.add(ROOT / '.next', arcname='.next', filter=include)
for source in deployment['SOURCE_FILES']:
archive.add(ROOT / source, arcname=source)
digest = hashlib.sha256(package.read_bytes()).hexdigest()
print('Uploading portal', RELEASE, round(package.stat().st_size / 1024**2, 1), 'MB', flush=True)
with client.open_sftp() as sftp:
sftp.put(str(package), f'/tmp/nomadro-portal-{RELEASE}.tgz')
run(client, f'''set -eu
echo '{digest} /tmp/nomadro-portal-{RELEASE}.tgz' | sha256sum -c -
mkdir -p {stage} {backup}
tar -xzf /tmp/nomadro-portal-{RELEASE}.tgz -C {stage}
cd {PORTAL}
tar -czf {backup}/sources.tgz {' '.join(deployment['SOURCE_FILES'])} 2>/dev/null || test -s {backup}/sources.tgz
cp -a /etc/systemd/system/nomadro.service.d {backup}/runtime
''')
try:
run(client, f'''set -eu
cd {PORTAL}
systemctl stop nomadro
mv .next {backup}/next
mv {stage}/.next .next
cp -a {stage}/. .
if [ -f src/proxy.ts ]; then mv src/proxy.ts {backup}/proxy.ts; fi
systemctl start nomadro
for attempt in $(seq 1 30); do
if curl -fsS --max-time 3 http://127.0.0.1:4003/api/health >/dev/null; then break; fi
sleep 1
done
curl -fsS --max-time 15 https://nomadro.com/ >/dev/null
curl -fsS --max-time 15 https://nomadro.com/social/chat >/dev/null
curl -fsS --max-time 15 https://nomadro.com/notifications/settings >/dev/null
systemctl is-active nomadro
cat .next/BUILD_ID
''')
except Exception:
run(client, f'''set -eu
systemctl stop nomadro
if [ -d {backup}/next ]; then
mv {PORTAL}/.next {stage}/failed-next
mv {backup}/next {PORTAL}/.next
fi
tar -xzf {backup}/sources.tgz -C {PORTAL}
systemctl start nomadro
''')
raise
path = ROOT / 'artifacts/platform-upgrade/deployment.json'
report = json.loads(path.read_text(encoding='utf-8'))
report['portal'] = {'release': RELEASE, 'buildId': (ROOT / '.next/BUILD_ID').read_text().strip(), 'backup': backup, 'sha256': digest, 'httpsHealthPassed': True}
path.write_text(json.dumps(report, indent=2), encoding='utf-8')
print('PORTAL DEPLOYED', json.dumps(report['portal']), flush=True)
finally:
client.close()