diff options
| author | Rick Harris <rconradharris@gmail.com> | 2012-09-06 21:42:08 +0000 |
|---|---|---|
| committer | Rick Harris <rconradharris@gmail.com> | 2012-09-07 21:27:24 +0000 |
| commit | 6392ad2924874487b7428e70f7cd49748fff8e5c (patch) | |
| tree | f4356db5cfc2ae85e029f69a1d598deabc5a9112 /plugins | |
| parent | a68dfb7c9a66eb55638f8370b1cabe2fe3050786 (diff) | |
| download | nova-6392ad2924874487b7428e70f7cd49748fff8e5c.tar.gz nova-6392ad2924874487b7428e70f7cd49748fff8e5c.tar.xz nova-6392ad2924874487b7428e70f7cd49748fff8e5c.zip | |
xenapi: Make dom0 serialization consistent.
The dom0 plugin code had been using `pickle` for serializing input and
`json` for serializing output which was needlessly inconsistent. This
patch makes the code use `pickle`--chosen for its better handling of
`datetime` objects--for both sending and receiving data.
This patch also refactors the code so that neither the caller nor the
callee need to explicitly worry about serialization: the caller just
passes in args and kwargs, and the callee's function signature just
accepts the args and kwargs as usual.
Bonus: Removes unecessary imports
Change-Id: I3abb42eeebd8d37d67e6c26fa7bcae66d876b3ee
Diffstat (limited to 'plugins')
4 files changed, 34 insertions, 92 deletions
diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/glance b/plugins/xenserver/xenapi/etc/xapi.d/plugins/glance index a574bb406..924bf10d7 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/glance +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/glance @@ -20,18 +20,9 @@ """Handle the uploading and downloading of images via Glance.""" -import cPickle as pickle import httplib -try: - import json -except ImportError: - import simplejson as json import md5 -import os -import shutil - import urllib2 -import XenAPIPlugin import utils @@ -202,19 +193,11 @@ def _upload_tarball(staging_path, image_id, glance_host, glance_port, conn.close() -def download_vhd(session, args): +def download_vhd(session, image_id, glance_host, glance_port, glance_use_ssl, + uuid_stack, sr_path, auth_token): """Download an image from Glance, unbundle it, and then deposit the VHDs into the storage repository """ - params = pickle.loads(exists(args, 'params')) - image_id = params["image_id"] - glance_host = params["glance_host"] - glance_port = params["glance_port"] - glance_use_ssl = params["glance_use_ssl"] - uuid_stack = params["uuid_stack"] - sr_path = params["sr_path"] - auth_token = params["auth_token"] - staging_path = utils.make_staging_area(sr_path) try: # Download tarball into staging area and extract it @@ -223,28 +206,15 @@ def download_vhd(session, args): glance_use_ssl, auth_token) # Move the VHDs from the staging area into the storage repository - imported_vhds = utils.import_vhds(sr_path, staging_path, uuid_stack) + return utils.import_vhds(sr_path, staging_path, uuid_stack) finally: utils.cleanup_staging_area(staging_path) - # Right now, it's easier to return a single string via XenAPI, - # so we'll json encode the list of VHDs. - return json.dumps(imported_vhds) - -def upload_vhd(session, args): +def upload_vhd(session, vdi_uuids, image_id, glance_host, glance_port, + glance_use_ssl, sr_path, auth_token, properties): """Bundle the VHDs comprising an image and then stream them into Glance. """ - params = pickle.loads(exists(args, 'params')) - vdi_uuids = params["vdi_uuids"] - image_id = params["image_id"] - glance_host = params["glance_host"] - glance_port = params["glance_port"] - glance_use_ssl = params["glance_use_ssl"] - sr_path = params["sr_path"] - auth_token = params["auth_token"] - properties = params["properties"] - staging_path = utils.make_staging_area(sr_path) try: utils.prepare_staging_area(sr_path, staging_path, vdi_uuids) @@ -253,9 +223,6 @@ def upload_vhd(session, args): finally: utils.cleanup_staging_area(staging_path) - return "" # Nothing useful to return on an upload - if __name__ == '__main__': - XenAPIPlugin.dispatch({'upload_vhd': upload_vhd, - 'download_vhd': download_vhd}) + utils.register_plugin_calls(download_vhd, upload_vhd) diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/migration b/plugins/xenserver/xenapi/etc/xapi.d/plugins/migration index 85ad0bdbc..35316a9b8 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/migration +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/migration @@ -18,37 +18,18 @@ """ XenAPI Plugin for transfering data between host nodes """ - -import cPickle as pickle -try: - import json -except ImportError: - import simplejson as json -import os -import os.path -import shlex -import shutil -import subprocess - -import XenAPIPlugin - import utils from pluginlib_nova import * configure_logging('migration') -def move_vhds_into_sr(session, args): +def move_vhds_into_sr(session, instance_uuid, sr_path, uuid_stack): """Moves the VHDs from their copied location to the SR""" - params = pickle.loads(exists(args, 'params')) - instance_uuid = params['instance_uuid'] - sr_path = params['sr_path'] - uuid_stack = params['uuid_stack'] - staging_path = "/images/instance%s" % instance_uuid imported_vhds = utils.import_vhds(sr_path, staging_path, uuid_stack) utils.cleanup_staging_area(staging_path) - return json.dumps(imported_vhds) + return imported_vhds def _rsync_vhds(instance_uuid, host, staging_path, user="root"): @@ -65,15 +46,8 @@ def _rsync_vhds(instance_uuid, host, staging_path, user="root"): utils.finish_subprocess(rsync_proc, rsync_cmd) -def transfer_vhd(session, args): +def transfer_vhd(session, instance_uuid, host, vdi_uuid, sr_path, seq_num): """Rsyncs a VHD to an adjacent host""" - params = pickle.loads(exists(args, 'params')) - instance_uuid = params['instance_uuid'] - host = params['host'] - vdi_uuid = params['vdi_uuid'] - sr_path = params['sr_path'] - seq_num = params['seq_num'] - staging_path = utils.make_staging_area(sr_path) try: utils.prepare_staging_area( @@ -82,9 +56,6 @@ def transfer_vhd(session, args): finally: utils.cleanup_staging_area(staging_path) - return "" - if __name__ == '__main__': - XenAPIPlugin.dispatch({'transfer_vhd': transfer_vhd, - 'move_vhds_into_sr': move_vhds_into_sr}) + utils.register_plugin_calls(move_vhds_into_sr, transfer_vhd) diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/utils.py b/plugins/xenserver/xenapi/etc/xapi.d/plugins/utils.py index 9eba58936..027d26a67 100644 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/utils.py +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/utils.py @@ -14,6 +14,7 @@ """Various utilities used by XenServer plugins.""" +import cPickle as pickle import logging import os import shlex @@ -21,6 +22,7 @@ import shutil import subprocess import tempfile +import XenAPIPlugin CHUNK_SIZE = 8192 @@ -362,3 +364,21 @@ def extract_tarball(fileobj, path, callback=None): tar_proc.stdin.write(chunk) finish_subprocess(tar_proc, tar_cmd) + + +def _handle_serialization(func): + def wrapped(session, params): + params = pickle.loads(params['params']) + rv = func(session, *params['args'], **params['kwargs']) + return pickle.dumps(rv) + return wrapped + + +def register_plugin_calls(*funcs): + """Wrapper around XenAPIPlugin.dispatch which handles pickle + serialization. + """ + wrapped_dict = {} + for func in funcs: + wrapped_dict[func.__name__] = _handle_serialization(func) + XenAPIPlugin.dispatch(wrapped_dict) diff --git a/plugins/xenserver/xenapi/etc/xapi.d/plugins/workarounds b/plugins/xenserver/xenapi/etc/xapi.d/plugins/workarounds index 771c27a0d..244939f00 100755 --- a/plugins/xenserver/xenapi/etc/xapi.d/plugins/workarounds +++ b/plugins/xenserver/xenapi/etc/xapi.d/plugins/workarounds @@ -17,21 +17,14 @@ """Handle the uploading and downloading of images via Glance.""" -import cPickle as pickle -try: - import json -except ImportError: - import simplejson as json import os import shutil -import XenAPIPlugin - import utils #FIXME(sirp): should this use pluginlib from 5.6? from pluginlib_nova import * -configure_logging('hacks') +configure_logging('workarounds') def _copy_vdis(sr_path, staging_path, vdi_uuids): @@ -43,23 +36,14 @@ def _copy_vdis(sr_path, staging_path, vdi_uuids): seq_num += 1 -def safe_copy_vdis(session, args): - params = pickle.loads(exists(args, 'params')) - sr_path = params["sr_path"] - vdi_uuids = params["vdi_uuids"] - uuid_stack = params["uuid_stack"] - +def safe_copy_vdis(session, sr_path, vdi_uuids, uuid_stack): staging_path = utils.make_staging_area(sr_path) try: _copy_vdis(sr_path, staging_path, vdi_uuids) - imported_vhds = utils.import_vhds(sr_path, staging_path, uuid_stack) + return utils.import_vhds(sr_path, staging_path, uuid_stack) finally: utils.cleanup_staging_area(staging_path) - # Right now, it's easier to return a single string via XenAPI, - # so we'll json encode the list of VHDs. - return json.dumps(imported_vhds) - if __name__ == '__main__': - XenAPIPlugin.dispatch({'safe_copy_vdis': safe_copy_vdis}) + utils.register_plugin_calls(safe_copy_vdis) |
