Skip to content

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

TypeScript
5.8 KB
Raw Blame
#!/usr/bin/env bash
# Resolve the branch-owned Control UI URL for a Minikube profile and sync
# CONTROL_API_CONTROL_UI_BASE_URL on control-api so Codex subscription browser
# OAuth builds the correct cluster-local redirect_uri.
set -euo pipefail

HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "${HERE}/../.." && pwd)"

# shellcheck source=scripts/e2e/load-dotenv.sh
source "${ROOT}/scripts/e2e/load-dotenv.sh"

PROFILE="${MINIKUBE_PROFILE:-${KUBECONTEXT:-}}"
PORTS_ENV="${CLERUM_PROFILE_PORTS_ENV:-${E2E_PROFILE_PORTS_ENV:-}}"
DRY_RUN=false
EVIDENCE_DIR="${ROOT}/.local-notes/infra/runs"
CONFIGMAP="control-api-config"
NAMESPACE="control-plane"
DEPLOYMENT="control-api"
TARGET_KEY="CONTROL_API_CONTROL_UI_BASE_URL"
CALLBACK_SUFFIX="/control-api/api/v1/auth/codex-subscription/callback"

log() {
  printf '[codex-subscription-url] %s\n' "$*"
}

die() {
  log "ERROR: $*" >&2
  exit 1
}

usage() {
  cat <<'EOF'
Usage: scripts/minikube/sync-codex-subscription-control-ui-url.sh [options]

Options:
  --context <name>     Kubernetes context / Minikube profile (default: MINIKUBE_PROFILE)
  --ports-env <path>   Branch profile ports.env (default: CLERUM_PROFILE_PORTS_ENV or profile cache)
  --dry-run            Print the resolved URL and redirect_uri without patching the cluster
  -h, --help           Show this help

Resolution order for the Control UI origin:
  1. CONTROL_UI_URL or CONTROL_UI_BASE_URL already exported in the environment
  2. CONTROL_UI_URL / CONTROL_UI_BASE_URL from the profile ports.env
  3. http://127.0.0.1:${CONTROL_UI_PORT} when the port variable is present

The script patches control-api-config and restarts control-api when the value drifts.
EOF
}

while [[ $# -gt 0 ]]; do
  case "$1" in
    --context)
      PROFILE="${2:-}"
      shift 2
      ;;
    --context=*)
      PROFILE="${1#--context=}"
      shift
      ;;
    --ports-env)
      PORTS_ENV="${2:-}"
      shift 2
      ;;
    --ports-env=*)
      PORTS_ENV="${1#--ports-env=}"
      shift
      ;;
    --dry-run)
      DRY_RUN=true
      shift
      ;;
    -h|--help)
      usage
      exit 0
      ;;
    *)
      die "unknown argument: $1"
      ;;
  esac
done

[[ -n "${PROFILE}" ]] || die "MINIKUBE_PROFILE or --context is required"

if [[ -z "${PORTS_ENV}" ]]; then
  PORTS_ENV="${HOME}/.cache/clerum/minikube-profiles/${PROFILE}/ports.env"
fi

normalize_url() {
  local value="${1%/}"
  printf '%s' "${value}"
}

read_ports_env_value() {
  local key="$1"
  awk -F= -v expected_key="${key}" '$1 == expected_key { sub(/^[^=]*=/, ""); print; exit }' "${PORTS_ENV}"
}

resolve_control_ui_url() {
  local from_env="${CONTROL_UI_URL:-${CONTROL_UI_BASE_URL:-}}"
  if [[ -n "${from_env}" ]]; then
    normalize_url "${from_env}"
    return 0
  fi

  if [[ -f "${PORTS_ENV}" ]]; then
    dotenv_load_file "${PORTS_ENV}"
    from_env="${CONTROL_UI_URL:-${CONTROL_UI_BASE_URL:-}}"
    if [[ -n "${from_env}" ]]; then
      normalize_url "${from_env}"
      return 0
    fi
    if [[ -n "${CONTROL_UI_PORT:-}" ]]; then
      printf 'http://127.0.0.1:%s' "${CONTROL_UI_PORT}"
      return 0
    fi
  fi

  if [[ -n "${CONTROL_UI_PORT:-}" ]]; then
    printf 'http://127.0.0.1:%s' "${CONTROL_UI_PORT}"
    return 0
  fi

  return 1
}

validate_control_ui_url() {
  local url="$1"
  case "${url}" in
    http://127.0.0.1:[0-9]*|http://localhost:[0-9]*|https://*)
      ;;
    *)
      die "refusing non-loopback/non-https Control UI URL for Codex OAuth sync: ${url}"
      ;;
  esac
}

make_config_patch() {
  CONTROL_UI_ORIGIN="$1" TARGET_KEY="$2" python3 - <<'PY'
import json
import os

print(json.dumps({"data": {os.environ["TARGET_KEY"]: os.environ["CONTROL_UI_ORIGIN"]}}))
PY
}

write_local_env_file() {
  local control_ui_url="$1"
  local redirect_uri="$2"
  local safe_profile="${PROFILE//[^A-Za-z0-9_.-]/_}"
  local env_file="${EVIDENCE_DIR}/codex-subscription-${safe_profile}.env"

  mkdir -p "${EVIDENCE_DIR}"
  cat >"${env_file}" <<EOF
# Generated by scripts/minikube/sync-codex-subscription-control-ui-url.sh
# Profile: ${PROFILE}
CONTROL_UI_URL=${control_ui_url}
CONTROL_UI_BASE_URL=${control_ui_url}
CONTROL_API_CONTROL_UI_BASE_URL=${control_ui_url}
CODEX_BROWSER_REDIRECT_URI=${redirect_uri}
EOF
  log "Wrote local env evidence: ${env_file}"
}

if ! CONTROL_UI_URL="$(resolve_control_ui_url)"; then
  die "could not resolve Control UI URL; export CONTROL_UI_URL or provide ${PORTS_ENV}"
fi

validate_control_ui_url "${CONTROL_UI_URL}"
CODEX_BROWSER_REDIRECT_URI="${CONTROL_UI_URL}${CALLBACK_SUFFIX}"

log "Profile: ${PROFILE}"
log "Control UI origin: ${CONTROL_UI_URL}"
log "Codex browser redirect_uri: ${CODEX_BROWSER_REDIRECT_URI}"

write_local_env_file "${CONTROL_UI_URL}" "${CODEX_BROWSER_REDIRECT_URI}"

if [[ "${DRY_RUN}" == "true" ]]; then
  log "Dry run complete; cluster unchanged"
  exit 0
fi

KCTL=(kubectl --context="${PROFILE}")

if ! "${KCTL[@]}" get configmap "${CONFIGMAP}" -n "${NAMESPACE}" >/dev/null 2>&1; then
  die "missing ${NAMESPACE}/${CONFIGMAP}; deploy the minikube overlay first"
fi

current_value="$("${KCTL[@]}" get configmap "${CONFIGMAP}" -n "${NAMESPACE}" -o "jsonpath={.data.${TARGET_KEY}}" 2>/dev/null || true)"
if [[ "${current_value}" == "${CONTROL_UI_URL}" ]]; then
  log "${CONFIGMAP}.${TARGET_KEY} already matches ${CONTROL_UI_URL}"
  exit 0
fi

log "Patching ${NAMESPACE}/${CONFIGMAP}.${TARGET_KEY}"
"${KCTL[@]}" patch configmap "${CONFIGMAP}" -n "${NAMESPACE}" --type=merge \
  -p "$(make_config_patch "${CONTROL_UI_URL}" "${TARGET_KEY}")" >/dev/null

if "${KCTL[@]}" get "deployment/${DEPLOYMENT}" -n "${NAMESPACE}" >/dev/null 2>&1; then
  log "Restarting ${NAMESPACE}/${DEPLOYMENT} to pick up Codex OAuth callback origin"
  "${KCTL[@]}" rollout restart "deployment/${DEPLOYMENT}" -n "${NAMESPACE}" >/dev/null
  "${KCTL[@]}" rollout status "deployment/${DEPLOYMENT}" -n "${NAMESPACE}" --timeout=180s
fi

log "Codex subscription Control UI origin synced"