name: ovirt-api-simulator

x-simulator-env: &simulator-env
  DATABASE_URL: postgresql://ovirt:ovirt@postgres:5432/ovirt_simulator
  LOG_LEVEL: INFO
  TICKET_SIGNING_KEY: development-only-signing-key-change-me
  APP_PORT: "8080"
  OVIRT_SERIES: "4.5"

x-dev-env: &dev-env
  DATABASE_URL: postgresql://ovirt:ovirt@postgres:5432/ovirt_simulator
  TEST_DATABASE_URL: postgresql://ovirt:ovirt@postgres:5432/ovirt_simulator
  LOG_LEVEL: INFO
  TICKET_SIGNING_KEY: development-only-signing-key-change-me
  APP_PORT: "8080"
  OVIRT_SERIES: "4.5"

networks:
  simulator:
    driver: bridge

volumes:
  postgres-data:

services:
  postgres:
    image: postgres:17.5-bookworm
    restart: unless-stopped
    networks: [simulator]
    environment:
      POSTGRES_DB: ovirt_simulator
      POSTGRES_USER: ovirt
      POSTGRES_PASSWORD: ovirt
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ovirt -d ovirt_simulator"]
      interval: 5s
      timeout: 3s
      retries: 10
      start_period: 5s
    volumes:
      - postgres-data:/var/lib/postgresql/data

  migrate:
    build:
      context: .
      target: runtime
    image: ovirt-api-simulator:0.1.0
    networks: [simulator]
    working_dir: /workspace
    volumes:
      - .:/workspace
    env_file:
      - path: .env
        required: false
    environment:
      <<: *simulator-env
      PYTHONPATH: /workspace
    depends_on:
      postgres:
        condition: service_healthy
    entrypoint: ["python"]
    command: ["-m", "app.db.migrate_cli"]
    restart: "no"

  simulator:
    build:
      context: .
      target: dev
    image: ovirt-api-simulator-dev:0.1.0
    restart: unless-stopped
    networks: [simulator]
    working_dir: /workspace
    volumes:
      - .:/workspace
    env_file:
      - path: .env
        required: false
    environment:
      <<: *dev-env
    depends_on:
      migrate:
        condition: service_completed_successfully
    entrypoint: []
    command:
      [
        "uvicorn",
        "app.main:app",
        "--host",
        "0.0.0.0",
        "--port",
        "8080",
        "--reload",
        "--reload-dir",
        "/workspace/app",
        "--reload-include",
        "*.html",
      ]
    healthcheck:
      test:
        [
          "CMD",
          "python",
          "-c",
          "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready', timeout=2)",
        ]
      interval: 10s
      timeout: 3s
      retries: 8
      start_period: 25s
    expose:
      - "8080"

  api-gateway:
    image: nginx:1.28.0-alpine
    restart: unless-stopped
    networks: [simulator]
    depends_on:
      simulator:
        condition: service_healthy
    ports:
      # Only two host ports (Engine HTTPS + Web UI). Override via env if needed.
      - "${OVIRT_ENGINE_PORT:-443}:443"   # Engine HTTPS (/ovirt-engine/api, SSO)
      - "${OVIRT_UI_PORT:-5000}:5000"      # Web UI console
    volumes:
      - ./docker/gateway/ovirt-engine.conf:/etc/nginx/conf.d/default.conf:ro
      - ./docker/tls/server.crt:/etc/nginx/tls/server.crt:ro
      - ./docker/tls/server.key:/etc/nginx/tls/server.key:ro
    healthcheck:
      test:
        [
          "CMD-SHELL",
          "wget -qO- http://127.0.0.1:5000/health/live || exit 1",
        ]
      interval: 10s
      timeout: 3s
      retries: 5
      start_period: 5s
    read_only: true
    tmpfs:
      - /var/cache/nginx
      - /var/run
      - /tmp
    security_opt:
      - no-new-privileges:true

  dev:
    profiles: [tools]
    build:
      context: .
      target: dev
    image: ovirt-api-simulator-dev:0.1.0
    networks: [simulator]
    working_dir: /workspace
    volumes:
      - .:/workspace
    env_file:
      - path: .env
        required: false
    environment:
      <<: *dev-env
    depends_on:
      postgres:
        condition: service_healthy
    entrypoint: []
