#!/usr/bin/env bash # Idempotent local dev environment setup. # Run once after cloning; safe to re-run at any time. set -euo pipefail SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" mkdir -p "$PROJECT_ROOT/logs" LOG_FILE="$PROJECT_ROOT/logs/setup-$(date +%Y%m%d-%H%M%S).log" exec > >(tee "$LOG_FILE") 2>&1 GREEN='\033[0;32m'; YELLOW='\033[1;33m'; RED='\033[0;31m'; BOLD='\033[1m'; NC='\033[0m' info() { echo -e "${GREEN}[apix]${NC} $*"; } warn() { echo -e "${YELLOW}[warn]${NC} $*"; } die() { echo -e "${RED}[fail]${NC} $*" >&2; exit 1; } step() { echo -e "\n${BOLD}── $* ──${NC}"; } _on_exit() { echo "" echo -e "${BOLD}Full log:${NC} $LOG_FILE" read -rp "Press Enter to close…" _ } trap _on_exit EXIT info "Logging to $LOG_FILE" # ── 1. Java 21 ────────────────────────────────────────────────────────────── step "Java 21" if java -version 2>&1 | grep -q 'version "21'; then info "Java 21 detected" else warn "Java 21 not found." echo " Install via SDKMAN (recommended):" echo " curl -s https://get.sdkman.io | bash" echo " sdk install java 21-tem" echo "" echo " Or download from: https://adoptium.net/" die "Please install Java 21 and re-run this script." fi # ── 2. Maven ───────────────────────────────────────────────────────────────── step "Maven" if command -v mvn &>/dev/null; then MVN_VER="$(mvn -q --version 2>&1 | head -1 || true)" info "Maven ${MVN_VER}" else die "Maven not found. Install: https://maven.apache.org/install.html" fi # ── 3. Docker ──────────────────────────────────────────────────────────────── step "Docker" if docker info &>/dev/null; then info "Docker running" else die "Docker not running. Start Docker Desktop (or the Docker daemon) and re-run." fi # ── 4. .env file ───────────────────────────────────────────────────────────── step ".env" cd "$PROJECT_ROOT" if [[ ! -f .env.example ]]; then die ".env.example not found in $PROJECT_ROOT — repository may be incomplete." fi if [[ ! -f .env ]]; then cp .env.example .env info "Created .env from .env.example" warn "Review .env and set real values before running in any shared environment." else info ".env already exists — skipping copy" fi # Load .env into this shell so DB vars are available below set -a # shellcheck disable=SC1091 source .env set +a DB_USER="${APIX_DB_USER:-apix}" DB_PASS="${APIX_DB_PASSWORD:-apix}" DB_NAME="${APIX_DB_NAME:-apix}" DB_PORT="${APIX_DB_PORT:-5432}" # ── 5. PostgreSQL container ─────────────────────────────────────────────────── step "PostgreSQL" CONTAINER=apix-postgres if docker ps --format '{{.Names}}' | grep -qx "$CONTAINER"; then info "Container '$CONTAINER' already running" elif docker ps -a --format '{{.Names}}' | grep -qx "$CONTAINER"; then info "Starting existing container '$CONTAINER'" docker start "$CONTAINER" >/dev/null else info "Creating container '$CONTAINER' (postgres:16-alpine, port $DB_PORT)" docker run -d \ --name "$CONTAINER" \ --restart unless-stopped \ -e POSTGRES_USER="$DB_USER" \ -e POSTGRES_PASSWORD="$DB_PASS" \ -e POSTGRES_DB="$DB_NAME" \ -p "${DB_PORT}:5432" \ postgres:16-alpine >/dev/null fi # Wait for Postgres to accept connections info "Waiting for PostgreSQL to become ready…" for i in $(seq 1 30); do if docker exec "$CONTAINER" pg_isready -U "$DB_USER" -q 2>/dev/null; then info "PostgreSQL ready" break fi if [[ $i -eq 30 ]]; then die "PostgreSQL did not become ready within 30 s. Check: docker logs $CONTAINER" fi sleep 1 done # ── 6. Liquibase migrations ─────────────────────────────────────────────────── step "Database migrations" JDBC_URL="jdbc:postgresql://localhost:${DB_PORT}/${DB_NAME}" if [[ ! -f "$PROJECT_ROOT/pom.xml" ]]; then warn "No pom.xml found — Maven project not scaffolded yet (WORKLOG Block 1 / C-00)." warn "Skipping Liquibase migrations. Run this script again after completing Block 1." else # apix-registry depends on apix-common and apix-verification; install them # to the local repository first so Maven can resolve them during the # liquibase:update goal (no source files yet — this completes in seconds). info "Installing shared modules to local repository…" mvn -q install -pl apix-common,apix-verification -DskipTests info "Running Liquibase migrations on $JDBC_URL" mvn -q liquibase:update -pl apix-registry \ -Dliquibase.url="$JDBC_URL" \ -Dliquibase.username="$DB_USER" \ -Dliquibase.password="$DB_PASS" info "Migrations applied" fi # ── Done ────────────────────────────────────────────────────────────────────── echo "" echo -e "${GREEN}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" echo -e "${GREEN}${BOLD} APIX dev environment ready${NC}" echo -e "${GREEN}${BOLD}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}" echo "" echo " Start all services: ./scripts/dev.sh" echo " View logs: ./scripts/logs.sh [registry|portal|spider]" echo " Stop everything: ./scripts/stop.sh" echo " Full reset (drop DB): ./scripts/reset.sh" echo "" echo " Registry API → http://localhost:8180" echo " Portal → http://localhost:8081" echo ""