← back to Pm2 Migration

finalize-on-kamatera.sh

143 lines

#!/usr/bin/env bash
# finalize-on-kamatera.sh
#
# Single atomic operation that:
#   1. Adds the 24 migrated agents to the SKIP_RESTART allowlist in
#      /root/.skills/fix-ngx/scripts/monitor-all-services.sh
#      (the script already supports SKIP_RESTART — we just append names).
#   2. Saves a backup of the original script before modifying.
#   3. pm2 delete each migrated agent on Kamatera.
#   4. pm2 save the new (smaller) state.
#   5. Verifies: lists what's still running on Kamatera.
#
# Run: bash finalize-on-kamatera.sh
#
# Reversible: restore the .bak file, then for each agent:
#   ssh root@kamatera "cd <agent_cwd> && pm2 start ecosystem.config.js"

set -euo pipefail

KAMATERA_USER="${KAMATERA_USER:-root}"
KAMATERA_HOST="${KAMATERA_HOST:-45.61.58.125}"

# The 40 confirmed-healthy local migrations.
MIGRATED=(
  archive-agent
  blog-agent
  claudette-agent
  color-search
  cost-hunter
  dex-dedup-agent
  dex-hash-backfill
  dw-central
  full-monty-agent
  gap-agent
  gemini-catalog-tagger
  gemini-classifier
  gemini-tag-engine
  george-gmail
  graphic-agent
  hawke
  hormuz-orchestrator
  launch-supervisor
  momentum-pricer
  norma-bluesky
  norma-email
  norma-facebook
  norma-instagram
  norma-price
  norma-twitter
  plumbing-agent
  product-agent
  resize-it
  room-setting-agent
  shopify-expert-agent
  shopify-queue-worker
  silas-sku-agent
  sku-check-skill
  slack-dm-viewer
  timeout-agent
  update-sku-agent
  vendor-command-center
  vendor-discount-agent
  vendor-onboarding
  yolo-agent
)

# Build space-separated list for SKIP_RESTART export
SKIP_LIST="${MIGRATED[*]}"

echo "═══════════════════════════════════════════════════════════"
echo "  Finalize PM2 migration on Kamatera"
echo "  Will allowlist + delete ${#MIGRATED[@]} agents"
echo "═══════════════════════════════════════════════════════════"
echo
echo "Agents to delete from Kamatera:"
printf '  - %s\n' "${MIGRATED[@]}"
echo
read -p "Proceed? Type 'yes' to confirm: " ans
[[ "$ans" == "yes" ]] || { echo "Aborted."; exit 0; }

ssh "$KAMATERA_USER@$KAMATERA_HOST" "SKIP_LIST='$SKIP_LIST' bash -s" <<'REMOTE'
set -euo pipefail
SCRIPT="/root/.skills/fix-ngx/scripts/monitor-all-services.sh"

echo "→ backing up monitor script"
cp -n "$SCRIPT" "${SCRIPT}.bak-$(date +%Y%m%d-%H%M%S)"

echo "→ adding $(echo "$SKIP_LIST" | wc -w) names to SKIP_RESTART allowlist"
# Match the indented `local SKIP_RESTART="..."` line (or unindented), single sed pass.
# Use python for safe substring replacement to avoid sed-quoting hell.
python3 - "$SCRIPT" "$SKIP_LIST" <<'PY'
import re, sys
path, names = sys.argv[1], sys.argv[2]
with open(path) as f: src = f.read()
def repl(m):
    indent, prefix, existing, suffix = m.group(1), m.group(2), m.group(3), m.group(4)
    have = set(existing.split())
    add = [n for n in names.split() if n not in have]
    new = (existing + " " + " ".join(add)).strip()
    return f'{indent}{prefix}SKIP_RESTART="{new}"{suffix}'
new_src, n = re.subn(
    r'^(\s*)(local\s+)?SKIP_RESTART="([^"]*)"(.*)$',
    repl, src, count=1, flags=re.MULTILINE
)
if n == 0:
    sys.exit("ERROR: SKIP_RESTART line not found")
with open(path, 'w') as f: f.write(new_src)
print(f"OK: appended {len([n for n in names.split()])} names (skipping dups)")
PY

echo "→ verifying"
grep -E "SKIP_RESTART=" "$SCRIPT" | head -1

echo
echo "→ deleting migrated agents from PM2"
for n in $SKIP_LIST; do
  pm2 delete "$n" 2>&1 | grep -E "✓|not found" | head -1 || true
done

echo
echo "→ saving PM2 state"
pm2 save

echo
echo "→ remaining PM2 processes:"
pm2 jlist 2>/dev/null | jq -r '.[] | "\(.pm2_env.status)\t\(.name)"' | sort | column -t

echo
echo "✓ Finalize complete."
REMOTE

echo
echo "═══════════════════════════════════════════════════════════"
echo "  Done. Watch for 5 min — if nothing reappears, you're clear."
echo "═══════════════════════════════════════════════════════════"
echo
echo "Verify in 5 minutes (after service-monitor.timer fires once):"
echo "  ssh root@$KAMATERA_HOST 'pm2 list'"
echo
echo "If any of the 24 reappear, the SKIP_RESTART pattern doesn't match —"
echo "  inspect the script with:"
echo "  ssh root@$KAMATERA_HOST 'grep -n SKIP_RESTART /root/.skills/fix-ngx/scripts/monitor-all-services.sh'"