Files
Brandon LiandClaude Opus 5.5 ccad48cf0a Instance scripts: read node - arguments from argv[2]
`node -` puts '-' in argv[1], so the registry snippets in add-instance.sh
and remove-instance.sh were one slot off: they wrote the registry to a file
named "-" in the working directory, with the instances.json path as the
instance name, a null port and the port as the db. scripts/instances.json
was never created, so the updater and start-all only ever knew instance 1 —
after an update, instance 2 and up kept running old code against the new
build until someone ran `pm2 restart all`.

Devices that already ran add-instance.sh need their scripts/instances.json
written by hand (and the stray "-" file deleted).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 20:18:33 -05:00

108 lines
4.8 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');
// `node -` puts '-' in argv[1], so the arguments start at argv[2].
const [file, name, port, db] = process.argv.slice(2);
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"