Fixing RM.DEPLOY being executed after/during RM.RELEASE by adding a release_lock...
[nepi.git] / src / nepi / resources / linux / ccn / fibentry.py
index ace4f52..1010f2f 100644 (file)
@@ -26,6 +26,8 @@ from nepi.resources.linux.ccn.ccnd import LinuxCCND
 from nepi.util.timefuncs import tnow
 
 import os
+import socket
+import time
 
 
 # TODO: Add rest of options for ccndc!!!
@@ -51,7 +53,7 @@ class LinuxFIBEntry(LinuxApplication):
                 flags = Flags.ExecReadOnly)
 
         host = Attribute("host",
-                "Peer host used in network connection for this FIB entry. ",
+                "Peer hostname used in network connection for this FIB entry. ",
                 flags = Flags.ExecReadOnly)
 
         port = Attribute("port",
@@ -59,24 +61,32 @@ class LinuxFIBEntry(LinuxApplication):
                 "for this FIB entry.",
                 flags = Flags.ExecReadOnly)
 
+        ip = Attribute("ip",
+                "Peer host public IP used in network connection for this FIB entry. ",
+                flags = Flags.ReadOnly)
+
         cls._register_attribute(uri)
         cls._register_attribute(protocol)
         cls._register_attribute(host)
         cls._register_attribute(port)
+        cls._register_attribute(ip)
 
     @classmethod
     def _register_traces(cls):
-        ping = Trace("ping", "Continuous ping to the peer end")
-        mtr = Trace("mtr", "Continuous mtr to the peer end")
+        ping = Trace("ping", "Ping to the peer end")
+        mtr = Trace("mtr", "Mtr to the peer end")
+        traceroute = Trace("traceroute", "Tracerout to the peer end")
 
         cls._register_trace(ping)
         cls._register_trace(mtr)
+        cls._register_trace(traceroute)
 
     def __init__(self, ec, guid):
         super(LinuxFIBEntry, self).__init__(ec, guid)
         self._home = "fib-%s" % self.guid
         self._ping = None
         self._mtr = None
+        self._traceroute = None
 
     @property
     def ccnd(self):
@@ -94,58 +104,61 @@ class LinuxFIBEntry(LinuxApplication):
             return self.ec.trace(self._ping, "stdout", attr, block, offset)
         if name == "mtr":
             return self.ec.trace(self._mtr, "stdout", attr, block, offset)
+        if name == "traceroute":
+            return self.ec.trace(self._traceroute, "stdout", attr, block, offset)
 
         return super(LinuxFIBEntry, self).trace(name, attr, block, offset)
-        
-    def deploy(self):
+    
+    def do_deploy(self):
         # Wait until associated ccnd is provisioned
         if not self.ccnd or self.ccnd.state < ResourceState.READY:
             # ccnr needs to wait until ccnd is deployed and running
             self.ec.schedule(reschedule_delay, self.deploy)
         else:
-            try:
-                if not self.get("command"):
-                    self.set("command", self._start_command)
+            if not self.get("ip"):
+                host = self.get("host")
+                ip = socket.gethostbyname(host)
+                self.set("ip", ip)
+
+            if not self.get("command"):
+                self.set("command", self._start_command)
+
+            if not self.get("env"):
+                self.set("env", self._environment)
 
-                if not self.get("env"):
-                    self.set("env", self._environment)
+            command = self.get("command")
 
-                command = self.get("command")
+            self.info("Deploying command '%s' " % command)
 
-                self.info("Deploying command '%s' " % command)
+            self.do_discover()
+            self.do_provision()
+            self.configure()
 
-                self.discover()
-                self.provision()
-                self.configure()
-            except:
-                self.fail()
-                raise
             self.debug("----- READY ---- ")
-            self._ready_time = tnow()
-            self._state = ResourceState.READY
+            self.set_ready()
 
     def upload_start_command(self):
         command = self.get("command")
         env = self.get("env")
 
-        if command:
-            # We want to make sure the FIB entries are created
-            # before the experiment starts.
-            # Run the command as a bash script in the background, 
-            # in the host ( but wait until the command has
-            # finished to continue )
-            env = env and self.replace_paths(env)
-            command = self.replace_paths(command)
-
-            (out, err), proc = self.execute_command(command, env)
-
-            if proc.poll():
-                self._state = ResourceState.FAILED
-                msg = "Failed to execute command"
-                self.error(msg, out, err)
-                raise RuntimeError, msg
-
+        # We want to make sure the FIB entries are created
+        # before the experiment starts.
+        # Run the command as a bash script in the background, 
+        # in the host ( but wait until the command has
+        # finished to continue )
+        env = env and self.replace_paths(env)
+        command = self.replace_paths(command)
+
+        # ccndc seems to return exitcode OK even if a (dns) error
+        # occurred, so we need to account for this case here. 
+        (out, err), proc = self.execute_command(command, 
+                env, blocking = True)
+
+        if proc.poll():
+            msg = "Failed to execute command"
+            self.error(msg, out, err)
+            raise RuntimeError, msg
+        
     def configure(self):
         if self.trace_enabled("ping"):
             self.info("Configuring PING trace")
@@ -153,44 +166,42 @@ class LinuxFIBEntry(LinuxApplication):
             self.ec.set(self._ping, "printTimestamp", True)
             self.ec.set(self._ping, "target", self.get("host"))
             self.ec.register_connection(self._ping, self.node.guid)
-            # force waiting until ping is READY before we starting the FIB
-            self.ec.register_condition(self.guid, ResourceAction.START, 
-                    self._ping, ResourceState.READY)
-            # schedule ping deploy and start
-            self.ec.deploy(group=[self._ping])
-            self.ec.start_with_conditions(self._ping)
+            # schedule ping deploy
+            self.ec.deploy(guids=[self._ping], group = self.deployment_group)
 
         if self.trace_enabled("mtr"):
-            self.info("Configuring TRACE trace")
+            self.info("Configuring MTR trace")
             self._mtr = self.ec.register_resource("LinuxMtr")
             self.ec.set(self._mtr, "noDns", True)
             self.ec.set(self._mtr, "printTimestamp", True)
             self.ec.set(self._mtr, "continuous", True)
             self.ec.set(self._mtr, "target", self.get("host"))
             self.ec.register_connection(self._mtr, self.node.guid)
-            self.ec.deploy(group=[self._mtr])
-            # force waiting until mtr is READY before we starting the FIB
-            self.ec.register_condition(self.guid, ResourceAction.START, 
-                    self._mtr, ResourceState.READY)
-            # schedule mtr deploy and start
-            self.ec.deploy(group=[self._mtr])
-            self.ec.start_with_conditions(self._mtr)
-
-    
-    def start(self):
-        if self._state in [ResourceState.READY, ResourceState.STARTED]:
+            # schedule mtr deploy
+            self.ec.deploy(guids=[self._mtr], group = self.deployment_group)
+
+        if self.trace_enabled("traceroute"):
+            self.info("Configuring TRACEROUTE trace")
+            self._traceroute = self.ec.register_resource("LinuxTraceroute")
+            self.ec.set(self._traceroute, "printTimestamp", True)
+            self.ec.set(self._traceroute, "continuous", True)
+            self.ec.set(self._traceroute, "target", self.get("host"))
+            self.ec.register_connection(self._traceroute, self.node.guid)
+            # schedule mtr deploy
+            self.ec.deploy(guids=[self._traceroute], group = self.deployment_group)
+
+    def do_start(self):
+        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
 
-    def stop(self):
+    def do_stop(self):
         command = self.get('command')
         env = self.get('env')
         
@@ -198,30 +209,28 @@ class LinuxFIBEntry(LinuxApplication):
             self.info("Stopping command '%s'" % command)
 
             command = self._stop_command
-            (out, err), proc = self.execute_command(command, env)
-
-            if proc.poll():
-                pass
+            (out, err), proc = self.execute_command(command, env,
+                    blocking = True)
 
-            self._stop_time = tnow()
-            self._state = ResourceState.STOPPED
+            self.set_stopped()
 
-    @property
-    def state(self):
-        return self._state
+            if err:
+                msg = " Failed to execute command '%s'" % command
+                self.error(msg, out, err)
+                raise RuntimeError, msg
 
     @property
     def _start_command(self):
         uri = self.get("uri") or ""
         protocol = self.get("protocol") or ""
-        host = self.get("host") or ""
+        ip = self.get("ip") or "" 
         port = self.get("port") or ""
 
         # add ccnx:/example.com/ udp 224.0.0.204 52428
         return "ccndc add %(uri)s %(protocol)s %(host)s %(port)s" % ({
             "uri" : uri,
             "protocol": protocol,
-            "host": host,
+            "host": ip,
             "port": port
             })
 
@@ -229,14 +238,14 @@ class LinuxFIBEntry(LinuxApplication):
     def _stop_command(self):
         uri = self.get("uri") or ""
         protocol = self.get("protocol") or ""
-        host = self.get("host") or ""
+        ip = self.get("ip") or ""
         port = self.get("port") or ""
 
         # add ccnx:/example.com/ udp 224.0.0.204 52428
         return "ccndc del %(uri)s %(protocol)s %(host)s %(port)s" % ({
             "uri" : uri,
             "protocol": protocol,
-            "host": host,
+            "host": ip,
             "port": port
             })