+
+
+def AddToEtcHosts(hostnames):
+ """Adds hostnames to /etc/hosts.
+
+ @param hostnames: List of hostnames first used A records, all other CNAMEs
+
+ """
+ master = qa_config.GetMasterNode()
+ tmp_hosts = UploadData(master["primary"], "", mode=0644)
+
+ quoted_tmp_hosts = utils.ShellQuote(tmp_hosts)
+ data = []
+ for localhost in ("::1", "127.0.0.1"):
+ data.append("%s %s" % (localhost, " ".join(hostnames)))
+
+ try:
+ AssertCommand(("cat /etc/hosts > %s && echo -e '%s' >> %s && mv %s"
+ " /etc/hosts") % (quoted_tmp_hosts, "\\n".join(data),
+ quoted_tmp_hosts, quoted_tmp_hosts))
+ except qa_error.Error:
+ AssertCommand(["rm", tmp_hosts])
+
+
+def RemoveFromEtcHosts(hostnames):
+ """Remove hostnames from /etc/hosts.
+
+ @param hostnames: List of hostnames first used A records, all other CNAMEs
+
+ """
+ master = qa_config.GetMasterNode()
+ tmp_hosts = UploadData(master["primary"], "", mode=0644)
+ quoted_tmp_hosts = utils.ShellQuote(tmp_hosts)
+
+ sed_data = " ".join(hostnames)
+ try:
+ AssertCommand(("sed -e '/^\(::1\|127\.0\.0\.1\)\s\+%s/d' /etc/hosts > %s"
+ " && mv %s /etc/hosts") % (sed_data, quoted_tmp_hosts,
+ quoted_tmp_hosts))
+ except qa_error.Error:
+ AssertCommand(["rm", tmp_hosts])
+
+
+def RunInstanceCheck(instance, running):
+ """Check if instance is running or not.
+
+ """
+ instance_name = _GetName(instance, "name")
+
+ script = qa_config.GetInstanceCheckScript()
+ if not script:
+ return
+
+ master_node = qa_config.GetMasterNode()
+
+ # Build command to connect to master node
+ master_ssh = GetSSHCommand(master_node["primary"], "--")
+
+ if running:
+ running_shellval = "1"
+ running_text = ""
+ else:
+ running_shellval = ""
+ running_text = "not "
+
+ print FormatInfo("Checking if instance '%s' is %srunning" %
+ (instance_name, running_text))
+
+ args = [script, instance_name]
+ env = {
+ "PATH": constants.HOOKS_PATH,
+ "RUN_UUID": _RUN_UUID,
+ "MASTER_SSH": utils.ShellQuoteArgs(master_ssh),
+ "INSTANCE_NAME": instance_name,
+ "INSTANCE_RUNNING": running_shellval,
+ }
+
+ result = os.spawnve(os.P_WAIT, script, args, env)
+ if result != 0:
+ raise qa_error.Error("Instance check failed with result %s" % result)
+
+
+def _InstanceCheckInner(expected, instarg, args, result):
+ """Helper function used by L{InstanceCheck}.
+
+ """
+ if instarg == FIRST_ARG:
+ instance = args[0]
+ elif instarg == RETURN_VALUE:
+ instance = result
+ else:
+ raise Exception("Invalid value '%s' for instance argument" % instarg)
+
+ if expected in (INST_DOWN, INST_UP):
+ RunInstanceCheck(instance, (expected == INST_UP))
+ elif expected is not None:
+ raise Exception("Invalid value '%s'" % expected)
+
+
+def InstanceCheck(before, after, instarg):
+ """Decorator to check instance status before and after test.
+
+ @param before: L{INST_DOWN} if instance must be stopped before test,
+ L{INST_UP} if instance must be running before test, L{None} to not check.
+ @param after: L{INST_DOWN} if instance must be stopped after test,
+ L{INST_UP} if instance must be running after test, L{None} to not check.
+ @param instarg: L{FIRST_ARG} to use first argument to test as instance (a
+ dictionary), L{RETURN_VALUE} to use return value (disallows pre-checks)
+
+ """
+ def decorator(fn):
+ @functools.wraps(fn)
+ def wrapper(*args, **kwargs):
+ _InstanceCheckInner(before, instarg, args, NotImplemented)
+
+ result = fn(*args, **kwargs)
+
+ _InstanceCheckInner(after, instarg, args, result)
+
+ return result
+ return wrapper
+ return decorator