Nir Soffer has uploaded a new change for review.

Change subject: virt.vmdevices.core: Add Lease device
......................................................................

virt.vmdevices.core: Add Lease device

Previously virt.vmdevices.storage.Drive was creating lease device xml
for a drive. We want to add vm leases which are not attached to any
drive, so we need to separate the lease from the Drive class.

This patch adds new Lease device class and use it in Drive to generate
the drive lease element.

Unfinished: need to initialize vm lease devices in the vm device list
dict, and add the lease xml to the domain xml.

Change-Id: I10fc2be51eccdd93e4c2440008f22a4ba019c466
Signed-off-by: Nir Soffer <nsof...@redhat.com>
---
M tests/vmTests.py
M vdsm/virt/vm.py
M vdsm/virt/vmdevices/core.py
M vdsm/virt/vmdevices/storage.py
4 files changed, 73 insertions(+), 16 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/vdsm refs/changes/65/65465/1

diff --git a/tests/vmTests.py b/tests/vmTests.py
index 6b7bb61..b793dcc 100644
--- a/tests/vmTests.py
+++ b/tests/vmTests.py
@@ -1732,6 +1732,22 @@
         self.assertEqual(devices[:-1], filtered)
 
 
+class TestLease(XMLTestCase):
+
+    def test_getxml(self):
+        lease = vmdevices.core.Lease({}, self.log, id="id", sd_id="sd_id",
+                                     path="/path", offset=1048576)
+        lease_xml = lease.getXML().toxml()
+        xml = """
+        <lease>
+            <key>id</key>
+            <lockspace>sd_id</lockspace>
+            <target offset="1048576" path="/path" />
+        </lease>
+        """
+        self.assertXMLEqual(lease_xml, xml)
+
+
 def _load_xml(name):
     test_path = os.path.realpath(__file__)
     data_path = os.path.join(os.path.split(test_path)[0], 'devices', 'data')
diff --git a/vdsm/virt/vm.py b/vdsm/virt/vm.py
index 12cc56e..c20571f 100644
--- a/vdsm/virt/vm.py
+++ b/vdsm/virt/vm.py
@@ -1545,6 +1545,10 @@
         if serial_console is not None:
             domxml._devices.appendChild(serial_console.getSerialDeviceXML())
 
+        # TODO: add vm lease device
+        # for lease in self._devices[hwclass.LEASE]:
+        #     domxml._devices.appendChild(lease.getXML())
+
         for drive in self._devices[hwclass.DISK][:]:
             for leaseElement in drive.getLeasesXML():
                 domxml._devices.appendChild(leaseElement)
diff --git a/vdsm/virt/vmdevices/core.py b/vdsm/virt/vmdevices/core.py
index 90b8cd8..725036a 100644
--- a/vdsm/virt/vmdevices/core.py
+++ b/vdsm/virt/vmdevices/core.py
@@ -631,3 +631,45 @@
         target.appendChild(node)
 
         return mem
+
+
+class Lease(Base):
+    """
+    VM lease device.
+
+    See https://libvirt.org/formatdomain.html#elementsLease
+    """
+    __slots__ = ("id", "sd_id", "path", "offset")
+
+    def __init__(self, conf, log, **kwargs):
+        super(Lease, self).__init__(conf, log, **kwargs)
+        """
+        Initialize a lease element.
+
+        :param uuid id: Lease id, e.g. volume id for a volume lease, or vm id
+            for a vm lease
+        :param uuid sd_id: Storage domain uuid where lease file is located
+        :param str path: Path to lease file or block device
+        :param int offset: Offset in lease file in bytes
+        """
+        # TODO: validate arguments
+
+    def getXML(self):
+        """
+        Return xml element.
+
+        <lease>
+            <key>12523e3d-ad22-410c-8977-d2a7bf458a65</key>
+            <lockspace>c2a6d7c8-8d81-4e01-9ed4-7eb670713448</lockspace>
+            <target offset="1048576"
+                    path="/dev/c2a6d7c8-8d81-4e01-9ed4-7eb670713448/leases"/>
+        </lease>
+
+        :rtype: `vmxml.Element`
+        """
+        lease = vmxml.Element('lease')
+        lease.appendChildWithArgs('key', text=self.id)
+        lease.appendChildWithArgs('lockspace', text=self.sd_id)
+        lease.appendChildWithArgs('target', path=self.path,
+                                  offset=str(self.offset))
+        return lease
diff --git a/vdsm/virt/vmdevices/storage.py b/vdsm/virt/vmdevices/storage.py
index 78bf00c..39ff524 100644
--- a/vdsm/virt/vmdevices/storage.py
+++ b/vdsm/virt/vmdevices/storage.py
@@ -26,9 +26,8 @@
 from vdsm import utils
 
 from .. import vmxml
+from . import core
 from . import hwclass
-
-from .core import Base
 
 DEFAULT_INTERFACE_FOR_ARCH = {
     cpuarch.X86_64: 'ide',
@@ -55,7 +54,7 @@
         return (cls.NONE, cls.EXCLUSIVE, cls.SHARED, cls.TRANSIENT)
 
 
-class Drive(Base):
+class Drive(core.Base):
     __slots__ = ('iface', '_path', 'readonly', 'bootOrder', 'domainID',
                  'poolID', 'imageID', 'UUID', 'volumeID', 'format',
                  'propagateErrors', 'address', 'apparentsize', 'volumeInfo',
@@ -385,13 +384,9 @@
 
     def getLeasesXML(self):
         """
-        Create domxml for the drive lease.
+        Create domxml for the drive leases.
 
-        <lease>
-            <key>volumeID</key>
-            <lockspace>domainID</lockspace>
-            <target offset="0" path="/path/to/lease"/>
-        </lease>
+        See `.lease.Lease.getXML` for more info.
         """
         if not self.hasVolumeLeases:
             return  # empty items generator
@@ -400,13 +395,13 @@
         # when libvirt will support shared leases this will loop over all the
         # volumes
         for volInfo in self.volumeChain[-1:]:
-            lease = vmxml.Element('lease')
-            lease.appendChildWithArgs('key', text=volInfo['volumeID'])
-            lease.appendChildWithArgs('lockspace',
-                                      text=volInfo['domainID'])
-            lease.appendChildWithArgs('target', path=volInfo['leasePath'],
-                                      offset=str(volInfo['leaseOffset']))
-            yield lease
+            lease = core.Lease(self.conf,
+                               self.log,
+                               id=volInfo['volumeID'],
+                               sd_id=volInfo['domainID'],
+                               path=volInfo['leasePath'],
+                               offset=volInfo['leaseOffset'])
+            yield lease.getXML()
 
     def getXML(self):
         """


-- 
To view, visit https://gerrit.ovirt.org/65465
To unsubscribe, visit https://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I10fc2be51eccdd93e4c2440008f22a4ba019c466
Gerrit-PatchSet: 1
Gerrit-Project: vdsm
Gerrit-Branch: master
Gerrit-Owner: Nir Soffer <nsof...@redhat.com>
_______________________________________________
vdsm-patches mailing list -- vdsm-patches@lists.fedorahosted.org
To unsubscribe send an email to vdsm-patches-le...@lists.fedorahosted.org

Reply via email to