示例#1
0
    def change_data_dir(self, password=None):
        data_dir = mssapi.config.get('MySQL', 'data_dir', '/mnt')
        fresh_install = False
        is_mysql_running_command = ShellCommand('mysqladmin ping') # exit status 0 if mysql is running
        is_mysql_running_command.run()
        if is_mysql_running_command.getStatus() == 0:
            # mysql is running. This is the state apt-get will leave it in. If it isn't running,
            # that means mysql was already installed on the AMI and there's no need to stop it,
            # saving 40 seconds on instance startup.
            time.sleep(10) #trying to stop mysql immediately after installing it fails
            # We need to wait until mysql creates the root account before we kill it
            # or bad things will happen
            i = 0
            while self.run("echo 'quit' | mysql -u root") != 0 and i < 5:
                time.sleep(5)
                i = i + 1
            self.run('/etc/init.d/mysql stop')
            self.run("pkill -9 mysql")

        mysql_path = os.path.join(data_dir, 'mysql')
        if not os.path.exists(mysql_path):
            self.run('mkdir %s' % mysql_path)
            fresh_install = True
        self.run('chown -R mysql:mysql %s' % mysql_path)
        fp = open('/etc/mysql/conf.d/use_mnt.cnf', 'w')
        fp.write('# created by pyami\n')
        fp.write('# use the %s volume for data\n' % data_dir)
        fp.write('[mysqld]\n')
        fp.write('datadir = %s\n' % mysql_path)
        fp.write('log_bin = %s\n' % os.path.join(mysql_path, 'mysql-bin.log'))
        fp.close()
        if fresh_install:
            self.run('cp -pr /var/lib/mysql/* %s/' % mysql_path)
            self.start('mysql')
        else:
            #get the password ubuntu expects to use:
            config_parser = ConfigParser()
            config_parser.read('/etc/mysql/debian.cnf')
            password = config_parser.get('client', 'password')
            # start the mysql deamon, then mysql with the required grant statement piped into it:
            self.start('mysql')
            time.sleep(10) #time for mysql to start
            grant_command = "echo \"GRANT ALL PRIVILEGES ON *.* TO 'debian-sys-maint'@'localhost' IDENTIFIED BY '%s' WITH GRANT OPTION;\" | mysql" % password
            while self.run(grant_command) != 0:
                time.sleep(5)
示例#2
0
 def get(self, section, name, default=None):
     try:
         val = ConfigParser.get(self, section, name)
     except:
         val = default
     return val