|
| 1 | +#!/bin/sh |
| 2 | +set -eo pipefail |
| 3 | + |
| 4 | +# Previously the Solr Config and Solr Data Dir was both kept in the persistent volume: |
| 5 | +# - Solr data: /opt/solr/server/solr/mycores/${corename}/data |
| 6 | +# - Solr config: /opt/solr/server/solr/mycores/${corename}/config |
| 7 | +# - Persistent Volume: /opt/solr/server/solr/mycores/ |
| 8 | +# The Solr Config was copied from the Docker Image into the Solr Config directory on the very first time solr started. |
| 9 | +# This had the problem that if a new Solr Config was shipped in a new Docker Iamage the config was not copied again. |
| 10 | +# Therefore there was no possibility to ship with updated configs and also the system does not follow any other |
| 11 | +# services like nginx or php which have their configs not existing in persistent volumes and insted in Docker Images. |
| 12 | +# The following script migrates from to the new directory structure: |
| 13 | +# - Solr data: /var/solr/${corename} |
| 14 | +# - Solr config: /opt/solr/server/solr/mycores/${corename}/config |
| 15 | +# - Persistent Volume: /var/solr/ |
| 16 | +# It does: |
| 17 | +# 1. Move folders from /var/solr/${corename}/data to /var/solr/${corename} - this is needed if the existing persistent volume is |
| 18 | +# mounted now to /var/solr/ but the data is still within data/ |
| 19 | +# 2. Create the folder /opt/solr/server/solr/mycores/${corename} if not existing (because there is no persistent volume mounted anymore) |
| 20 | +# and copy the config from the persistent storage to that folder. |
| 21 | + |
| 22 | +# It then also tries to update existing non-compatible configs inside solrconfig.xml: |
| 23 | +# - dataDir now needs to be `<dataDir>/var/solr/${solr.core.name}</dataDir>` to point to the new persistent Volume |
| 24 | +# - lockType needs to be `<lockType>${solr.lock.type:none}</lockType>` to prevent issues with the default file based Lock system which |
| 25 | +# can cause issues if the solr is not stopped correctly |
| 26 | +# The script does that for existing configs in `/opt/solr/server/solr/mycores/${corename}/config` if that folder exists which can be on two cases: |
| 27 | +# 1. During a docker build the solr core has already been created via `precreate-core` (which should be used now all the time) |
| 28 | +# 2. The first part of the script has copied the config from the previous persistent volume into these folders |
| 29 | +# If `/opt/solr/server/solr/mycores` is empty, this means the container has never been started, had no previous persistent volume and also did not |
| 30 | +# run `precreate-core` yet, it checks if the common used folder `/solr-conf/conf/` has a config in it and tries to adapt it. |
| 31 | +# This probably fails because of permissions issues, it will throw an error and exit. |
| 32 | + |
| 33 | +if [ ! -n "$(ls /opt/solr/server/solr/mycores)" ]; then |
| 34 | + echo 'No pre-created Solr Cores found in `/opt/solr/server/solr/mycores` this probably means that your Dockerfile does not run' |
| 35 | + echo ' RUN precreate-core corename /solr-conf' |
| 36 | + echo 'within Dockerfile and instead uses' |
| 37 | + echo ' CMD ["solr-precreate", "corename", "/solr-conf"]' |
| 38 | + echo 'Please update your Dockerfile to:' |
| 39 | + echo ' RUN precreate-core corename /solr-conf' |
| 40 | + echo ' CMD ["solr-foreground"]' |
| 41 | + printf "\n\n" |
| 42 | +fi |
| 43 | + |
| 44 | +if [ -n "$(ls /var/solr)" ]; then |
| 45 | + # Iterate through all existing solr cores |
| 46 | + for solrcorepath in $(ls -d /var/solr/*) ; do |
| 47 | + corename=$(basename $solrcorepath) |
| 48 | + if [ -d ${solrcorepath}/data ]; then |
| 49 | + echo "${solrcorepath} has it's data in deprecated location ${solrcorepath}/data, moving to ${solrcorepath}." |
| 50 | + # moving the contents of /var/solr/${corename}/data to /var/solr/${corename} |
| 51 | + # the datadir now has the layout that a newly created core would. |
| 52 | + mv ${solrcorepath}/data/* ${solrcorepath}/ |
| 53 | + # remove empty directory |
| 54 | + rm -Rf ${solrcorepath}/data || mv ${solrcorepath}/data ${solrcorepath}/data-delete |
| 55 | + fi |
| 56 | + |
| 57 | + # If the core has no files in /opt/solr/server/solr/mycores/${corename} this means: |
| 58 | + # The Docker Image did not run `precreate-core corename /solr-conf` during the Dockerfile |
| 59 | + # and instead is running `solr-precreate corname solr-conf` as CMD of the container. |
| 60 | + # But we already have an existing solr config from the persistent storage, we copy that over |
| 61 | + if [ ! -d /opt/solr/server/solr/mycores/${corename} ]; then |
| 62 | + mkdir -p /opt/solr/server/solr/mycores/${corename} |
| 63 | + # Copy the solr config from the persistent volume in the solr home config directory |
| 64 | + cp -R ${solrcorepath}/conf /opt/solr/server/solr/mycores/${corename}/ |
| 65 | + echo "copied pre-existing solr config from '${solrcorepath}/conf' to '/opt/solr/server/solr/mycores/${corename}/conf'" |
| 66 | + printf "\n\n" |
| 67 | + # there must be a core.properties to be recognized as a core |
| 68 | + touch /opt/solr/server/solr/mycores/${corename}/core.properties |
| 69 | + fi |
| 70 | + done |
| 71 | +fi |
| 72 | + |
| 73 | +function fixConfig { |
| 74 | + fail=0 |
| 75 | + if cat $1/solrconfig.xml | grep dataDir | grep -qv '<dataDir>/var/solr/${solr.core.name}</dataDir>'; then |
| 76 | + echo "Found old non lagoon compatible dataDir config in solrconfig.xml:" |
| 77 | + cat $1/solrconfig.xml | grep dataDir |
| 78 | + if [ -w $1/ ]; then |
| 79 | + sed -ibak 's/<dataDir>.*/<dataDir>\/var\/solr\/${solr.core.name}<\/dataDir>/' $1/solrconfig.xml |
| 80 | + echo "automagically updated to compatible config: " |
| 81 | + echo ' <dataDir>/var/solr/${solr.core.name}</dataDir>' |
| 82 | + echo "Please update your solrconfig.xml to make this persistent." |
| 83 | + else |
| 84 | + echo "but no write permission to automagically change to compatible config: " |
| 85 | + echo ' <dataDir>/var/solr/${solr.core.name}</dataDir>' |
| 86 | + echo "Please update your solrconfig.xml and commit again." |
| 87 | + fail=1 |
| 88 | + fi |
| 89 | + printf "\n\n" |
| 90 | + fi |
| 91 | + # change lockType to none |
| 92 | + if cat $1/solrconfig.xml | grep lockType | grep -qv '<lockType>${solr.lock.type:none}</lockType>'; then |
| 93 | + echo "Found old non lagoon compatible lockType config in solrconfig.xml:" |
| 94 | + cat $1/solrconfig.xml | grep lockType |
| 95 | + if [ -w $1/ ]; then |
| 96 | + sed -ibak 's/<lockType>\${solr\.lock\.type:native}<\/lockType>/<lockType>${solr.lock.type:none}<\/lockType>/' $1/solrconfig.xml |
| 97 | + echo "automagically updated to compatible config: " |
| 98 | + echo ' <lockType>${solr.lock.type:none}</lockType>' |
| 99 | + echo "Please update your solrconfig.xml to make this persistent." |
| 100 | + else |
| 101 | + echo "but no write permission to automagically change to compatible config: " |
| 102 | + echo ' <lockType>${solr.lock.type:none}</lockType>' |
| 103 | + echo "Please update your solrconfig.xml and commit again." |
| 104 | + fail=1 |
| 105 | + fi |
| 106 | + printf "\n\n" |
| 107 | + fi |
| 108 | + if [ "$fail" == "1" ]; then |
| 109 | + exit 1; |
| 110 | + fi |
| 111 | +} |
| 112 | + |
| 113 | +# check if `/opt/solr/server/solr/mycores` has cores, which means that `precreate-core` has already be called so we check the configs there |
| 114 | +if [ -n "$(ls /opt/solr/server/solr/mycores)" ]; then |
| 115 | + # Iterate through all solr cores |
| 116 | + for solrcorepath in $(ls -d /opt/solr/server/solr/mycores/*) ; do |
| 117 | + corename=$(basename $solrcorepath) |
| 118 | + # Check and Update the solr config with lagoon compatible config |
| 119 | + if [ -f /opt/solr/server/solr/mycores/${corename}/conf/solrconfig.xml ]; then |
| 120 | + fixConfig /opt/solr/server/solr/mycores/${corename}/conf |
| 121 | + fi |
| 122 | + done |
| 123 | +else |
| 124 | + # `/opt/solr/server/solr/mycores` is empty, meaning that no `precreate-core` has been called and probably this container is started via `solr-precreate |
| 125 | + # We try to update the solr configs within `/solr-conf/conf` to the new lagoon default config as this one will most probably be used to create a new core |
| 126 | + if [ -f /solr-conf/conf/solrconfig.xml ]; then |
| 127 | + fixConfig /solr-conf/conf |
| 128 | + else |
| 129 | + echo "No config found in '/solr-conf/conf' and was not able to automatically update solr config to newest lagoon compatible version." |
| 130 | + echo "Cannot guarantee if this Solr config will work!" |
| 131 | + fi |
| 132 | +fi |
| 133 | + |
0 commit comments