diff options
| author | Daniel P. Berrange <berrange@redhat.com> | 2012-07-20 12:14:32 +0100 |
|---|---|---|
| committer | Daniel P. Berrange <berrange@redhat.com> | 2012-07-20 13:47:02 +0100 |
| commit | 2766ee2b6ed68f1784aad1b41e81baa75cc305df (patch) | |
| tree | 474caeff58cf7ed7e5a780e561867a9f09cda7f0 | |
| parent | 1ec856db22c798bffbf0f7d5def0e880a8fc9842 (diff) | |
Refactor libvirt imagebackend module to reduce code duplication
Currently each subclass of the Image class in imagebackend,
repeats the impl of the 'libvirt_info' method. These methods
are almost identical, only varying on two per-class settings.
By providing the driver_format and source_type to the Image
class constructor, it is possible to get to a single impl
of the 'libvirt_info' method.
Change-Id: I0807eed7f03aedf4980eedeb70195076ed5f4710
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
| -rw-r--r-- | nova/virt/libvirt/imagebackend.py | 64 |
1 files changed, 24 insertions, 40 deletions
diff --git a/nova/virt/libvirt/imagebackend.py b/nova/virt/libvirt/imagebackend.py index 7140e6527..bc0489fbd 100644 --- a/nova/virt/libvirt/imagebackend.py +++ b/nova/virt/libvirt/imagebackend.py @@ -50,14 +50,14 @@ FLAGS.register_opts(__imagebackend_opts) class Image(object): __metaclass__ = abc.ABCMeta - @abc.abstractmethod - def __init__(self, instance, name): + def __init__(self, source_type, driver_format): """Image initialization. - :instance: Instance name. - :name: Image name. + :source_type: block or file + :driver_format: raw or qcow2 """ - pass + self.source_type = source_type + self.driver_format = driver_format @abc.abstractmethod def create_image(self, prepare_template, base, size, *args, **kwargs): @@ -72,7 +72,6 @@ class Image(object): """ pass - @abc.abstractmethod def libvirt_info(self, disk_bus, disk_dev, device_type, cache_mode): """Get `LibvirtConfigGuestDisk` filled for this image. @@ -81,7 +80,15 @@ class Image(object): :device_type: Device type for this image. :cache_mode: Caching mode for this image """ - pass + info = config.LibvirtConfigGuestDisk() + info.source_type = self.source_type + info.source_device = device_type + info.target_bus = disk_bus + info.target_dev = disk_dev + info.driver_cache = cache_mode + info.driver_format = self.driver_format + info.source_path = self.path + return info def cache(self, fn, fname, size=None, *args, **kwargs): """Creates image from template. @@ -112,20 +119,11 @@ class Image(object): class Raw(Image): def __init__(self, instance, name): + super(Raw, self).__init__("file", "raw") + self.path = os.path.join(FLAGS.instances_path, instance, name) - def libvirt_info(self, disk_bus, disk_dev, device_type, cache_mode): - info = config.LibvirtConfigGuestDisk() - info.source_type = 'file' - info.source_device = device_type - info.target_bus = disk_bus - info.target_dev = disk_dev - info.driver_cache = cache_mode - info.driver_format = 'raw' - info.source_path = self.path - return info - def create_image(self, prepare_template, base, size, *args, **kwargs): @utils.synchronized(base) def copy_raw_image(base, target, size): @@ -143,17 +141,12 @@ class Raw(Image): copy_raw_image(base, self.path, size) -class Qcow2(Raw): - def libvirt_info(self, disk_bus, disk_dev, device_type, cache_mode): - info = config.LibvirtConfigGuestDisk() - info.source_type = 'file' - info.source_device = device_type - info.target_bus = disk_bus - info.target_dev = disk_dev - info.driver_cache = cache_mode - info.driver_format = 'qcow2' - info.source_path = self.path - return info +class Qcow2(Image): + def __init__(self, instance, name): + super(Qcow2, self).__init__("file", "qcow2") + + self.path = os.path.join(FLAGS.instances_path, + instance, name) def create_image(self, prepare_template, base, size, *args, **kwargs): @utils.synchronized(base) @@ -178,18 +171,9 @@ class Lvm(Image): def escape(fname): return fname.replace('_', '__') - def libvirt_info(self, disk_bus, disk_dev, device_type, cache_mode): - info = config.LibvirtConfigGuestDisk() - info.source_type = 'block' - info.source_device = device_type - info.target_bus = disk_bus - info.target_dev = disk_dev - info.driver_cache = cache_mode - info.driver_format = 'raw' - info.source_path = self.path - return info - def __init__(self, instance, name): + super(Lvm, self).__init__("block", "raw") + if not FLAGS.libvirt_images_volume_group: raise RuntimeError(_('You should specify' ' libvirt_images_volume_group' |
