#!/bin/bash
# This is the database-relation-joined hook for the SpagoBI Charm.
# The MySQL database and username are created for you.
# If any command fails, stop execution of the hook with that error
set -e
# Show the commands that are getting executed.
set -x
status-set waiting "waiting for database"
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.
db_user=`relation-get user`
db_db=`relation-get database`
db_pass=`relation-get password`
db_host=`relation-get private-address`
spagobi_dir="/home/ubuntu/SpagoBI"
if [ -z "${db_user}" ]; then
juju-log "No database user information sent yet. Silently exiting"
exit 0
fi
juju-log "db_user=${db_user} db_pass=${db_pass} db_host=${db_host}"
#selects spagobi tables from database if any exists
Result=`mysql -h${db_host} -P3306 -u${db_user} -p${db_pass} ${db_db} -e "SHOW TABLES LIKE '%SBI_%';"`
if [ -z "$Result" ]; then
#creates tables for spagobi database
juju-log "Creating and initializing spagobi database."
mysql -h${db_host} -P3306 -u${db_user} -p${db_pass} ${db_db} --execute="source $spagobi_dir/scripts/MySQL_create.sql"
mysql -h${db_host} -P3306 -u${db_user} -p${db_pass} ${db_db} --execute="source $spagobi_dir/scripts/MySQL_create_quartz_schema.sql"
mysql -h${db_host} -P3306 -u${db_user} -p${db_pass} ${db_db} --execute="source $spagobi_dir/scripts/MySQL_create_social.sql"
mysql -h${db_host} -P3306 -u${db_user} -p${db_pass} ${db_db} --execute="INSERT INTO hibernate_sequences(SEQUENCE_NAME,NEXT_VAL) VALUES('SBI_DATA_SOURCE',1);"
juju-log "$CHARM_NAME updated database schema successfully."
fi
db_resource=''
# Shut down Tomcat.
$CHARM_DIR/hooks/stop
#sets the resource information into tomcat's server.xml file
#sets the environment variables into tomcat server.xml file
public_address=`unit-get public-address`
spagobi_host=''
spagobi_service=''
spagobi_resources=''
sed -i "/ /a \ ${spagobi_host}" /etc/${container}/server.xml
sed -i "/ /a \ ${spagobi_service}" /etc/${container}/server.xml
sed -i '/ /a \ ' /etc/${container}/server.xml
sed -i "/ /a \ ${spagobi_resources}" /etc/${container}/server.xml
sed -i "/ /a \ ${db_resource}" /etc/${container}/server.xml
# To achieve idempotency, delete the previous application folders if any already exists.
if [[ -d /var/lib/${container}/webapps/SpagoBI ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBI
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIAccessibilityEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIAccessibilityEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIBirtReportEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIBirtReportEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIChartEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIChartEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBICockpitEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBICockpitEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBICommonJEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBICommonJEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIConsoleEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIConsoleEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIDataMiningEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIDataMiningEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIGeoEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIGeoEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIGeoReportEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIGeoReportEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIJasperReportEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIJasperReportEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIJPivotEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIJPivotEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIMobileEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIMobileEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBINetworkEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBINetworkEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIQbeEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIQbeEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBISocialAnalysis ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBISocialAnalysis
fi
if [[ -d /var/lib/${container}/webapps/SpagoBITalendEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBITalendEngine
fi
if [[ -d /var/lib/${container}/webapps/SpagoBIWhatIfEngine ]]; then
rm -rf /var/lib/${container}/webapps/SpagoBIWhatIfEngine
fi
# Copy the Web applications to the Tomcat webapps directory.
juju-log "Copying SpagoBI webapps to /var/lib/${container}/webapps/"
cp -r /home/ubuntu/SpagoBI/apps/. /var/lib/${container}/webapps/
# Restart Tomcat.
$CHARM_DIR/hooks/start
juju-log "$CHARM_NAME Database relation setup and ready."
status-set active