# For https kobotoolbox server behind jwilder/nginx-proxy with ssl certificate provided by docker-letsencrypt-nginx-proxy-companion.
# Follow the instructions under "docker-letsencrypt-nginx-proxy-companion" to set up jwilder/nginx-proxy prior to starting kobotoolbox.
# References:
#   docker-letsencrypt-nginx-proxy-companion = https://github.com/JrCs/docker-letsencrypt-nginx-proxy-companion
#   jwilder/nginx-proxy = https://github.com/jwilder/nginx-proxy ==> SSL Support
# For http kobotoolbox server see nginx: activate (delete #) HTTPS_METHOD and deactivate (add #) LETSENCRYPT lines.
#    - HTTPS_METHOD
# #   - LETSENCRYPT_HOST
# #   - LETSENCRYPT_EMAIL
# See "envfile.server.txt" to set the domain and subdomains. 

rabbit:
  image: kobotoolbox/rabbit:latest
  # Dev: Build the image locally.
  # build: ./base_images/rabbit
  hostname: rabbit
  environment:
    - RABBITMQ_LOG_BASE=/var/log/rabbitmq
  volumes:
    - ./log/rabbitmq:/var/log/rabbitmq
  restart: on-failure

postgres:
  image: kobotoolbox/postgres:latest
  # Dev: Build the image locally.
  # build: ./base_images/postgres
  hostname: postgres
  volumes:
    - ./.vols/db:/srv/db
    - ./backups/postgres:/srv/backups
    - ./base_images/postgres/init_postgres.bash:/etc/my_init.d/10_init_postgres.bash:ro
  restart: on-failure

mongo:
  image: kobotoolbox/mongo:latest
  # Dev: Build the image locally.
  # build: ./base_images/mongo
  hostname: mongo
  environment:
    - MONGO_DATA=/srv/db
  # NOTE: On OS X and Windows, comment out the following `volumes` directive 
  #   as a workaround to a VirtualBox folder sharing+Mongo incompatibility.
  #   See the "WARNING" at https://hub.docker.com/_/mongo/
  volumes:
    - ./.vols/mongo:/srv/db
  restart: on-failure

kobocat:
  image: kobotoolbox/kobocat:latest
  # Dev: Build the image locally.
  # build: ../kobocat
  hostname: kobocat
  env_file:
    - ./envfile.server.txt
    - ./envfiles/aws.txt
    - ./envfiles/external_services.txt
    - ./envfiles/kobocat.txt
    - ./envfiles/nginx.txt
    - ./envfiles/smtp.txt
  links:
    - rabbit
    - mongo
    - postgres
    - kpi
  volumes:
    - ./.vols/static/kobocat:/srv/static
    - ./.vols/kobocat_media_uploads:/srv/src/kobocat/media
    - ./scripts/wait_for_rabbit.bash:/etc/my_init.d/01_wait_for_rabbit.bash:ro
    - ./scripts/wait_for_mongo.bash:/etc/my_init.d/02_wait_for_mongo.bash:ro
    - ./scripts/wait_for_postgres.bash:/etc/my_init.d/03_wait_for_postgres.bash:ro
    - ./scripts/wait_for_kpi.bash:/etc/my_init.d/04_wait_for_kpi.bash:ro
    - ./scripts/runtime_variables_kobocat.source.bash:/etc/profile.d/runtime_variables_kobocat.source.bash.sh:ro
    # Dev: Use the live `kobocat` directory from the host machine.
    # - ../kobocat:/srv/src/kobocat
    # Dev: Share your `.transifexrc` into the container.
    # - ./.transifexrc:/root/.transifexrc
  environment:
    - ENKETO_PROTOCOL=https
  restart: on-failure
  # NOTE: Workaround to force name resolution inside containers. If enabled, 
  #   customize with your actual sub$${PUBLIC_DOMAIN_NAME}s+$${PUBLIC_DOMAIN_NAME} and server IP. Clients 
  #   might also need similar manipulations to their `hosts` files.
  extra_hosts:
      - 'ee.myorg.org:192.168.101.40'
      - 'kobo.myorg.org:192.168.101.40'

kpi:
  image: kobotoolbox/kpi:latest
  # Dev: Build the image locally.
  # build: ../kpi
  hostname: kpi
  env_file:
    - ./envfile.server.txt
    - ./envfiles/aws.txt
    - ./envfiles/external_services.txt
    - ./envfiles/kpi.txt
    - ./envfiles/nginx.txt
    - ./envfiles/smtp.txt
  links:
    - postgres
    - rabbit
    - mongo
  volumes:
    - ./.vols/static/kpi:/srv/static
    # The Whoosh search index needs persistent storage
    - ./.vols/whoosh:/srv/whoosh
    - ./scripts/wait_for_rabbit.bash:/etc/my_init.d/01_wait_for_rabbit.bash:ro
    - ./scripts/wait_for_mongo.bash:/etc/my_init.d/02_wait_for_mongo.bash:ro
    - ./scripts/wait_for_postgres.bash:/etc/my_init.d/03_wait_for_postgres.bash:ro
    - ./scripts/create_kobo_superuser.bash://etc/my_init.d/20_create_kobo_superuser.bash:ro
    - ./scripts/runtime_variables_kpi.source.bash:/etc/profile.d/runtime_variables_kpi.source.bash.sh:ro
    # Dev: Use the live `kpi` directory from the host machine.
    # - ../kpi:/srv/src/kpi
    # Dev: Share your `.transifexrc` into the container.
    # - ./.transifexrc:/root/.transifexrc
  restart: on-failure
  # NOTE: Workaround to force name resolution inside containers. If enabled, 
  #   customize with your actual sub$${PUBLIC_DOMAIN_NAME}s+$${PUBLIC_DOMAIN_NAME} and server IP. Clients 
  #   might also need similar manipulations to their `hosts` files.
  extra_hosts:
      - 'ee.myorg.org:192.168.101.40'
      - 'kc.myorg.org:192.168.101.40'

nginx:
  image: kobotoolbox/nginx:latest
  # Dev: Build the image locally.
  # build: ./base_images/nginx
  hostname: nginx
  env_file:
    - ./envfile.server.txt
    - ./envfiles/nginx.txt
    - ./envfiles/kobocat.txt
    - ./envfiles/kpi.txt
  environment:
    - NGINX_CONFIG_FILE_NAME=nginx_site_https.conf
    - TEMPLATED_VAR_REFS=$${PUBLIC_DOMAIN_NAME} $${KOBOFORM_PUBLIC_SUBDOMAIN} $${KOBOCAT_PUBLIC_SUBDOMAIN} $${ENKETO_EXPRESS_PUBLIC_SUBDOMAIN}
# manually replace host variables below with envfile.server.txt subdomains and domain    
    - VIRTUAL_HOST=kobo.myorg.org,kc.myorg.org,ee.myorg.org
#    - HTTPS_METHOD=noredirect
    - LETSENCRYPT_HOST=kobo.myorg.org,kc.myorg.org,ee.myorg.org
    - LETSENCRYPT_EMAIL=admin@PUBLIC_DOMAIN_NAME
#  ports:
#    - 80:80
#    - 443:443
  volumes:
      - ./.vols/static:/srv/www:ro
      # get the logs out of glusterfs!
      - ./log/nginx:/var/log/nginx
      - ./nginx/:/tmp/kobo_nginx/:ro
      - ./nginx/nginx_command.bash:/etc/service/nginx/run:ro
  links:
    - kobocat
    - kpi
    - enketo_express
  restart: on-failure

# Adapted from https://github.com/kobotoolbox/enketo-express/blob/docker/docker-compose.yml.
enketo_express:
  image: kobotoolbox/enketo_express
  # Dev: Build the image locally.
  # build: ../enketo-express/
  env_file:
    - ./envfile.server.txt
    - ./envfiles/external_services.txt
  links:
    - redis_main
    - redis_cache
  restart: on-failure
  volumes:
    - ./scripts/runtime_variables_enketo_express.source.bash:/etc/profile.d/runtime_variables_enketo_express.source.bash.sh:ro
    - ./enketo_express/config.json:/srv/tmp/enketo_express_config.json:ro
    - ./scripts/enketo_express_copy_config.bash:/etc/my_init.d/01_enketo_express_copy_config.bash:ro
    # Override Enketo Express icons.
    - ./enketo_express/favicon.ico:/srv/src/enketo_express/public/images/favicon.ico:ro
    - ./enketo_express/icon_180x180.png:/srv/src/enketo_express/public/images/icon_180x180.png:ro
    # Dev: Use the live `enketo-express` directory from the host machine.
    # - ../enketo-express:/srv/src/enketo_express
  # NOTE: Workaround to force name resolution inside containers. If enabled, 
  #   customize with your actual sub$${PUBLIC_DOMAIN_NAME}s+$${PUBLIC_DOMAIN_NAME} and server IP. Clients 
  #   might also need similar manipulations to their `hosts` files.
  extra_hosts:
      - 'kobo.myorg.org:192.168.101.40'
      - 'kc.myorg.org:192.168.101.40'

# Adapted from https://github.com/kobotoolbox/enketo-express/blob/docker/docker-compose.yml.
redis_main:
  image: redis:2.6
  # Map our "main" Redis config into the container.
  volumes:
    - ./redis/redis-enketo-main.conf:/etc/redis/redis.conf:ro
    - ./.vols/redis_main_data/:/data/
  restart: on-failure

# Adapted from https://github.com/kobotoolbox/enketo-express/blob/docker/docker-compose.yml.
redis_cache:
  image: redis:2.6
  # Map our "cache" Redis config into the container.
  volumes:
    - ./redis/redis-enketo-cache.conf:/etc/redis/redis.conf:ro
  restart: on-failure
