Beispiel #1
0
    def change_data_dir(self, password=None):
        data_dir = niftycloud.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)
Beispiel #2
0
 def save_option(self, path, section, option, value):
     """
     Write the specified Section.Option to the config file specified by path.
     Replace any previous value.  If the path doesn't exist, create it.
     Also add the option the the in-memory config.
     """
     config = ConfigParser()
     config.read(path)
     if not config.has_section(section):
         config.add_section(section)
     config.set(section, option, value)
     fp = open(path, 'w')
     config.write(fp)
     fp.close()
     if not self.has_section(section):
         self.add_section(section)
     self.set(section, option, value)