def init_privileges_tables(port):
    """ Bootstap a MySQL instance

    Args:
    port - the port on which to act upon on localhost
    """
    version = mysql_lib.get_installed_mysqld_version()
    if version[0:3] < '5.7':
        install_command = MYSQL_INSTALL_DB
    else:
        install_command = MYSQL_INITIALIZE

    datadir = host_utils.get_cnf_setting('datadir', port)
    cmd = ('{MYSQL_INSTALL_DB} --datadir={datadir}'
           ' --user=mysql'.format(MYSQL_INSTALL_DB=install_command,
                                  datadir=datadir))
    log.info(cmd)
    (std_out, std_err, return_code) = host_utils.shell_exec(cmd)
    if return_code:
        raise Exception("Return {return_code} != 0 \n"
                        "std_err:{std_err}\n"
                        "std_out:{std_out}".format(return_code=return_code,
                                                   std_err=std_err,
                                                   std_out=std_out))
def init_privileges_tables(port):
    """ Bootstap a MySQL instance

    Args:
    port - the port on which to act upon on localhost
    """
    version = mysql_lib.get_installed_mysqld_version()
    if version[0:3] < '5.7':
        install_command = MYSQL_INSTALL_DB
    else:
        install_command = MYSQL_INITIALIZE

    datadir = host_utils.get_cnf_setting('datadir', port)
    cmd = ('{MYSQL_INSTALL_DB} --datadir={datadir}'
           ' --user=mysql'.format(MYSQL_INSTALL_DB=install_command,
                                  datadir=datadir))
    log.info(cmd)
    (std_out, std_err, return_code) = host_utils.shell_exec(cmd)
    if return_code:
        raise Exception("Return {return_code} != 0 \n"
                        "std_err:{std_err}\n"
                        "std_out:{std_out}".format(return_code=return_code,
                                                   std_err=std_err,
                                                   std_out=std_out))
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)
Example #4
0
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)