def change_data_dir(self, password=None): data_dir = boto.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)
def change_data_dir(self, password=None): data_dir = boto.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 = SafeConfigParser() 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)
def run(self, command, notify=True, exit_on_error=False): self.last_command = ShellCommand(command) if self.last_command.status != 0: boto.log.error(self.last_command.output) if notify: self.notify('Error encountered', self.last_command.output) if exit_on_error: sys.exit(-1) return self.last_command.status
def run(self, command, notify=True, exit_on_error=False): self.last_command = ShellCommand(command) if self.last_command.status != 0: boto.log.error('Error running command: "%s". Output: "%s"' % (command, self.last_command.output)) if notify: self.notify('Error encountered', \ 'Error running the following command:\n\t%s\n\nCommand output:\n\t%s' % \ (command, self.last_command.output)) if exit_on_error: sys.exit(-1) return self.last_command.status