version: '3'

services:
  postgresql:
    image: bitnami/postgresql:${POSTGRES_VERSION}
    container_name: postgres
    volumes:
      - pgdata:/var/lib/postgresql/data
    networks:
      - openreplay-net
    environment:
      POSTGRESQL_PASSWORD: ${COMMON_PG_PASSWORD}

  redis:
    image: bitnami/redis:${REDIS_VERSION}
    container_name: redis
    volumes:
      - redisdata:/var/lib/postgresql/data
    networks:
      - openreplay-net
    environment:
      ALLOW_EMPTY_PASSWORD: "yes"

  minio:
    image: bitnami/minio:${MINIO_VERSION}
    container_name: minio
    volumes:
      - miniodata:/bitnami/minio/data
    networks:
      - openreplay-net
    ports:
      - 9001:9001
    environment:
      MINIO_ROOT_USER: ${COMMON_S3_KEY}
      MINIO_ROOT_PASSWORD: ${COMMON_S3_SECRET}

  fs-permission:
    image: debian:stable-slim
    container_name: fs-permission
    profiles:
      - "migration"
    volumes:
      - shared-volume:/mnt/efs
      - miniodata:/mnt/minio
      - pgdata:/mnt/postgres
    entrypoint:
      - /bin/bash
      - -c
      - |
        chown -R 1001:1001 /mnt/{efs,minio,postgres}
    restart: on-failure

  minio-migration:
    image: bitnami/minio:2020.10.9-debian-10-r6
    container_name: minio-migration
    profiles:
      - "migration"
    depends_on:
      - minio
      - fs-permission
    networks:
      - openreplay-net
    volumes:
      - ../helmcharts/openreplay/files/minio.sh:/tmp/minio.sh
    environment:
      MINIO_HOST: http://minio:9000
      MINIO_ACCESS_KEY: ${COMMON_S3_KEY}
      MINIO_SECRET_KEY: ${COMMON_S3_SECRET}
    user: root
    entrypoint:
      - /bin/bash
      - -c
      - |
          apt update && apt install netcat -y
          # Wait for Minio to be ready
          until nc -z -v -w30 minio 9000; do
              echo "Waiting for Minio server to be ready..."
              sleep 1
          done
          bash /tmp/minio.sh init || exit 100

  db-migration:
    image: bitnami/postgresql:14.5.0 
    container_name: db-migration
    profiles:
      - "migration"
    depends_on:
      - postgresql
      - minio-migration
    networks:
      - openreplay-net
    volumes:
      - ../schema/db/init_dbs/postgresql/init_schema.sql:/tmp/init_schema.sql
    environment:
      PGHOST: postgresql
      PGPORT: 5432
      PGDATABASE: postgres
      PGUSER: postgres
      PGPASSWORD: ${COMMON_PG_PASSWORD}
    entrypoint:
      - /bin/bash
      - -c
      - |
          until PGPASSWORD=${COMMON_PG_PASSWORD} psql -h postgresql -U postgres -d postgres -c '\q'; do
          echo "PostgreSQL is unavailable - sleeping"
          sleep 1
          done
          echo "PostgreSQL is up - executing command"
          psql -v ON_ERROR_STOP=1 -f /tmp/init_schema.sql

  frontend-openreplay:
    image: public.ecr.aws/p1t3u8a3/frontend:${COMMON_VERSION}
    container_name: frontend
    networks:
      - openreplay-net
    restart: unless-stopped

  alerts-openreplay:
    image: public.ecr.aws/p1t3u8a3/alerts:${COMMON_VERSION}
    container_name: alerts
    networks:
      - openreplay-net
    env_file:
      - alerts.env
    restart: unless-stopped

  assets-openreplay:
    image: public.ecr.aws/p1t3u8a3/assets:${COMMON_VERSION}
    container_name: assets
    networks:
      - openreplay-net
    env_file:
      - assets.env
    restart: unless-stopped

  assist-openreplay:
    image: public.ecr.aws/p1t3u8a3/assist:${COMMON_VERSION}
    container_name: assist
    networks:
      - openreplay-net
    env_file:
      - assist.env
    restart: unless-stopped

  db-openreplay:
    image: public.ecr.aws/p1t3u8a3/db:${COMMON_VERSION}
    container_name: db
    networks:
      - openreplay-net
    env_file:
      - db.env
    restart: unless-stopped

  ender-openreplay:
    image: public.ecr.aws/p1t3u8a3/ender:${COMMON_VERSION}
    container_name: ender
    networks:
      - openreplay-net
    env_file:
      - ender.env
    restart: unless-stopped

  heuristics-openreplay:
    image: public.ecr.aws/p1t3u8a3/heuristics:${COMMON_VERSION}
    domainname: app.svc.cluster.local
    container_name: heuristics
    networks:
      openreplay-net:
        aliases:
          - heuristics-openreplay.app.svc.cluster.local
    env_file:
      - heuristics.env
    restart: unless-stopped

  imagestorage-openreplay:
    image: public.ecr.aws/p1t3u8a3/imagestorage:${COMMON_VERSION}
    container_name: imagestorage
    env_file:
      - imagestorage.env
    networks:
      - openreplay-net
    restart: unless-stopped

  integrations-openreplay:
    image: public.ecr.aws/p1t3u8a3/integrations:${COMMON_VERSION}
    container_name: integrations
    networks:
      - openreplay-net
    env_file:
      - integrations.env
    restart: unless-stopped

  peers-openreplay:
    image: public.ecr.aws/p1t3u8a3/peers:${COMMON_VERSION}
    container_name: peers
    networks:
      - openreplay-net
    env_file:
      - peers.env
    restart: unless-stopped

  sourcemapreader-openreplay:
    image: public.ecr.aws/p1t3u8a3/sourcemapreader:${COMMON_VERSION}
    container_name: sourcemapreader
    networks:
      - openreplay-net
    env_file:
      - sourcemapreader.env
    restart: unless-stopped

  videostorage-openreplay:
    image: public.ecr.aws/p1t3u8a3/videostorage:${COMMON_VERSION}
    container_name: videostorage
    networks:
      - openreplay-net
    env_file:
      - videostorage.env
    restart: unless-stopped

  http-openreplay:
    image: public.ecr.aws/p1t3u8a3/http:${COMMON_VERSION}
    container_name: http
    networks:
      - openreplay-net
    env_file:
      - http.env
    restart: unless-stopped

  chalice-openreplay:
    image: public.ecr.aws/p1t3u8a3/chalice:${COMMON_VERSION}
    container_name: chalice
    volumes:
      - shared-volume:/mnt/efs
    networks:
      - openreplay-net
    env_file:
      - chalice.env
    restart: unless-stopped

  sink-openreplay:
    image: public.ecr.aws/p1t3u8a3/sink:${COMMON_VERSION}
    container_name: sink
    volumes:
      - shared-volume:/mnt/efs
    networks:
      - openreplay-net
    env_file:
      - sink.env
    restart: unless-stopped

  storage-openreplay:
    image: public.ecr.aws/p1t3u8a3/storage:${COMMON_VERSION}
    container_name: storage
    volumes:
      - shared-volume:/mnt/efs
    networks:
      - openreplay-net
    env_file:
      - storage.env
    restart: unless-stopped

  nginx-openreplay:
    image: nginx:latest
    container_name: nginx
    networks:
      - openreplay-net
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf
    restart: unless-stopped


  caddy:
    image: caddy:latest
    container_name: caddy
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./Caddyfile:/etc/caddy/Caddyfile
      - caddy_data:/data
      - caddy_config:/config
    networks:
      - openreplay-net
    environment:
      - ACME_AGREE=true # Agree to Let's Encrypt Subscriber Agreement
      - CADDY_DOMAIN=${CADDY_DOMAIN}
    restart: unless-stopped


volumes:
  pgdata:
  redisdata:
  miniodata:
  shared-volume:
  caddy_data:
  caddy_config:

networks:
  openreplay-net:
