#!/usr/bin/env bash set -Eeuo pipefail repo_root=$(git rev-parse --show-toplevel) project="heimdall-agent-startup-$(date -u +%Y%m%d%H%M%S)-$(openssl rand -hex 3)" image_tag="heimdall-agent:${project}-agent" temp_dir=$(mktemp -d "${TMPDIR:-/tmp}/${project}.XXXXXX") env_file="$temp_dir/acceptance.env" compose_file="$temp_dir/compose.yaml" local_image_tags=("$image_tag") cleanup() { status=$? trap - EXIT cleanup_failed=0 docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \ down --volumes --remove-orphans >/dev/null 2>&1 || cleanup_failed=1 containers= volumes= networks= containers=$(docker ps -aq --filter "label=com.docker.compose.project=$project") || cleanup_failed=1 volumes=$(docker volume ls -q --filter "label=com.docker.compose.project=$project") || cleanup_failed=1 networks=$(docker network ls -q --filter "label=com.docker.compose.project=$project") || cleanup_failed=1 [[ -z "${containers:-}" && -z "${volumes:-}" && -z "${networks:-}" ]] || cleanup_failed=1 for tag in "${local_image_tags[@]}"; do docker image rm "$tag" >/dev/null 2>&1 || true if docker image inspect "$tag" >/dev/null 2>&1; then cleanup_failed=1 fi done rm -f -- "$env_file" "$compose_file" || cleanup_failed=1 rmdir -- "$temp_dir" || cleanup_failed=1 if (( cleanup_failed )); then printf 'Disposable Agent startup cleanup failed for project %s\n' "$project" >&2 status=1 else printf 'Cleanup verified: project=%s; containers/volumes/networks absent; image tag %s absent.\n' \ "$project" "$image_tag" fi exit "$status" } trap cleanup EXIT umask 077 cat >"$env_file" <"$compose_file" <<'EOF' services: agent: image: ${IMAGE_TAG} build: context: ${SOURCE_ROOT} dockerfile: docker/Dockerfile.agent network: host restart: "no" environment: Agent__CentralUrl: http://127.0.0.1:1 Agent__AllowInsecureHttp: "true" Agent__SiteToken: ${SYNTHETIC_SITE_TOKEN} Agent__AgentName: ${PROJECT} Agent__RegistrationMaxRetries: "20" Agent__HeartbeatIntervalSeconds: "5" Agent__DiscoveryEnabled: "false" Agent__UpdateCheckIntervalHours: "0" Agent__StateDirectory: /var/lib/heimdall Sentry__Dsn: "" volumes: - agent_state:/var/lib/heimdall networks: - isolated volumes: agent_state: networks: isolated: internal: true EOF docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \ up --build --detach agent container_id=$(docker compose -p "$project" --env-file "$env_file" -f "$compose_file" ps -q agent) [[ -n "$container_id" ]] || { printf 'Agent container was not created.\n' >&2; exit 1; } deadline=$((SECONDS + 90)) live_ready=0 while (( SECONDS < deadline )); do running=$(docker inspect --format '{{.State.Running}}' "$container_id" 2>/dev/null || true) if [[ "$running" != true ]]; then printf 'Agent container exited before its live endpoint became ready.\n' >&2 docker compose -p "$project" --env-file "$env_file" -f "$compose_file" ps >&2 || true exit 1 fi if docker compose -p "$project" --env-file "$env_file" -f "$compose_file" \ exec -T agent wget -q --tries=1 --spider http://127.0.0.1:8080/health/live >/dev/null 2>&1; then live_ready=1 break fi sleep 1 done (( live_ready == 1 )) || { printf 'Agent /health/live did not become ready.\n' >&2; exit 1; } restart_count=$(docker inspect --format '{{.RestartCount}}' "$container_id") [[ "$restart_count" == 0 ]] || { printf 'Agent container restarted unexpectedly.\n' >&2; exit 1; } if docker compose -p "$project" --env-file "$env_file" -f "$compose_file" logs agent 2>&1 | rg -q "Unable to resolve service for type 'Heimdall\.Agent\.Services\.SnapshotPipelineServiceOptions'|Hosting failed to start"; then printf 'Agent startup log contains a dependency-injection or host-start failure.\n' >&2 exit 1 fi printf 'Agent host started; /health/live returned success; container remained running without restart.\n' printf 'Project: %s\nLocal image tag: %s\n' "$project" "$image_tag"