Files
autofirmer-expanded/scripts/add-instance.sh
T
Brandon LiandClaude Fable 5 8154f25bb9 Backups to master, replacement-VPS restore, broker proxy, multi-instance
Four features:

- Backup: lib/backup-sync.ts pushes the full settings table and firm
  credentials (the deliberate exception to "credentials never leave the
  machine") to the master's bearer-gated /api/instances/backup — every
  5 minutes when the content hash changed, within seconds of a settings
  or credential edit, and on the new backup-now command. Enabled by the
  master_backup_token setting.

- Restore: a dead VPS's replacement pulls the backup and adopts its
  uuid/name, so roster, payouts and card links carry on. Pre-boot via
  seed-settings.js --restore-from (setup-linux.sh prompts for it) or
  the Settings page's restore picker (lib/backup-restore.ts). Stop the
  old machine first; the master 409s a live-name rename.

- Proxy: the proxy_url setting routes BROKER traffic only — TopstepX
  REST + SignalR hub, Tradovate REST + websocket, Yahoo quotes — via
  lib/outbound-proxy.ts (axios agents, undici dispatcher, ws agent).
  The master link, reporter, catalog and backup push stay direct, so a
  dead proxy cannot take down the control channel. Restart to apply.

- Multi-instance: AUTOFIRMER_DB moves the SQLite file so one checkout
  runs N instances; scripts/instances.json (per-device, gitignored)
  drives start-all/update-check, and add-instance.sh / remove-instance.sh
  manage the extra PM2 processes. Absent registry = single-instance
  behavior, unchanged. Instance 1 keeps port 3000 for the clicker and
  extension.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-09-22 13:55:35 -05:00

107 lines
4.7 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Register an additional autofirmer instance on this device: same checkout,
# same build, its own PM2 process, port and database.
#
# ./scripts/add-instance.sh <N> [--name <instance_name>] [--master <url>]
# [--port <port>] [--token <backup_token>]
# [--restore-from <instance_uuid>]
#
# N instance number, 2 or higher (1 is the legacy install:
# pm2 name `autofirmer`, port 3000, ./autotrader.sqlite —
# registered by install-autostart.sh, never by this script)
# --name instance_name to seed (default: <hostname>-N)
# --master master dashboard url to seed (default: leave the app's seed)
# --port listen port (default: 3000 + N - 1; NOTE the master dev
# server uses 3001 — pick another port on a dev machine)
# --token master_backup_token to seed
# --restore-from pull that instance's backup from the master before first
# boot and adopt its identity (requires --master and --token)
#
# The instance lands in scripts/instances.json, which start-all.mjs and
# update-check.mjs iterate. The first run of this script also records the
# legacy instance 1 there so updates keep restarting it.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
REGISTRY="$ROOT/scripts/instances.json"
info() { printf ' %s\n' "$1"; }
die() { printf ' [X] %s\n' "$1" >&2; exit 1; }
[ $# -ge 1 ] || die "usage: $0 <N> [--name ...] [--master ...] [--port ...] [--token ...] [--restore-from ...]"
N="$1"; shift
case "$N" in (*[!0-9]*|'') die "N must be a number, got: $N" ;; esac
[ "$N" -ge 2 ] || die "N starts at 2 — instance 1 is the legacy install (install-autostart.sh)"
NAME="$(hostname)-$N"
MASTER=""
PORT=$((3000 + N - 1))
TOKEN=""
RESTORE_FROM=""
while [ $# -gt 0 ]; do
case "$1" in
--name) NAME="$2"; shift 2 ;;
--master) MASTER="$2"; shift 2 ;;
--port) PORT="$2"; shift 2 ;;
--token) TOKEN="$2"; shift 2 ;;
--restore-from) RESTORE_FROM="$2"; shift 2 ;;
*) die "unknown option: $1" ;;
esac
done
PM2_NAME="autofirmer-$N"
DB_REL=".data/instance-$N/autotrader.sqlite"
DB_ABS="$ROOT/$DB_REL"
if [ -n "$RESTORE_FROM" ]; then
[ -n "$MASTER" ] || die "--restore-from needs --master"
[ -n "$TOKEN" ] || die "--restore-from needs --token"
fi
NEXT_BIN="$ROOT/node_modules/next/dist/bin/next"
[ -f "$NEXT_BIN" ] || die "next not found at $NEXT_BIN - run npm install first"
[ -d "$ROOT/.next" ] || die "no production build - run npm run build first"
command -v pm2 >/dev/null 2>&1 || die "PM2 not installed - run install-autostart.sh first"
# ── registry: create with the legacy entry, refuse duplicates, append ───────
node - "$REGISTRY" "$PM2_NAME" "$PORT" "$DB_REL" <<'NODE'
const { existsSync, readFileSync, writeFileSync } = require('node:fs');
const [, file, name, port, db] = process.argv;
const entries = existsSync(file)
? JSON.parse(readFileSync(file, 'utf8'))
: [{ name: 'autofirmer', port: 3000, db: 'autotrader.sqlite' }];
for (const e of entries) {
if (e.name === name) { console.error(` [X] ${name} is already registered`); process.exit(1); }
if (e.port === Number(port)) { console.error(` [X] port ${port} is already used by ${e.name}`); process.exit(1); }
if (e.db === db) { console.error(` [X] db ${db} is already used by ${e.name}`); process.exit(1); }
}
entries.push({ name, port: Number(port), db });
writeFileSync(file, JSON.stringify(entries, null, 4) + '\n');
console.log(` registry: ${name} on :${port} -> ${db}`);
NODE
mkdir -p "$(dirname "$DB_ABS")"
# ── seed (and optionally restore) the new database ──────────────────────────
cd "$ROOT"
SEED_ARGS=()
[ -n "$TOKEN" ] && SEED_ARGS+=(--token "$TOKEN")
[ -n "$RESTORE_FROM" ] && SEED_ARGS+=(--restore-from "$RESTORE_FROM")
AUTOFIRMER_DB="$DB_ABS" node scripts/seed-settings.js "$MASTER" "$NAME" ${SEED_ARGS[@]+"${SEED_ARGS[@]}"}
# ── PM2 ─────────────────────────────────────────────────────────────────────
pm2 delete "$PM2_NAME" >/dev/null 2>&1 || true
PORT="$PORT" AUTOFIRMER_DB="$DB_ABS" pm2 start "$NEXT_BIN" --name "$PM2_NAME" --interpreter node --update-env -- start
pm2 save >/dev/null
info "pm2: $PM2_NAME registered and saved"
echo
info "instance $N is up: http://localhost:$PORT"
info "database: $DB_REL"
if [ -n "$RESTORE_FROM" ]; then
info "restored from backup $RESTORE_FROM - stop the old machine if it is still running"
fi
info "remove later with: ./scripts/remove-instance.sh $N"