#!/usr/bin/env bash set -euo pipefail ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" ENV_FILE="$ROOT_DIR/.env.test2" AUTH_CONFIG_TEST2="$ROOT_DIR/.generated/auth-config.test2.env" echo "Creating clean $ENV_FILE..." if [[ -f "$ROOT_DIR/.env.sample" ]]; then grep -vE "PORT|URL|CALLBACK|ALLOWED_RETURN" "$ROOT_DIR/.env.sample" > "$ENV_FILE" else touch "$ENV_FILE" fi cat >> "$ENV_FILE" <> "$ENV_FILE" || true fi # Ensure mandatory secrets exist for test2 grep -q "COOKIE_SECRET=" "$ENV_FILE" || echo "COOKIE_SECRET=test2_cookie_secret_12345678" >> "$ENV_FILE" grep -q "JWT_SECRET=" "$ENV_FILE" || echo "JWT_SECRET=test2_jwt_secret_12345678" >> "$ENV_FILE" echo "Generating auth config for test2..." ( if [[ -f "$ROOT_DIR/.env" ]]; then mv "$ROOT_DIR/.env" "$ROOT_DIR/.env.tmp"; fi cp "$ENV_FILE" "$ROOT_DIR/.env" bash "$ROOT_DIR/scripts/auth_config.sh" build cp "$ROOT_DIR/.generated/auth-config.env" "$AUTH_CONFIG_TEST2" rm "$ROOT_DIR/.env" if [[ -f "$ROOT_DIR/.env.tmp" ]]; then mv "$ROOT_DIR/.env.tmp" "$ROOT_DIR/.env"; fi ) echo "Starting test2 stack..." docker compose -p test2 down --remove-orphans || true # Export variables for docker-compose substitution export DB_PORT=25432 export CLICKHOUSE_PORT_HTTP=28123 export CLICKHOUSE_PORT_NATIVE=29000 export BACKEND_PORT=23000 export ADMINFRONT_PORT=25173 export DEVFRONT_PORT=25174 export USERFRONT_PORT=25000 export REDIS_PORT=26399 export OATHKEEPER_PROXY_PORT=24467 # Load generated auth config variables if [[ -f "$AUTH_CONFIG_TEST2" ]]; then export $(grep -v '^#' "$AUTH_CONFIG_TEST2" | xargs) fi # 1. Start DBs first docker compose -p test2 -f compose.infra.test2.yaml -f compose.ory.test2.yaml up -d postgres postgres_ory redis echo "Waiting for DBs to be healthy..." sleep 15 # 2. Force create databases echo "Creating databases if they don't exist..." docker exec test2-postgres-1 psql -U baron -d postgres -c "CREATE DATABASE baron_sso;" || echo "DB baron_sso might already exist" docker exec test2-postgres_ory-1 psql -U ory -d postgres -c "CREATE DATABASE ory_kratos;" || echo "DB ory_kratos might already exist" docker exec test2-postgres_ory-1 psql -U ory -d postgres -c "CREATE DATABASE ory_hydra;" || echo "DB ory_hydra might already exist" docker exec test2-postgres_ory-1 psql -U ory -d postgres -c "CREATE DATABASE ory_keto;" || echo "DB ory_keto might already exist" # 3. Start everything else docker compose -p test2 \ -f compose.infra.test2.yaml \ -f compose.ory.test2.yaml \ -f docker-compose.test2.yaml \ up -d --build echo "test2 stack is up!" echo "UserFront: http://localhost:25000" echo "AdminFront: http://localhost:25173" echo "DevFront: http://localhost:25174"