Skip to content

Commit

Permalink
Merge pull request #26 from jamf/allow-database-overrides
Browse files Browse the repository at this point in the history
Allow database overrides via ENV / config mount
  • Loading branch information
battlecow authored Jul 6, 2023
2 parents d19ae93 + 1bd5cb4 commit 1f49d44
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 36 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ DATABASE_NAME [ jamfsoftware ]
DATABASE_USERNAME [ jamfsoftware ]
DATABASE_PASSWORD [ jamfsw03 ]
DATABASE_PORT [ 3306 ]
JDBC_PARAMETERS [ ]
JMXREMOTE true / [ false ]
JMXREMOTE_PORT
Expand Down Expand Up @@ -77,6 +78,9 @@ tomcat_host_stuck_threads_threshold - 600
This image requires that either a `/data/ROOT.war` be bind-mounted and exist, or the `/usr/bin/tomcat/webapps/ROOT` directory exist.
A ROOT.war will be auto-unpacked and configured based upon the above environment variables, or if the ROOT directory already exists, nothing will be unpacked but logging paths, database information, JMX, and Java opts will be set.

## File Override Config
The `DataBase.xml` file can be optionally mounted into the container's `/config` directory, this will override any environment variables set.

## Example
Run a basic JamfPro instance with port 8080 exposed locally on port 8080, setup remote database, bind-mounted ROOT.war, and bind-mounted webapps directory.

Expand Down
83 changes: 47 additions & 36 deletions configuration.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash -e

WEBAPPS_DIR="/usr/local/tomcat/webapps"

##########################################################
####################### Functions ########################
echo_time() {
Expand All @@ -13,17 +15,17 @@ unpack_root_war() {
exit 1
fi
#Unpack the warfile
echo_time "Unpacking ROOT.war to /usr/local/tomcat/webapps/ROOT \n"
unzip -q /data/ROOT.war -d /usr/local/tomcat/webapps/ROOT
echo_time "Unpacking ROOT.war to ${WEBAPPS_DIR}/ROOT \n"
unzip -q /data/ROOT.war -d ${WEBAPPS_DIR}/ROOT
}

setup_linux_logging_paths() {
#Replace Mac logging paths with linux based paths
echo_time "Set logging file paths to use linux file paths"
CONFIG_FILES=(
"/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j.properties"
"/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j2.xml"
"/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/ESAPI.properties"
"${WEBAPPS_DIR}/ROOT/WEB-INF/classes/log4j.properties"
"${WEBAPPS_DIR}/ROOT/WEB-INF/classes/log4j2.xml"
"${WEBAPPS_DIR}/ROOT/WEB-INF/classes/ESAPI.properties"
)
for config in "${CONFIG_FILES[@]}"; do
if test -f "${config}"; then
Expand All @@ -38,54 +40,63 @@ setup_stdout_logging() {
#Add stdout output for Jamf specific log files while maintaining logging to the files
echo_time "STDOUT_LOGGING is true, add stdout logging for all logfiles"
#Check whether log4j.properties (<= Jamf Pro 10.30.3) or log4j2.xml (>= Jamf Pro 10.31.0) exists
FILE=/usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j.properties
FILE=${WEBAPPS_DIR}/ROOT/WEB-INF/classes/log4j.properties
if test -f "$FILE"; then
echo_time "Found log4j.properties, enable logging to stdout"
if grep -Fxq "log4j.rootLogger=INFO,JAMF,stdout" /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j.properties; then
if grep -Fxq "log4j.rootLogger=INFO,JAMF,stdout" ${WEBAPPS_DIR}/ROOT/WEB-INF/classes/log4j.properties; then
echo_time "stdout logging appears to be present in log4j.properties file, skipping"
else
echo_time "Add stdout logging to log4j.properties file"
sed -e '/log4j.rootLogger/ {r /log4j.stdout.replace
d}' -i /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j.properties
d}' -i ${WEBAPPS_DIR}/ROOT/WEB-INF/classes/log4j.properties
fi
else
echo_time "Found log4j2.xml, enable logging to stdout"
if grep -Fxq ' <Console name="Console" target="SYSTEM_OUT">' /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j2.xml; then
if grep -Fxq ' <Console name="Console" target="SYSTEM_OUT">' ${WEBAPPS_DIR}/ROOT/WEB-INF/classes/log4j2.xml; then
echo_time "stdout logging appears to be present in log4j2.xml file, skipping"
else
echo_time "Add stdout logging to log4j2.xml file"
sed -e '/<Appenders>/ {r /log4j2.stdout.appenders.replace
d}' -i /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j2.xml
d}' -i ${WEBAPPS_DIR}/ROOT/WEB-INF/classes/log4j2.xml
sed -e '/<Root level="info" includeLocation="false">/ {r /log4j2.stdout.loggers.root.replace
d}' -i /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j2.xml
d}' -i ${WEBAPPS_DIR}/ROOT/WEB-INF/classes/log4j2.xml
sed -e '/<Logger name="com.jamfsoftware.analytics.file" level="info" additivity="false" includeLocation="false">/ {r /log4j2.stdout.loggers.analytics.replace
d}' -i /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j2.xml
d}' -i ${WEBAPPS_DIR}/ROOT/WEB-INF/classes/log4j2.xml
sed -e '/<AppenderRef ref="JAMFVPP"\/>/ {r /log4j2.stdout.loggers.vpp.replace
d}' -i /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/log4j2.xml
d}' -i ${WEBAPPS_DIR}/ROOT/WEB-INF/classes/log4j2.xml
fi
fi
fi
}

setup_remote_database() {
DATABASE_HOST=${DATABASE_HOST:-localhost}
DATABASE_NAME=${DATABASE_NAME:-jamfsoftware}
DATABASE_USERNAME=${DATABASE_USERNAME:-jamfsoftware}
DATABASE_PASSWORD=${DATABASE_PASSWORD:-jamfsw03}
DATABASE_PORT=${DATABASE_PORT:-3306}

echo_time "\n\nDatabase connection information: \n DATABASE_HOST: ${DATABASE_HOST} \n DATABASE_NAME: ${DATABASE_NAME} \n DATABASE_USERNAME: ${DATABASE_USERNAME}\n\n"

echo_time "Setting up the DataBase.xml file to use remote MySQL database"
if [ ! -f "/usr/local/tomcat/webapps/ROOT/WEB-INF/xml/DataBase.xml" ]; then
echo_time "FATAL ERROR: DataBase.xml not where expected, cannot continue"
exit 1
if [[ -f "/config/DataBase.xml" ]]; then
echo_time "Database xml config override found, ignoring environment variables"
cp /config/DataBase.xml ${WEBAPPS_DIR}/ROOT/WEB-INF/xml/DataBase.xml
else
sed -i s#\<ServerName.*#\<ServerName\>$DATABASE_HOST\</ServerName\># /usr/local/tomcat/webapps/ROOT/WEB-INF/xml/DataBase.xml
sed -i s#\<DataBaseName.*#\<DataBaseName\>$DATABASE_NAME\</DataBaseName\># /usr/local/tomcat/webapps/ROOT/WEB-INF/xml/DataBase.xml
sed -i s#\<DataBaseUser.*#\<DataBaseUser\>$DATABASE_USERNAME\</DataBaseUser\># /usr/local/tomcat/webapps/ROOT/WEB-INF/xml/DataBase.xml
sed -i s#\<DataBasePassword.*#\<DataBasePassword\>$DATABASE_PASSWORD\</DataBasePassword\># /usr/local/tomcat/webapps/ROOT/WEB-INF/xml/DataBase.xml
sed -i s#\<ServerPort.*#\<ServerPort\>$DATABASE_PORT\</ServerPort\># /usr/local/tomcat/webapps/ROOT/WEB-INF/xml/DataBase.xml
DATABASE_HOST=${DATABASE_HOST:-localhost}
DATABASE_NAME=${DATABASE_NAME:-jamfsoftware}
DATABASE_USERNAME=${DATABASE_USERNAME:-jamfsoftware}
DATABASE_PASSWORD=${DATABASE_PASSWORD:-jamfsw03}
DATABASE_PORT=${DATABASE_PORT:-3306}

echo_time "\n\nDatabase connection information: \n DATABASE_HOST: ${DATABASE_HOST} \n DATABASE_NAME: ${DATABASE_NAME} \n DATABASE_USERNAME: ${DATABASE_USERNAME}\n\n"

echo_time "Setting up the DataBase.xml file to use remote MySQL database"
if [ ! -f "${WEBAPPS_DIR}/ROOT/WEB-INF/xml/DataBase.xml" ]; then
echo_time "FATAL ERROR: DataBase.xml not where expected, cannot continue"
exit 1
else
sed -i s#\<ServerName.*#\<ServerName\>$DATABASE_HOST\</ServerName\># ${WEBAPPS_DIR}/ROOT/WEB-INF/xml/DataBase.xml
sed -i s#\<DataBaseName.*#\<DataBaseName\>$DATABASE_NAME\</DataBaseName\># ${WEBAPPS_DIR}/ROOT/WEB-INF/xml/DataBase.xml
sed -i s#\<DataBaseUser.*#\<DataBaseUser\>$DATABASE_USERNAME\</DataBaseUser\># ${WEBAPPS_DIR}/ROOT/WEB-INF/xml/DataBase.xml
sed -i s#\<DataBasePassword.*#\<DataBasePassword\>$DATABASE_PASSWORD\</DataBasePassword\># ${WEBAPPS_DIR}/ROOT/WEB-INF/xml/DataBase.xml
sed -i s#\<ServerPort.*#\<ServerPort\>$DATABASE_PORT\</ServerPort\># ${WEBAPPS_DIR}/ROOT/WEB-INF/xml/DataBase.xml
if [[ -n "${JDBC_PARAMETERS}" ]]; then
echo_time "JDBC environment variable is set, replacing JDBC database config"
sed -i s#\<jdbcParameters.*#\<jdbcParameters\>${JDBC_PARAMETERS}\</jdbcParameters\># ${WEBAPPS_DIR}/ROOT/WEB-INF/xml/DataBase.xml
fi
fi
fi
}

Expand Down Expand Up @@ -127,14 +138,14 @@ setup_java_opts() {

create_cache_properties(){
echo_time "Setting up the cache.properties to be memcached"
cat <<-EOF > /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/dal/cache.properties
cat <<-EOF > ${WEBAPPS_DIR}/ROOT/WEB-INF/classes/dal/cache.properties
cache.type=memcached
EOF
}

create_memcached_properties(){
echo_time "Setting up the memcached.properties"
cat <<-EOF > /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/dal/memcached.properties
cat <<-EOF > ${WEBAPPS_DIR}/ROOT/WEB-INF/classes/dal/memcached.properties
memcached.endpoints[0]=$MEMCACHED_HOST
memcached.timeToLiveSeconds=120
EOF
Expand All @@ -152,7 +163,7 @@ tomcatServerXML() {
##########################################################
create_cluster_properties() {
echo_time "Creating the clustering properties file"
cat <<- EOF > /usr/local/tomcat/webapps/ROOT/WEB-INF/classes/clustering.properties
cat <<- EOF > ${WEBAPPS_DIR}/ROOT/WEB-INF/classes/clustering.properties
cluster.settings.enabled=true
cluster.settings.monitor_frequency=60
cluster.node[0]=$1
Expand All @@ -164,12 +175,12 @@ EOF
####################### Executions #######################

echo_time "Check if Tomcat ROOT directory exists, will NOT overwrite if exists"
if [ ! -d /usr/local/tomcat/webapps/ROOT ]; then
echo_time "/usr/local/tomcat/webapps/ROOT directory does not exist, attempt to deploy ROOT.war from /data"
if [ ! -d ${WEBAPPS_DIR}/ROOT ]; then
echo_time "${WEBAPPS_DIR}/ROOT directory does not exist, attempt to deploy ROOT.war from /data"
unpack_root_war

else
echo_time "/usr/local/tomcat/webapps/ROOT exists, skipping ROOT.war deploy"
echo_time "${WEBAPPS_DIR}/ROOT exists, skipping ROOT.war deploy"
fi

# Check to see if clustering should be enabled by existence of PRIMARY_NODE_NAME
Expand Down

0 comments on commit 1f49d44

Please sign in to comment.