version: '3.8'

x-allianceauth-base: &allianceauth-base
  image: ${AA_DOCKER_TAG?err}
  # build:
  #   context: .
  #   dockerfile: custom.dockerfile
  #   args:
  #     AA_DOCKER_TAG: ${AA_DOCKER_TAG?err}
  restart: always
  env_file:
    - ./.env
  volumes:
    - ./conf/local.py:/home/allianceauth/myauth/myauth/settings/local.py
    - ./conf/celery.py:/home/allianceauth/myauth/myauth/celery.py
    - ./conf/urls.py:/home/allianceauth/myauth/myauth/urls.py
    - ./conf/memory_check.sh:/memory_check.sh
    - ./templates:/home/allianceauth/myauth/myauth/templates/
    - static-volume:/var/www/myauth/static
  depends_on:
    - redis
    - auth_mysql
  working_dir: /home/allianceauth/myauth/
  stop_grace_period: 10m
  logging:
    driver: "json-file"
    options:
      max-size: "50Mb"
      max-file: "5"

x-allianceauth-health-check: &allianceauth-health-checks
  healthcheck:
    test: [
      "CMD",
      "/memory_check.sh",
      "500000000"
    ]
    interval: 60s
    timeout: 10s
    retries: 3
    start_period: 5m
  labels:
    - "autoheal=true"

services:
  auth_mysql:
    image: mariadb:11.8
    command: [mariadbd, --default-authentication-plugin=mysql_native_password]
    volumes:
      - ./mysql-data:/var/lib/mysql
      - ./setup.sql:/docker-entrypoint-initdb.d/setup.sql
      - ./conf/aa_mariadb.cnf:/etc/mysql/conf.d/aa_mariadb.cnf
    environment:
      - MYSQL_ROOT_PASSWORD=${AA_DB_ROOT_PASSWORD?err}
      - MARIADB_MYSQL_LOCALHOST_USER=1
    healthcheck:
      test: ["CMD", "healthcheck.sh",  "--su=mysql", "--connect", "--innodb_initialized"]
    restart: unless-stopped
    logging:
      driver: "json-file"
      options:
        max-size: "10Mb"
        max-file: "5"

  nginx:
    image: nginx:stable
    restart: always
    volumes:
      - ./conf/nginx.conf:/etc/nginx/nginx.conf
      - static-volume:/var/www/myauth/static
    depends_on:
      - allianceauth_gunicorn
    logging:
      driver: "json-file"
      options:
        max-size: "10Mb"
        max-file: "5"

  redis:
    image: redis:8
    command: redis-server
    restart: always
    volumes:
      - "redis-data:/data"
      - ./conf/redis_healthcheck.sh:/usr/local/bin/redis_healthcheck.sh
    healthcheck:
      test: ["CMD", "/usr/local/bin/redis_healthcheck.sh"]
    logging:
      driver: "json-file"
      options:
        max-size: "10Mb"
        max-file: "5"

  allianceauth_gunicorn:
    ports:
      - 8000:8000
    container_name: allianceauth_gunicorn
    <<: [*allianceauth-base]
    entrypoint: [
      "gunicorn",
      "myauth.wsgi",
      "--bind=0.0.0.0:8000",
      "--workers=3",
      "--timeout=120",
      "--max-requests=500",
      "--max-requests-jitter=50"
    ]

  allianceauth_beat:
    container_name: allianceauth_worker_beat
    <<: [*allianceauth-base]
    entrypoint: [
      "celery",
      "-A",
      "myauth",
      "beat"
    ]

  allianceauth_worker:
    <<: [*allianceauth-base, *allianceauth-health-checks]
    entrypoint: [
      "celery",
      "-A",
      "myauth",
      "worker",
      "--pool=threads",
      "--concurrency=5",
      "-n",
      "worker_%n"
    ]
    deploy:
      replicas: 2

  grafana:
    image: grafana/grafana-oss:latest
    restart: always
    depends_on:
      - auth_mysql
    volumes:
      - ./grafana-datasource.yml:/etc/grafana/provisioning/datasources/datasource.yaml
      - ./grafana-dashboards.yml:/etc/grafana/provisioning/dashboards/datasource.yaml
      - ./grafana/dashboards:/var/lib/grafana/dashboards:ro
      - grafana-data:/var/lib/grafana
    environment:
      GF_INSTALL_PLUGINS: grafana-piechart-panel,grafana-clock-panel,grafana-simple-json-datasource
      GF_SECURITY_ADMIN_USERNAME: ${GF_SECURITY_ADMIN_USERNAME}
      GF_SECURITY_ADMIN_PASSWORD: ${GF_SECURITY_ADMIN_PASSWORD}
      GF_AUTH_DATABASE_PASSWORD: ${GRAFANA_DB_PASSWORD}
    logging:
      driver: "json-file"
      options:
        max-size: "10Mb"
        max-file: "5"

  proxy:
    image: jc21/nginx-proxy-manager:latest
    restart: always
    ports:
      - ${PROXY_HTTP_PORT:-80}:80
      - ${PROXY_DASH_PORT:-81}:81
      - ${PROXY_HTTPS_PORT:-443}:443
    # Uncomment this section to use a dedicated database for Nginx Proxy Manager
    # environment:
    #   DB_MYSQL_HOST: "proxy-db"
    #   DB_MYSQL_PORT: 3306
    #   DB_MYSQL_USER: "npm"
    #   DB_MYSQL_PASSWORD: "${PROXY_MYSQL_PASS?err}"
    #   DB_MYSQL_NAME: "npm"
    volumes:
      - proxy-data:/data
      - proxy-le:/etc/letsencrypt
    healthcheck:
      test: ["CMD", "/bin/check-health"]
    logging:
      driver: "json-file"
      options:
        max-size: "10Mb"
        max-file: "5"

  # Uncomment this section to use a dedicated database for Nginx Proxy Manager
  # proxy-db:
  #   image: 'jc21/mariadb-aria:latest'
  #   restart: always
  #   environment:
  #     MYSQL_ROOT_PASSWORD: "${PROXY_MYSQL_PASS_ROOT?err}"
  #     MYSQL_DATABASE: 'npm'
  #     MYSQL_USER: 'npm'
  #     MYSQL_PASSWORD: "${PROXY_MYSQL_PASS?err}"
  #   ports:
  #     - 3306
  #   volumes:
  #     - proxy-db:/var/lib/mysql
  #   logging:
  #     driver: "json-file"
  #     options:
  #       max-size: "1Mb"
  #       max-file: "5"

volumes:
    redis-data:
    static-volume:
    grafana-data:
    proxy-data:
    proxy-le:
    proxy-db:
