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

38 lines
1.5 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# Unregister an instance added by add-instance.sh: stops its PM2 process and
# drops it from scripts/instances.json. The database directory is left on
# disk — deleting credentials is a decision for a human with rm.
#
# ./scripts/remove-instance.sh <N>
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; }
[ $# -eq 1 ] || die "usage: $0 <N>"
N="$1"
case "$N" in (*[!0-9]*|'') die "N must be a number, got: $N" ;; esac
[ "$N" -ge 2 ] || die "instance 1 is the legacy install - remove it with pm2 delete autofirmer"
[ -f "$REGISTRY" ] || die "no scripts/instances.json - nothing to remove"
PM2_NAME="autofirmer-$N"
node - "$REGISTRY" "$PM2_NAME" <<'NODE'
const { readFileSync, writeFileSync } = require('node:fs');
// `node -` puts '-' in argv[1], so the arguments start at argv[2].
const [file, name] = process.argv.slice(2);
const entries = JSON.parse(readFileSync(file, 'utf8'));
if (!entries.some((e) => e.name === name)) { console.error(` [X] ${name} is not registered`); process.exit(1); }
writeFileSync(file, JSON.stringify(entries.filter((e) => e.name !== name), null, 4) + '\n');
console.log(` registry: ${name} removed`);
NODE
pm2 delete "$PM2_NAME" >/dev/null 2>&1 || info "pm2: $PM2_NAME was not running"
pm2 save >/dev/null
info "pm2: $PM2_NAME deleted and process list saved"
info "database left at .data/instance-$N/ - delete by hand if you mean it"