version: '3'

services:
  server-base:
    platform: linux/amd64
    restart: always
    build:
      context: .
      dockerfile: $PWD/docker/images/stork.Dockerfile
      target: server
    networks:
      - storknet
    expose:
      - "8080"
    ports:
      - "42080:8080"
    hostname: server
    environment:
      STORK_DATABASE_HOST: postgres
      STORK_DATABASE_PORT: 5432
      STORK_DATABASE_USER_NAME: stork
      STORK_DATABASE_PASSWORD: stork
      STORK_DATABASE_NAME: stork
      STORK_SERVER_ENABLE_METRICS: 1
      STORK_SERVER_INITIAL_PULLER_INTERVAL: 1
      STORK_DATABASE_READ_TIMEOUT: 2s
      STORK_DATABASE_WRITE_TIMEOUT: 2s
    volumes:
      - $PWD/docker/config/supervisor/supervisord.conf:/etc/supervisor/supervisord.conf
      - $PWD/tests/system/config/supervisor/stork-server.conf:/etc/supervisor/conf.d/stork-server.conf
      - $PWD/tests/system/config/supervisor/supervisor_monitor.conf:/etc/supervisor/conf.d/supervisor_monitor.conf
      - $PWD/docker/tools/supervisor_monitor.py:/usr/lib/supervisor_monitor.py
    healthcheck:
      interval: 250ms
      timeout: 1s
      retries: 40

  server:
    extends: server-base
    depends_on:
      - postgres

  server-db-ssl-require:
    extends: server-base
    environment:
      STORK_DATABASE_SSLMODE: require
    depends_on:
      - postgres

  server-db-ssl-verify-ca:
    extends: server-base
    environment:
      STORK_DATABASE_SSLMODE: verify-ca
    depends_on:
      - postgres

  server-db-auth-trust:
    extends: server-base
    environment:
      STORK_DATABASE_USER_NAME: stork_trust
      STORK_DATABASE_PASSWORD:
    depends_on:
      - postgres

  server-db-auth-md5:
    extends: server-base
    environment:
      STORK_DATABASE_USER_NAME: stork_md5
      STORK_DATABASE_PASSWORD: stork_md5
    depends_on:
      - postgres

  server-db-auth-scram-sha-256:
    extends: server-base
    environment:
      STORK_DATABASE_USER_NAME: stork_scram-sha-256
      STORK_DATABASE_PASSWORD: stork_scram-sha-256
    depends_on:
      - postgres

  agent-kea-base:
    platform: linux/amd64
    restart: always
    hostname: agent-kea
    build:
      context: .
      dockerfile: $PWD/docker/images/stork.Dockerfile
      target: kea
      args:
        KEA_REPO: public/${KEA_REPO:-isc/kea-dev}
        KEA_VERSION: ${KEA_VERSION:-2.7.0-isc20240624090938}
        KEA_LEGACY_PKGS: ${KEA_LEGACY_PKGS:-false}

  agent-kea:
    extends: agent-kea-base
    ports:
      - "42888:8080"
      - "42547:9547"
    networks:
      storknet:
        ipv4_address: 172.42.42.100
        priority: 1000
      subnet_00:
        ipv4_address: 172.100.42.100
        priority: 500
      subnet_01:
        ipv4_address: 172.101.42.100
        priority: 400
      subnet_02:
        ipv4_address: 172.102.42.100
        priority: 300
      subnet6_00:
        ipv6_address: 3000:db8:1:42::100
        priority: 500
      subnet6_01:
        ipv6_address: 3001:db8:1:42::100
        priority: 400
    environment:
      DB_TYPE: none
      STORK_AGENT_SERVER_URL: ${STORK_SERVER_URL-http://server:8080}
      STORK_AGENT_LISTEN_PROMETHEUS_ONLY: ${STORK_AGENT_LISTEN_PROMETHEUS_ONLY-false}
      STORK_AGENT_HOST: 172.42.42.100
    healthcheck:
      interval: 250ms
      timeout: 1s
      retries: 40
    volumes:
      - $PWD/tests/system/config/kea/kea-ctrl-agent.conf:/etc/kea/kea-ctrl-agent.conf
      - $PWD/tests/system/config/kea/kea-ctrl-agent-tls.json:/etc/kea/kea-ctrl-agent-tls.json
      - $PWD/tests/system/config/kea/kea-ctrl-agent-auth.json:/etc/kea/kea-ctrl-agent-auth.json
      - $PWD/tests/system/config/kea/kea-dhcp4.conf:/etc/kea/kea-dhcp4.conf
      - $PWD/tests/system/config/kea/kea-dhcp6.conf:/etc/kea/kea-dhcp6.conf
      - $PWD/tests/system/config/kea-multi-threading/multi-threading-disabled.json:/etc/kea/multi-threading.json
      - $PWD/tests/system/config/kea-multi-threading/ha-multi-threading-disabled.json:/etc/kea/ha-multi-threading.json
      - $PWD/tests/system/config/kea/disabled.json:/etc/kea/hook-hostcmds.json
      - $PWD/tests/system/config/kea/disabled.json:/etc/kea/hook-subnetcmds.json
      - $PWD/tests/system/config/kea/disabled.json:/etc/kea/hook-ha-dhcp4.json
      - $PWD/tests/system/config/kea/disabled.json:/etc/kea/hook-ha-dhcp6.json
      - $PWD/tests/system/config/kea/disabled.json:/etc/kea/kea-host-database.json
      - $PWD/tests/system/config/kea/kea-leases4.csv:/var/lib/kea/kea-leases4.csv
      - $PWD/tests/system/config/kea/kea-leases6.csv:/var/lib/kea/kea-leases6.csv
      - $PWD/docker/config/supervisor/supervisord.conf:/etc/supervisor/supervisord.conf
      - $PWD/docker/config/supervisor/kea-agent.conf:/etc/supervisor/conf.d/kea-agent.conf
      - $PWD/docker/config/supervisor/kea-dhcp4.conf:/etc/supervisor/conf.d/kea-dhcp4.conf
      - $PWD/docker/config/supervisor/kea-dhcp6.conf:/etc/supervisor/conf.d/kea-dhcp6.conf
      - $PWD/docker/config/supervisor/stork-agent.conf:/etc/supervisor/conf.d/stork-agent.conf
      - $PWD/tests/system/config/supervisor/supervisor_monitor.conf:/etc/supervisor/conf.d/supervisor_monitor.conf
      - $PWD/docker/tools/supervisor_monitor.py:/usr/lib/supervisor_monitor.py

  agent-kea-many-subnets:
    extends: agent-kea
    volumes:
      - $PWD/tests/system/config/kea-many-subnets/kea-dhcp4.conf:/etc/kea/kea-dhcp4.conf

  agent-kea-many-subnets-and-shared-networks-1:
    extends: agent-kea
    volumes:
      - $PWD/tests/system/config/kea-many-subnets/kea-dhcp4-sn4400-s13000.conf:/etc/kea/kea-dhcp4.conf

  agent-kea-many-subnets-and-shared-networks-2:
    extends: agent-kea-many-subnets-and-shared-networks-1
    hostname: agent-kea2
    ports: !override
      - 8080
      - "42548:9547"
    environment:
      STORK_AGENT_HOST: 172.42.42.101
    networks:
      storknet:
        ipv4_address: 172.42.42.101
        priority: 1000
      subnet_00:
        ipv4_address: 172.100.42.101
        priority: 500
      subnet_01:
        ipv4_address: 172.101.42.101
        priority: 400
      subnet_02:
        ipv4_address: 172.102.42.101
        priority: 300
      subnet6_00:
        ipv6_address: 3000:db8:1:42::101
        priority: 500
      subnet6_01:
        ipv6_address: 3001:db8:1:42::101
        priority: 400

  agent-kea6:
    extends: agent-kea
    networks:
      storknet:
        ipv6_address: 3009:db8:1:42::100
    environment:
      STORK_AGENT_HOST: 3009:db8:1:42::100

  agent-kea-tls-optional-client-cert-no-verify:
    extends: agent-kea
    volumes:
      - $PWD/tests/system/config/kea-tls/optional-client-cert.json:/etc/kea/kea-ctrl-agent-tls.json
      - $PWD/tests/system/config/certs/cert.pem:/root/certs/cert.pem
      - $PWD/tests/system/config/certs/key.pem:/root/certs/key.pem
      - $PWD/tests/system/config/certs/CA:/root/certs/CA
    environment:
      STORK_AGENT_SKIP_TLS_CERT_VERIFICATION: "true"

  agent-kea-tls-required-client-cert-no-verify:
    extends: agent-kea-tls-optional-client-cert-no-verify
    volumes:
      - $PWD/tests/system/config/kea-tls/required-client-cert.json:/etc/kea/kea-ctrl-agent-tls.json

  agent-kea-tls-optional-client-cert-verify:
    extends: agent-kea-tls-optional-client-cert-no-verify
    environment:
      STORK_AGENT_SKIP_TLS_CERT_VERIFICATION: "false"

  agent-kea-config-review:
    extends: agent-kea
    volumes:
      - $PWD/tests/system/config/kea-config-review/kea-dhcp4.conf:/etc/kea/kea-dhcp4.conf
      - $PWD/tests/system/config/kea-config-review/kea-dhcp6.conf:/etc/kea/kea-dhcp6.conf

  agent-kea-basic-auth-no-credentials:
    extends: agent-kea
    environment:
      STORK_AGENT_SKIP_TLS_CERT_VERIFICATION: "true"
    volumes:
      - $PWD/tests/system/config/kea-basic-auth/kea-ctrl-agent-auth.json:/etc/kea/kea-ctrl-agent-auth.json

  agent-kea-basic-auth:
    extends: agent-kea-basic-auth-no-credentials
    volumes:
      - $PWD/tests/system/config/kea-basic-auth/agent-credentials.json:/etc/stork/agent-credentials.json

  agent-kea-premium:
    extends: agent-kea
    build:
      args:
        KEA_REPO: ${CS_REPO_ACCESS_TOKEN}/${KEA_REPO:-isc/kea-dev}-prv
        KEA_PREMIUM: "premium"
    profiles:
      - premium

  agent-kea-premium-host-database-base:
    extends: agent-kea-premium
    environment:
      DB_TYPE: pgsql
      DB_NAME: kea_hostcmds
      DB_USER: kea_hostcmds
      DB_PASSWORD: kea_hostcmds
      DB_HOST: postgres
      DB_ROOT_USER: stork
      DB_ROOT_PASSWORD: stork
    volumes:
      - $PWD/tests/system/config/kea-premium-host-database/kea-host-database.json:/etc/kea/kea-host-database.json
      - $PWD/tests/system/config/kea-premium-host-database/kea-hook-hostcmds.json:/etc/kea/hook-hostcmds.json
      - $PWD/tests/system/config/kea-premium-host-database/init_pgsql_query.sql:/var/lib/db/init_pgsql_query.sql

  agent-kea-premium-host-database:
    extends: agent-kea-premium-host-database-base
    depends_on:
      - postgres

  agent-kea-premium-subnet-commands:
    extends: agent-kea-premium
    volumes:
      - $PWD/tests/system/config/kea-premium-subnet-commands/kea-hook-subnetcmds.json:/etc/kea/hook-subnetcmds.json

  agent-kea-premium-subnet-commands-dhcp4-offline:
    extends: agent-kea-premium-subnet-commands
    volumes:
      # The Kea DHCPv4 daemon is not running but it is still specified in the
      # control sockets section of Kea CA.
      - /etc/supervisor/conf.d/kea-dhcp4.conf

  agent-kea-ha1:
    extends: agent-kea
    hostname: agent-kea-ha1
    volumes:
      - $PWD/tests/system/config/kea-ha1/hook-ha.json:/etc/kea/hook-ha-dhcp4.json
      - $PWD/tests/system/config/kea-ha1/hook-ha.json:/etc/kea/hook-ha-dhcp6.json

  agent-kea-ha1-only-top-mt:
    extends: agent-kea-ha1
    volumes:
      - $PWD/tests/system/config/kea-multi-threading/multi-threading.json:/etc/kea/multi-threading.json

  agent-kea-ha1-mt:
    extends: agent-kea-ha1-only-top-mt
    volumes:
      - $PWD/tests/system/config/kea-multi-threading/ha-multi-threading.json:/etc/kea/ha-multi-threading.json
      - $PWD/tests/system/config/kea-multi-threading/ha1/hook-ha-dhcp4.json:/etc/kea/hook-ha-dhcp4.json
      - $PWD/tests/system/config/kea-multi-threading/ha1/hook-ha-dhcp6.json:/etc/kea/hook-ha-dhcp6.json

  agent-kea-ha2:
    extends: agent-kea
    hostname: agent-kea-ha2
    ports: !override
      - 8080
      - "42548:9547"
    environment:
      STORK_AGENT_HOST: 172.42.42.101
    networks:
      storknet:
        ipv4_address: 172.42.42.101
        priority: 1000
      subnet_00:
        ipv4_address: 172.100.42.101
        priority: 500
      subnet_01:
        ipv4_address: 172.101.42.101
        priority: 400
      subnet_02:
        ipv4_address: 172.102.42.101
        priority: 300
      subnet6_00:
        ipv6_address: 3000:db8:1:42::101
        priority: 500
      subnet6_01:
        ipv6_address: 3001:db8:1:42::101
        priority: 400
    volumes:
      - $PWD/tests/system/config/kea-ha2/hook-ha.json:/etc/kea/hook-ha-dhcp4.json
      - $PWD/tests/system/config/kea-ha2/hook-ha.json:/etc/kea/hook-ha-dhcp6.json

  agent-kea-ha2-only-top-mt:
    extends: agent-kea-ha2
    volumes:
      - $PWD/tests/system/config/kea-multi-threading/multi-threading.json:/etc/kea/multi-threading.json

  agent-kea-ha2-mt:
    extends: agent-kea-ha2-only-top-mt
    volumes:
      - $PWD/tests/system/config/kea-multi-threading/ha-multi-threading.json:/etc/kea/ha-multi-threading.json
      - $PWD/tests/system/config/kea-multi-threading/ha2/hook-ha-dhcp4.json:/etc/kea/hook-ha-dhcp4.json
      - $PWD/tests/system/config/kea-multi-threading/ha2/hook-ha-dhcp6.json:/etc/kea/hook-ha-dhcp6.json

  agent-bind9:
    platform: linux/amd64
    restart: always
    build:
      context: .
      dockerfile: $PWD/docker/images/stork.Dockerfile
      target: bind
      args:
        BIND9_VERSION: ${BIND9_VERSION:-9.18}
    hostname: agent-bind9
    networks:
      storknet:
        ipv4_address: 172.42.42.53
    environment:
      STORK_AGENT_SERVER_URL: ${STORK_SERVER_URL-http://server:8080}
      STORK_AGENT_LISTEN_PROMETHEUS_ONLY: ${STORK_AGENT_LISTEN_PROMETHEUS_ONLY-false}
      STORK_AGENT_HOST: 172.42.42.53
      STORK_LOG_LEVEL: DEBUG
    ports:
      - "42119:9119"
    volumes:
      - $PWD/tests/system/config/bind/db.test:/etc/bind/db.test
      - $PWD/tests/system/config/bind/named.conf:/etc/bind/named.conf
      - $PWD/docker/config/supervisor/supervisord.conf:/etc/supervisor/supervisord.conf
      - $PWD/docker/config/supervisor/named.conf:/etc/supervisor/conf.d/named.conf
      - $PWD/docker/config/supervisor/stork-agent.conf:/etc/supervisor/conf.d/stork-agent.conf
      - $PWD/tests/system/config/supervisor/supervisor_monitor.conf:/etc/supervisor/conf.d/supervisor_monitor.conf
      - $PWD/docker/tools/supervisor_monitor.py:/usr/lib/supervisor_monitor.py

  agent-bind9-rndc:
    extends: agent-bind9
    volumes:
      - $PWD/tests/system/config/bind-rndc/named.conf:/etc/bind/named.conf

  agent-bind9-package:
    extends: agent-bind9
    volumes:
      - $PWD/tests/system/config/bind-package/named.conf:/etc/bind/named.conf

  agent-bind9-rndc-custom:
    extends: agent-bind9
    volumes:
      - $PWD/tests/system/config/bind-rndc-custom/named.conf:/etc/bind/named.conf
      - $PWD/tests/system/config/bind-rndc-custom/rndc.key:/etc/bind/rndc.key
      - $PWD/tests/system/config/bind-rndc-custom/rndc.conf:/etc/bind/rndc.conf

  agent-bind9-chroot:
    extends: agent-bind9
    build:
      target: bind-chroot
    volumes:
      - $PWD/tests/system/config/supervisor/named.conf:/etc/supervisor/conf.d/named.conf
      # Wipe out the inherited configuration files.
      - /etc/bind/db.test
      - /etc/bind/named.conf
      # Mount the configuration files in the chroot environment.
      - $PWD/tests/system/config/bind/db.test:/chroot/etc/bind/db.test
      - $PWD/tests/system/config/bind/named.conf:/chroot/etc/bind/named.conf

  agent-bind9-chroot-rndc-custom:
    extends: agent-bind9-chroot
    volumes:
      - $PWD/tests/system/config/bind-rndc-custom/named.conf:/chroot/etc/bind/named.conf
      - $PWD/tests/system/config/bind-rndc-custom/rndc.key:/chroot/etc/bind/rndc.key
      - $PWD/tests/system/config/bind-rndc-custom/rndc.conf:/chroot/etc/bind/rndc.conf

  perfdhcp:
    extends: agent-kea-base
    entrypoint: /usr/sbin/perfdhcp
    hostname: perfdhcp
    networks:
      storknet:
        ipv4_address: 172.42.42.200
        priority: 1000
      subnet_00:
        ipv4_address: 172.100.42.200
        priority: 500
      subnet_01:
        ipv4_address: 172.101.42.200
        priority: 400
      subnet_02:
        ipv4_address: 172.102.42.200
        priority: 300
      subnet6_00:
        ipv6_address: 3000:db8:1:42::200
        priority: 500
      subnet6_01:
        ipv6_address: 3001:db8:1:42::200
        priority: 400
    healthcheck:
      disable: true

  register:
    platform: linux/amd64
    hostname: agent-register
    build:
      context: .
      dockerfile: $PWD/docker/images/stork.Dockerfile
      target: agent
    ports:
      - "42188:8080"
      - "42147:9547"
    networks:
      storknet:
        ipv4_address: 172.42.42.100
    environment:
      STORK_AGENT_SERVER_URL: ${STORK_SERVER_URL-http://server:8080}
      STORK_AGENT_HOST: 172.42.42.100
    healthcheck:
      disable: true

  packages:
    build:
      context: .
      dockerfile: $PWD/docker/images/stork.Dockerfile
      target: external-packages
      args:
        STORK_CS_VERSION: ${STORK_CLOUDSMITH_VERSION-}*
    ports:
      - "42080:8080"
    networks:
      - storknet
    hostname: server
    environment:
      STORK_DATABASE_HOST: postgres
      STORK_DATABASE_PORT: 5432
      STORK_DATABASE_USER_NAME: stork
      STORK_DATABASE_PASSWORD: stork
      STORK_DATABASE_NAME: stork
      STORK_SERVER_ENABLE_METRICS: 1
      STORK_AGENT_SERVER_URL: http://127.0.0.1:8080
      STORK_AGENT_HOST: 127.0.0.1
      STORK_AGENT_PORT: 8888
    healthcheck:
      interval: 2s
      timeout: 10s
      retries: 5
      start_period: 0s
    volumes:
      - $PWD/docker/config/supervisor/supervisord.conf:/etc/supervisor/supervisord.conf
      - $PWD/docker/config/supervisor/stork-agent.conf:/etc/supervisor/conf.d/stork-agent.conf
      - $PWD/docker/config/supervisor/stork-server.conf:/etc/supervisor/conf.d/stork-server.conf
    depends_on:
      - postgres

  postgres:
    platform: linux/amd64
    image: postgres:${POSTGRES_VERSION:-16}
    command: -c ssl=on -c ssl_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem -c ssl_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
    networks:
      storknet:
        ipv4_address: 172.42.42.234
    environment:
      POSTGRES_USER: stork
      POSTGRES_PASSWORD: stork
      POSTGRES_DB: stork
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U stork" ]
      interval: 250ms
      timeout: 1s
      retries: 40
    volumes:
      - $PWD/tests/system/config/postgres/init_pg_hba.sql:/docker-entrypoint-initdb.d/init_pg_hba.sql
      - $PWD/tests/system/config/postgres/create_pgcrypto_extension.sql:/docker-entrypoint-initdb.d/create_pgcrypto_extension.sql
    ports:
      - "42432:5432"

networks:
  storknet:
    enable_ipv6: true
    ipam:
      config:
        - subnet: 172.42.42.0/24
        - subnet: 3009:db8:1:42::/64
  subnet_00:
    ipam:
      config:
        - subnet: 172.100.42.0/24
  subnet_01:
    ipam:
      config:
        - subnet: 172.101.42.0/24
  subnet_02:
    ipam:
      config:
        - subnet: 172.102.42.0/24
  subnet6_00:
    enable_ipv6: true
    ipam:
      config:
        - subnet: 3000:db8:1:42::/64
  subnet6_01:
    enable_ipv6: true
    ipam:
      config:
        - subnet: 3001:db8:1:42::/64
