reconnect to libvirt when the (unique) connection object looks broken
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Wed, 4 Nov 2015 10:39:44 +0000 (11:39 +0100)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Wed, 4 Nov 2015 10:39:44 +0000 (11:39 +0100)
sliver_libvirt.py

index a70d18f..f62c1a7 100644 (file)
@@ -41,11 +41,33 @@ class Sliver_Libvirt(Account):
 
     @staticmethod
     def getConnection(sliver_type):
-        # TODO: error checking
-        # vtype is of the form sliver.[LXC/QEMU] we need to lower case to lxc/qemu
+        """
+        returns a connection to the underlying libvirt service
+        a single connection is created and shared among slivers
+        this call ensures the connection is alive
+        and will reconnect if it appears to be necessary
+        """
+        # sliver_type comes from rec['type'] and is of the form sliver.{LXC,QEMU}
+        # so we need to lower case to lxc/qemu
         vtype = sliver_type.split('.')[1].lower()
-        uri = vtype + '://'
-        return connections.setdefault(uri, libvirt.open(uri))
+        uri = vtype + ':///'
+        if uri not in connections:
+            # create connection
+            conn = libvirt.open(uri)
+            connections[uri] = conn
+            return conn
+        else:
+            # connection already available : check for health
+            conn = connections[uri]
+            # see if a reconnection is needed
+            try:
+                numDomains = conn.numOfDomains()
+            except:
+                logger.log("libvirt connection to {} looks broken - reconnecting".format(uri))
+                conn = libvirt.open(uri)
+                # if this fails then an expection is thrown outside of this function
+                numDomains = conn.numOfDomains()
+            return conn
 
     def __init__(self, rec):
         self.name = rec['name']