#!/usr/bin/env bash set -Eeuo pipefail env_file="${1:-compose/.env}" compose_file="${BACKEND_COMPOSE_FILE:-compose/docker-compose.backend.yml}" for required_command in curl docker jq sed; do if ! command -v "$required_command" >/dev/null 2>&1; then printf 'Required command is unavailable: %s\n' "$required_command" >&2 exit 1 fi done if [[ ! -r "$env_file" ]]; then printf 'Environment file not found: %s\n' "$env_file" >&2 exit 1 fi env_value() { sed -n "s/^${1}=//p" "$env_file" | head -n 1 } backend_bind_address="$(env_value BACKEND_BIND_ADDRESS)" backend_host_port="$(env_value BACKEND_HOST_PORT)" oss_public_endpoint="$(env_value OSS_PUBLIC_ENDPOINT)" project_name="$(env_value BACKEND_COMPOSE_PROJECT_NAME)" if [[ -z "$project_name" || "$project_name" == "$(env_value COMPOSE_PROJECT_NAME)" ]]; then printf 'BACKEND_COMPOSE_PROJECT_NAME must be set and differ from the infrastructure project.\n' >&2 exit 1 fi compose_command=( docker compose --project-name "$project_name" --env-file "$env_file" -f "$compose_file" ) "${compose_command[@]}" config --quiet health_url="http://${backend_bind_address}:${backend_host_port}/api/health" health_json="" for _attempt in $(seq 1 18); do if health_json="$(curl --fail --silent --show-error --max-time 5 "$health_url" 2>/dev/null)"; then if printf '%s' "$health_json" | jq -e '.status == "healthy" and .database == "connected"' >/dev/null; then break fi fi health_json="" sleep 5 done if [[ -z "$health_json" ]]; then printf 'Backend health check did not become healthy: %s\n' "$health_url" >&2 "${compose_command[@]}" ps >&2 exit 1 fi "${compose_command[@]}" exec -T backend python -c \ 'import os, redis; assert redis.from_url(os.environ["REDIS_URL"]).ping()' "${compose_command[@]}" exec -T backend python -c \ 'import os; from urllib.parse import urlsplit; from minio import Minio; endpoint=urlsplit(os.environ["OSS_ENDPOINT"]); client=Minio(endpoint.netloc, access_key=os.environ["OSS_ACCESS_KEY_ID"], secret_key=os.environ["OSS_ACCESS_KEY_SECRET"], secure=endpoint.scheme == "https"); assert client.bucket_exists(os.environ["OSS_BUCKET_NAME"])' curl --fail --silent --show-error --max-time 5 \ "${oss_public_endpoint}/minio/health/live" >/dev/null printf '%s\n' "$health_json" | jq . "${compose_command[@]}" ps printf 'Backend, PostgreSQL, Redis, and MinIO connectivity checks passed.\n'