def setup_and_get_tmp_path(self):
     """ Figure out where to temporarily store csv backups,
         and clean it up
     """
     tmp_dir_root = os.path.join(host_utils.find_root_volume(),
                                 'csv_export',
                                 str(self.instance.port))
     if not os.path.exists(tmp_dir_root):
         os.makedirs(tmp_dir_root)
     host_utils.change_owner(tmp_dir_root, 'mysql', 'mysql')
     self.dump_base_path = tmp_dir_root
def build_cnf(host=None,
              override_dir=None,
              override_mysql_version=None):
    # There are situations where we don't want to overwrite the
    # existing config file, because we are testing, etc...
    if os.path.isfile(TOUCH_FOR_NO_CONFIG_OVERWRITE):
        log.info('Found {path}.  Will not overwrite anything.\n'
                 'Exiting now.'.format(path=TOUCH_FOR_NO_CONFIG_OVERWRITE))
        return

    if not host:
        host = host_utils.HostAddr(host_utils.HOSTNAME)

    if override_mysql_version:
        major_version = override_mysql_version
    else:
        major_version = mysql_lib.get_installed_mysqld_version()[:3]

    if major_version not in environment_specific.SUPPORTED_MYSQL_MAJOR_VERSIONS:
        log.info('CNF building is not supported in '
                 '{major_version}'.format(major_version=major_version))
        return

    config_files = list()
    parser = ConfigParser.RawConfigParser(allow_no_value=True)

    # Always use the local config files for the executing script
    RELATIVE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                CONFIG_SUB_DIR)
    config_files.append(os.path.join(RELATIVE_DIR,
                                     CNF_DEFAULTS))

    log.info('MySQL major version detected as {major_version}'
             ''.format(major_version=major_version))
    config_files.append(os.path.join(RELATIVE_DIR,
                                     major_version))

    instance_type = host_utils.get_instance_type()
    log.info('Hardware detected as {instance_type}'
             ''.format(instance_type=instance_type))
    config_files.append(os.path.join(RELATIVE_DIR,
                                     instance_type))

    log.info('Hostname "{hostname}" results in hostname prefix "{prefix}"'
             ''.format(hostname=host.hostname,
                       prefix=host.replica_type))
    config_files.append(os.path.join(RELATIVE_DIR, host.replica_type))

    # Using the config files, setup a config file parser
    log.info('Using config files {files}'.format(files=config_files))
    parser.read(config_files)

    # Set the server server_id based upon hostname
    server_id = hostname_to_server_id(host.hostname)
    log.info('Setting server_id to {server_id}'.format(server_id=server_id))
    parser.set(MYSQLD_SECTION, 'server_id', server_id)

    # Set read_only based upon service discovery
    parser.set(MYSQLD_SECTION, 'read_only', config_read_only(host))

    # If needed, turn on safe updates via an init_file
    create_init_sql(host.replica_type, parser, override_dir)

    # Set the hostname and root volume through the config
    replace_config_tag(parser, HOSTNAME_TAG, host.hostname)
    replace_config_tag(parser, ROOTVOL_TAG, host_utils.find_root_volume())

    # Remove config elements as directed
    remove_config_by_override(parser)

    # Write out the mysql cnf files
    create_mysql_cnf_files(parser, override_dir)

    # Create log rotate conf for MySQL
    create_log_rotate_conf(parser, override_dir)

    # Create .my.cnf to set username/password defaults for local usage
    create_root_cnf(parser, override_dir)

    # Create pt heartbeat conf in order to be able to calculate replication lag
    create_pt_heartbeat_conf(override_dir)

    # Create pt kill conf in order to kill long running queries
    create_pt_kill_conf(override_dir)
def build_cnf(host=None, override_dir=None, override_mysql_version=None):
    # There are situations where we don't want to overwrite the
    # existing config file, because we are testing, etc...
    if os.path.isfile(TOUCH_FOR_NO_CONFIG_OVERWRITE):
        log.info('Found {}.  Will not overwrite anything.\n'
                 'Exiting now.'.format(TOUCH_FOR_NO_CONFIG_OVERWRITE))
        return

    if not host:
        host = host_utils.HostAddr(host_utils.HOSTNAME)

    if override_mysql_version:
        major_version = override_mysql_version
    else:
        major_version = mysql_lib.get_installed_mysqld_version()[:3]

    if major_version not in environment_specific.SUPPORTED_MYSQL_MAJOR_VERSIONS:
        log.info('CNF building is not supported in '
                 '{}'.format(major_version))
        return

    config_files = list()
    parser = ConfigParser.RawConfigParser(allow_no_value=True)

    # Always use the local config files for the executing script
    RELATIVE_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                                CONFIG_SUB_DIR)
    config_files.append(os.path.join(RELATIVE_DIR, CNF_DEFAULTS))

    log.info('MySQL major version detected as {major_version}'
             ''.format(major_version=major_version))
    config_files.append(os.path.join(RELATIVE_DIR, major_version))

    instance_type = host_utils.get_instance_type()
    log.info('Hardware detected as {instance_type}'
             ''.format(instance_type=instance_type))
    config_files.append(os.path.join(RELATIVE_DIR, instance_type))

    log.info('Hostname "{hostname}" results in hostname prefix "{prefix}"'
             ''.format(hostname=host.hostname, prefix=host.hostname_prefix))
    config_files.append(os.path.join(RELATIVE_DIR, host.replica_type))

    # Using the config files, setup a config file parser
    log.info('Using config files {}'.format(config_files))
    parser.read(config_files)

    # Set the server server_id based upon hostname
    server_id = hostname_to_server_id(host.hostname)
    log.info('Setting server_id to {}'.format(server_id))
    parser.set(MYSQLD_SECTION, 'server_id', server_id)

    ro_status = config_read_only(host)
    parser.set(MYSQLD_SECTION, 'read_only', ro_status)

    # If needed, turn on safe updates via an init_file
    create_init_sql(host.hostname_prefix, parser, override_dir)

    # Set the hostname and root volume through the config
    replace_config_tag(parser, HOSTNAME_TAG, host.hostname)
    replace_config_tag(parser, ROOTVOL_TAG, host_utils.find_root_volume())

    # Remove config elements as directed
    remove_config_by_override(parser)

    # Write out the mysql cnf files
    create_mysql_cnf_files(parser, override_dir)

    # Create log rotate conf for MySQL
    create_log_rotate_conf(parser, override_dir)

    # Create .my.cnf to set username/password defaults for local usage
    create_root_cnf(parser, override_dir)

    # Create pt heartbeat conf in order to be able to calculate replication lag
    create_pt_heartbeat_conf(override_dir)

    # Create pt kill conf in order to kill long running queries
    create_pt_kill_conf(override_dir)