solrcopy 0.9.2

Command line tool useful for migration, transformations, backup, and restore of documents stored inside cores of Apache Solr
#!/usr/bin/env -S bash -x -c 'docker compose up --no-recreate --wait --detach'

#@ https://solr.apache.org/guide/solr/latest/deployment-guide/solr-in-docker.html#docker-compose

services:
  solr:
    container_name: solr4test
    image: "solr:${TAG:-slim}" # Or: latest, 10-slim, 10, 9, ...
    hostname: solrhost
    environment:
      - SOLR_IMAGE_TAG=${TAG:-latest}
      - SOLR_CREATE_CORE=${CORE:-example}
      - SOLR_RUN_MODE=${MODE:-testing}
      - JAVA_TOOL_OPTIONS="--sun-misc-unsafe-memory-access=allow"
    ports:
      - "8983:8983"
    volumes:
      - type: volume
        source: data
        target: /var/solr
      - type: bind
        source: ./configuration
        target: /opt/configuration
    ulimits:
      nofile:
        soft: 65000
        hard: 65000
    healthcheck:
      # test: solr status || exit 121
      test: curl -X GET --fail http://localhost:8983/solr/admin/info/health
      start_period: 10s
      timeout: 10s
      retries: 1
      interval: 60s
    configs:
      - source: bashrc.sh
        target: /home/solr/.bashrc
        mode: 0644
      - source: solr-setup-precreate.sh
        target: /opt/solr/docker/scripts/solr-setup-precreate
        mode: 0755
      - source: solr-ingest-core.sh
        target: /opt/solr/docker/scripts/solr-ingest-core
        mode: 0755
      - source: solr-ingest-examples.sh
        target: /opt/solr/docker/scripts/solr-ingest-examples
        mode: 0755
      - source: solr-commit.sh
        target: /opt/solr/docker/scripts/solr-commit
        mode: 0755
      - source: solr-runas-user-managed.sh
        target: /opt/solr/docker/scripts/solr-runas-user-managed
        mode: 0755
      - source: solr-start-server.sh
        target: /opt/solr/docker/scripts/solr-start-server
        mode: 0755
    command:
      #? Solr docs example:
      # - solr-precreate
      # - gettingstarted
      # - solr-demo
      #? Solr cloud/standalone startup example:
      # - solr-foreground
      # - --user-managed
      #? Choose Solr run mode from MODE/SOLR_RUN_MODE environment variable:
      - solr-start-server

volumes:
  data:
  configuration:
    external: true
    name: configuration

configs:
  bashrc.sh:
    content: |
      # ~/.bashrc: executed by bash(1) for non-login shells.
      # see /usr/share/doc/bash/examples/startup-files (in the package bash-doc)
      # for examples

      # If not running interactively, don't do anything
      case $- in
          *i*) ;;
            *) return;;
      esac

      $ Some useful aliases
      alias ls='ls --escape --dereference-command-line --human-readable --time-style=iso --no-group --color=auto'
      alias ll='ls --escape --dereference-command-line --human-readable --time-style=iso --no-group --color=auto --almost-all -l'
      alias la='ls --escape --dereference-command-line --human-readable --time-style=iso --no-group --color=auto --almost-all'
      alias l='ls --escape --dereference-command-line --human-readable --time-style=iso --no-group --color=auto -CF'
      # END OF SCRIPT #

  solr-setup-precreate.sh:
    content: |
      #!/bin/bash
      set -eu -o pipefail
      echo 'CREATE: Checking if it needs to create Solr cores before starting up...'
      if [ -d /var/solr/data/demo ]; then
        echo 'CREATE: Solr core named demo already exists; skipping core creation.'
      else
        for core in demo gettingstarted films films2 target "$$@"; do
          echo "CREATE: Creating Solr core named [$${core}]..."
          if ! precreate-core "$${core}"; then
            echo "ERROR: Failed to create Solr core named $${core}. Exiting container..."
            exit 21
          fi
        done
        echo 'CREATE: Initializing solr db...'
        if ! source run-initdb; then
          echo "ERROR: Failed to initialize solr db. Exiting container..."
          exit 22
        fi
      fi
      echo 'CREATE: Finished Solr cores creation successfully.'
      # END OF SCRIPT #

  solr-ingest-core.sh:
    content: |
      #!/bin/bash
      set -eu -o pipefail
      export CORE="$${1:-}"; shift;
      echo "INGEST: Ingesting data into a Solr core named $${CORE:-}..."
      if test -z "$${CORE:-}"; then
        echo 'INGEST: No core name provided; aborting container execution...'
        exit 29
      fi
      if ! test -d "/var/solr/data/$${CORE}"; then
        echo "INGEST: Solr core named $${CORE} does not exists; skipping core ingestion."
      else
        for docpath in "$$@"; do
          if ! test -f "$${docpath}"; then
            echo "INGEST: Source data for '$${docpath}' does not exists; skipping core $${CORE} ingestion."
            continue
          fi
          echo "INGEST: Uploading data from '$${docpath}' into Solr core $${CORE} ingestion..."
          if ! solr post -c "$${CORE}" "$${docpath}"; then
            echo "INGEST: Failed to ingest data into core $${CORE} from $${docpath}; skipping data ingestion in core $${CORE}."
            continue
          else
            echo "INGEST: Commiting ingested data in core $${CORE}..."
            echo '{"commit": {}}' | solr post -c "$${CORE}"
          fi
        done
      fi
      echo "INGEST: Finished to ingest data into Solr core $${CORE}."
      # END OF SCRIPT #

  solr-ingest-examples.sh:
    content: |
      #!/bin/bash
      set -eu -o pipefail
      echo 'INGEST: Starting example data ingestion in Solr cores. Wait some seconds...'
      solr-ingest-core demo /opt/solr/example/exampledocs/books.json /opt/solr/example/exampledocs/books.csv /opt/solr/example/exampledocs/*.xml
      solr-ingest-core films /opt/solr/example/films/films.xml
      solr-ingest-core films2 /opt/solr/example/films/films.json
      echo 'INGEST: Finished ingestion of Solr example data successfully.'
      # END OF SCRIPT #

  solr-commit.sh:
    content: |
      #!/bin/bash
      # Sends a commit to the Solr Core
      set -eu -o pipefail
      echo '{"commit": {}}' | solr post -c "$${1:-}" -type application/json
      # END OF SCRIPT #

  solr-runas-user-managed.sh:
    content: |
      #!/bin/bash
      set -eu -o pipefail
      echo "TEST: Creating Solr cores: $${SOLR_IMAGE_TAG:-}..."
      if ! solr-setup-precreate; then
        echo "ERROR: Solr cores creation failed. Exiting container..."
        exit 21
      fi
      echo "TEST: Setting up Solr configsets for Solr cores..."
      mkdir -p /var/solr/data/configsets
      cp --recursive /opt/solr/server/solr/configsets/_default /var/solr/data/configsets/
      # Solr 10+ defaults to SolrCloud; precreated cores require standalone (user-managed) mode.
      echo "TEST: Starting Solr Server in user-managed mode..."
      if ! exec solr-fg --user-managed "$$@"; then
        echo "ERROR: Solr Server in user-managed mode failed. Exiting container..."
        exit 22
      fi
      echo 'TEST: Solr Server in user-managed mode finished successfully.'
      # END OF SCRIPT #

  solr-start-server.sh:
    content: |
      #!/bin/bash
      set -eu -o pipefail
      if test -n "$${DEBUG:-}"; then set -x; fi
      echo "RUN: Setting system limits..."
      if ! ulimit -n 65000; then
        echo "RUN: Failed to set system limits. Exiting container..."
        exit 11
      fi
      echo "RUN: Starting Solr version $${SOLR_IMAGE_TAG:-} in $${SOLR_RUN_MODE:-} mode..."
      case "$${SOLR_RUN_MODE:-testing}" in
        standalone) SOLR_RUN_COMMAND="solr-foreground --user-managed";;
        cloud)      SOLR_RUN_COMMAND="solr-foreground";;
        precreate)  SOLR_RUN_COMMAND="solr-precreate $${SOLR_CREATE_CORE:-}";;
        demo)       SOLR_RUN_COMMAND="solr-demo";;
        testing)    SOLR_RUN_COMMAND="solr-runas-user-managed";;
        *) echo "ERROR: Invalid Solr run mode: $${SOLR_RUN_MODE:-}."
           echo "RUN: Please set the SOLR_RUN_MODE environment variable to one of the allowed values: standalone, cloud, precreate, demo, testing."
           echo "RUN: For example:"
           echo "$ cargo make --env SOLR_RUN_MODE=testing test"
           echo "$ SOLR_RUN_MODE=standalone docker compose up --no-recreate --wait --detach"
           echo "RUN: Aborting container execution..."
           exit 99 ;;
      esac
      echo "RUN: Running Solr using command: [$${SOLR_RUN_COMMAND} $$*]"
      if ! exec $${SOLR_RUN_COMMAND} "$$@"; then
        echo "RUN: Failed to run Solr version $${SOLR_IMAGE_TAG:-} in $${SOLR_RUN_MODE:-} mode. Exiting container..."
        exit 13
      fi
      echo 'RUN: Solr Server execution finished successfully. Exiting container...'
      # END OF SCRIPT #