#!/bin/bash # This is the database-relation-changed hook for the OpenMRS Charm. # If any command fails, stop execution of the hook with that error set -e # Show the commands that are getting executed. set -x container=tomcat7 if [[ -e /var/lib/tomcat7 ]]; then container=tomcat7 elif [[ -e /var/lib/tomcat6 ]]; then container=tomcat6 fi # Get the database values from the relation change. db_user=`relation-get user` db_db=`relation-get database` db_pass=`relation-get password` db_host=`relation-get private-address` juju-log "db_user=${db_user} db_pass=${db_pass} db_db=${db_db} db_host=${db_host}" if [ -z "${db_db}" ]; then juju-log "No database information sent yet. Silently exiting" exit 0 fi # Shut down Tomcat. $CHARM_DIR/hooks/stop openmrs_directory="/usr/share/${container}/.OpenMRS" # Copy the Web application ARchive (WAR) to the Tomcat webapps directory. juju-log "Copying ${openmrs_directory}/openmrs.war to /var/lib/${container}/webapps/" cp ${openmrs_directory}/openmrs.war /var/lib/${container}/webapps/ # Construct the database URL for OpenMRS. db_url=jdbc:mysql://${db_host}:3306/${db_db}?autoReconnect=true # Runtime properties file path is: /var/lib/tomcat7/openmrs-runtime.properties # OR: /usr/share/tomcat7/.OpenMRS/openmrs-runtime.properties openmrs_config="${openmrs_directory}/openmrs-runtime.properties" if [[ -f ${openmrs_config} ]]; then # Need to escape the slashes in the URL to use the sed command. db_url=jdbc:mysql:\\/\\/${db_host}:3306\\/${db_db}?autoReconnect=true # Edit the existing properties file with new database values. sed -i "s/connection.url=.*$/connection.url=\\${db_url}/" ${openmrs_config} sed -i "s/connection.username=.*$/connection.username=\\${db_user}/" ${openmrs_config} sed -i "s/connection.password=.*$/connection.password=\\${db_pass}/" ${openmrs_config} else if [[ ! -d ${openmrs_directory} ]]; then # Create the OpenMRS configuration directory. mkdir -p ${openmrs_directory} chmod 777 ${openmrs_directory} fi # Create the OpenMRS runtime properties file. cat << EOF > ${openmrs_config} # OpenMRS Runtime Properties file. connection.username=${db_user} connection.password=${db_pass} connection.url=${db_url} EOF chmod 666 ${openmrs_config} fi # Restart Tomcat. $CHARM_DIR/hooks/start juju-log "$CHARM_NAME Database relation values updated successfully."