~george-edison55/charms/oneiric/thinkup/update-to-1-0-2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/bin/bash

set -e

# Retrieve this machine's hostname
HOSTNAME=`unit-get public-address`
juju-log "Hostname: $HOSTNAME"

# Retrieve the settings from the database
juju-log "Retrieving database relationship"
DATABASE=`relation-get database`

if [ -z "$DATABASE" ] ; then
    juju-log "No database relationship set"
    exit 0
fi

# Fetch the database configuration
juju-log "Retrieving database configuration"
DB_USER=`relation-get user`
DB_PASS=`relation-get password`
DB_HOST=`relation-get private-address`

# Update the data in the configuration file
juju-log "Updating configuration file"
update_thinkup_config <<EOD
db_host='$DB_HOST'
db_user='$DB_USER'
db_password='$DB_PASS'
db_name='$DATABASE'
EOD

# We only want the SQL tables entered into the database _once_ and
# after that the step needs to be skipped. So we create a file in
# /opt the first time and check for it in later iterations.
if [ ! -f /opt/created_database_tables ] ; then
    
    # Execute the SQL that creates the tables
    juju-log "Creating database tables and finalizing configuration"
    cat > /tmp/enable_open_registration.sql <<EOD
INSERT INTO $DATABASE.tu_options (namespace, option_name, option_value, last_updated, created) VALUES ('application_options', 'is_registration_open', 'true', NOW(), NOW());
UPDATE $DATABASE.tu_plugins SET is_active = 1;
EOD
    cat /var/www/install/sql/build-db_mysql.sql /tmp/enable_open_registration.sql | mysql -u$DB_USER -p$DB_PASS -h$DB_HOST $DATABASE
    
    # Create the file signaling that the tables have been created
    # (Added bonus: if there was an error inserting the tables, this
    # line will not be reached and will be retried next time!)
    echo "" >/opt/created_database_tables
fi