~ibm-demo/charms/trusty/memcached/trunk

« back to all changes in this revision

Viewing changes to formulas/mysql/hooks/db-relation-changed

  • Committer: Clint Byrum
  • Date: 2011-04-18 18:20:26 UTC
  • mfrom: (35.1.7 update-hook-semantics)
  • Revision ID: clint@ubuntu.com-20110418182026-9qey2mhsy9csg9db
committing hook updates

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
 
3
 
import os
4
 
import random
5
 
import string
6
 
import subprocess
7
 
import urllib
8
 
 
9
 
import MySQLdb
10
 
 
11
 
change_type = os.environ.get("ENSEMBLE_CHANGE")
12
 
 
13
 
change_unit = os.environ.get("ENSEMBLE_REMOTE_UNIT")
14
 
# We'll name the database the same as the service.
15
 
database_name, _ = change_unit.split("/")
16
 
# A user per service unit so we can deny access quickly
17
 
user = change_unit.replace("/","-")
18
 
connection = None
19
 
 
20
 
def get_db_cursor():
21
 
    # Connect to mysql
22
 
    passwd = open("/var/lib/ensemble/mysql.passwd").read().strip()
23
 
    print passwd
24
 
    connection = MySQLdb.connect(user="root", host="localhost", passwd=passwd)
25
 
 
26
 
    return connection.cursor()
27
 
 
28
 
def on_join():
29
 
 
30
 
    cursor = get_db_cursor()
31
 
 
32
 
    # Find existing databases
33
 
    cursor.execute("show databases")
34
 
    databases = [i[0] for i in cursor.fetchall()]
35
 
 
36
 
    # Determine if we need to create a new database
37
 
    if database_name not in databases:
38
 
      # Create new database
39
 
      cursor.execute(
40
 
          "create database `%s` character set utf8" % database_name)
41
 
 
42
 
    # Create database user and grant access
43
 
    service_password = "".join(random.sample(string.letters, 10))
44
 
    cursor.execute(
45
 
        "grant all on `%s`.* to `%s` identified by '%s'" % (
46
 
            database_name,
47
 
            user,
48
 
            service_password))
49
 
 
50
 
    cursor.execute("flush privileges")
51
 
 
52
 
    cursor.close()
53
 
 
54
 
    hostname = urllib.urlopen(
55
 
        "http://169.254.169.254/latest/meta-data/local-hostname").read()
56
 
 
57
 
    print "setting values"
58
 
    print "host", hostname
59
 
    print "database", database_name
60
 
    print "user", user
61
 
    print "password", service_password
62
 
 
63
 
 
64
 
    print str(["relation-set",
65
 
     "database=%s" % database_name,
66
 
     "user=%s" % user,
67
 
     "password=%s" % service_password,
68
 
     'host=%s' % hostname,])
69
 
 
70
 
    # Store new values in relation settings.
71
 
    p = subprocess.Popen(
72
 
        ["relation-set",
73
 
         "database=%s" % database_name,
74
 
         "user=%s" % user,
75
 
         "password=%s" % service_password,
76
 
         'host=%s' % hostname,],
77
 
        close_fds = True)
78
 
 
79
 
    os.waitpid(p.pid, 0)
80
 
 
81
 
def on_depart():
82
 
    cursor = get_db_cursor()
83
 
    cursor.execute("revoke all privileges on `%s`.* from `%s`" % (database_name, user))
84
 
    print "revoked privileges for `%s` on database `%s`" % (user, database_name)
85
 
 
86
 
if change_type == "joined":
87
 
    on_join()
88
 
elif change_type == "departed":
89
 
    on_departed()
90
 
 
91
 
if connection is not None:
92
 
  connection.close()
 
1
#!/bin/bash
 
2
echo "Nothing to do on modified"
 
3
relation-get