2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License version 2 as
7 # published by the Free Software Foundation;
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.resource import clsinit_copy, ResourceState, \
22 from nepi.resources.linux.application import LinuxApplication
23 from nepi.resources.linux.ccn.ccnr import LinuxCCNR
24 from nepi.util.timefuncs import tnow
29 class LinuxCCNContent(LinuxApplication):
30 _rtype = "linux::CCNContent"
33 def _register_attributes(cls):
34 content_name = Attribute("contentName",
35 "The name of the content to publish (e.g. ccn:/VIDEO) ",
38 content = Attribute("content",
39 "The content to publish. It can be a path to a file or plain text ",
42 scope = Attribute("scope",
43 "Use the given scope on the start-write request (if -r specified). "
44 "scope can be 1 (local), 2 (neighborhood), or 3 (unlimited). "
45 "Note that a scope of 3 is encoded as the absence of any scope in the interest. ",
50 cls._register_attribute(content_name)
51 cls._register_attribute(content)
52 cls._register_attribute(scope)
54 def __init__(self, ec, guid):
55 super(LinuxCCNContent, self).__init__(ec, guid)
56 self._home = "content-%s" % self.guid
60 ccnr = self.get_connected(LinuxCCNR.get_rtype())
61 if ccnr: return ccnr[0]
66 if self.ccnr: return self.ccnr.ccnd
71 if self.ccnr: return self.ccnr.node
75 if not self.ccnr or self.ccnr.state < ResourceState.READY:
76 self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
78 # ccnr needs to wait until ccnd is deployed and running
79 self.ec.schedule(self.reschedule_delay, self.deploy)
81 if not self.get("command"):
82 self.set("command", self._start_command)
84 if not self.get("env"):
85 self.set("env", self._environment)
87 # set content to stdin, so the content will be
88 # uploaded during provision
89 self.set("stdin", self.get("content"))
91 command = self.get("command")
93 self.info("Deploying command '%s' " % command)
100 def upload_start_command(self):
101 command = self.get("command")
102 env = self.get("env")
104 self.info("Uploading command '%s'" % command)
106 # We want to make sure the content is published
107 # before the experiment starts.
108 # Run the command as a bash script in the background,
109 # in the host ( but wait until the command has
110 # finished to continue )
111 env = self.replace_paths(env)
112 command = self.replace_paths(command)
114 (out, err), proc = self.execute_command(command,
115 env, blocking = True)
118 msg = "Failed to execute command"
119 self.error(msg, out, err)
120 raise RuntimeError(msg)
123 if self.state == ResourceState.READY:
124 command = self.get("command")
125 self.info("Starting command '%s'" % command)
129 msg = " Failed to execute command '%s'" % command
130 self.error(msg, out, err)
131 raise RuntimeError(msg)
134 def _start_command(self):
135 command = ["ccnseqwriter"]
136 command.append("-r %s" % self.get("contentName"))
137 command.append("-s %d" % self.get("scope"))
138 command.append("< %s" % os.path.join(self.app_home, 'stdin'))
140 command = " ".join(command)
144 def _environment(self):
145 return self.ccnd.path
147 def valid_connection(self, guid):