LinuxApplication: making single deploy.sh script out of http_sources.sh, build.sh...
[nepi.git] / src / nepi / resources / linux / ccn / ccncontent.py
index 3b08171..5222d03 100644 (file)
 
 from nepi.execution.attribute import Attribute, Flags, Types
 from nepi.execution.resource import clsinit_copy, ResourceState, \
-    ResourceAction
-from nepi.resources.linux.ccn.ccnapplication import LinuxCCNApplication
+    ResourceAction, reschedule_delay
+from nepi.resources.linux.application import LinuxApplication
 from nepi.resources.linux.ccn.ccnr import LinuxCCNR
-from nepi.util.timefuncs import strfnow, strfdiff
+from nepi.util.timefuncs import tnow
 
 import os
 
 @clsinit_copy
-class LinuxCCNContent(LinuxCCNApplication):
+class LinuxCCNContent(LinuxApplication):
     _rtype = "LinuxCCNContent"
 
     @classmethod
@@ -53,6 +53,11 @@ class LinuxCCNContent(LinuxCCNApplication):
         if ccnr: return ccnr[0]
         return None
 
+    @property
+    def ccnd(self):
+        if self.ccnr: return self.ccnr.ccnd
+        return None
+
     @property
     def node(self):
         if self.ccnr: return self.ccnr.node
@@ -62,44 +67,61 @@ class LinuxCCNContent(LinuxCCNApplication):
         if not self.ccnr or self.ccnr.state < ResourceState.READY:
             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
             
-            reschedule_delay = "0.5s"
             # ccnr needs to wait until ccnd is deployed and running
             self.ec.schedule(reschedule_delay, self.deploy)
         else:
-            command = self._start_command
-            env = self._environment
+            try:
+                if not self.get("command"):
+                    self.set("command", self._start_command)
 
-            self.set("command", command)
-            self.set("env", env)
+                if not self.get("env"):
+                    self.set("env", self._environment)
 
-            # set content to stdin, so the content will be
-            # uploaded during provision
-            self.set("stdin", self.get("content"))
+                # set content to stdin, so the content will be
+                # uploaded during provision
+                self.set("stdin", self.get("content"))
 
-            self.info("Deploying command '%s' " % command)
+                command = self.get("command")
 
-            self.node.mkdir(self.app_home)
+                self.info("Deploying command '%s' " % command)
+
+                self.discover()
+                self.provision()
+            except:
+                self.fail()
+                raise
+            self.debug("----- READY ---- ")
+            self._ready_time = tnow()
+            self._state = ResourceState.READY
 
-            # upload content 
-            self.upload_stdin()
+    def upload_start_command(self):
+        command = self.get("command")
+        env = self.get("env")
 
+        if command:
             # We want to make sure the content is published
             # 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 )
-            self.execute_command(command, env)
+            env = self.replace_paths(env)
+            command = self.replace_paths(command)
 
-            self.debug("----- READY ---- ")
-            self._ready_time = strfnow()
-            self._state = ResourceState.READY
+            (out, err), proc = self.execute_command(command, env)
+
+            if proc.poll():
+                self.fail()
+                msg = "Failed to execute command"
+                self.error(msg, out, err)
+                raise RuntimeError, msg
 
     def start(self):
         if self._state == ResourceState.READY:
             command = self.get("command")
             self.info("Starting command '%s'" % command)
 
-            self._start_time = strfnow()
+            self._start_time = tnow()
             self._state = ResourceState.STARTED
         else:
             msg = " Failed to execute command '%s'" % command
@@ -116,6 +138,17 @@ class LinuxCCNContent(LinuxCCNApplication):
         return "ccnseqwriter -r %s < %s" % (self.get("contentName"),
                 os.path.join(self.app_home, 'stdin'))
 
+    @property
+    def _environment(self):
+        return self.ccnd.path
+       
+    def execute_command(self, command, env):
+        environ = self.node.format_environment(env, inline = True)
+        command = environ + command
+        command = self.replace_paths(command)
+
+        return self.node.execute(command)
+
     def valid_connection(self, guid):
         # TODO: Validate!
         return True