name: proxmox-api-simulator

x-simulator-env: &simulator-env
  DATABASE_URL: postgresql://proxmox:proxmox@postgres:5432/proxmox_simulator
  CONTRACT_SNAPSHOT: /app/contracts/pve-9.2.3.json
  COMPATIBILITY_EVIDENCE: /app/evidence/pve-9.2.3.json
  LOG_LEVEL: INFO
  TICKET_SIGNING_KEY: development-only-signing-key-change-me

x-dev-env: &dev-env
  DATABASE_URL: postgresql://proxmox:proxmox@postgres:5432/proxmox_simulator
  TEST_DATABASE_URL: postgresql://proxmox:proxmox@postgres:5432/proxmox_simulator
  CONTRACT_SNAPSHOT: /workspace/contracts/e61a893e996d05d376579226e7dfbedbcfce8b71787adacffbc557e6e35901c1/snapshot.json
  COMPATIBILITY_EVIDENCE: /workspace/evidence/pve-9.2.3.json
  # proxmoxer is HTTPS-only — set when using --profile tls (see Makefile test-compatibility)
  PROXMOXER_HOST: ${PROXMOXER_HOST:-}
  PROXMOXER_PORT: ${PROXMOXER_PORT:-8443}
  LOG_LEVEL: INFO
  TICKET_SIGNING_KEY: development-only-signing-key-change-me

networks:
  simulator:
    driver: bridge

volumes:
  postgres-data:

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

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

  simulator:
    build:
      context: .
      target: dev
    image: proxmox-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",
        "8006",
        "--reload",
        "--reload-dir",
        "/workspace/app",
        "--reload-include",
        "*.html",
      ]
    healthcheck:
      test:
        [
          "CMD",
          "python",
          "-c",
          "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8006/health/ready', timeout=2)",
        ]
      interval: 10s
      timeout: 3s
      retries: 8
      start_period: 20s
    ports:
      - "${SIMULATOR_PORT:-8006}:8006"

  # Optional HTTPS front for clients that cannot speak plain HTTP (proxmoxer).
  # Default lab URL is http://localhost:8006/ — K8s TLS is Ingress-only.
  tls-gateway:
    profiles: [tls]
    image: nginx:1.28.0-alpine
    restart: unless-stopped
    networks: [simulator]
    depends_on:
      simulator:
        condition: service_healthy
    ports:
      - "${TLS_GATEWAY_PORT:-8443}:8443"
    volumes:
      - ./docker/tls/gateway.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- --no-check-certificate https://127.0.0.1:8443/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: proxmox-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: []
