from ganeti import ssconf
from ganeti import runtime
from ganeti import compat
+from ganeti import rpc_defs
# Special module generated at build time
from ganeti import _generated_rpc
return self._CombineResults(results, requests, procedure)
-class RpcRunner(_generated_rpc.RpcClientDefault,
+class _RpcClientBase:
+ def __init__(self, resolver, encoder_fn, lock_monitor_cb=None):
+ """Initializes this class.
+
+ """
+ self._proc = _RpcProcessor(resolver,
+ netutils.GetDaemonPort(constants.NODED),
+ lock_monitor_cb=lock_monitor_cb)
+ self._encoder = compat.partial(self._EncodeArg, encoder_fn)
+
+ @staticmethod
+ def _EncodeArg(encoder_fn, (argkind, value)):
+ """Encode argument.
+
+ """
+ if argkind is None:
+ return value
+ else:
+ return encoder_fn(argkind)(value)
+
+ def _Call(self, node_list, procedure, timeout, argdefs, args):
+ """Entry point for automatically generated RPC wrappers.
+
+ """
+ assert len(args) == len(argdefs), "Wrong number of arguments"
+
+ body = serializer.DumpJson(map(self._encoder, zip(argdefs, args)),
+ indent=False)
+
+ return self._proc(node_list, procedure, body, read_timeout=timeout)
+
+
+def _ObjectToDict(value):
+ """Converts an object to a dictionary.
+
+ @note: See L{objects}.
+
+ """
+ return value.ToDict()
+
+
+def _ObjectListToDict(value):
+ """Converts a list of L{objects} to dictionaries.
+
+ """
+ return map(_ObjectToDict, value)
+
+
+def _EncodeNodeToDiskDict(value):
+ """Encodes a dictionary with node name as key and disk objects as values.
+
+ """
+ return dict((name, _ObjectListToDict(disks))
+ for name, disks in value.items())
+
+
+def _PrepareFileUpload(filename):
+ """Loads a file and prepares it for an upload to nodes.
+
+ """
+ data = _Compress(utils.ReadFile(filename))
+ st = os.stat(filename)
+ getents = runtime.GetEnts()
+ return [filename, data, st.st_mode, getents.LookupUid(st.st_uid),
+ getents.LookupGid(st.st_gid), st.st_atime, st.st_mtime]
+
+
+def _PrepareFinalizeExportDisks(snap_disks):
+ """Encodes disks for finalizing export.
+
+ """
+ flat_disks = []
+
+ for disk in snap_disks:
+ if isinstance(disk, bool):
+ flat_disks.append(disk)
+ else:
+ flat_disks.append(disk.ToDict())
+
+ return flat_disks
+
+
+def _EncodeImportExportIO((ieio, ieioargs)):
+ """Encodes import/export I/O information.
+
+ """
+ if ieio == constants.IEIO_RAW_DISK:
+ assert len(ieioargs) == 1
+ return (ieio, (ieioargs[0].ToDict(), ))
+
+ if ieio == constants.IEIO_SCRIPT:
+ assert len(ieioargs) == 2
+ return (ieio, (ieioargs[0].ToDict(), ieioargs[1]))
+
+ return (ieio, ieioargs)
+
+
+def _EncodeBlockdevRename(value):
+ """Encodes information for renaming block devices.
+
+ """
+ return [(d.ToDict(), uid) for d, uid in value]
+
+
+#: Generic encoders
+_ENCODERS = {
+ rpc_defs.ED_OBJECT_DICT: _ObjectToDict,
+ rpc_defs.ED_OBJECT_DICT_LIST: _ObjectListToDict,
+ rpc_defs.ED_NODE_TO_DISK_DICT: _EncodeNodeToDiskDict,
+ rpc_defs.ED_FILE_DETAILS: _PrepareFileUpload,
+ rpc_defs.ED_COMPRESS: _Compress,
+ rpc_defs.ED_FINALIZE_EXPORT_DISKS: _PrepareFinalizeExportDisks,
+ rpc_defs.ED_IMPEXP_IO: _EncodeImportExportIO,
+ rpc_defs.ED_BLOCKDEV_RENAME: _EncodeBlockdevRename,
+ }
+
+
+class RpcRunner(_RpcClientBase,
+ _generated_rpc.RpcClientDefault,
_generated_rpc.RpcClientBootstrap,
_generated_rpc.RpcClientConfig):
"""RPC runner class.
@param context: Ganeti context
"""
+ self._cfg = context.cfg
+
+ encoders = _ENCODERS.copy()
+
+ # Add encoders requiring configuration object
+ encoders.update({
+ rpc_defs.ED_INST_DICT: self._InstDict,
+ rpc_defs.ED_INST_DICT_HVP_BEP: self._InstDictHvpBep,
+ rpc_defs.ED_INST_DICT_OSP: self._InstDictOsp,
+ })
+
+ # Resolver using configuration
+ resolver = compat.partial(_NodeConfigResolver, self._cfg.GetNodeInfo,
+ self._cfg.GetAllNodesInfo)
+
# Pylint doesn't recognize multiple inheritance properly, see
# <http://www.logilab.org/ticket/36586> and
# <http://www.logilab.org/ticket/35642>
# pylint: disable=W0233
+ _RpcClientBase.__init__(self, resolver, encoders.get,
+ lock_monitor_cb=context.glm.AddToLockMonitor)
_generated_rpc.RpcClientConfig.__init__(self)
_generated_rpc.RpcClientBootstrap.__init__(self)
_generated_rpc.RpcClientDefault.__init__(self)
- self._cfg = context.cfg
- self._proc = _RpcProcessor(compat.partial(_NodeConfigResolver,
- self._cfg.GetNodeInfo,
- self._cfg.GetAllNodesInfo),
- netutils.GetDaemonPort(constants.NODED),
- lock_monitor_cb=context.glm.AddToLockMonitor)
-
def _InstDict(self, instance, hvp=None, bep=None, osp=None):
"""Convert the given instance to a dict.
"""
return self._InstDict(instance, osp=osparams)
- def _Call(self, node_list, procedure, timeout, args):
- """Entry point for automatically generated RPC wrappers.
-
- """
- body = serializer.DumpJson(args, indent=False)
-
- return self._proc(node_list, procedure, body, read_timeout=timeout)
-
@staticmethod
def _MigrationStatusPostProc(result):
if not result.fail_msg and result.payload is not None:
return result
@staticmethod
- def _PrepareFinalizeExportDisks(snap_disks):
- flat_disks = []
-
- for disk in snap_disks:
- if isinstance(disk, bool):
- flat_disks.append(disk)
- else:
- flat_disks.append(disk.ToDict())
-
- return flat_disks
-
- @staticmethod
def _ImpExpStatusPostProc(result):
"""Post-processor for import/export status.
return result
- @staticmethod
- def _EncodeImportExportIO((ieio, ieioargs)):
- """Encodes import/export I/O information.
-
- """
- if ieio == constants.IEIO_RAW_DISK:
- assert len(ieioargs) == 1
- return (ieio, (ieioargs[0].ToDict(), ))
-
- if ieio == constants.IEIO_SCRIPT:
- assert len(ieioargs) == 2
- return (ieio, (ieioargs[0].ToDict(), ieioargs[1]))
-
- return (ieio, ieioargs)
-
- @staticmethod
- def _PrepareFileUpload(filename):
- """Loads a file and prepares it for an upload to nodes.
-
- """
- data = _Compress(utils.ReadFile(filename))
- st = os.stat(filename)
- getents = runtime.GetEnts()
- return [filename, data, st.st_mode, getents.LookupUid(st.st_uid),
- getents.LookupGid(st.st_gid), st.st_atime, st.st_mtime]
-
#
# Begin RPC calls
#
read_timeout=int(duration + 5))
-class JobQueueRunner(_generated_rpc.RpcClientJobQueue):
+class JobQueueRunner(_RpcClientBase, _generated_rpc.RpcClientJobQueue):
"""RPC wrappers for job queue.
"""
- _Compress = staticmethod(_Compress)
-
def __init__(self, context, address_list):
"""Initializes this class.
"""
- _generated_rpc.RpcClientJobQueue.__init__(self)
-
if address_list is None:
resolver = _SsconfResolver
else:
# Caller provided an address list
resolver = _StaticResolver(address_list)
- self._proc = _RpcProcessor(resolver,
- netutils.GetDaemonPort(constants.NODED),
- lock_monitor_cb=context.glm.AddToLockMonitor)
-
- def _Call(self, node_list, procedure, timeout, args):
- """Entry point for automatically generated RPC wrappers.
-
- """
- body = serializer.DumpJson(args, indent=False)
-
- return self._proc(node_list, procedure, body, read_timeout=timeout)
+ _RpcClientBase.__init__(self, resolver, _ENCODERS.get,
+ lock_monitor_cb=context.glm.AddToLockMonitor)
+ _generated_rpc.RpcClientJobQueue.__init__(self)
-class BootstrapRunner(_generated_rpc.RpcClientBootstrap):
+class BootstrapRunner(_RpcClientBase, _generated_rpc.RpcClientBootstrap):
"""RPC wrappers for bootstrapping.
"""
"""Initializes this class.
"""
+ _RpcClientBase.__init__(self, _SsconfResolver, _ENCODERS.get)
_generated_rpc.RpcClientBootstrap.__init__(self)
- self._proc = _RpcProcessor(_SsconfResolver,
- netutils.GetDaemonPort(constants.NODED))
-
- def _Call(self, node_list, procedure, timeout, args):
- """Entry point for automatically generated RPC wrappers.
-
- """
- body = serializer.DumpJson(args, indent=False)
-
- return self._proc(node_list, procedure, body, read_timeout=timeout)
-
-class ConfigRunner(_generated_rpc.RpcClientConfig):
+class ConfigRunner(_RpcClientBase, _generated_rpc.RpcClientConfig):
"""RPC wrappers for L{config}.
"""
- _PrepareFileUpload = \
- staticmethod(RpcRunner._PrepareFileUpload) # pylint: disable=W0212
-
def __init__(self, address_list):
"""Initializes this class.
"""
- _generated_rpc.RpcClientConfig.__init__(self)
-
if address_list is None:
resolver = _SsconfResolver
else:
# Caller provided an address list
resolver = _StaticResolver(address_list)
- self._proc = _RpcProcessor(resolver,
- netutils.GetDaemonPort(constants.NODED))
-
- def _Call(self, node_list, procedure, timeout, args):
- """Entry point for automatically generated RPC wrappers.
-
- """
- body = serializer.DumpJson(args, indent=False)
-
- return self._proc(node_list, procedure, body, read_timeout=timeout)
+ _RpcClientBase.__init__(self, resolver, _ENCODERS.get)
+ _generated_rpc.RpcClientConfig.__init__(self)
- List of arguments as tuples
- Name as string
- - Wrapper code ("%s" is replaced with argument name, see L{OBJECT_TO_DICT})
+ - Argument kind used for encoding/decoding
- Description for docstring (can be C{None})
- Return value wrapper (e.g. for deserializing into L{objects}-based objects)
SINGLE = "single-node"
MULTI = "multi-node"
-OBJECT_TO_DICT = "%s.ToDict()"
-OBJECT_LIST_TO_DICT = "map(lambda d: d.ToDict(), %s)"
-INST_TO_DICT = "self._InstDict(%s)"
-
-NODE_TO_DISK_DICT = \
- ("dict((name, %s) for name, disks in %%s.items())" %
- (OBJECT_LIST_TO_DICT % "disks"))
+# Constants for encoding/decoding
+(ED_OBJECT_DICT,
+ ED_OBJECT_DICT_LIST,
+ ED_INST_DICT,
+ ED_INST_DICT_HVP_BEP,
+ ED_NODE_TO_DISK_DICT,
+ ED_INST_DICT_OSP,
+ ED_IMPEXP_IO,
+ ED_FILE_DETAILS,
+ ED_FINALIZE_EXPORT_DISKS,
+ ED_COMPRESS,
+ ED_BLOCKDEV_RENAME) = range(1, 12)
_FILE_STORAGE_CALLS = [
("file_storage_dir_create", SINGLE, TMO_FAST, [
("hypervisor_list", None, "Hypervisors to query for instances"),
], None, "Returns the list of running instances on the given nodes"),
("instance_reboot", SINGLE, TMO_NORMAL, [
- ("inst", INST_TO_DICT, "Instance object"),
+ ("inst", ED_INST_DICT, "Instance object"),
("reboot_type", None, None),
("shutdown_timeout", None, None),
], None, "Returns the list of running instances on the given nodes"),
("instance_shutdown", SINGLE, TMO_NORMAL, [
- ("instance", INST_TO_DICT, "Instance object"),
+ ("instance", ED_INST_DICT, "Instance object"),
("timeout", None, None),
], None, "Stops an instance"),
("instance_run_rename", SINGLE, TMO_SLOW, [
- ("instance", INST_TO_DICT, "Instance object"),
+ ("instance", ED_INST_DICT, "Instance object"),
("old_name", None, None),
("debug", None, None),
], None, "Run the OS rename script for an instance"),
("instance_migratable", SINGLE, TMO_NORMAL, [
- ("instance", INST_TO_DICT, "Instance object"),
+ ("instance", ED_INST_DICT, "Instance object"),
], None, "Checks whether the given instance can be migrated"),
("migration_info", SINGLE, TMO_NORMAL, [
- ("instance", INST_TO_DICT, "Instance object"),
+ ("instance", ED_INST_DICT, "Instance object"),
], None,
"Gather the information necessary to prepare an instance migration"),
("accept_instance", SINGLE, TMO_NORMAL, [
- ("instance", INST_TO_DICT, "Instance object"),
+ ("instance", ED_INST_DICT, "Instance object"),
("info", None, "Result for the call_migration_info call"),
("target", None, "Target hostname (usually an IP address)"),
], None, "Prepare a node to accept an instance"),
("instance_finalize_migration_dst", SINGLE, TMO_NORMAL, [
- ("instance", INST_TO_DICT, "Instance object"),
+ ("instance", ED_INST_DICT, "Instance object"),
("info", None, "Result for the call_migration_info call"),
("success", None, "Whether the migration was a success or failure"),
], None, "Finalize any target-node migration specific operation"),
("instance_migrate", SINGLE, TMO_SLOW, [
- ("instance", INST_TO_DICT, "Instance object"),
+ ("instance", ED_INST_DICT, "Instance object"),
("target", None, "Target node name"),
("live", None, "Whether the migration should be done live or not"),
], None, "Migrate an instance"),
("instance_finalize_migration_src", SINGLE, TMO_SLOW, [
- ("instance", INST_TO_DICT, "Instance object"),
+ ("instance", ED_INST_DICT, "Instance object"),
("success", None, "Whether the migration succeeded or not"),
("live", None, "Whether the user requested a live migration or not"),
], None, "Finalize the instance migration on the source node"),
("instance_get_migration_status", SINGLE, TMO_SLOW, [
- ("instance", INST_TO_DICT, "Instance object"),
+ ("instance", ED_INST_DICT, "Instance object"),
], "self._MigrationStatusPostProc", "Report migration status"),
("instance_start", SINGLE, TMO_NORMAL, [
- ("instance_hvp_bep", "self._InstDictHvpBep(%s)", None),
+ ("instance_hvp_bep", ED_INST_DICT_HVP_BEP, None),
("startup_paused", None, None),
], None, "Starts an instance"),
("instance_os_add", SINGLE, TMO_1DAY, [
- ("instance_osp", "self._InstDictOsp(%s)", None),
+ ("instance_osp", ED_INST_DICT_OSP, None),
("reinstall", None, None),
("debug", None, None),
], None, "Starts an instance"),
_IMPEXP_CALLS = [
("import_start", SINGLE, TMO_NORMAL, [
- ("opts", OBJECT_TO_DICT, None),
- ("instance", INST_TO_DICT, None),
+ ("opts", ED_OBJECT_DICT, None),
+ ("instance", ED_INST_DICT, None),
("component", None, None),
- ("dest", "self._EncodeImportExportIO(%s)", "Import destination"),
+ ("dest", ED_IMPEXP_IO, "Import destination"),
], None, "Starts an import daemon"),
("export_start", SINGLE, TMO_NORMAL, [
- ("opts", OBJECT_TO_DICT, None),
+ ("opts", ED_OBJECT_DICT, None),
("host", None, None),
("port", None, None),
- ("instance", INST_TO_DICT, None),
+ ("instance", ED_INST_DICT, None),
("component", None, None),
- ("source", "self._EncodeImportExportIO(%s)", "Export source"),
+ ("source", ED_IMPEXP_IO, "Export source"),
], None, "Starts an export daemon"),
("impexp_status", SINGLE, TMO_FAST, [
("names", None, "Import/export names"),
("path", None, None),
], None, "Queries the export information in a given path"),
("finalize_export", SINGLE, TMO_NORMAL, [
- ("instance", INST_TO_DICT, None),
- ("snap_disks", "self._PrepareFinalizeExportDisks(%s)", None),
+ ("instance", ED_INST_DICT, None),
+ ("snap_disks", ED_FINALIZE_EXPORT_DISKS, None),
], None, "Request the completion of an export operation"),
("export_list", MULTI, TMO_FAST, [], None, "Gets the stored exports list"),
("export_remove", SINGLE, TMO_FAST, [
("devices", None, None),
], None, "Gets the sizes of requested block devices present on a node"),
("blockdev_create", SINGLE, TMO_NORMAL, [
- ("bdev", OBJECT_TO_DICT, None),
+ ("bdev", ED_OBJECT_DICT, None),
("size", None, None),
("owner", None, None),
("on_primary", None, None),
("info", None, None),
], None, "Request creation of a given block device"),
("blockdev_wipe", SINGLE, TMO_SLOW, [
- ("bdev", OBJECT_TO_DICT, None),
+ ("bdev", ED_OBJECT_DICT, None),
("offset", None, None),
("size", None, None),
], None,
"Request wipe at given offset with given size of a block device"),
("blockdev_remove", SINGLE, TMO_NORMAL, [
- ("bdev", OBJECT_TO_DICT, None),
+ ("bdev", ED_OBJECT_DICT, None),
], None, "Request removal of a given block device"),
("blockdev_pause_resume_sync", SINGLE, TMO_NORMAL, [
- ("disks", OBJECT_LIST_TO_DICT, None),
+ ("disks", ED_OBJECT_DICT_LIST, None),
("pause", None, None),
], None, "Request a pause/resume of given block device"),
("blockdev_assemble", SINGLE, TMO_NORMAL, [
- ("disk", OBJECT_TO_DICT, None),
+ ("disk", ED_OBJECT_DICT, None),
("owner", None, None),
("on_primary", None, None),
("idx", None, None),
], None, "Request assembling of a given block device"),
("blockdev_shutdown", SINGLE, TMO_NORMAL, [
- ("disk", OBJECT_TO_DICT, None),
+ ("disk", ED_OBJECT_DICT, None),
], None, "Request shutdown of a given block device"),
("blockdev_addchildren", SINGLE, TMO_NORMAL, [
- ("bdev", OBJECT_TO_DICT, None),
- ("ndevs", OBJECT_LIST_TO_DICT, None),
+ ("bdev", ED_OBJECT_DICT, None),
+ ("ndevs", ED_OBJECT_DICT_LIST, None),
], None, "Request adding a list of children to a (mirroring) device"),
("blockdev_removechildren", SINGLE, TMO_NORMAL, [
- ("bdev", OBJECT_TO_DICT, None),
- ("ndevs", OBJECT_LIST_TO_DICT, None),
+ ("bdev", ED_OBJECT_DICT, None),
+ ("ndevs", ED_OBJECT_DICT_LIST, None),
], None, "Request removing a list of children from a (mirroring) device"),
("blockdev_close", SINGLE, TMO_NORMAL, [
("instance_name", None, None),
- ("disks", OBJECT_LIST_TO_DICT, None),
+ ("disks", ED_OBJECT_DICT_LIST, None),
], None, "Closes the given block devices"),
("blockdev_getsize", SINGLE, TMO_NORMAL, [
- ("disks", OBJECT_LIST_TO_DICT, None),
+ ("disks", ED_OBJECT_DICT_LIST, None),
], None, "Returns the size of the given disks"),
("drbd_disconnect_net", MULTI, TMO_NORMAL, [
("nodes_ip", None, None),
- ("disks", OBJECT_LIST_TO_DICT, None),
+ ("disks", ED_OBJECT_DICT_LIST, None),
], None, "Disconnects the network of the given drbd devices"),
("drbd_attach_net", MULTI, TMO_NORMAL, [
("nodes_ip", None, None),
- ("disks", OBJECT_LIST_TO_DICT, None),
+ ("disks", ED_OBJECT_DICT_LIST, None),
("instance_name", None, None),
("multimaster", None, None),
], None, "Connects the given DRBD devices"),
("drbd_wait_sync", MULTI, TMO_SLOW, [
("nodes_ip", None, None),
- ("disks", OBJECT_LIST_TO_DICT, None),
+ ("disks", ED_OBJECT_DICT_LIST, None),
], None, "Waits for the synchronization of drbd devices is complete"),
("blockdev_grow", SINGLE, TMO_NORMAL, [
- ("cf_bdev", OBJECT_TO_DICT, None),
+ ("cf_bdev", ED_OBJECT_DICT, None),
("amount", None, None),
("dryrun", None, None),
], None, "Request a snapshot of the given block device"),
("blockdev_export", SINGLE, TMO_1DAY, [
- ("cf_bdev", OBJECT_TO_DICT, None),
+ ("cf_bdev", ED_OBJECT_DICT, None),
("dest_node", None, None),
("dest_path", None, None),
("cluster_name", None, None),
], None, "Export a given disk to another node"),
("blockdev_snapshot", SINGLE, TMO_NORMAL, [
- ("cf_bdev", OBJECT_TO_DICT, None),
+ ("cf_bdev", ED_OBJECT_DICT, None),
], None, "Export a given disk to another node"),
("blockdev_rename", SINGLE, TMO_NORMAL, [
- ("devlist", "[(d.ToDict(), uid) for d, uid in %s]", None),
+ ("devlist", ED_BLOCKDEV_RENAME, None),
], None, "Request rename of the given block devices"),
("blockdev_find", SINGLE, TMO_NORMAL, [
- ("disk", OBJECT_TO_DICT, None),
+ ("disk", ED_OBJECT_DICT, None),
], "self._BlockdevFindPostProc",
"Request identification of a given block device"),
("blockdev_getmirrorstatus", SINGLE, TMO_NORMAL, [
- ("disks", OBJECT_LIST_TO_DICT, None),
+ ("disks", ED_OBJECT_DICT_LIST, None),
], "self._BlockdevGetMirrorStatusPostProc",
"Request status of a (mirroring) device"),
("blockdev_getmirrorstatus_multi", MULTI, TMO_NORMAL, [
- ("node_disks", NODE_TO_DISK_DICT, None),
+ ("node_disks", ED_NODE_TO_DISK_DICT, None),
], "self._BlockdevGetMirrorStatusMultiPostProc",
"Request status of (mirroring) devices from multiple nodes"),
]
"RpcClientJobQueue": [
("jobqueue_update", MULTI, TMO_URGENT, [
("file_name", None, None),
- ("content", "self._Compress(%s)", None),
+ ("content", ED_COMPRESS, None),
], None, "Update job queue file"),
("jobqueue_purge", SINGLE, TMO_NORMAL, [], None, "Purge job queue"),
("jobqueue_rename", MULTI, TMO_URGENT, [
],
"RpcClientConfig": [
("upload_file", MULTI, TMO_NORMAL, [
- ("file_name", "self._PrepareFileUpload(%s)", None),
+ ("file_name", ED_FILE_DETAILS, None),
], None, "Upload a file"),
("write_ssconf_files", MULTI, TMO_NORMAL, [
("values", None, None),