#!/bin/bash
set -ex
source charms.reactive.sh
@when 'mysql.available'
@when_not 'mysql-config.done'
function config_db() {
juju-log "IBM PAC: Logging into mysql console."
my_host=`relation_call --state 'mysql.available' 'host'`
my_pwd=`relation_call --state 'mysql.available' 'password'`
#my_user=`relation_call --state 'mysql.available' 'username'`
#my_port=`relation_call --state 'mysql.available' 'port'`
set +e
result=`mysqlshow -h ${my_host} -u root -p${my_pwd} pac | grep -o pac`
set -e
if [ "$result" == "pac" ]; then
juju-log "IBM PAC Helper: pac database already exists. connecting to pac database."
else
mysql -h ${my_host} -u root -p${my_pwd} << EOF
create database pac default character set utf8 default collate utf8_bin;
CREATE USER 'pacuser'@'%' IDENTIFIED BY 'pacuser';
GRANT ALL PRIVILEGES ON pac.* to pacuser@'%' IDENTIFIED BY 'pacuser';
GRANT ALL PRIVILEGES ON pac.* to pacuser@'localhost' IDENTIFIED BY 'pacuser';
EOF
set_state 'mysql-config.done'
status-set active "Ready"
fi
}
reactive_handler_main