Repository Public
1#!/bin/bash2# Local control-api for the Apple clients (development, integration tests, UI tests).3# local-backend.sh build build control-api, action-orchestrator and mccctl into /tmp/mcc-swift4# local-backend.sh start <database> create the database if needed, start control-api on 127.0.0.1:8760 and the5# action orchestrator (executes actions, alerts, maintenance; health on :8764)6# local-backend.sh stop stop the instances started by this script (by PID — never by name)7# local-backend.sh env <database> print the environment (for mccctl)8# local-backend.sh owner <database> <handle> <name> print a one-time setup token for the first owner9set -euo pipefail10ROOT="$(cd "$(dirname "$0")/../../.." && pwd)"11BIN=/tmp/mcc-swift12PIDFILE="$BIN/control-api.pid"13ORCH_PIDFILE="$BIN/action-orchestrator.pid"14PORT="${MCC_LOCAL_PORT:-8760}"15PSQL_BIN="$(command -v psql || echo /opt/homebrew/bin/psql)"1617backend_env() {18 cat <<EOF19MCC_ENV=dev20MCC_DATABASE_URL=postgres://127.0.0.1/$1?sslmode=disable21MCC_SECRETS_DIR=$HOME/.config/mcc-swiftdev22MCC_API_LISTEN=127.0.0.1:$PORT23MCC_PUBLIC_ORIGIN=http://localhost:$PORT24MCC_RP_ID=localhost25MCC_WEB_UPSTREAM=26MCC_APPLE_APP_IDS=3YM54G49SN.ai.spboucher.clustr,3YM54G49SN.ai.spboucher.clustr.debug27MCC_ORCHESTRATOR_LISTEN=127.0.0.1:${MCC_LOCAL_ORCH_PORT:-8764}28EOF29}3031case "${1:-}" in32 build)33 mkdir -p "$BIN"34 (cd "$ROOT/services" && go build -o "$BIN/control-api" ./cmd/control-api && go build -o "$BIN/mccctl" ./cmd/mccctl \35 && go build -o "$BIN/action-orchestrator" ./cmd/action-orchestrator)36 ;;37 env) backend_env "$2" ;;38 start)39 db="$2"40 "$0" stop >/dev/null 2>&1 || true41 if lsof -nP -iTCP:"$PORT" -sTCP:LISTEN >/dev/null 2>&1; then echo "port $PORT is in use by another process; not touching it" >&2; exit 1; fi42 "$PSQL_BIN" -X -q -d postgres -tAc "select 1 from pg_database where datname='$db'" | grep -q 1 || "$PSQL_BIN" -X -q -d postgres -c "create database \"$db\""43 mkdir -p "$HOME/.config/mcc-swiftdev"44 # shellcheck disable=SC204645 env $(backend_env "$db" | xargs) ${MCC_EXTRA_ENV:-} "$BIN/control-api" >>"$BIN/control-api.log" 2>&1 &46 echo $! > "$PIDFILE"47 for _ in $(seq 1 100); do48 if curl -fsS "http://127.0.0.1:$PORT/api/v1/health/ready" >/dev/null 2>&1; then49 echo "control-api ready on :$PORT (db $db, pid $(cat "$PIDFILE"))"50 if [ -x "$BIN/action-orchestrator" ] && [ "${MCC_NO_ORCHESTRATOR:-}" != 1 ]; then51 # shellcheck disable=SC204652 env $(backend_env "$db" | xargs) ${MCC_EXTRA_ENV:-} "$BIN/action-orchestrator" >>"$BIN/action-orchestrator.log" 2>&1 &53 echo $! > "$ORCH_PIDFILE"54 echo "action-orchestrator started (pid $(cat "$ORCH_PIDFILE"))"55 fi56 exit 057 fi58 sleep 0.159 done60 echo "control-api did not become ready; see $BIN/control-api.log" >&2; exit 161 ;;62 stop)63 for pf in "$ORCH_PIDFILE" "$PIDFILE"; do64 if [ -f "$pf" ]; then65 pid="$(cat "$pf")"66 if kill -0 "$pid" 2>/dev/null; then kill "$pid"; for _ in $(seq 1 50); do kill -0 "$pid" 2>/dev/null || break; sleep 0.1; done; fi67 mv "$pf" "$pf.stopped"68 fi69 done70 ;;71 owner)72 # shellcheck disable=SC204673 env $(backend_env "$2" | xargs) "$BIN/mccctl" bootstrap-owner --handle "$3" --name "$4" | sed -n 's/.*setup#//p'74 ;;75 *) sed -n '2,8p' "$0"; exit 2 ;;76esac77