version: "3"


services:

  postgres:
    image: local_postgres
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: $POSTGRES_PASSWORD
      POSTGRES_DB: sprint
    volumes:
      - /sprint-data/postgres-data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    deploy:
      mode: replicated
      restart_policy:
        condition: on-failure

  migrations:
    image: mathwave/sprint-repo:sprint
    command: ./manage.py migrate
    depends_on:
      - postgres
    environment:
      PORT: 80
      SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions"
      DB_HOST: $DB_HOST
      RABBIT_HOST: $RABBIT_HOST
      FS_HOST: $FS_HOST
      DEBUG: $DEBUG
      TELEGRAM_TOKEN: $TELEGRAM_TOKEN
    deploy:
      mode: replicated
      restart_policy:
        condition: none

  web:
    image: mathwave/sprint-repo:sprint
    environment:
      PORT: 80
      SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions"
      DB_HOST: $DB_HOST
      RABBIT_HOST: $RABBIT_HOST
      FS_HOST: $FS_HOST
      DEBUG: $DEBUG
      TELEGRAM_TOKEN: $TELEGRAM_TOKEN
    command: ./manage.py runserver 0.0.0.0:$PORT --noreload
    ports:
      - "${PORT}:${PORT}"
    depends_on:
      - migrations
      - rabbitmq
      - storage
    deploy:
      mode: replicated
      restart_policy:
        condition: on-failure

  storage:
    image: mathwave/sprint-repo:sprint
    command: ./manage.py storage
    environment:
      PORT: 80
      SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions"
      DB_HOST: $DB_HOST
      RABBIT_HOST: $RABBIT_HOST
      FS_HOST: $FS_HOST
      DEBUG: $DEBUG
      TELEGRAM_TOKEN: $TELEGRAM_TOKEN
    ports:
      - "5555:5555"
    volumes:
      - /sprint-data/data:/usr/src/app/data
    deploy:
      mode: replicated
      restart_policy:
        condition: on-failure

  bot:
    image: mathwave/sprint-repo:sprint
    environment:
      PORT: 80
      SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions"
      DB_HOST: $DB_HOST
      RABBIT_HOST: $RABBIT_HOST
      FS_HOST: $FS_HOST
      DEBUG: $DEBUG
      TELEGRAM_TOKEN: $TELEGRAM_TOKEN
    command: ./manage.py bot
    depends_on:
      - web
    deploy:
      mode: replicated
      restart_policy:
        condition: on-failure

  loop:
    image: mathwave/sprint-repo:sprint
    environment:
      PORT: 80
      SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions"
      DB_HOST: $DB_HOST
      RABBIT_HOST: $RABBIT_HOST
      FS_HOST: $FS_HOST
      DEBUG: $DEBUG
      TELEGRAM_TOKEN: $TELEGRAM_TOKEN
    command: ./manage.py loop
    depends_on:
      - web
    deploy:
      mode: replicated
      restart_policy:
        condition: on-failure

  rabbitmq:
    image: local_rabbitmq
    ports:
      - "15672:15672"
      - "5672:5672"
    environment:
      PORT: 80
      SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions"
      DB_HOST: $DB_HOST
      RABBIT_HOST: $RABBIT_HOST
      FS_HOST: $FS_HOST
      DEBUG: $DEBUG
      TELEGRAM_TOKEN: $TELEGRAM_TOKEN
    deploy:
      mode: replicated
      restart_policy:
        condition: on-failure

  worker:
    image: mathwave/sprint-repo:sprint
    privileged: true
    command: ./manage.py receive
    environment:
      PORT: 80
      SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions"
      DB_HOST: $DB_HOST
      RABBIT_HOST: $RABBIT_HOST
      FS_HOST: $FS_HOST
      DEBUG: $DEBUG
      TELEGRAM_TOKEN: $TELEGRAM_TOKEN
    depends_on:
      - web
      - rabbitmq
      - migrations
      - storage
    volumes:
      - /sprint-data/solutions:/usr/src/app/solutions
      - /var/run/docker.sock:/var/run/docker.sock
    deploy:
      mode: replicated
      replicas: 4
      restart_policy:
        condition: on-failure

  apply-languages:
    image: mathwave/sprint-repo:sprint
    command: ./manage.py apply_languages
    environment:
      PORT: 80
      SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions"
      DB_HOST: $DB_HOST
      RABBIT_HOST: $RABBIT_HOST
      FS_HOST: $FS_HOST
      DEBUG: $DEBUG
      TELEGRAM_TOKEN: $TELEGRAM_TOKEN
    depends_on:
      - migrations
      - web
      - postgres
    deploy:
      mode: replicated
      restart_policy:
        condition: none

  notify-admin:
    image: mathwave/sprint-repo:sprint
    command: ./manage.py success_deploy
    environment:
      PORT: 80
      SOLUTIONS_ROOT_EXTERNAL: "/sprint-data/data/solutions"
      DB_HOST: $DB_HOST
      RABBIT_HOST: $RABBIT_HOST
      FS_HOST: $FS_HOST
      DEBUG: $DEBUG
      TELEGRAM_TOKEN: $TELEGRAM_TOKEN
    depends_on:
      - migrations
      - web
      - worker
      - storage
      - rabbitmq
      - bot
      - postgres
      - apply-languages
      - loop
    deploy:
      mode: replicated
      restart_policy:
        condition: none
