ec_shutdown
[nepi.git] / src / nepi / resources / linux / ccn / fibentry.py
index fe0330e..48c2d6d 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,10 +61,15 @@ 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):
@@ -109,6 +116,11 @@ class LinuxFIBEntry(LinuxApplication):
             self.ec.schedule(reschedule_delay, self.deploy)
         else:
             try:
+                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)
 
@@ -124,11 +136,10 @@ class LinuxFIBEntry(LinuxApplication):
                 self.configure()
             except:
                 self.fail()
-                raise
+                return
+
             self.debug("----- READY ---- ")
-            self._ready_time = tnow()
-            self._state = ResourceState.READY
+            self.set_ready()
 
     def upload_start_command(self):
         command = self.get("command")
@@ -142,14 +153,16 @@ class LinuxFIBEntry(LinuxApplication):
         env = env and self.replace_paths(env)
         command = self.replace_paths(command)
 
-        (out, err), proc = self.execute_command(command, env)
+        # 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():
-            self._state = ResourceState.FAILED
             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")
@@ -158,7 +171,7 @@ class LinuxFIBEntry(LinuxApplication):
             self.ec.set(self._ping, "target", self.get("host"))
             self.ec.register_connection(self._ping, self.node.guid)
             # schedule ping deploy
-            self.ec.deploy(group=[self._ping])
+            self.ec.deploy(guids=[self._ping], group = self.deployment_group)
 
         if self.trace_enabled("mtr"):
             self.info("Configuring MTR trace")
@@ -169,7 +182,7 @@ class LinuxFIBEntry(LinuxApplication):
             self.ec.set(self._mtr, "target", self.get("host"))
             self.ec.register_connection(self._mtr, self.node.guid)
             # schedule mtr deploy
-            self.ec.deploy(group=[self._mtr])
+            self.ec.deploy(guids=[self._mtr], group = self.deployment_group)
 
         if self.trace_enabled("traceroute"):
             self.info("Configuring TRACEROUTE trace")
@@ -179,20 +192,18 @@ class LinuxFIBEntry(LinuxApplication):
             self.ec.set(self._traceroute, "target", self.get("host"))
             self.ec.register_connection(self._traceroute, self.node.guid)
             # schedule mtr deploy
-            self.ec.deploy(group=[self._traceroute])
+            self.ec.deploy(guids=[self._traceroute], group = self.deployment_group)
 
     def start(self):
-        if self._state in [ResourceState.READY, ResourceState.STARTED]:
+        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
+            self.fail()
 
     def stop(self):
         command = self.get('command')
@@ -202,30 +213,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)
+                self.fail()
 
     @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
             })
 
@@ -233,14 +242,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
             })