~openstack-ubuntu-testing/charms/precise/mysql/trunk

« back to all changes in this revision

Viewing changes to hooks/shared-db-common

  • Committer: Adam Gandelman
  • Date: 2012-12-03 20:21:07 UTC
  • mfrom: (90.1.1 mysql)
  • Revision ID: adamg@canonical.com-20121203202107-j0w08n54jk1h9hgy
Merge James Page's multi-user/db support and Python rewrite of shared-db.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/bash
2
 
#
3
 
# Common utility functions used for hooks in shared-db-relations
4
 
#
5
 
# Notes:
6
 
#   - /var/lib/juju/mysql.passwd is created during install hook
7
 
#
8
 
# Author: Adam Gandelman <adam.gandelman@canonical.com>
9
 
 
10
 
DEFAULT_ETH=$(ip route  | grep default | awk '{ print $5 }')
11
 
 
12
 
database_exists() {
13
 
  mysql -u root -p$(cat /var/lib/juju/mysql.passwd) \
14
 
    -NBe "SHOW DATABASES" | grep $DATABASE >/dev/null
15
 
}
16
 
 
17
 
create_database() {
18
 
  juju-log "mysql - shared-db: Creating database $DATABASE"
19
 
  mysql -u root -p$(cat /var/lib/juju/mysql.passwd) \
20
 
    -NBe "CREATE DATABASE $DATABASE" >/dev/null
21
 
}
22
 
 
23
 
grant_exists() {
24
 
  [[ $(mysql -u root -p$(cat /var/lib/juju/mysql.passwd) \
25
 
    -NBe "SELECT User, Host FROM user \
26
 
            WHERE User='$DB_USER' AND Host='$REMOTE_IP'" mysql | wc -l) -gt 0 ]]
27
 
}
28
 
 
29
 
create_grant() {
30
 
  juju-log "mysql - shared-db: Creating grant for $DB_USER@$REMOTE_IP"
31
 
  mysql -u root -p$(cat /var/lib/juju/mysql.passwd) \
32
 
    -NBe "GRANT ALL PRIVILEGES ON $DATABASE.* TO '$DB_USER'@'$REMOTE_IP' \
33
 
          IDENTIFIED BY '$PASSWORD'"
34
 
}
35
 
 
36
 
cleanup_grant() {
37
 
  juju-log "mysql - shared-db: Cleaning up grant for $DB_USER@$REMOTE_IP"
38
 
  mysql -u root -p$(cat /var/lib/juju/mysql.passwd) \
39
 
    -e "DROP FROM user WHERE user='$DB_USER' AND HOST='$REMOTE_IP'" mysql
40
 
}
41