# 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=noredirect
# #   - LETSENCRYPT_HOST=kobo.ripcordengineering.com,kobocat.ripcordengineering.com,enketo.ripcordengineering.com
# #   - LETSENCRYPT_EMAIL=admin@ripcordengineering.com
# See "envfile.server.txt" for domain and subdomain settings. Adjust nginx HOST to match.
#    - VIRTUAL_HOST=subdomain1.domain,subdomain2.domain,subdomain3.domain
#    - LETSENCRYPT_HOST=subdomain1.domain,subdomain2.domain,subdomain3.domain

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: unless-stopped

postgres:
  image: kobotoolbox/postgres:latest
  # Dev: Build the image locally.
  # build: ./base_images/postgres
  hostname: postgres
  env_file:
    - ./envfile.server.txt
  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: unless-stopped

mongo:
  image: kobotoolbox/mongo:latest
  # Dev: Build the image locally.
  # build: ./base_images/mongo
  hostname: mongo
  environment:
    - MONGO_DATA=/srv/db
  env_file:
    - ./envfile.server.txt
  # 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
    - ./backups/mongo:/srv/backups
  restart: unless-stopped

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
    - ./backups/kobocat:/srv/backups
    - ./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 live source directories from the host machine.
    #- ../kobocat:/srv/src/kobocat
    #- ../formpack/src/formpack:/usr/local/lib/python2.7/dist-packages/formpack
    # Dev: Share PyDev remote debugger into container.
    #- ~/devel/liclipse/plugins/org.python.pydev_5.4.0.201611281405/pysrc:/srv/pydev_orig:ro
    # Dev: Share your `.transifexrc` into the container.
    # - ./.transifexrc:/root/.transifexrc
  environment:
    - ENKETO_PROTOCOL=https
  restart: unless-stopped
  # NOTE: Workaround to force name resolution inside containers. If enabled, 
  #   customize with your actual subdomains+domain and server IP. Clients 
  #   might also need similar manipulations to their `hosts` files.
  # extra_hosts:
  #   - 'ee-local.kobotoolbox.org:172.17.0.1'
  #   - 'kf-local.kobotoolbox.org:172.17.0.1'

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
  environment:
    - SECURE_PROXY_SSL_HEADER=HTTP_X_FORWARDED_PROTO, https
  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 live source directories from the host machine.
    #- ../kpi:/srv/src/kpi
    #- ../formpack/src/formpack:/usr/local/lib/python2.7/dist-packages/formpack
    # Dev: Share PyDev remote debugger source into container.
    #- ~/devel/liclipse/plugins/org.python.pydev_5.4.0.201611281405/pysrc:/srv/pydev_orig:ro
    # Dev: Share your `.transifexrc` into the container.
    # - ./.transifexrc:/root/.transifexrc
  restart: unless-stopped
  # NOTE: Workaround to force name resolution inside containers. If enabled, 
  #   customize with your actual subdomains+domain and server IP. Clients 
  #   might also need similar manipulations to their `hosts` files.
  # extra_hosts:
  #   - 'ee-local.kobotoolbox.org:172.17.0.1'
  #   - 'kc-local.kobotoolbox.org:172.17.0.1'

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}
    - VIRTUAL_HOST=kobo.myorg.org,kc.myorg.org,ee.myorg.org
    - LETSENCRYPT_HOST=kobo.myorg.org,kc.myorg.org,ee.myorg.org
    - LETSENCRYPT_EMAIL=admin@myorg.org
#  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
      - ./secrets/:/tmp/kobo_toolbox_secrets/:ro
  links:
    - kobocat
    - kpi
    - enketo_express
  restart: unless-stopped

# 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: unless-stopped
  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 subdomains+domain and server IP. Clients 
  #   might also need similar manipulations to their `hosts` files.
  # extra_hosts:
  #   - 'kf-local.kobotoolbox.org:172.17.0.1'
  #   - 'kc-local.kobotoolbox.org:172.17.0.1'

# 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: unless-stopped

# 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: unless-stopped
