version: '3.5'

x-postgres-variables: &postgres-variables
  POSTGRES_DB: "${POSTGRES_DB}"
  POSTGRES_HOST: "${POSTGRES_HOST}"
  POSTGRES_PORT: "${POSTGRES_PORT}"
  POSTGRES_USER: "${POSTGRES_USER}"
  POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
  POSTGRES_HOST_AUTH_METHOD: "${POSTGRES_HOST_AUTH_METHOD}"

x-django-variables: &django-variables
  PYTHONUNBUFFERED: 1
  DJANGO_PORT: "${DJANGO_PORT}"
  DEBUG: "${DEBUG}"

x-frontend-variables: &frontend-variables
  DOMAIN: "${DOMAIN}"
  REACT_APP_DOMAIN_URL: "https://${DOMAIN}/"

x-martin-variables: &martin-variables
  MARTIN_PORT: "${MARTIN_PORT}"

services:
  django:
    container_name: ${CONTAINERS_NAME}_django
    image: ${IMAGE_NAME}:${CI_COMMIT_SHORT_SHA}
    command: >
      sh -c "python manage.py migrate &&
             python manage.py collectstatic --noinput &&
             python manage.py loaddata fixtures/groups.json
             python manage.py runserver 0.0.0.0:${DJANGO_PORT}"
    environment:
      <<: *postgres-variables
      <<: *django-variables
      <<: *frontend-variables
    volumes:
      - ${VOLUMES_PATH}/django_static/:/code/django_static/
      - ${VOLUMES_PATH}/media/:/code/media/
    ports:
      - "${DJANGO_PORT}:${DJANGO_PORT}"
    expose:
      - "${DJANGO_PORT}"
    restart: always
    depends_on:
      - db
      - loyalty-rabbit

  loyalty-rabbit:
    image: rabbitmq:3.9-management
    container_name: ${CONTAINERS_NAME}_loyalty-rabbit
    volumes:
      - rabbitmq_data:/var/lib/rabbitmq
      - rabbitmq_log:/var/log/rabbitmq
    logging:
        driver: none
    restart: always
    ports:
      - 5672:5672

  beat:
    restart: always
    environment:
      <<: *postgres-variables
      <<: *django-variables
    container_name: ${CONTAINERS_NAME}_beat
    image: ${IMAGE_NAME}:${CI_COMMIT_SHORT_SHA}
    entrypoint: [ "celery", "-A", "postamates", "beat","-l", "info", "--scheduler", "django_celery_beat.schedulers:DatabaseScheduler" ]
    depends_on:
      - db
      - loyalty-rabbit
      - django
      - worker

  worker:
    restart: always
    container_name: ${CONTAINERS_NAME}_worker
    environment:
        <<: *postgres-variables
        <<: *django-variables
    image: ${IMAGE_NAME}:${CI_COMMIT_SHORT_SHA}
    entrypoint: [ "celery", "-A", "postamates.celery:app", "worker" ]
    depends_on:
      - db
      - loyalty-rabbit
      - django

  db:
    container_name: ${CONTAINERS_NAME}_db
    image: mdillon/postgis
    environment:
      <<: *postgres-variables
    ports:
      - "${POSTGRES_PORT}:${POSTGRES_PORT}"
    expose:
      - "${POSTGRES_PORT}"
    volumes:
      - ${VOLUMES_PATH}/pg_data:/var/lib/postgresql/data
    command: -p ${POSTGRES_PORT}

  martin:
    container_name: ${CONTAINERS_NAME}_martin
    image: urbica/martin
    ports:
      - "${MARTIN_PORT}:3000"
    environment:
      <<: *martin-variables
      WATCH_MODE: "true"
      DATABASE_URL: "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:${POSTGRES_PORT}/${POSTGRES_DB}"
    depends_on:
      - db
      - django
    restart: always

volumes:
  rabbitmq_data:
  rabbitmq_log:
