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)
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)
def bootstrap(options, distribution, additional_options=()): start = time.time() defaults_file = os.path.join(options.basedir, 'my.sandbox.cnf') logfile = os.path.join(options.basedir, 'bootstrap.log') info(" - Logging bootstrap output to %s", logfile) bootstrap_cmd = cmd.shell_format("{0} --defaults-file={1}", distribution.mysqld, defaults_file) additional_options = ('--bootstrap', '--default-storage-engine=myisam') + \ additional_options additional = ' '.join(map(cmd.shell_quote, additional_options)) if additional: bootstrap_cmd += ' ' + additional bootstrap_sql = os.path.join(options.basedir, 'bootstrap.sql') with codecs.open(bootstrap_sql, 'wb', 'utf8') as fileobj: os.fchmod(fileobj.fileno(), 0o0660) for line in mysql_install_db(options, distribution): print(line, file=fileobj) info(" - Generated bootstrap SQL") with open(logfile, 'wb') as stderr: with open(bootstrap_sql, 'rb') as fileobj: info(" - Running %s", bootstrap_cmd) returncode = cmd.run(bootstrap_cmd, stdin=fileobj, stdout=stderr, stderr=stderr) if returncode != 0: raise SandboxError("Bootstrapping failed. Details in %s" % stderr.name) info(" * Bootstrapped sandbox in %.2f seconds", time.time() - start)
def prepare_datadir(datadir, options): innobackupex = pycompat.which('innobackupex') if not innobackupex: raise common.SandboxError("innobackupex not found in path. Aborting.") xb_log = os.path.join(datadir, 'innobackupex.log') xb_cmd = cmd.shell_format('{0} --apply-log {1!s} .', innobackupex, options.innobackupex_options) info(" - Running: %s", xb_cmd) info(" - (cwd: %s)", datadir) with open(xb_log, 'wb') as fileobj: returncode = cmd.run(xb_cmd, stdout=fileobj, stderr=fileobj, cwd=datadir) if returncode != 0: info(" ! innobackupex --apply-log failed. See details in %s", xb_log) raise common.SandboxError("Data preloading failed") else: info(" - innobackupex --apply-log succeeded. datadir is ready.")