+
+
+def _ResolveName(cmd, key):
+ """Helper function.
+
+ """
+ master = qa_config.GetMasterNode()
+
+ output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd))
+ for line in output.splitlines():
+ (lkey, lvalue) = line.split(':', 1)
+ if lkey == key:
+ return lvalue.lstrip()
+ raise KeyError("Key not found")
+
+
+def ResolveInstanceName(instance):
+ """Gets the full name of an instance.
+
+ """
+ return _ResolveName(['gnt-instance', 'info', instance['name']],
+ 'Instance name')
+
+
+def ResolveNodeName(node):
+ """Gets the full name of a node.
+
+ """
+ return _ResolveName(['gnt-node', 'info', node['primary']],
+ 'Node name')
+
+
+def GetNodeInstances(node, secondaries=False):
+ """Gets a list of instances on a node.
+
+ """
+ master = qa_config.GetMasterNode()
+ node_name = ResolveNodeName(node)
+
+ # Get list of all instances
+ cmd = ['gnt-instance', 'list', '--separator=:', '--no-headers',
+ '--output=name,pnode,snodes']
+ output = GetCommandOutput(master['primary'], utils.ShellQuoteArgs(cmd))
+
+ instances = []
+ for line in output.splitlines():
+ (name, pnode, snodes) = line.split(':', 2)
+ if ((not secondaries and pnode == node_name) or
+ (secondaries and node_name in snodes.split(','))):
+ instances.append(name)
+
+ return instances
+
+
+def _FormatWithColor(text, seq):
+ if not seq:
+ return text
+ return "%s%s%s" % (seq, text, _RESET_SEQ)
+
+
+FormatWarning = lambda text: _FormatWithColor(text, _WARNING_SEQ)
+FormatError = lambda text: _FormatWithColor(text, _ERROR_SEQ)
+FormatInfo = lambda text: _FormatWithColor(text, _INFO_SEQ)