← back to Pm2 Migration

start-tunnels.sh

73 lines

#!/usr/bin/env bash
# autossh tunnel for Kamatera postgres + redis.
# Run via PM2 so it survives reboots/restarts.
#
#   pm2 start ./start-tunnels.sh --name kamatera-tunnels --no-autorestart
#   pm2 save
#
# Local ports avoid collision with the user's existing local postgres on 5432.
# Migrated agents have their connection strings rewritten to point here.

set -u

KAMATERA_USER="${KAMATERA_USER:-root}"
KAMATERA_HOST="${KAMATERA_HOST:-45.61.58.125}"
LOCAL_PG_PORT="${LOCAL_PG_PORT:-15432}"
LOCAL_REDIS_PORT="${LOCAL_REDIS_PORT:-16379}"

# Pre-exec orphan cleanup — kill stray ssh forwarders to the same host that
# aren't parented by a live autossh. Stops the "bind: Address already in use"
# loop that happens after a network blip when the previous ssh hasn't released
# its local sockets yet.
own_pid=$$
for pid in $(pgrep -f "ssh.*${KAMATERA_HOST}" 2>/dev/null || true); do
  [ "$pid" = "$own_pid" ] && continue
  parent=$(ps -o ppid= -p "$pid" 2>/dev/null | tr -d ' ')
  [ -z "$parent" ] && continue
  pcomm=$(ps -o comm= -p "$parent" 2>/dev/null | xargs)
  case "$pcomm" in
    */autossh|autossh) continue ;;
  esac
  echo "[start-tunnels] killing orphan ssh pid=$pid (parent comm=$pcomm)" >&2
  kill "$pid" 2>/dev/null || true
done
sleep 1

# autossh tuning — quieter logs, faster dead-peer detection.
export AUTOSSH_POLL=30
export AUTOSSH_GATETIME=0          # don't wait for first successful connection
export AUTOSSH_LOGLEVEL=1          # errors only (was 4 = verbose — log spam source)
export AUTOSSH_LOGFILE=/Users/macstudio3/.pm2/logs/kamatera-tunnels.log
export AUTOSSH_MAXLIFETIME=43200   # recycle after 12h to clear lingering sockets

# Dedup repeated identical stderr lines (port-bind / timeout spam during outages)
# into "(repeated N times)" summaries. autossh's own log captures reconnect detail.
dedup_stderr() {
  awk '
    { fflush() }
    $0 == last { count++; next }
    {
      if (count > 1) print "(... previous line repeated " count " times)" > "/dev/stderr"
      print > "/dev/stderr"
      last = $0
      count = 1
    }
    END {
      if (count > 1) print "(... previous line repeated " count " times)" > "/dev/stderr"
    }
  '
}

# ServerAliveCountMax lowered 3→2 (60s vs 90s dead-detection), ConnectTimeout
# added to fail fast on network drops.
exec autossh -M 0 -N \
  -o ServerAliveInterval=30 \
  -o ServerAliveCountMax=2 \
  -o ExitOnForwardFailure=yes \
  -o StrictHostKeyChecking=no \
  -o ConnectTimeout=15 \
  -L "${LOCAL_PG_PORT}:127.0.0.1:5432" \
  -L "${LOCAL_REDIS_PORT}:127.0.0.1:6379" \
  "${KAMATERA_USER}@${KAMATERA_HOST}" \
  2> >(dedup_stderr)