+
+def _Decompress(data):
+ """Unpacks data compressed by the RPC client.
+
+ @type data: list or tuple
+ @param data: Data sent by RPC client
+ @rtype: str
+ @return: Decompressed data
+
+ """
+ assert isinstance(data, (list, tuple))
+ assert len(data) == 2
+ (encoding, content) = data
+ if encoding == constants.RPC_ENCODING_NONE:
+ return content
+ elif encoding == constants.RPC_ENCODING_ZLIB_BASE64:
+ return zlib.decompress(base64.b64decode(content))
+ else:
+ raise AssertionError("Unknown data encoding")
+
+
+def _CleanDirectory(path, exclude=None):
+ """Removes all regular files in a directory.
+
+ @type path: str
+ @param path: the directory to clean
+ @type exclude: list
+ @param exclude: list of files to be excluded, defaults
+ to the empty list
+
+ """
+ if not os.path.isdir(path):
+ return
+ if exclude is None:
+ exclude = []
+ else:
+ # Normalize excluded paths
+ exclude = [os.path.normpath(i) for i in exclude]
+
+ for rel_name in utils.ListVisibleFiles(path):
+ full_name = os.path.normpath(os.path.join(path, rel_name))
+ if full_name in exclude:
+ continue
+ if os.path.isfile(full_name) and not os.path.islink(full_name):
+ utils.RemoveFile(full_name)
+
+
+def JobQueuePurge():
+ """Removes job queue files and archived jobs.
+
+ @rtype: None
+
+ """
+ _CleanDirectory(constants.QUEUE_DIR, exclude=[constants.JOB_QUEUE_LOCK_FILE])
+ _CleanDirectory(constants.JOB_QUEUE_ARCHIVE_DIR)
+
+
+def GetMasterInfo():
+ """Returns master information.
+
+ This is an utility function to compute master information, either
+ for consumption here or from the node daemon.
+
+ @rtype: tuple
+ @return: (master_netdev, master_ip, master_name) if we have a good
+ configuration, otherwise (None, None, None)
+
+ """
+ try:
+ cfg = _GetConfig()
+ master_netdev = cfg.GetMasterNetdev()
+ master_ip = cfg.GetMasterIP()
+ master_node = cfg.GetMasterNode()
+ except errors.ConfigurationError, err:
+ logging.exception("Cluster configuration incomplete")
+ return (None, None, None)
+ return (master_netdev, master_ip, master_node)
+
+
+def StartMaster(start_daemons):
+ """Activate local node as master node.
+
+ The function will always try activate the IP address of the master
+ (unless someone else has it). It will also start the master daemons,
+ based on the start_daemons parameter.
+
+ @type start_daemons: boolean
+ @param start_daemons: whther to also start the master
+ daemons (ganeti-masterd and ganeti-rapi)
+ @rtype: None
+
+ """
+ ok = True
+ master_netdev, master_ip, _ = GetMasterInfo()
+ if not master_netdev: