SPB Git

Repository Public

install.sh 4.1 KB 95 lines shellscript

1#!/bin/sh2# Installs mcc-agent on a Mac. Idempotent. Touches only its own files and its own launchd job3# (label ai.spboucher.mcc-agent); no pre-existing job is ever modified.4#5#   install.sh --binary ./mcc-agent-darwin-arm64 \6#              --endpoints https://clustr.spboucher.ai[,host:port] \7#              --token <one-time token> --ca-fingerprint sha256:<hex> \8#              [--daemon]        LaunchDaemon with UserName (Macs without a graphical session; asks for sudo)9#              [--no-start]      install files only10#11# Endpoints: a URL selects the signed-request transport over public HTTPS (the default in production:12# macOS blocks LAN connections of launchd-started binaries); host:port selects direct mTLS.13# Default (LaunchAgent) mode never uses sudo. Re-running with an existing identity skips enrollment.14set -eu15SUDO="${SUDO:-sudo}"   # e.g. SUDO="sudo -A" with SUDO_ASKPASS for non-interactive --daemon installs16LABEL=ai.spboucher.mcc-agent17BIN_SRC=""; ENDPOINTS=""; TOKEN=""; FPR=""; MODE=agent; START=118while [ $# -gt 0 ]; do19  case "$1" in20    --binary) BIN_SRC=$2; shift 2;;21    --endpoints) ENDPOINTS=$2; shift 2;;22    --token) TOKEN=$2; shift 2;;23    --ca-fingerprint) FPR=$2; shift 2;;24    --daemon) MODE=daemon; shift;;25    --no-start) START=0; shift;;26    *) echo "unknown option: $1" >&2; exit 2;;27  esac28done29[ "$(id -u)" -ne 0 ] || { echo "run this as the ordinary user that will own the agent, not as root" >&2; exit 1; }30[ "$(uname -s)" = Darwin ] || { echo "this installer is for macOS" >&2; exit 1; }31HERE=$(cd "$(dirname "$0")" && pwd)32STATE_DIR="$HOME/.mcc-agent"33BIN_DIR="$STATE_DIR/bin"34BIN="$BIN_DIR/mcc-agent"35LOG_DIR="$HOME/Library/Logs/mcc-agent"36USER_NAME=$(id -un)37UID_NUM=$(id -u)3839mkdir -p "$BIN_DIR" "$LOG_DIR"40chmod 700 "$STATE_DIR"41if [ -n "$BIN_SRC" ]; then42  [ -f "$BIN_SRC" ] || { echo "binary not found: $BIN_SRC" >&2; exit 1; }43  if [ -f "$HERE/SHA256SUMS" ]; then44    want=$(grep " $(basename "$BIN_SRC")\$" "$HERE/SHA256SUMS" | cut -d' ' -f1 || true)45    got=$(shasum -a 256 "$BIN_SRC" | cut -d' ' -f1)46    [ -z "$want" ] || [ "$want" = "$got" ] || { echo "checksum mismatch for $BIN_SRC" >&2; exit 1; }47  fi48  cp "$BIN_SRC" "$BIN.new" && chmod 755 "$BIN.new" && mv -f "$BIN.new" "$BIN"   # atomic replace49fi50[ -x "$BIN" ] || { echo "no agent binary: pass --binary" >&2; exit 1; }51"$BIN" version5253if [ ! -f "$STATE_DIR/identity.json" ]; then54  [ -n "$ENDPOINTS" ] && [ -n "$TOKEN" ] && [ -n "$FPR" ] || {55    echo "not enrolled yet: --endpoints, --token and --ca-fingerprint are required" >&2; exit 1; }56  MCC_ENROLL_TOKEN="$TOKEN" "$BIN" enroll --endpoints "$ENDPOINTS" --ca-fingerprint "$FPR" --state-dir "$STATE_DIR"57else58  echo "already enrolled: keeping the existing identity"59fi6061render() { # template -> destination62  sed -e "s|@BIN@|$BIN|g" -e "s|@STATE_DIR@|$STATE_DIR|g" -e "s|@LOG_DIR@|$LOG_DIR|g" \63      -e "s|@USER@|$USER_NAME|g" -e "s|@HOME@|$HOME|g" "$1" > "$2"64}6566if [ "$MODE" = agent ]; then67  PLIST="$HOME/Library/LaunchAgents/$LABEL.plist"68  mkdir -p "$HOME/Library/LaunchAgents"69  render "$HERE/$LABEL.agent.plist.tmpl" "$PLIST.new"70  plutil -lint "$PLIST.new" >/dev/null71  mv -f "$PLIST.new" "$PLIST"; chmod 644 "$PLIST"72  if [ "$START" = 1 ]; then73    DOMAIN="gui/$UID_NUM"74    launchctl print "$DOMAIN" >/dev/null 2>&1 || {75      echo "no graphical session for $USER_NAME: use --daemon on this Mac" >&2; exit 1; }76    launchctl bootout "$DOMAIN/$LABEL" 2>/dev/null || true     # only our own label77    launchctl bootstrap "$DOMAIN" "$PLIST"78    launchctl kickstart "$DOMAIN/$LABEL" >/dev/null 2>&1 || true79  fi80else81  PLIST="/Library/LaunchDaemons/$LABEL.plist"82  TMP=$(mktemp)83  render "$HERE/$LABEL.daemon.plist.tmpl" "$TMP"84  plutil -lint "$TMP" >/dev/null85  echo "installing $PLIST (administrator rights needed once)"86  $SUDO install -o root -g wheel -m 644 "$TMP" "$PLIST"87  rm -f "$TMP"88  if [ "$START" = 1 ]; then89    $SUDO launchctl bootout "system/$LABEL" 2>/dev/null || true  # only our own label90    $SUDO launchctl bootstrap system "$PLIST"91  fi92fi93[ "$START" = 1 ] && sleep 3 && "$BIN" status --state-dir "$STATE_DIR" || true94echo "done. Local policy: $STATE_DIR/policy.json (absent = read-only commands). Kill switch: touch $STATE_DIR/FREEZE"95
Files main
Menu

Theme