Commit 7ddf5120 authored by Antoine Millet's avatar Antoine Millet
Browse files

Refactored storage and volume to enhance DRY

parent aeb2a583
Loading
Loading
Loading
Loading
+1 −16
Original line number Diff line number Diff line
@@ -107,22 +107,7 @@ class Handler(HostHandler):
        self.virt_connected = True

        # register hypervisor storage tags
        for name, storage in self.hypervisor.storage.storages.iteritems():
            self.tag_db.add_tags((
                Tag('sto%s_state' % name, lambda sto: sto.state, 5, 5, storage),
                Tag('sto%s_size' % name,
                    lambda sto: sto.capacity, 5, 5, storage),
                Tag('sto%s_free' % name,
                    lambda sto: sto.available, 5, 5, storage),
                Tag('sto%s_used' % name,
                    lambda sto: sto.capacity - sto.available, 5, 5, storage),
                Tag('sto%s_type' % name, lambda sto: sto.type, 5, 5, storage),
                Tag('sto%s_vol' % name,
                    lambda sto: ' '.join(sto.volumes) if sto.volumes and sto.type != 'rbd' else None,
                    5, 5, storage),
                Tag('sto%s_ratio' % name,
                    lambda sto: '%0.2f' % (1 - float(sto.available) / sto.capacity), 5, 5, storage),
            ))
        self.hypervisor.storage.update()

        # register domains
        for dom in self.hypervisor.domains.itervalues():
+2 −2
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ class VirtualMachine(object):
            for t in (
                Tag('disk%s_size' % i, v.capacity, 10),
                Tag('disk%s_path' % i, v.path, 10),
                Tag('disk%s_pool' % i, v.storage, 10),  # FIXME: change
                Tag('disk%s_pool' % i, v.storage.name, 10),  # FIXME: change
                Tag('disk%s_vol' % i, v.name, 10),
                Tag('disk%s_cache' % i, lambda: self.cache_behaviour.get(v.path), 10)
            ):
@@ -568,4 +568,4 @@ class VirtualMachine(object):
            if live:  # In live mode, we are responsible for source volume cleaning
                for volume in volumes:
                    # Delete VM storage after migration success:
                    self.hypervisor.storage.delete_volume(volume.storage, volume.name)
                    self.hypervisor.storage.delete_volume(volume.storage.name, volume.name)
+13 −29
Original line number Diff line number Diff line
@@ -298,21 +298,9 @@ class StorageIndex(object):
        """
        self.handler = handler
        self.lv_con = lv_con
        self.storages = dict(
            (s.name, s) for s in imap(
                Storage,
                imap(
                    lv_con.storagePoolLookupByName,
                    chain(
                        lv_con.listDefinedStoragePools(),
                        lv_con.listStoragePools(),
                    ),
                ),
            ),
        )

        self.storages = {}
        self.paths = None
        self.update_path_index()
        self.update()

    def update(self):
        """Update storage pools and volumes."""
@@ -339,6 +327,11 @@ class StorageIndex(object):
                    Tag('sto%s_used' % s.name,
                        lambda: s.capacity - s.available, 5, 5),
                    Tag('sto%s_type' % s.name, lambda: s.type, 5, 5),
                    Tag('sto%s_vol' % s.name,
                        lambda: ' '.join(s.volumes) if s.volumes and s.type != 'rbd' else None,
                        5, 5),
                    Tag('sto%s_ratio' % s.name,
                        lambda: '%0.2f' % (1 - float(s.available) / s.capacity), 5, 5),
                ))

        self.update_path_index()
@@ -385,11 +378,7 @@ class StorageIndex(object):
            logger.exception('Error while creating volume')
            raise

        new_volume = Volume(new_volume)
        # if success add the volume to the index
        self.paths[new_volume.path] = new_volume
        # and also to its storage pool
        self.storages[new_volume.storage].volumes[new_volume.name] = new_volume
        self.update()

        return new_volume

@@ -436,13 +425,8 @@ class Storage(object):
        self.type = et.ElementTree().parse(
            StringIO(lv_storage.XMLDesc(0))).get('type')

        self.volumes = dict((v.name, v) for v in imap(
            Volume,
            (lv_storage.storageVolLookupByName(n) for n in
             lv_storage.listVolumes()),
        ))

        self.update_attr()
        self.volumes = {}
        self.update()

    def update(self):
        self.update_attr()
@@ -454,7 +438,7 @@ class Storage(object):
                self.volumes[vol_name].update()
            else:
                # add volume
                v = Volume(self.lv_storage.storageVolLookupByName(vol_name))
                v = Volume(self, self.lv_storage.storageVolLookupByName(vol_name))
                self.volumes[v.name] = v

    def update_attr(self):
@@ -467,11 +451,11 @@ class Storage(object):

class Volume(object):
    """Volume abstraction."""
    def __init__(self, lv_volume):
    def __init__(self, storage, lv_volume):
        """
        :param lv_volume: Libvirt volume instance
        """
        self.storage = lv_volume.storagePoolLookupByVolume().name()
        self.storage = storage
        self.path = lv_volume.path()
        self.name = lv_volume.name()
        self.capacity, self.allocation = None, None