1
# Copyright (c) 2006-2009 Mitch Garnaat http://garnaat.org/
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-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
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
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
30
from boto.pyami.installers.ubuntu.installer import Installer
33
from boto.utils import ShellCommand
34
from ConfigParser import SafeConfigParser
39
root_password = <will be used as MySQL root password, default none>
40
data_dir = <new data dir for MySQL, default is /mnt>
43
class MySQL(Installer):
46
self.run('apt-get update')
47
self.run('apt-get -y install mysql-server', notify=True, exit_on_error=True)
49
# def set_root_password(self, password=None):
51
# password = boto.config.get('MySQL', 'root_password')
53
# self.run('mysqladmin -u root password %s' % password)
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
69
while self.run("echo 'quit' | mysql -u root") != 0 and i<5:
72
self.run('/etc/init.d/mysql stop')
73
self.run("pkill -9 mysql")
75
mysql_path = os.path.join(data_dir, 'mysql')
76
if not os.path.exists(mysql_path):
77
self.run('mkdir %s' % mysql_path)
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'))
88
self.run('cp -pr /var/lib/mysql/* %s/' % mysql_path)
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:
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:
101
# leave mysqld running
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()