示例#1
0
def initial_mysql_user(options):
    sbdir = options.basedir
    sbuser = options.mysql_user
    sbpass = options.password
    start = time.time()

    with tempfile.NamedTemporaryFile(dir=sbdir) as fileobj:
        os.fchmod(fileobj.fileno(), 0o0660)
        fileobj = codecs.getwriter('utf-8')(fileobj)
        template = template_loader.get_template('init_file.sql')
        sql = template.render(user=sbuser, password=sbpass, host='localhost')
        print(sql, file=fileobj)
        fileobj.flush()
        logging.info("    - Generated init-file: %s", fileobj.name)
        with open(os.devnull, 'rb') as devnull:
            sandbox_sh = "%s/sandbox.sh" % (sbdir,)
            start_cmd = "%s start --init-file=%s" % (sandbox_sh, fileobj.name)
            stop_cmd = "%s stop" % (sandbox_sh,)
            logging.info("    - Running: %s", start_cmd)
            status = cmd.run(start_cmd, stdout=devnull, stderr=devnull)
            if status != 0:
                logging.error("Failed to initialize sandbox. "
                              "Check %s for details",
                              os.path.join(sbdir, "data", "mysqld.log"))
            logging.info("    - Running: %s", stop_cmd)
            cmd.run(stop_cmd, stdout=devnull, stderr=devnull)
    logging.info("    * Initialized MySQL user in %.2f seconds",
                 time.time() - start)
示例#2
0
文件: common.py 项目: abg/dbsake
def initialize_mysql_user(options):
    sbdir = options.basedir
    sbuser = options.mysql_user
    sbpass = options.password
    start = time.time()

    with tempfile.NamedTemporaryFile(dir=sbdir) as fileobj:
        os.fchmod(fileobj.fileno(), 0o0660)
        fileobj = codecs.getwriter('utf-8')(fileobj)
        template = template_loader.get_template('init_file.sql')
        sql = template.render(user=sbuser, password=sbpass, host='localhost')
        print(sql, file=fileobj)
        fileobj.flush()
        logging.info("    - Generated init-file: %s", fileobj.name)
        with open(os.devnull, 'rb') as devnull:
            sandbox_sh = "%s/sandbox.sh" % (sbdir,)
            start_cmd = "%s start --init-file=%s" % (sandbox_sh, fileobj.name)
            stop_cmd = "%s stop" % (sandbox_sh,)
            logging.info("    - Running: %s", start_cmd)
            start_status = cmd.run(start_cmd, stdout=devnull, stderr=devnull)
            if start_status != 0:
                logging.error("    !! Failed to initialize sandbox. "
                              "Check %s for details",
                              os.path.join(sbdir, "data", "mysqld.log"))
            logging.info("    - Running: %s", stop_cmd)
            stop_status = cmd.run(stop_cmd, stdout=devnull, stderr=devnull)
            status = start_status or stop_status

    if status != 0:
        raise SandboxError("Sandbox user initialization failed")
    logging.info("    * Initialized MySQL user in %.2f seconds",
                 time.time() - start)
示例#3
0
def generate_defaults(options, **kwargs):
    """Generate a my.sandbox.cnf file

    :param options: SandboxOptions instance
    :param kwargs: options to be passed directly to the my.sandbox.cnf
                   template
    """
    start = time.time()
    defaults_file = os.path.join(options.basedir, 'my.sandbox.cnf')

    # Check for innodb-log-file-size
    ib_logfile0 = os.path.join(options.datadir, 'ib_logfile0')
    ib_logfile_size = None
    try:
        ib_logfile_size = os.path.getsize(ib_logfile0)
    except OSError:
        debug("    # No ib_logfile0 found")
    if ib_logfile_size:
        kwargs['innodb_log_file_size'] = _format_logsize(ib_logfile_size)
        info("    + Existing ib_logfile0 detected.")
        info("    + Setting innodb-log-file-size=%s",
             kwargs['innodb_log_file_size'])

    ibdata = []
    ibdata_pattern = os.path.join(options.datadir, 'ibdata*')
    for path in sorted(glob.glob(ibdata_pattern)):
        rpath = os.path.basename(path)
        ibdata.append(rpath + ':' + _format_logsize(os.stat(path).st_size))
    if ibdata:
        kwargs['innodb_data_file_path'] = ';'.join(ibdata) + ':autoextend'
        info("    + Found existing shared innodb tablespace(s).")
        logging.info("    + Setting innodb-data-file-path=%s",
                     ';'.join(ibdata) + ':autoextend')
    else:
        kwargs['innodb_data_file_path'] = None

    # Check for innodb_log_files_in_group
    innodb_log_files_in_group = sum(1 for name in os.listdir(options.datadir)
                                    if name.startswith('ib_logfile'))
    if innodb_log_files_in_group > 2:
        kwargs['innodb_log_files_in_group'] = innodb_log_files_in_group
        info("    - Multiple ib_logfile* logs found.")
        info("    - Setting innodb-log-files-in-group=%s",
             kwargs['innodb_log_files_in_group'])
    else:
        kwargs['innodb_log_files_in_group'] = 2

    template = template_loader.get_template('my.sandbox.cnf')
    content = template.render(**kwargs)
    with codecs.open(defaults_file, 'wb', encoding='utf8') as stream:
        os.fchmod(stream.fileno(), 0o0660)
        stream.write(content)
        stream.write("\n")
    info("    * Generated %s in %.2f seconds",
         defaults_file, time.time() - start)
    return defaults_file
示例#4
0
文件: common.py 项目: abg/dbsake
def generate_defaults(options, **kwargs):
    """Generate a my.sandbox.cnf file

    :param options: SandboxOptions instance
    :param kwargs: options to be passed directly to the my.sandbox.cnf
                   template
    """
    start = time.time()
    defaults_file = os.path.join(options.basedir, 'my.sandbox.cnf')

    # Check for innodb-log-file-size
    ib_logfile0 = os.path.join(options.datadir, 'ib_logfile0')
    ib_logfile_size = None
    try:
        ib_logfile_size = os.path.getsize(ib_logfile0)
    except OSError:
        debug("    # No ib_logfile0 found")
    if ib_logfile_size:
        kwargs['innodb_log_file_size'] = _format_logsize(ib_logfile_size)
        info("    + Existing ib_logfile0 detected.")
        info("    + Setting innodb-log-file-size=%s",
             kwargs['innodb_log_file_size'])

    ibdata = []
    ibdata_pattern = os.path.join(options.datadir, 'ibdata*')
    for path in sorted(glob.glob(ibdata_pattern)):
        rpath = os.path.basename(path)
        ibdata.append(rpath + ':' + _format_logsize(os.stat(path).st_size))
    if ibdata:
        kwargs['innodb_data_file_path'] = ';'.join(ibdata) + ':autoextend'
        info("    + Found existing shared innodb tablespace(s).")
        logging.info("    + Setting innodb-data-file-path=%s",
                     ';'.join(ibdata) + ':autoextend')
    else:
        kwargs['innodb_data_file_path'] = None

    # Check for innodb_log_files_in_group
    innodb_log_files_in_group = sum(1 for name in os.listdir(options.datadir)
                                    if name.startswith('ib_logfile'))
    if innodb_log_files_in_group > 2:
        kwargs['innodb_log_files_in_group'] = innodb_log_files_in_group
        info("    - Multiple ib_logfile* logs found.")
        info("    - Setting innodb-log-files-in-group=%s",
             kwargs['innodb_log_files_in_group'])
    else:
        kwargs['innodb_log_files_in_group'] = 2

    template = template_loader.get_template('my.sandbox.cnf')
    content = template.render(**kwargs)
    with codecs.open(defaults_file, 'wb', encoding='utf8') as stream:
        os.fchmod(stream.fileno(), 0o0660)
        stream.write(content)
        stream.write("\n")
    info("    * Generated %s in %.2f seconds",
         defaults_file, time.time() - start)
    return defaults_file
示例#5
0
文件: common.py 项目: theburn/dbsake
def generate_initscript(sandbox_directory, **kwargs):
    """Generate an init script"""
    start = time.time()
    template = template_loader.get_template('sandbox.sh')
    content = template.render(sandbox_root=sandbox_directory, **kwargs)

    sandbox_sh_path = os.path.join(sandbox_directory, 'sandbox.sh')
    with codecs.open(sandbox_sh_path, 'w', encoding='utf8') as fileobj:
        # ensure initscript is executable by current user + group
        os.fchmod(fileobj.fileno(), 0o0755)
        fileobj.write(content)
        fileobj.write("\n")
    info("    * Generated initscript in %.2f seconds", time.time() - start)
示例#6
0
文件: common.py 项目: abg/dbsake
def generate_initscript(sandbox_directory, **kwargs):
    """Generate an init script"""
    start = time.time()
    template = template_loader.get_template('sandbox.sh')
    content = template.render(sandbox_root=sandbox_directory,
                              **kwargs)

    sandbox_sh_path = os.path.join(sandbox_directory, 'sandbox.sh')
    with codecs.open(sandbox_sh_path, 'w', encoding='utf8') as fileobj:
        # ensure initscript is executable by current user + group
        os.fchmod(fileobj.fileno(), 0o0755)
        fileobj.write(content)
        fileobj.write("\n")
    info("    * Generated initscript in %.2f seconds", time.time() - start)
示例#7
0
def mysql_install_db(options, distribution, **kwargs):
    join = os.path.join

    def cat(path):
        with codecs.open(path, 'r', 'utf8') as fileobj:
            return fileobj.read()

    sharedir = distribution.sharedir
    mysql_system_tables = cat(join(sharedir, 'mysql_system_tables.sql'))
    mysql_system_tables_data = cat(join(sharedir,
                                        'mysql_system_tables_data.sql'))
    try:
        # MariaDB uses a separate mysql_performance_tables.sql file
        p_s_sql_path = join(sharedir, 'mysql_performance_tables.sql')
        mysql_p_s_tables = cat(p_s_sql_path)
    except IOError as exc:
        if exc.errno != errno.ENOENT:
            raise
        mysql_p_s_tables = ''

    fill_help_tables = cat(join(sharedir, 'fill_help_tables.sql'))

    # If the ./mysql/ system database does not exist under the datadir
    # we run the bootstrap initialization logic
    # This will instruct the template to ensure a `test` database exists
    # and the basic mysql_secure_installation process is run.
    bootstrap_data = not os.path.exists(os.path.join(options.datadir, 'mysql'))

    template = template_loader.get_template('bootstrap.sql')
    sql = template.render(distribution=distribution,
                          mysql_system_tables=mysql_system_tables,
                          mysql_system_tables_data=mysql_system_tables_data,
                          mysql_performance_tables=mysql_p_s_tables,
                          fill_help_tables=fill_help_tables,
                          bootstrap_data=bootstrap_data,
                          password=options.password,
                          **kwargs)
    for line in sql.splitlines():
        yield line
示例#8
0
文件: common.py 项目: abg/dbsake
def mysql_install_db(options, distribution, **kwargs):
    join = os.path.join

    def cat(path):
        with codecs.open(path, 'r', 'utf8') as fileobj:
            return fileobj.read()

    sharedir = distribution.sharedir
    mysql_system_tables = cat(join(sharedir, 'mysql_system_tables.sql'))
    mysql_system_tables_data = cat(join(sharedir,
                                        'mysql_system_tables_data.sql'))
    try:
        # MariaDB uses a separate mysql_performance_tables.sql file
        p_s_sql_path = join(sharedir, 'mysql_performance_tables.sql')
        mysql_p_s_tables = cat(p_s_sql_path)
    except IOError as exc:
        if exc.errno != errno.ENOENT:
            raise
        mysql_p_s_tables = ''

    fill_help_tables = cat(join(sharedir, 'fill_help_tables.sql'))

    # If the ./mysql/ system database does not exist under the datadir
    # we run the bootstrap initialization logic
    # This will instruct the template to ensure a `test` database exists
    # and the basic mysql_secure_installation process is run.
    bootstrap_data = not os.path.exists(os.path.join(options.datadir, 'mysql'))

    template = template_loader.get_template('bootstrap.sql')
    sql = template.render(distribution=distribution,
                          mysql_system_tables=mysql_system_tables,
                          mysql_system_tables_data=mysql_system_tables_data,
                          mysql_performance_tables=mysql_p_s_tables,
                          fill_help_tables=fill_help_tables,
                          bootstrap_data=bootstrap_data,
                          password=options.password,
                          **kwargs)
    for line in sql.splitlines():
        yield line