# Copyright (c) Abstract Machines
# SPDX-License-Identifier: Apache-2.0

# This docker-compose file contains optional bootstrap services. Since it's optional, this file is
# dependent of docker-compose file from <project_root>/docker. In order to run this services, execute command:
# docker compose -f docker/docker-compose.yaml -f docker/addons/bootstrap/docker-compose.yaml up
# from project root.

networks:
  magistrala-base-net:
    external: true

volumes:
  magistrala-bootstrap-db-volume:

services:
  bootstrap-db:
    image: postgres:16.2-alpine
    container_name: magistrala-bootstrap-db
    restart: on-failure
    environment:
      POSTGRES_USER: ${MG_BOOTSTRAP_DB_USER}
      POSTGRES_PASSWORD: ${MG_BOOTSTRAP_DB_PASS}
      POSTGRES_DB: ${MG_BOOTSTRAP_DB_NAME}
    networks:
      - magistrala-base-net
    volumes:
      - magistrala-bootstrap-db-volume:/var/lib/postgresql/data

  bootstrap:
    image: ghcr.io/absmach/magistrala/bootstrap:${MG_RELEASE_TAG}
    container_name: magistrala-bootstrap
    depends_on:
      - bootstrap-db
    restart: on-failure
    ports:
      - ${MG_BOOTSTRAP_HTTP_PORT}:${MG_BOOTSTRAP_HTTP_PORT}
    environment:
      MG_BOOTSTRAP_LOG_LEVEL: ${MG_BOOTSTRAP_LOG_LEVEL}
      MG_BOOTSTRAP_ENCRYPT_KEY: ${MG_BOOTSTRAP_ENCRYPT_KEY}
      MG_BOOTSTRAP_EVENT_CONSUMER: ${MG_BOOTSTRAP_EVENT_CONSUMER}
      MG_ES_URL: ${MG_ES_URL}
      MG_BOOTSTRAP_HTTP_HOST: ${MG_BOOTSTRAP_HTTP_HOST}
      MG_BOOTSTRAP_HTTP_PORT: ${MG_BOOTSTRAP_HTTP_PORT}
      MG_BOOTSTRAP_HTTP_SERVER_CERT: ${MG_BOOTSTRAP_HTTP_SERVER_CERT}
      MG_BOOTSTRAP_HTTP_SERVER_KEY: ${MG_BOOTSTRAP_HTTP_SERVER_KEY}
      MG_BOOTSTRAP_DB_HOST: ${MG_BOOTSTRAP_DB_HOST}
      MG_BOOTSTRAP_DB_PORT: ${MG_BOOTSTRAP_DB_PORT}
      MG_BOOTSTRAP_DB_USER: ${MG_BOOTSTRAP_DB_USER}
      MG_BOOTSTRAP_DB_PASS: ${MG_BOOTSTRAP_DB_PASS}
      MG_BOOTSTRAP_DB_NAME: ${MG_BOOTSTRAP_DB_NAME}
      MG_BOOTSTRAP_DB_SSL_MODE: ${MG_BOOTSTRAP_DB_SSL_MODE}
      MG_BOOTSTRAP_DB_SSL_CERT: ${MG_BOOTSTRAP_DB_SSL_CERT}
      MG_BOOTSTRAP_DB_SSL_KEY: ${MG_BOOTSTRAP_DB_SSL_KEY}
      MG_BOOTSTRAP_DB_SSL_ROOT_CERT: ${MG_BOOTSTRAP_DB_SSL_ROOT_CERT}
      MG_BOOTSTRAP_INSTANCE_ID: ${MG_BOOTSTRAP_INSTANCE_ID}
      MG_AUTH_GRPC_URL: ${MG_AUTH_GRPC_URL}
      MG_AUTH_GRPC_TIMEOUT: ${MG_AUTH_GRPC_TIMEOUT}
      MG_AUTH_GRPC_CLIENT_CERT: ${MG_AUTH_GRPC_CLIENT_CERT:+/auth-grpc-client.crt}
      MG_AUTH_GRPC_CLIENT_KEY: ${MG_AUTH_GRPC_CLIENT_KEY:+/auth-grpc-client.key}
      MG_AUTH_GRPC_SERVER_CA_CERTS: ${MG_AUTH_GRPC_SERVER_CA_CERTS:+/auth-grpc-server-ca.crt}
      MG_DOMAINS_GRPC_URL: ${MG_DOMAINS_GRPC_URL}
      MG_DOMAINS_GRPC_TIMEOUT: ${MG_DOMAINS_GRPC_TIMEOUT}
      MG_DOMAINS_GRPC_CLIENT_CERT: ${MG_DOMAINS_GRPC_CLIENT_CERT:+/domains-grpc-client.crt}
      MG_DOMAINS_GRPC_CLIENT_KEY: ${MG_DOMAINS_GRPC_CLIENT_KEY:+/domains-grpc-client.key}
      MG_DOMAINS_GRPC_SERVER_CA_CERTS: ${MG_DOMAINS_GRPC_SERVER_CA_CERTS:+/domains-grpc-server-ca.crt}
      MG_CLIENTS_URL: ${MG_CLIENTS_URL}
      MG_CHANNELS_URL: ${MG_CHANNELS_URL}
      MG_JAEGER_URL: ${MG_JAEGER_URL}
      MG_JAEGER_TRACE_RATIO: ${MG_JAEGER_TRACE_RATIO}
      MG_SEND_TELEMETRY: ${MG_SEND_TELEMETRY}
      MG_SPICEDB_PRE_SHARED_KEY: ${MG_SPICEDB_PRE_SHARED_KEY}
      MG_SPICEDB_HOST: ${MG_SPICEDB_HOST}
      MG_SPICEDB_PORT: ${MG_SPICEDB_PORT}
      MG_ALLOW_UNVERIFIED_USER: ${MG_ALLOW_UNVERIFIED_USER}
    networks:
      - magistrala-base-net
    volumes:
      - type: bind
        source: ${MG_ADDONS_CERTS_PATH_PREFIX}${MG_AUTH_GRPC_CLIENT_CERT:-./ssl/placeholder}
        target: /auth-grpc-client${MG_AUTH_GRPC_CLIENT_CERT:+.crt}
        bind:
          create_host_path: true
      - type: bind
        source: ${MG_ADDONS_CERTS_PATH_PREFIX}${MG_AUTH_GRPC_CLIENT_KEY:-./ssl/placeholder}
        target: /auth-grpc-client${MG_AUTH_GRPC_CLIENT_KEY:+.key}
        bind:
          create_host_path: true
      - type: bind
        source: ${MG_ADDONS_CERTS_PATH_PREFIX}${MG_AUTH_GRPC_SERVER_CA_CERTS:-./ssl/placeholder}
        target: /auth-grpc-server-ca${MG_AUTH_GRPC_SERVER_CA_CERTS:+.crt}
        bind:
          create_host_path: true
