Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
3.5 KB
Raw Blame
#!/usr/bin/env bash
# Run one mutating Minikube command under the canonical branch-profile lease.
# A nested T2/full-setup/pre-gate child validates the inherited lease instead
# of acquiring a second lock for the same profile.
set -euo pipefail

WRC_RECOVERY_ONLY=false
if [[ "${1:-}" == "--recover-wrc-egress" && "$#" -eq 1 ]]; then
  WRC_RECOVERY_ONLY=true
elif [[ "${1:-}" != "--" || "$#" -lt 2 ]]; then
  printf 'usage: %s -- command [args...]\n' "${0##*/}" >&2
  exit 2
fi
shift

# GNU Make executes recipe lines containing $(MAKE) during `make -n` so the
# recursive make can print its own plan. Never let that dry-run escape hatch
# enter a mutating wrapper. GNU Make may compact short flags into tokens such
# as `ns` or `rns`; inspect only flag-shaped tokens so `--no-print-directory`
# and variable assignments containing the letter n are not false positives.
t2_makeflags_is_dry_run() {
  local make_flag short_flags
  for make_flag in ${MAKEFLAGS:-}; do
    case "${make_flag}" in
      -n|n|--just-print|--dry-run|--recon) return 0 ;;
      --*|*=*) continue ;;
    esac
    short_flags="${make_flag#-}"
    if [[ "${short_flags}" =~ ^[a-zA-Z]+$ ]] && [[ "${short_flags}" == *n* ]]; then
      return 0
    fi
  done
  return 1
}

if t2_makeflags_is_dry_run; then
  exit 0
fi

ROOT="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/../.." && pwd -P)"
T2_PROJECT_DIR="${T2_PROJECT_DIR:-${ROOT}}"
T2_PROFILE="${T2_PROFILE:-${MINIKUBE_PROFILE:-}}"
T2_CONTEXT="${T2_CONTEXT:-${CONTROL_API_REAL_PG_CONTEXT:-${T2_PROFILE}}}"
MINIKUBE_PROFILE="${MINIKUBE_PROFILE:-${T2_PROFILE}}"
CONTROL_API_REAL_PG_CONTEXT="${CONTROL_API_REAL_PG_CONTEXT:-${T2_CONTEXT}}"
T2_SKIP_LOCK="${T2_SKIP_LOCK:-false}"
T2_GATE_ID="${T2_GATE_ID:-mutation-wrapper}"

if [[ -z "${T2_PROFILE}" || -z "${T2_CONTEXT}" || "${T2_PROFILE}" != "${T2_CONTEXT}" ]]; then
  printf 'PROFILE_LOCK_REQUIRED: mutation target profile and Kubernetes context must match\n' >&2
  exit 1
fi

# shellcheck source=scripts/minikube/t2-common.sh
source "${ROOT}/scripts/minikube/t2-common.sh"
t2_require_commands
t2_repo_metadata
t2_profile_scope
t2_profile_status
if [[ "${T2_BOOTSTRAP_REQUIRED}" == true ]]; then
  T2_NEXT_COMMAND="bootstrap the branch-owned profile, then retry the mutating target"
  t2_fail DEVELOPMENT_SCOPE_REQUIRED "branch-owned Minikube profile is not bootstrapped: ${T2_PROFILE}"
fi
t2_context_check
t2_profile_context_identity_check
if [[ "$WRC_RECOVERY_ONLY" == true ]]; then
  # Recovery is a dedicated command, not a general skip-guard environment flag.
  # It still requires the exact live profile lease; only the recovery body is
  # permitted to run while that profile has a pending fault-injection journal.
  if [[ "$T2_SKIP_LOCK" == true ]]; then t2_lock_validate_inherited; else t2_lock_acquire; fi
else
  t2_mutation_lock
fi

export T2_PROJECT_DIR T2_PROFILE T2_CONTEXT T2_PROFILE_ROOT T2_PROFILE_ENV T2_PORTS_ENV \
  T2_LOCK_ROOT T2_LOCK_TOKEN MINIKUBE_PROFILE CONTROL_API_REAL_PG_CONTEXT
export T2_SKIP_LOCK=true T2_MUTATION_LOCK_WRAPPED=true

cleanup_wrapper() {
  local status=$? cleanup_status=0
  trap - EXIT INT TERM
  t2_lock_release "${status}" || cleanup_status=$?
  if [[ "${status}" -eq 0 && "${cleanup_status}" -ne 0 ]]; then
    status="${cleanup_status}"
  fi
  exit "${status}"
}
trap cleanup_wrapper EXIT
trap 'exit 130' INT
trap 'exit 143' TERM

run_status=0
if [[ "$WRC_RECOVERY_ONLY" == true ]]; then
  bash "$ROOT/scripts/e2e/e2e-wrc-egress-degradation.sh" --recover || run_status=$?
else
  "$@" || run_status=$?
fi
exit "${run_status}"