+ self.port = utils.GetNodeDaemonPort()
+
+ def _InstDict(self, instance, hvp=None, bep=None):
+ """Convert the given instance to a dict.
+
+ This is done via the instance's ToDict() method and additionally
+ we fill the hvparams with the cluster defaults.
+
+ @type instance: L{objects.Instance}
+ @param instance: an Instance object
+ @type hvp: dict or None
+ @param hvp: a dictionary with overriden hypervisor parameters
+ @type bep: dict or None
+ @param bep: a dictionary with overriden backend parameters
+ @rtype: dict
+ @return: the instance dict, with the hvparams filled with the
+ cluster defaults
+
+ """
+ idict = instance.ToDict()
+ cluster = self._cfg.GetClusterInfo()
+ idict["hvparams"] = cluster.FillHV(instance)
+ if hvp is not None:
+ idict["hvparams"].update(hvp)
+ idict["beparams"] = cluster.FillBE(instance)
+ if bep is not None:
+ idict["beparams"].update(bep)
+ return idict
+
+ def _ConnectList(self, client, node_list, call):
+ """Helper for computing node addresses.
+
+ @type client: L{Client}
+ @param client: a C{Client} instance
+ @type node_list: list
+ @param node_list: the node list we should connect
+ @type call: string
+ @param call: the name of the remote procedure call, for filling in
+ correctly any eventual offline nodes' results
+
+ """
+ all_nodes = self._cfg.GetAllNodesInfo()
+ name_list = []
+ addr_list = []
+ skip_dict = {}
+ for node in node_list:
+ if node in all_nodes:
+ if all_nodes[node].offline:
+ skip_dict[node] = RpcResult(node=node, offline=True, call=call)
+ continue
+ val = all_nodes[node].primary_ip
+ else:
+ val = None
+ addr_list.append(val)
+ name_list.append(node)
+ if name_list:
+ client.ConnectList(name_list, address_list=addr_list)
+ return skip_dict
+
+ def _ConnectNode(self, client, node, call):
+ """Helper for computing one node's address.
+
+ @type client: L{Client}
+ @param client: a C{Client} instance
+ @type node: str
+ @param node: the node we should connect
+ @type call: string
+ @param call: the name of the remote procedure call, for filling in
+ correctly any eventual offline nodes' results
+
+ """
+ node_info = self._cfg.GetNodeInfo(node)
+ if node_info is not None:
+ if node_info.offline:
+ return RpcResult(node=node, offline=True, call=call)
+ addr = node_info.primary_ip
+ else:
+ addr = None
+ client.ConnectNode(node, address=addr)
+
+ def _MultiNodeCall(self, node_list, procedure, args):
+ """Helper for making a multi-node call
+
+ """
+ body = serializer.DumpJson(args, indent=False)
+ c = Client(procedure, body, self.port)
+ skip_dict = self._ConnectList(c, node_list, procedure)
+ skip_dict.update(c.GetResults())
+ return skip_dict
+
+ @classmethod
+ def _StaticMultiNodeCall(cls, node_list, procedure, args,
+ address_list=None):
+ """Helper for making a multi-node static call
+
+ """
+ body = serializer.DumpJson(args, indent=False)
+ c = Client(procedure, body, utils.GetNodeDaemonPort())
+ c.ConnectList(node_list, address_list=address_list)
+ return c.GetResults()
+
+ def _SingleNodeCall(self, node, procedure, args):
+ """Helper for making a single-node call
+
+ """
+ body = serializer.DumpJson(args, indent=False)
+ c = Client(procedure, body, self.port)
+ result = self._ConnectNode(c, node, procedure)
+ if result is None:
+ # we did connect, node is not offline
+ result = c.GetResults()[node]
+ return result
+
+ @classmethod
+ def _StaticSingleNodeCall(cls, node, procedure, args):
+ """Helper for making a single-node static call
+
+ """
+ body = serializer.DumpJson(args, indent=False)
+ c = Client(procedure, body, utils.GetNodeDaemonPort())
+ c.ConnectNode(node)
+ return c.GetResults()[node]
+
+ @staticmethod
+ def _Compress(data):
+ """Compresses a string for transport over RPC.
+
+ Small amounts of data are not compressed.
+
+ @type data: str
+ @param data: Data
+ @rtype: tuple
+ @return: Encoded data to send
+
+ """
+ # Small amounts of data are not compressed
+ if len(data) < 512:
+ return (constants.RPC_ENCODING_NONE, data)
+
+ # Compress with zlib and encode in base64
+ return (constants.RPC_ENCODING_ZLIB_BASE64,
+ base64.b64encode(zlib.compress(data, 3)))
+
+ #
+ # Begin RPC calls
+ #