#
# The contents of this file are subject to the license and copyright
# detailed in the LICENSE and NOTICE files at the root of the source
# tree and available online at
#
# http://www.dspace.org/license/
#

# Docker Compose for running the DSpace backend for e2e testing in a CI environment
# This is used by our GitHub CI at .github/workflows/build.yml
# It is based heavily on the Backend's Docker Compose:
# https://github.com/DSpace/DSpace/blob/main/docker-compose.yml
version: '3.7'
networks:
  dspacenet:
services:
  # DSpace (backend) webapp container
  dspace:
    container_name: dspace
    environment:
      # Below syntax may look odd, but it is how to override dspace.cfg settings via env variables.
      # See https://github.com/DSpace/DSpace/blob/main/dspace/config/config-definition.xml
      # __P__ => "." (e.g. dspace__P__dir => dspace.dir)
      # __D__ => "-" (e.g. google__D__metadata => google-metadata)
      # dspace.dir, dspace.server.url and dspace.ui.url
      dspace__P__dir: /dspace
      dspace__P__server__P__url: http://localhost:8080/server
      dspace__P__ui__P__url: http://localhost:4000
      # db.url: Ensure we are using the 'dspacedb' image for our database
      db__P__url: 'jdbc:postgresql://dspacedb:5432/dspace'
      # solr.server: Ensure we are using the 'dspacesolr' image for Solr
      solr__P__server: http://dspacesolr:8983/solr
    depends_on:
    - dspacedb
    image: dspace/dspace:dspace-7_x-test
    networks:
      dspacenet:
    ports:
    - published: 8080
      target: 8080
    stdin_open: true
    tty: true
    volumes:
    - assetstore:/dspace/assetstore
    # Mount DSpace's solr configs to a volume, so that we can share to 'dspacesolr' container (see below)
    - solr_configs:/dspace/solr
    # Ensure that the database is ready BEFORE starting tomcat
    # 1. While a TCP connection to dspacedb port 5432 is not available, continue to sleep
    # 2. Then, run database migration to init database tables
    # 3. Finally, start Tomcat
    entrypoint:
    - /bin/bash
    - '-c'
    - |
      while (!</dev/tcp/dspacedb/5432) > /dev/null 2>&1; do sleep 1; done;
      /dspace/bin/dspace database migrate
      catalina.sh run
  # DSpace database container
  # NOTE: This is customized to use our loadsql image, so that we are using a database with existing test data
  dspacedb:
    container_name: dspacedb
    environment:
      # This LOADSQL should be kept in sync with the LOADSQL in
      # https://github.com/DSpace/DSpace/blob/main/dspace/src/main/docker-compose/db.entities.yml
      # This SQL is available from https://github.com/DSpace-Labs/AIP-Files/releases/tag/demo-entities-data
      LOADSQL: https://github.com/DSpace-Labs/AIP-Files/releases/download/demo-entities-data/dspace7-entities-data.sql
      PGDATA: /pgdata
    image: dspace/dspace-postgres-pgcrypto:loadsql
    networks:
      dspacenet:
    stdin_open: true
    tty: true
    volumes:
    - pgdata:/pgdata
  # DSpace Solr container
  dspacesolr:
    container_name: dspacesolr
    # Uses official Solr image at https://hub.docker.com/_/solr/
    image: solr:8.11-slim
    # Needs main 'dspace' container to start first to guarantee access to solr_configs
    depends_on:
    - dspace
    networks:
      dspacenet:
    ports:
    - published: 8983
      target: 8983
    stdin_open: true
    tty: true
    working_dir: /var/solr/data
    volumes:
    # Mount our "solr_configs" volume available under the Solr's configsets folder (in a 'dspace' subfolder)
    # This copies the Solr configs from main 'dspace' container into 'dspacesolr' via that volume
    - solr_configs:/opt/solr/server/solr/configsets/dspace
    # Keep Solr data directory between reboots
    - solr_data:/var/solr/data
    # Initialize all DSpace Solr cores using the mounted configsets (see above), then start Solr
    entrypoint:
    - /bin/bash
    - '-c'
    - |
      init-var-solr
      precreate-core authority /opt/solr/server/solr/configsets/dspace/authority
      precreate-core oai /opt/solr/server/solr/configsets/dspace/oai
      precreate-core search /opt/solr/server/solr/configsets/dspace/search
      precreate-core statistics /opt/solr/server/solr/configsets/dspace/statistics
      exec solr -f
volumes:
  assetstore:
  pgdata:
  solr_data:
  # Special volume used to share Solr configs from 'dspace' to 'dspacesolr' container (see above)
  solr_configs: