#!/usr/bin/env bash set -euo pipefail script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" repo_dir="$(cd -- "$script_dir/.." && pwd)" env_file="${AIGW_DEBUG_ENV_FILE:-$repo_dir/.env.debug}" log() { printf '[aigw-debug] %s\n' "$*" } fail() { printf '[aigw-debug] error: %s\n' "$*" >&2 exit 1 } command -v docker >/dev/null 2>&1 || fail "docker is not installed" docker info >/dev/null 2>&1 || fail "cannot access Docker; check that the daemon is running and your user belongs to the docker group" if [[ ! -f "$env_file" ]]; then command -v openssl >/dev/null 2>&1 || fail "openssl is required to generate local credentials" credential_key="$(openssl rand -base64 32 | tr -d '\n')" admin_token="aigw-admin-$(openssl rand -hex 24)" umask 077 { printf 'AIGW_CREDENTIAL_KEY=%s\n' "$credential_key" printf 'AIGW_ADMIN_TOKEN=%s\n' "$admin_token" } >"$env_file" chmod 600 "$env_file" log "created $env_file" fi for required_name in AIGW_CREDENTIAL_KEY AIGW_ADMIN_TOKEN; do grep -q "^${required_name}=." "$env_file" || fail "$required_name is missing from $env_file" done compose=(docker compose --project-directory "$repo_dir") if [[ -n "${AIGW_COMPOSE_PROJECT_NAME:-}" ]]; then compose+=(--project-name "$AIGW_COMPOSE_PROJECT_NAME") fi compose+=(--env-file "$env_file") cd "$repo_dir" if [[ "${AIGW_DEBUG_SKIP_BUILD:-0}" == "1" ]]; then log "starting all services without rebuilding the gateway image" up_args=(up -d --no-build --remove-orphans --wait --wait-timeout 120) else log "building and starting all services" up_args=(up -d --build --remove-orphans --wait --wait-timeout 120) fi if ! "${compose[@]}" "${up_args[@]}"; then "${compose[@]}" ps >&2 || true "${compose[@]}" logs --no-color --tail=120 aigw >&2 || true fail "services did not become healthy" fi log "all services are ready" printf '\nAdmin UI: http://127.0.0.1:8080/admin/\n' printf 'Health: http://127.0.0.1:8080/readyz\n' printf 'Secrets: %s (mode 0600)\n' "$env_file" printf '\nLogs: docker compose --project-directory %q --env-file %q logs -f\n' "$repo_dir" "$env_file" printf 'Stop: ./scripts/stop-debug.sh\n'