nomadro/scripts/deploy_portal_upgrade_com.py
Eric 499f2d3e26 Ship coffeechat scheduling, notify PB access, and social/admin UI polish for .com.
Document the 2026-11-05 UI/UX upgrade and keep roster/live gates aligned with face-check and presence.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 18:19:37 +08:00

86 lines
3.9 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
import subprocess
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']
sources = set(deployment['SOURCE_FILES'])
for command in [
['git', 'diff', '--name-only', '--', 'src'],
['git', 'ls-files', '--others', '--exclude-standard', '--', 'src/lib', 'src/modules/coffee', 'src/modules/notify', 'src/modules/social'],
]:
sources.update(line for line in subprocess.check_output(command, cwd=ROOT, text=True, encoding='utf-8').splitlines() if (ROOT / line).is_file())
deployment['SOURCE_FILES'] = sorted(sources)
client = runpy.run_path(str(ROOT / 'apps/admin/deploy/_upload_next_us.py'))['connect']()
assert client.get_transport().getpeername()[0] == '108.174.50.68', 'US host only'
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
curl -fsS --max-time 15 https://nomadro.com/api/coffee/v1/hosts >/dev/null
curl -fsS --max-time 15 -H 'X-Social-Guest-Id: release-readonly-probe' 'https://nomadro.com/api/notify/v1/inbox?subscriber_id=release-readonly-probe' >/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()