"""Run on the US VPS via the focused deploy script; never print credentials.""" import json import pathlib import secrets import shutil import sqlite3 import subprocess import sys stage, backup = map(pathlib.Path, sys.argv[1:3]) targets = json.loads((stage / 'manifest.json').read_text()) extras = ['/etc/nomadro/uiux-services.env', '/etc/nomadro/uiux-notify.env', '/etc/systemd/system/nomadro-notify.service', '/etc/systemd/system/nomadro-coffee.service', '/etc/systemd/system/nomadro.service.d/uiux-notify.conf', '/etc/systemd/system/nomadro-social.service.d/uiux-notify.conf'] state = {} for target in targets + extras: path = pathlib.Path(target) state[target] = path.exists() if path.exists(): dest = backup / 'files' / target.lstrip('/') dest.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(path, dest) (backup / 'manifest.json').write_text(json.dumps(state)) # Consistent SQLite backup including committed WAL contents, before any schema changes. for source in pathlib.Path('/opt/pocketbase/pb_data').glob('*.db'): target = backup / ('pb-' + source.name) with sqlite3.connect(f'file:{source}?mode=ro', uri=True) as db, sqlite3.connect(target) as copy: db.backup(copy) assert copy.execute('PRAGMA quick_check').fetchone()[0] == 'ok' print('Database backup verified:', target.name, flush=True) pid = subprocess.check_output(['systemctl', 'show', 'nomadro-social', '-p', 'MainPID', '--value']).decode().strip() env = dict(item.split('=', 1) for item in pathlib.Path('/proc/' + pid + '/environ').read_bytes().decode().split('\0') if '=' in item) keys = ['PB_URL', 'PB_ADMIN_EMAIL', 'PB_ADMIN_PASSWORD', 'POCKETBASE_URL', 'POCKETBASE_EMAIL', 'POCKETBASE_PASSWORD'] selected = {key: env[key] for key in keys if key in env} assert selected.get('PB_ADMIN_EMAIL') or selected.get('POCKETBASE_EMAIL') assert selected.get('PB_ADMIN_PASSWORD') or selected.get('POCKETBASE_PASSWORD') selected['PB_INTERNAL_URL'] = 'http://127.0.0.1:8090' secretfile = pathlib.Path('/etc/nomadro/uiux-notify.env') if secretfile.exists(): token = secretfile.read_text().strip().split('=', 1)[1] else: token = secrets.token_hex(32) secretfile.write_text('NOTIFY_INTERNAL_TOKEN=' + token + '\n') secretfile.chmod(0o600) selected['NOTIFY_INTERNAL_TOKEN'] = token envfile = pathlib.Path('/etc/nomadro/uiux-services.env') envfile.write_text(''.join(key + '=' + json.dumps(value) + '\n' for key, value in selected.items())) envfile.chmod(0o600) for target in targets: path = pathlib.Path(target) path.parent.mkdir(parents=True, exist_ok=True) shutil.copy2(stage / target.lstrip('/'), path) for service, port in [('notify', 8023), ('coffee', 8024)]: unit = f"""[Unit] Description=Nomadro {service} service (.com) After=network.target pocketbase.service [Service] Type=simple WorkingDirectory=/var/www/nomadro.cn/apps/api EnvironmentFile=/etc/nomadro/uiux-services.env Environment=PYTHONUNBUFFERED=1 Environment=COFFEE_SEED_DEMO=1 ExecStart=/var/www/nomadro.cn/apps/api/.venv-social/bin/uvicorn {service}_main:app --host 127.0.0.1 --port {port} --workers 1 Restart=on-failure RestartSec=5 NoNewPrivileges=true PrivateTmp=true [Install] WantedBy=multi-user.target """ pathlib.Path('/etc/systemd/system/nomadro-' + service + '.service').write_text(unit) for service in ['nomadro', 'nomadro-social']: path = pathlib.Path('/etc/systemd/system/' + service + '.service.d/uiux-notify.conf') path.parent.mkdir(parents=True, exist_ok=True) path.write_text('[Service]\nEnvironmentFile=/etc/nomadro/uiux-notify.env\n') print('Backups and source installation complete:', str(backup), flush=True)