~justin-fathomdb/nova/justinsb-openstack-api-volumes

« back to all changes in this revision

Viewing changes to vendor/boto/boto/pyami/installers/ubuntu/mysql.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/
 
2
#
 
3
# Permission is hereby granted, free of charge, to any person obtaining a
 
4
# copy of this software and associated documentation files (the
 
5
# "Software"), to deal in the Software without restriction, including
 
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
 
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
 
8
# persons to whom the Software is furnished to do so, subject to the fol-
 
9
# lowing conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be included
 
12
# in all copies or substantial portions of the Software.
 
13
#
 
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
20
# IN THE SOFTWARE.
 
21
#
 
22
"""
 
23
This installer will install mysql-server on an Ubuntu machine.
 
24
In addition to the normal installation done by apt-get, it will
 
25
also configure the new MySQL server to store it's data files in
 
26
a different location.  By default, this is /mnt but that can be
 
27
configured in the [MySQL] section of the boto config file passed
 
28
to the instance.
 
29
"""
 
30
from boto.pyami.installers.ubuntu.installer import Installer
 
31
import os
 
32
import boto
 
33
from boto.utils import ShellCommand
 
34
from ConfigParser import SafeConfigParser
 
35
import time
 
36
 
 
37
ConfigSection = """
 
38
[MySQL]
 
39
root_password = <will be used as MySQL root password, default none>
 
40
data_dir = <new data dir for MySQL, default is /mnt>
 
41
"""
 
42
 
 
43
class MySQL(Installer):
 
44
 
 
45
    def install(self):
 
46
        self.run('apt-get update')
 
47
        self.run('apt-get -y install mysql-server', notify=True, exit_on_error=True)
 
48
 
 
49
#    def set_root_password(self, password=None):
 
50
#        if not password:
 
51
#            password = boto.config.get('MySQL', 'root_password')
 
52
#        if password:
 
53
#            self.run('mysqladmin -u root password %s' % password)
 
54
#        return password
 
55
 
 
56
    def change_data_dir(self, password=None):
 
57
        data_dir = boto.config.get('MySQL', 'data_dir', '/mnt')
 
58
        fresh_install = False;
 
59
        is_mysql_running_command = ShellCommand('mysqladmin ping') # exit status 0 if mysql is running
 
60
        is_mysql_running_command.run()
 
61
        if is_mysql_running_command.getStatus() == 0:
 
62
            # mysql is running. This is the state apt-get will leave it in. If it isn't running, 
 
63
            # that means mysql was already installed on the AMI and there's no need to stop it,
 
64
            # saving 40 seconds on instance startup.
 
65
            time.sleep(10) #trying to stop mysql immediately after installing it fails
 
66
            # We need to wait until mysql creates the root account before we kill it
 
67
            # or bad things will happen
 
68
            i = 0
 
69
            while self.run("echo 'quit' | mysql -u root") != 0 and i<5:
 
70
                time.sleep(5)
 
71
                i = i + 1
 
72
            self.run('/etc/init.d/mysql stop')
 
73
            self.run("pkill -9 mysql")
 
74
 
 
75
        mysql_path = os.path.join(data_dir, 'mysql')
 
76
        if not os.path.exists(mysql_path):
 
77
            self.run('mkdir %s' % mysql_path)
 
78
            fresh_install = True;
 
79
        self.run('chown -R mysql:mysql %s' % mysql_path)
 
80
        fp = open('/etc/mysql/conf.d/use_mnt.cnf', 'w')
 
81
        fp.write('# created by pyami\n')
 
82
        fp.write('# use the %s volume for data\n' % data_dir)
 
83
        fp.write('[mysqld]\n')
 
84
        fp.write('datadir = %s\n' % mysql_path)
 
85
        fp.write('log_bin = %s\n' % os.path.join(mysql_path, 'mysql-bin.log'))
 
86
        fp.close()
 
87
        if fresh_install:
 
88
            self.run('cp -pr /var/lib/mysql/* %s/' % mysql_path)
 
89
            self.start('mysql')
 
90
        else:
 
91
            #get the password ubuntu expects to use:
 
92
            config_parser = SafeConfigParser()
 
93
            config_parser.read('/etc/mysql/debian.cnf')
 
94
            password = config_parser.get('client', 'password')
 
95
            # start the mysql deamon, then mysql with the required grant statement piped into it:
 
96
            self.start('mysql')
 
97
            time.sleep(10) #time for mysql to start
 
98
            grant_command = "echo \"GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '%s' WITH GRANT OPTION;\" | mysql" % password
 
99
            while self.run(grant_command) != 0:
 
100
                time.sleep(5)
 
101
            # leave mysqld running
 
102
 
 
103
    def main(self):
 
104
        self.install()
 
105
        # change_data_dir runs 'mysql -u root' which assumes there is no mysql password, i
 
106
        # and changing that is too ugly to be worth it:
 
107
        #self.set_root_password()
 
108
        self.change_data_dir()
 
109