Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

6.2 KB
Raw Blame
#!/bin/sh
set -eu

# The step that configures the logged-in user needs the harness list the MDM's managed preferences
# (or the environment) give; install-endpoint.sh reads every other setting itself. Only that one
# value is taken here, so a forwarder token in the preferences never reaches the processes this
# script starts.
# shellcheck source=../beacon-settings.sh
. /opt/beacon/scripts/beacon-settings.sh
BEACON_ENDPOINT_HARNESSES="${BEACON_ENDPOINT_HARNESSES:-$(beacon_settings_harnesses)}"

# Only what this step decides is passed. install-endpoint.sh applies its own defaults for the rest,
# after reading the managed preferences, so a variable set here (even to an empty default) would
# shadow the MDM's value for it.
BEACON_BIN="${BEACON_BIN:-/opt/beacon/bin/beacon}" \
BEACON_COLLECTOR="${BEACON_COLLECTOR:-/opt/beacon/bin/beacon-otelcol}" \
BEACON_NO_START=1 \
  /opt/beacon/scripts/install-endpoint.sh

"${BEACON_BIN:-/opt/beacon/bin/beacon}" endpoint user-config repair-installed \
  --system \
  --harness "${BEACON_ENDPOINT_HARNESSES:-claude,codex}" || {
  echo "Warning: could not refresh installed user endpoint configuration after package install" >&2
}

forwarder_running() {
  label="$1"

  launchctl print "system/$label" 2>/dev/null | grep -Eq 'state = running|pid ='
}

forwarder_stably_running() {
  label="$1"
  observations=0

  while [ "$observations" -lt 3 ]; do
    if ! forwarder_running "$label"; then
      return 1
    fi
    observations=$((observations + 1))
    if [ "$observations" -lt 3 ]; then
      sleep 1
    fi
  done
}

wait_for_forwarder_bootout() {
  target="$1"
  attempts=0

  while launchctl print "$target" >/dev/null 2>&1; do
    attempts=$((attempts + 1))
    if [ "$attempts" -ge 65 ]; then
      return 1
    fi
    sleep 1
  done
}

restore_existing_forwarder() {
  label="$1"
  plist_path="$2"
  target="system/$label"

  # No plist means the customer has not installed this optional forwarder.
  if [ ! -f "$plist_path" ]; then
    return 0
  fi

  if forwarder_running "$label"; then
    out="$(launchctl kickstart -k "$target" 2>&1)" || {
      echo "Warning: launchctl kickstart failed for $label: $out" >&2
    }
    if forwarder_stably_running "$label"; then
      return 0
    fi
  fi

  for delay in 1 2 4 8 16; do
    out="$(launchctl bootstrap system "$plist_path" 2>&1)" || {
      echo "Warning: launchctl bootstrap failed for $label: $out" >&2
    }
    if forwarder_stably_running "$label"; then
      return 0
    fi

    out="$(launchctl bootout "$target" 2>&1)" || {
      echo "Warning: launchctl bootout retry failed for $label: $out" >&2
    }
    if ! wait_for_forwarder_bootout "$target"; then
      echo "Warning: timed out waiting for launchctl bootout for $label" >&2
      continue
    fi
    out="$(launchctl bootstrap system "$plist_path" 2>&1)" || {
      echo "Warning: launchctl bootstrap after bootout failed for $label: $out" >&2
    }
    if forwarder_stably_running "$label"; then
      return 0
    fi

    out="$(launchctl kickstart -k "$target" 2>&1)" || {
      echo "Warning: launchctl kickstart retry failed for $label: $out" >&2
    }
    if forwarder_stably_running "$label"; then
      return 0
    fi
    sleep "$delay"
  done

  out="$(launchctl print "$target" 2>&1)" || true
  echo "Error: could not restore existing forwarder $label after package install. launchctl print: $out" >&2
  return 1
}

if command -v launchctl >/dev/null 2>&1; then
  launchctl bootout system/com.beacon.endpoint.collector >/dev/null 2>&1 || true
  launchctl bootstrap system /Library/LaunchDaemons/com.beacon.endpoint.collector.plist >/dev/null 2>&1 || {
    launchctl print system/com.beacon.endpoint.collector >/dev/null 2>&1 || exit 1
  }
  restore_existing_forwarder "com.beacon.endpoint.falcon-forwarder" "/Library/LaunchDaemons/com.beacon.endpoint.falcon-forwarder.plist"
  restore_existing_forwarder "com.beacon.endpoint.s3-forwarder" "/Library/LaunchDaemons/com.beacon.endpoint.s3-forwarder.plist"
  restore_existing_forwarder "com.beacon.endpoint.gcs-forwarder" "/Library/LaunchDaemons/com.beacon.endpoint.gcs-forwarder.plist"
  "${BEACON_BIN:-/opt/beacon/bin/beacon}" endpoint update install-daemon || {
    echo "Error: could not reconcile Beacon endpoint updater daemon" >&2
    exit 1
  }
  # The scheduled inventory job is on by default; loading it fires the first heartbeat at once, so
  # an endpoint upgraded from a version without the job starts reporting inventory immediately.
  # Non-fatal: the endpoint is installed and collecting either way.
  "${BEACON_BIN:-/opt/beacon/bin/beacon}" endpoint inventory install-daemon --system || \
    echo "Warning: scheduled inventory job not configured (endpoint is installed and running)" >&2
  # The scheduled session sync is opt-in (`session_sync.enabled`, set with --session-sync). This
  # loads it when on and removes it when off, so an upgrade keeps whatever the endpoint chose.
  "${BEACON_BIN:-/opt/beacon/bin/beacon}" endpoint sessions install-daemon --system || \
    echo "Warning: scheduled session sync job not configured (endpoint is installed and running)" >&2
  # A connected endpoint's Beacon Cloud forwarder config is re-rendered from this version's pack and
  # validated, and a running forwarder is restarted, so it runs the Vector this package installed and
  # reads the current runtime log, which the install may have replaced with a copy (switching to
  # restricted log access does). The install above ran with --no-start, so it left this to the package. Never enrolls or touches the device key, and
  # a no-op on an endpoint that is not connected. Non-fatal: a rejected config keeps the old one.
  "${BEACON_BIN:-/opt/beacon/bin/beacon}" endpoint asymptote reconcile --system || \
    echo "Warning: Beacon Cloud forwarder config not reconciled (endpoint is installed and running)" >&2
  # A Sentinel forwarder set up with `beacon endpoint sentinel forwarder install` gets this version's
  # config and is restarted, so it runs the Vector this package just installed. A no-op where none
  # is set up. Non-fatal: a rejected config keeps the old one.
  "${BEACON_BIN:-/opt/beacon/bin/beacon}" endpoint sentinel forwarder reconcile --system || \
    echo "Warning: Sentinel forwarder not reconciled (endpoint is installed and running)" >&2
fi

exit 0