|
| 1 | +#!/usr/bin/env bash |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MPL-2.0 |
| 4 | +# SPDX-FileCopyrightText: 2023 NextGen Healthcare |
| 5 | +# |
| 6 | + |
| 7 | +set -e |
| 8 | + |
| 9 | +APP_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" |
| 10 | + |
| 11 | +custom_extension_count=`ls -1 "$APP_DIR"/custom-extensions/*.zip 2>/dev/null | wc -l` |
| 12 | +if [ $custom_extension_count != 0 ]; then |
| 13 | + echo "Found ${custom_extension_count} custom extensions." |
| 14 | + for extension in $(ls -1 "$APP_DIR"/custom-extensions/*.zip); do |
| 15 | + unzip -o -q $extension -d "$APP_DIR/extensions" |
| 16 | + done |
| 17 | +fi |
| 18 | + |
| 19 | +# set storepass and keypass to 'changeme' so they aren't overwritten later |
| 20 | +KEYSTORE_PASS=changeme |
| 21 | +sed -i "s/^keystore\.storepass\s*=\s*.*\$/keystore.storepass = ${KEYSTORE_PASS//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 22 | +sed -i "s/^keystore\.keypass\s*=\s*.*\$/keystore.keypass = ${KEYSTORE_PASS//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 23 | + |
| 24 | +# merge the environment variables into /opt/engine/conf/mirth.properties |
| 25 | +# db type |
| 26 | +if ! [ -z "${DATABASE+x}" ]; then |
| 27 | + sed -i "s/^database\s*=\s*.*\$/database = ${DATABASE//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 28 | +fi |
| 29 | + |
| 30 | +# db username |
| 31 | +if ! [ -z "${DATABASE_USERNAME+x}" ]; then |
| 32 | + sed -i "s/^database\.username\s*=\s*.*\$/database.username = ${DATABASE_USERNAME//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 33 | +fi |
| 34 | + |
| 35 | +# db password |
| 36 | +if ! [ -z "${DATABASE_PASSWORD+x}" ]; then |
| 37 | + sed -i "s/^database\.password\s*=\s*.*\$/database.password = ${DATABASE_PASSWORD//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 38 | +fi |
| 39 | + |
| 40 | +# db url |
| 41 | +if ! [ -z "${DATABASE_URL+x}" ]; then |
| 42 | + sed -i "s/^database\.url\s*=\s*.*\$/database.url = ${DATABASE_URL//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 43 | +fi |
| 44 | + |
| 45 | +# database max connections |
| 46 | +if ! [ -z "${DATABASE_MAX_CONNECTIONS+x}" ]; then |
| 47 | + sed -i "s/^database\.max-connections\s*=\s*.*\$/database.max-connections = ${DATABASE_MAX_CONNECTIONS//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 48 | +fi |
| 49 | + |
| 50 | +# database max retries |
| 51 | +if ! [ -z "${DATABASE_MAX_RETRY+x}" ]; then |
| 52 | + sed -i "s/^database\.connection\.maxretry\s*=\s*.*\$/database.connection.maxretry = ${DATABASE_MAX_RETRY//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 53 | +fi |
| 54 | + |
| 55 | +# database retry wait time |
| 56 | +if ! [ -z "${DATABASE_RETRY_WAIT+x}" ]; then |
| 57 | + sed -i "s/^database\.connection\.retrywaitinmilliseconds\s*=\s*.*\$/database.connection.retrywaitinmilliseconds = ${DATABASE_RETRY_WAIT//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 58 | +fi |
| 59 | + |
| 60 | +# keystore storepass |
| 61 | +if ! [ -z "${KEYSTORE_STOREPASS+x}" ]; then |
| 62 | + sed -i "s/^keystore\.storepass\s*=\s*.*\$/keystore.storepass = ${KEYSTORE_STOREPASS//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 63 | +fi |
| 64 | + |
| 65 | +# keystore keypass |
| 66 | +if ! [ -z "${KEYSTORE_KEYPASS+x}" ]; then |
| 67 | + sed -i "s/^keystore\.keypass\s*=\s*.*\$/keystore.keypass = ${KEYSTORE_KEYPASS//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 68 | +fi |
| 69 | + |
| 70 | +if ! [ -z "${KEYSTORE_TYPE+x}" ]; then |
| 71 | + sed -i "s/^keystore\.type\s*=\s*.*\$/keystore.type = ${KEYSTORE_TYPE//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 72 | +fi |
| 73 | + |
| 74 | +# session store |
| 75 | +if ! [ -z "${SESSION_STORE+x}" ]; then |
| 76 | + LINE_COUNT=`grep "server.api.sessionstore" "$APP_DIR/conf/mirth.properties" | wc -l` |
| 77 | + if [ $LINE_COUNT -lt 1 ]; then |
| 78 | + echo -e "\nserver.api.sessionstore = ${SESSION_STORE//\//\\/}" >> "$APP_DIR/conf/mirth.properties" |
| 79 | + else |
| 80 | + sed -i "s/^server\.api\.sessionstore\s*=\s*.*\$/server.api.sessionstore = ${SESSION_STORE//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 81 | + fi |
| 82 | +fi |
| 83 | + |
| 84 | +#server ID |
| 85 | +if ! [ -z "${SERVER_ID+x}" ]; then |
| 86 | + echo -e "server.id = ${SERVER_ID//\//\\/}" > "$APP_DIR/appdata/server.id" |
| 87 | +fi |
| 88 | + |
| 89 | +# merge extra environment variables starting with _MP_ into mirth.properties |
| 90 | +while read -r keyvalue; do |
| 91 | + KEY="${keyvalue%%=*}" |
| 92 | + VALUE="${keyvalue#*=}" |
| 93 | + VALUE=$(tr -dc '\40-\176' <<< "$VALUE") |
| 94 | + |
| 95 | + if ! [ -z "${KEY}" ] && ! [ -z "${VALUE}" ] && ! [[ ${VALUE} =~ ^\ +$ ]]; then |
| 96 | + |
| 97 | + # filter for variables starting with "_MP_" |
| 98 | + if [[ ${KEY} == _MP_* ]]; then |
| 99 | + |
| 100 | + # echo "found property ${KEY}=${VALUE}" |
| 101 | + |
| 102 | + # example: _MP_DATABASE_MAX__CONNECTIONS -> database.max-connections |
| 103 | + |
| 104 | + # remove _MP_ |
| 105 | + # example: DATABASE_MAX__CONNECTIONS |
| 106 | + ACTUAL_KEY=${KEY:4} |
| 107 | + |
| 108 | + # switch '__' to '-' |
| 109 | + # example: DATABASE_MAX-CONNECTIONS |
| 110 | + ACTUAL_KEY="${ACTUAL_KEY//__/-}" |
| 111 | + |
| 112 | + # switch '_' to '.' |
| 113 | + # example: DATABASE.MAX-CONNECTIONS |
| 114 | + ACTUAL_KEY="${ACTUAL_KEY//_/.}" |
| 115 | + |
| 116 | + # lower case |
| 117 | + # example: database.max-connections |
| 118 | + ACTUAL_KEY="${ACTUAL_KEY,,}" |
| 119 | + |
| 120 | + # if key does not exist in mirth.properties append it at bottom |
| 121 | + LINE_COUNT=`grep "^${ACTUAL_KEY}" "$APP_DIR/conf/mirth.properties" | wc -l` |
| 122 | + if [ $LINE_COUNT -lt 1 ]; then |
| 123 | + # echo "key ${ACTUAL_KEY} not found in mirth.properties, appending. Value = ${VALUE}" |
| 124 | + echo -e "\n${ACTUAL_KEY} = ${VALUE//\//\\/}" >> "$APP_DIR/conf/mirth.properties" |
| 125 | + else # otherwise key exists, overwrite it |
| 126 | + # echo "key ${ACTUAL_KEY} exists, overwriting. Value = ${VALUE}" |
| 127 | + ESCAPED_KEY="${ACTUAL_KEY//./\\.}" |
| 128 | + sed -i "s/^${ESCAPED_KEY}\s*=\s*.*\$/${ACTUAL_KEY} = ${VALUE//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 129 | + fi |
| 130 | + fi |
| 131 | + fi |
| 132 | +done <<< "`printenv`" |
| 133 | + |
| 134 | +# merge vmoptions into /opt/engine/oieserver.vmoptions |
| 135 | +if ! [ -z "${VMOPTIONS+x}" ]; then |
| 136 | + PREV_IFS="$IFS" |
| 137 | + IFS="," |
| 138 | + read -ra vmoptions <<< "$VMOPTIONS" |
| 139 | + IFS="$PREV_IFS" |
| 140 | + |
| 141 | + for vmoption in "${vmoptions[@]}" |
| 142 | + do |
| 143 | + echo "${vmoption}" >> "$APP_DIR/oieserver.vmoptions" |
| 144 | + done |
| 145 | +fi |
| 146 | + |
| 147 | +# merge the user's secret mirth.properties |
| 148 | +# takes a whole mirth.properties file and merges line by line with /opt/engine/conf/mirth.properties |
| 149 | +if [ -f /run/secrets/mirth_properties ]; then |
| 150 | + |
| 151 | + # add new line in case /opt/engine/conf/mirth.properties doesn't end with one |
| 152 | + echo "" >> "$APP_DIR/conf/mirth.properties" |
| 153 | + |
| 154 | + while read -r keyvalue; do |
| 155 | + KEY="${keyvalue%%=*}" |
| 156 | + VALUE="${keyvalue#*=}" |
| 157 | + |
| 158 | + # remove leading and trailing white space |
| 159 | + KEY="$(echo -e "${KEY}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" |
| 160 | + VALUE="$(echo -e "${VALUE}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')" |
| 161 | + |
| 162 | + if ! [ -z "${KEY}" ] && ! [ -z "${VALUE}" ] && ! [[ ${VALUE} =~ ^\ +$ ]]; then |
| 163 | + # if key does not exist in mirth.properties append it at bottom |
| 164 | + LINE_COUNT=`grep "^${KEY}" "$APP_DIR/conf/mirth.properties" | wc -l` |
| 165 | + if [ $LINE_COUNT -lt 1 ]; then |
| 166 | + # echo "key ${KEY} not found in mirth.properties, appending. Value = ${VALUE}" |
| 167 | + echo -e "${KEY} = ${VALUE//\//\\/}" >> "$APP_DIR/conf/mirth.properties" |
| 168 | + else # otherwise key exists, overwrite it |
| 169 | + # echo "key ${KEY} exists, overwriting. Value = ${VALUE}" |
| 170 | + ESCAPED_KEY="${KEY//./\\.}" |
| 171 | + sed -i "s/^${ESCAPED_KEY}\s*=\s*.*\$/${KEY} = ${VALUE//\//\\/}/" "$APP_DIR/conf/mirth.properties" |
| 172 | + fi |
| 173 | + fi |
| 174 | + done <<< "`cat /run/secrets/mirth_properties`" |
| 175 | +fi |
| 176 | + |
| 177 | +# merge the user's secret vmoptions |
| 178 | +# takes a whole oieserver.vmoptions file and merges line by line with /opt/engine/oieserver.vmoptions |
| 179 | +if [ -f /run/secrets/oieserver_vmoptions ]; then |
| 180 | + (cat /run/secrets/oieserver_vmoptions ; echo "") >> "$APP_DIR/oieserver.vmoptions" |
| 181 | +fi |
| 182 | + |
| 183 | +# download jars from this url "$CUSTOM_JARS_DOWNLOAD", set by user |
| 184 | +if ! [ -z "${CUSTOM_JARS_DOWNLOAD+x}" ]; then |
| 185 | + echo "Downloading Jars at ${CUSTOM_JARS_DOWNLOAD}" |
| 186 | + if ! [ -z "${ALLOW_INSECURE}" ] && [ "${ALLOW_INSECURE}" == "true" ]; then |
| 187 | + curl -ksSLf "${CUSTOM_JARS_DOWNLOAD}" -o userJars.zip || echo "problem with custom jars download" |
| 188 | + else |
| 189 | + curl -sSLf "${CUSTOM_JARS_DOWNLOAD}" -o userJars.zip || echo "problem with custom jars download" |
| 190 | + fi |
| 191 | + |
| 192 | + # Unzipping contents of userJars.zip into /opt/engine/server-launcher-lib folder |
| 193 | + if [ -e "userJars.zip" ]; then |
| 194 | + echo "Unzipping contents of userJars.zip into $APP_DIR/server-launcher-lib" |
| 195 | + unzip userJars.zip -d "$APP_DIR/server-launcher-lib" |
| 196 | + # removing the downloaded zip file |
| 197 | + rm userJars.zip |
| 198 | + fi |
| 199 | +fi |
| 200 | + |
| 201 | +# download extensions from this url "$EXTENSIONS_DOWNLOAD", set by user |
| 202 | +if ! [ -z "${EXTENSIONS_DOWNLOAD+x}" ]; then |
| 203 | + echo "Downloading extensions at ${EXTENSIONS_DOWNLOAD}" |
| 204 | + if ! [ -z "${ALLOW_INSECURE}" ] && [ "${ALLOW_INSECURE}" == "true" ]; then |
| 205 | + curl -ksSLf "${EXTENSIONS_DOWNLOAD}" -o userExtensions.zip || echo "problem with extensions download" |
| 206 | + else |
| 207 | + curl -sSLf "${EXTENSIONS_DOWNLOAD}" -o userExtensions.zip || echo "problem with extensions download" |
| 208 | + fi |
| 209 | + |
| 210 | + # Unzipping contents of userExtensions.zip |
| 211 | + if [ -e "userExtensions.zip" ]; then |
| 212 | + echo "Unzipping contents of userExtensions.zip" |
| 213 | + mkdir /tmp/userextensions |
| 214 | + unzip userExtensions.zip -d /tmp/userextensions |
| 215 | + # removing the downloaded zip file |
| 216 | + rm userExtensions.zip |
| 217 | + |
| 218 | + # Unzipping contents of individual extension zip files into /opt/engine/extensions folder |
| 219 | + zipFileCount=`ls -1 /tmp/userextensions/*.zip 2>/dev/null | wc -l` |
| 220 | + if [ $zipFileCount != 0 ]; then |
| 221 | + echo "Unzipping contents of /tmp/userextensions/ zips into $APP_DIR/extensions" |
| 222 | + for f in /tmp/userextensions/*.zip; do unzip "$f" -d "$APP_DIR/extensions"; done |
| 223 | + fi |
| 224 | + # removing the tmp folder |
| 225 | + rm -rf /tmp/userextensions |
| 226 | + fi |
| 227 | +fi |
| 228 | + |
| 229 | +# download keystore |
| 230 | +if ! [ -z "${KEYSTORE_DOWNLOAD+x}" ]; then |
| 231 | + echo "Downloading keystore at ${KEYSTORE_DOWNLOAD}" |
| 232 | + if ! [ -z "${ALLOW_INSECURE}" ] && [ "${ALLOW_INSECURE}" == "true" ]; then |
| 233 | + curl -ksSLf "${KEYSTORE_DOWNLOAD}" -o "$APP_DIR/appdata/keystore.jks" || echo "problem with keystore download" |
| 234 | + else |
| 235 | + curl -sSLf "${KEYSTORE_DOWNLOAD}" -o "$APP_DIR/appdata/keystore.jks" || echo "problem with keystore download" |
| 236 | + fi |
| 237 | +fi |
| 238 | + |
| 239 | +# if delay is set as an environment variable then wait that long in seconds |
| 240 | +if ! [ -z "${DELAY+x}" ]; then |
| 241 | + sleep $DELAY |
| 242 | +fi |
| 243 | + |
| 244 | +# if there are any arguments, invoke them as a command |
| 245 | +if [ $# -ne 0 ]; then |
| 246 | + exec "$@" |
| 247 | +fi |
0 commit comments