no change, just nicer
[nodemanager.git] / sliver_libvirt.py
index 1517ad5..734c72e 100644 (file)
@@ -2,6 +2,7 @@
 
 import sys
 import os, os.path
+import re
 import subprocess
 import pprint
 import random
@@ -72,11 +73,11 @@ class Sliver_Libvirt(Account):
 
     @staticmethod
     def dom_details (dom):
-        output=""
-        output += " id=%s - OSType=%s"%(dom.ID(),dom.OSType())
+        output = ""
+        output += " id=%s - OSType=%s"%(dom.ID(), dom.OSType())
         # calling state() seems to be working fine
-        (state,reason)=dom.state()
-        output += " state=%s, reason=%s"%(STATES.get(state,state),REASONS.get(reason,reason))
+        (state, reason) = dom.state()
+        output += " state=%s, reason=%s"%(STATES.get(state, state),REASONS.get(reason, reason))
         try:
             # try to use info() - this however does not work for some reason on f20
             # info cannot get info operation failed: Cannot read cputime for domain
@@ -89,23 +90,49 @@ class Sliver_Libvirt(Account):
 
     def __repr__(self):
         ''' Helper method to get a "nice" output of the domain struct for debug purposes'''
-        output="Domain %s"%self.name
-        dom=self.dom
+        output = "Domain %s"%self.name
+        dom = self.dom
         if dom is None: 
             output += " [no attached dom ?!?]"
         else:
             output += Sliver_Libvirt.dom_details (dom)
         return output
-            
+
+    # Thierry : I am not quite sure if /etc/libvirt/lxc/<>.xml holds a reliably up-to-date
+    # copy of the sliver XML config; I feel like issuing a virsh dumpxml first might be safer
+    def repair_veth(self):
+        # See workaround email, 2-14-2014, "libvirt 1.2.1 rollout"
+        xml = open("/etc/libvirt/lxc/%s.xml" % self.name).read()
+        veths = re.findall("<target dev='veth[0-9]*'/>", xml)
+        veths = [x[13:-3] for x in veths]
+        for veth in veths:
+            command = ["ip", "link", "delete", veth]
+            logger.log_call(command)
+
+        logger.log("trying to redefine the VM")
+        command = ["virsh", "define", "/etc/libvirt/lxc/%s.xml" % self.name]
+        logger.log_call(command)
 
     def start(self, delay=0):
-        ''' Just start the sliver '''
+        '''Just start the sliver'''
         logger.verbose('sliver_libvirt: %s start'%(self.name))
 
         # Check if it's running to avoid throwing an exception if the
-        # domain was already running, create actually means start
+        # domain was already running
         if not self.is_running():
-            self.dom.create()
+            try:
+                # create actually means start
+                self.dom.create()
+            except Exception, e:
+                # XXX smbaker: attempt to resolve slivers that are stuck in
+                #   "failed to allocate free veth".
+                if "ailed to allocate free veth" in str(e):
+                     logger.log("failed to allocate free veth on %s" % self.name)
+                     self.repair_veth()
+                     logger.log("trying dom.create again")
+                     self.dom.create()
+                else:
+                    raise
         else:
             logger.verbose('sliver_libvirt: sliver %s already started'%(self.name))
 
@@ -125,13 +152,13 @@ class Sliver_Libvirt(Account):
         try:
             self.dom.destroy()
         except:
-            logger.log_exc("in sliver_libvirt.stop",name=self.name)
+            logger.log_exc("in sliver_libvirt.stop", name=self.name)
 
     def is_running(self):
         ''' Return True if the domain is running '''
-        (state,_) = self.dom.state()
+        (state, _) = self.dom.state()
         result = (state == libvirt.VIR_DOMAIN_RUNNING)
-        logger.verbose('sliver_libvirt.is_running: %s => %s'%(self,result))
+        logger.verbose('sliver_libvirt.is_running: %s => %s'%(self, result))
         return result
 
     def configure(self, rec):