Modified FailureManager to abort only when critical resources fail
[nepi.git] / src / nepi / resources / planetlab / tap.py
index b969174..d9cf17c 100644 (file)
@@ -18,8 +18,8 @@
 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
 
 from nepi.execution.attribute import Attribute, Flags, Types
-from nepi.execution.resource import ResourceManager, clsinit_copy, ResourceState, \
-        reschedule_delay
+from nepi.execution.resource import clsinit_copy, ResourceState, \
+        reschedule_delay, failtrap
 from nepi.resources.linux.application import LinuxApplication
 from nepi.resources.planetlab.node import PlanetlabNode
 from nepi.util.timefuncs import tnow, tdiffsec
@@ -36,6 +36,8 @@ PYTHON_VSYS_VERSION = "1.0"
 @clsinit_copy
 class PlanetlabTap(LinuxApplication):
     _rtype = "PlanetlabTap"
+    _help = "Creates a TAP device on a PlanetLab host"
+    _backend = "planetlab"
 
     @classmethod
     def _register_attributes(cls):
@@ -142,9 +144,10 @@ class PlanetlabTap(LinuxApplication):
         self._run_in_background()
         
         # Retrive if_name
-        if_name = self._wait_if_name()
+        if_name = self.wait_if_name()
         self.set("deviceName", if_name) 
 
+    @failtrap
     def deploy(self):
         if not self.node or self.node.state < ResourceState.PROVISIONED:
             self.ec.schedule(reschedule_delay, self.deploy)
@@ -158,43 +161,36 @@ class PlanetlabTap(LinuxApplication):
             if not self.get("install"):
                 self.set("install", self._install)
 
-            try:
-                self.discover()
-                self.provision()
-            except:
-                self.fail()
-                raise
+            self.discover()
+            self.provision()
+
             self.debug("----- READY ---- ")
-            self._ready_time = tnow()
-            self._state = ResourceState.READY
+            self.set_ready()
 
+    @failtrap
     def start(self):
-        if self._state == ResourceState.READY:
+        if self.state == ResourceState.READY:
             command = self.get("command")
             self.info("Starting command '%s'" % command)
 
-            self._start_time = tnow()
-            self._state = ResourceState.STARTED
+            self.set_started()
         else:
             msg = " Failed to execute command '%s'" % command
             self.error(msg, out, err)
-            self._state = ResourceState.FAILED
             raise RuntimeError, msg
 
+    @failtrap
     def stop(self):
         command = self.get('command') or ''
-        state = self.state
         
-        if state == ResourceState.STARTED:
+        if self.state == ResourceState.STARTED:
             self.info("Stopping command '%s'" % command)
 
             command = "bash %s" % os.path.join(self.app_home, "stop.sh")
             (out, err), proc = self.execute_command(command,
                     blocking = True)
 
-            self._stop_time = tnow()
-            self._state = ResourceState.STOPPED
+            self.set_stopped()
 
     @property
     def state(self):
@@ -208,13 +204,30 @@ class PlanetlabTap(LinuxApplication):
 
                 if out.strip().find(self.get("deviceName")) == -1: 
                     # tap is not running is not running (socket not found)
-                    self._state = ResourceState.FINISHED
+                    self.finish()
 
             self._last_state_check = tnow()
 
         return self._state
 
-    def _wait_if_name(self):
+    def release(self):
+        # Node needs to wait until all associated RMs are released
+        # to be released
+        try:
+            from nepi.resources.linux.udptunnel import UdpTunnel
+            rms = self.get_connected(UdpTunnel.rtype())
+            for rm in rms:
+                if rm.state < ResourceState.STOPPED:
+                    self.ec.schedule(reschedule_delay, self.release)
+                    return 
+        except:
+            import traceback
+            err = traceback.format_exc()
+            self.error(err)
+
+        super(PlanetlabTap, self).release()
+
+    def wait_if_name(self):
         """ Waits until the if_name file for the command is generated, 
             and returns the if_name for the device """
         if_name = None
@@ -232,13 +245,13 @@ class PlanetlabTap(LinuxApplication):
         else:
             msg = "Couldn't retrieve if_name"
             self.error(msg, out, err)
-            self.fail()
             raise RuntimeError, msg
 
         return if_name
 
     def udp_connect_command(self, remote_ip, local_port_file, 
-            remote_port_file, ret_file):
+            remote_port_file, ret_file, cipher, cipher_key,
+            bwlimit, txqueuelen):
         command = ["sudo -S "]
         command.append("PYTHONPATH=$PYTHONPATH:${SRC}")
         command.append("python ${SRC}/pl-vif-udp-connect.py")
@@ -248,6 +261,14 @@ class PlanetlabTap(LinuxApplication):
         command.append("-r %s " % remote_port_file)
         command.append("-H %s " % remote_ip)
         command.append("-R %s " % ret_file)
+        if cipher:
+            command.append("-c %s " % cipher)
+        if cipher_key:
+            command.append("-k %s " % cipher_key)
+        if txqueuelen:
+            command.append("-q %s " % txqueuelen)
+        if bwlimit:
+            command.append("-b %s " % bwlimit)
 
         command = " ".join(command)
         command = self.replace_paths(command)