Restart vserver if spaces/net modified
[nodemanager-topo.git] / topo.py
diff --git a/topo.py b/topo.py
index e73eb8a..5cdc7c1 100755 (executable)
--- a/topo.py
+++ b/topo.py
@@ -176,11 +176,12 @@ def update_links(slice, myid, topospec, key, netns):
 
 
 """
-Write /etc/vservers/<slicename>/spaces/net
+Write /etc/vservers/<slicename>/spaces/net.  If the vserver is running and the spaces/net file is modified, we need to restart it.
 """
-def writeConf(slicename, value):
+def write_conf_and_restart(slicename, value):
     SLICEDIR="/etc/vservers/%s/" % slicename
     SPACESDIR="%s/spaces/" % SLICEDIR
+    FILENAME="%s/net" % SPACESDIR
     if os.path.exists(SLICEDIR):
         if not os.path.exists(SPACESDIR):
             try:
@@ -188,13 +189,27 @@ def writeConf(slicename, value):
             except os.error:
                 logger.log("topo: could not create %s\n" % SPACESDIR)
                 return
-        f = open("%s/net" % SPACESDIR, "w")
-        f.write("%s\n" % value)
-        f.close()
-        STATUS="OFF"
-        if value:
-            STATUS="ON"
-        logger.log("%s: network namespace %s\n" % (slicename, STATUS))
+            
+        if os.path.exists(FILENAME) != value:
+            sliver = vserver.VServer(slicename)
+            restart = sliver.is_running()
+            
+            if (restart):
+                sliver.stop()
+                
+            if value:
+                STATUS="ON"
+                f = open(FILENAME, "w")
+                f.close()
+            else:
+                STATUS="OFF"
+                os.remove(FILENAME)
+                
+            logger.log("%s: network namespace %s\n" % (slicename, STATUS))
+
+            if (restart):
+                logger.log("topo: restarting sliver %s\n" % slicename)
+                sliver.start()
 
 
 """
@@ -357,11 +372,12 @@ def GetSlivers(data):
         attrs = {}
         for attribute in sliver['attributes']:
             attrs[attribute['tagname']] = attribute['value']
+
         if 'netns' in attrs:
             netns = int(attrs['netns'])
-            writeConf(sliver['name'], netns)
         else:
             netns = 0
+        write_conf_and_restart(sliver['name'], netns)
 
         if vserver.VServer(sliver['name']).is_running():
             if 'egre_key' in attrs and 'topo_rspec' in attrs: