###################################
# 🦎 KOMODO COMPOSE - POSTGRES 🦎 #
###################################

## This compose file will deploy:
##   1. Postgres + FerretDB Mongo adapter (https://www.ferretdb.com)
##   2. Komodo Core
##   3. Komodo Periphery
name: komodo
secrets:
  komodo_db_user:
    file: ${SECRETSDIR}/komodo/komodo_db_user
  komodo_db_password:
    file: ${SECRETSDIR}/komodo/komodo_db_password
  komodo_passkey:
    file: ${SECRETSDIR}/komodo/komodo_passkey
  komodo_webhook_secret:
    file: ${SECRETSDIR}/komodo/komodo_webhook_secret
  komodo_jwt_secret:
    file: ${SECRETSDIR}/komodo/komodo_jwt_secret
  komodo_oidc_client_id:
    file: ${SECRETSDIR}/komodo/komodo_oidc_client_id
  komodo_oidc_client_secret:
    file: ${SECRETSDIR}/komodo/komodo_oidc_client_secret

services:
  #postgres:
  #  image: postgres
  #  labels:
  #    komodo.skip: # Prevent Komodo from stopping with StopAllContainers
  #  restart: unless-stopped
  #  logging:
  #    driver: ${COMPOSE_LOGGING_DRIVER:-local}
  #  networks:
  #    - default
    # ports:
    #   - 5432:5432
  #  volumes:
  #    - pg-data:/var/lib/postgresql/data
  #  environment:
  #    - POSTGRES_USER=${KOMODO_DB_USERNAME}
  #    - POSTGRES_PASSWORD=${KOMODO_DB_PASSWORD}
  #    - POSTGRES_DB=${KOMODO_DATABASE_DB_NAME:-komodo}

  ferretdb:
    #container_name: ${TRAEFIK_CONTAINER_NAME}
    #image: ${TRAEFIK_IMAGE}:${TRAEFIK_TAG}
    #restart: ${TRAEFIK_RESTART_POLICY}
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    image: ghcr.io/ferretdb/ferretdb
    labels:
      komodo.skip: # Prevent Komodo from stopping with StopAllContainers
    #depends_on:
    #  - postgres
    logging:
      driver: ${COMPOSE_LOGGING_DRIVER:-local}
    networks:
      - default
    # ports:
    #   - 27017:27017
    env_file: ./compose.env
    environment:
      #- FERRETDB_POSTGRESQL_URL=postgres://komodo:komodo@10.0.6.178/komodo?sslmode=disable
      - FERRETDB_POSTGRESQL_URL=postgres://${PSQL_HOST}:${PSQL_PORT}/${KOMODO_DATABASE_DB_NAME:-komodo}?sslmode=disable
  
  core:
    #container_name: ${TRAEFIK_CONTAINER_NAME}
    #image: ${TRAEFIK_IMAGE}:${TRAEFIK_TAG}
    #restart: ${TRAEFIK_RESTART_POLICY}
    restart: unless-stopped
    image: ghcr.io/mbecker20/komodo:${COMPOSE_KOMODO_IMAGE_TAG:-latest}
    secrets:
      - komodo_db_user
      - komodo_db_password
      - komodo_passkey
      - komodo_webhook_secret
      - komodo_jwt_secret
      - komodo_oidc_client_id
      - komodo_oidc_client_secret
    labels:
      komodo.skip: # Prevent Komodo from stopping with StopAllContainers
      traefik.enable: true
      ## HTTP Routers
      traefik.http.routers.komodo-rtr.entrypoints: https
      traefik.http.routers.komodo-rtr.rule: Host(`komodo.lab.gurulandia.eu`)
      ## Middlewares
#      - "traefik.http.routers.${PRIVATEBIN_HOSTNAME}-rtr.middlewares=chain-authelia@file"
      traefik.http.routers.komodo-rtr.middlewares: chain-no-auth@file
      ## HTTP Services
      traefik.http.routers.komodo-rtr.service: komodo-svc
      traefik.http.services.komodo-svc.loadbalancer.server.port: 9120
    depends_on:
      - ferretdb
    logging:
      driver: ${COMPOSE_LOGGING_DRIVER:-local}
    networks:
      - default
      - proxy
    ports:
      - 9120:9120
    env_file: ./compose.env  
    environment:
      KOMODO_DATABASE_URI: mongodb://${KOMODO_DB_USERNAME}:${KOMODO_DB_PASSWORD}@ferretdb:27017/${KOMODO_DATABASE_DB_NAME:-komodo}?authMechanism=PLAIN      
    volumes:
      ## Core cache for repos for latest commit hash / contents
      - repo-cache:/repo-cache
      ## Store sync files on server
      # - /path/to/syncs:/syncs
      ## Optionally mount a custom core.config.toml
      # - /path/to/core.config.toml:/config/config.toml
    ## Allows for systemd Periphery connection at 
    ## "http://host.docker.internal:8120"
    # extra_hosts:
    #   - host.docker.internal:host-gateway

  ## Deploy Periphery container using this block,
  ## or deploy the Periphery binary with systemd using 
  ## https://github.com/mbecker20/komodo/tree/main/scripts
  periphery:
    #container_name: ${TRAEFIK_CONTAINER_NAME}
    #image: ${TRAEFIK_IMAGE}:${TRAEFIK_TAG}
    #restart: ${TRAEFIK_RESTART_POLICY}
    restart: unless-stopped
    image: ghcr.io/mbecker20/periphery:${COMPOSE_KOMODO_IMAGE_TAG:-latest}
    labels:
      komodo.skip: # Prevent Komodo from stopping with StopAllContainers
    logging:
      driver: ${COMPOSE_LOGGING_DRIVER:-local}
    networks:
      - default
    env_file: ./compose.env
    environment:
      PERIPHERY_REPO_DIR: ${PERIPHERY_ROOT_DIRECTORY:-/etc/komodo}/repos
      PERIPHERY_STACK_DIR: ${PERIPHERY_ROOT_DIRECTORY:-/etc/komodo}/stacks
      PERIPHERY_SSL_KEY_FILE: ${PERIPHERY_ROOT_DIRECTORY:-/etc/komodo}/ssl/key.pem
      PERIPHERY_SSL_CERT_FILE: ${PERIPHERY_ROOT_DIRECTORY:-/etc/komodo}/ssl/cert.pem
    volumes:
      ## Mount external docker socket
      - /var/run/docker.sock:/var/run/docker.sock
      ## Allow Periphery to see processes outside of container
      - /proc:/proc
      ## Specify the Periphery agent root directory.
      ## Must be the same inside and outside the container,
      ## or docker will get confused. See https://github.com/mbecker20/komodo/discussions/180.
      ## Default: /etc/komodo.
      - ${PERIPHERY_ROOT_DIRECTORY:-/etc/komodo}:${PERIPHERY_ROOT_DIRECTORY:-/etc/komodo}
    secrets:
      - komodo_passkey

volumes:
  # Postgres
  #pg-data:
  # Core
  repo-cache:

networks:
  default: {}
  proxy: 
    external: true