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 as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
20 from nepi.execution.attribute import Attribute, Flags, Types
21 from nepi.execution.trace import Trace, TraceAttr
22 from nepi.execution.resource import ResourceManager, clsinit_copy, \
23 ResourceState, reschedule_delay
24 from nepi.resources.linux.application import LinuxApplication
25 from nepi.resources.linux.node import OSType
26 from nepi.util.timefuncs import tnow, tdiffsec
30 # TODO: use ccndlogging to dynamically change the logging level
34 class LinuxCCND(LinuxApplication):
38 def _register_attributes(cls):
39 debug = Attribute("debug", "Sets the CCND_DEBUG environmental variable. "
40 " Allowed values are : \n"
42 " 1 - basic messages (any non-zero value gets these) \n"
43 " 2 - interest messages \n"
44 " 4 - content messages \n"
45 " 8 - matching details \n"
46 " 16 - interest details \n"
47 " 32 - gory interest details \n"
48 " 64 - log occasional human-readable timestamps \n"
49 " 128 - face registration debugging \n"
50 " -1 - max logging \n"
51 " Or apply bitwise OR to these values to get combinations of them",
55 port = Attribute("port", "Sets the CCN_LOCAL_PORT environmental variable. "
59 sockname = Attribute("sockname",
60 "Sets the CCN_LOCAL_SCOKNAME environmental variable. "
61 "Defaults to /tmp/.ccnd.sock",
64 capacity = Attribute("capacity",
65 "Sets the CCND_CAP environmental variable. "
66 "Capacity limit in terms of ContentObjects",
69 mtu = Attribute("mtu", "Sets the CCND_MTU environmental variable. ",
72 data_pause = Attribute("dataPauseMicrosec",
73 "Sets the CCND_DATA_PAUSE_MICROSEC environmental variable. ",
76 default_stale = Attribute("defaultTimeToStale",
77 "Sets the CCND_DEFAULT_TIME_TO_STALE environmental variable. ",
80 max_stale = Attribute("maxTimeToStale",
81 "Sets the CCND_MAX_TIME_TO_STALE environmental variable. ",
84 max_rte = Attribute("maxRteMicrosec",
85 "Sets the CCND_MAX_RTE_MICROSEC environmental variable. ",
88 keystore = Attribute("keyStoreDirectory",
89 "Sets the CCND_KEYSTORE_DIRECTORY environmental variable. ",
92 listen_on = Attribute("listenOn",
93 "Sets the CCND_LISTEN_ON environmental variable. ",
96 autoreg = Attribute("autoreg",
97 "Sets the CCND_AUTOREG environmental variable. ",
100 prefix = Attribute("prefix",
101 "Sets the CCND_PREFIX environmental variable. ",
102 flags = Flags.Design)
104 cls._register_attribute(debug)
105 cls._register_attribute(port)
106 cls._register_attribute(sockname)
107 cls._register_attribute(capacity)
108 cls._register_attribute(mtu)
109 cls._register_attribute(data_pause)
110 cls._register_attribute(default_stale)
111 cls._register_attribute(max_stale)
112 cls._register_attribute(max_rte)
113 cls._register_attribute(keystore)
114 cls._register_attribute(listen_on)
115 cls._register_attribute(autoreg)
116 cls._register_attribute(prefix)
119 def _register_traces(cls):
120 log = Trace("log", "CCND log output")
121 status = Trace("status", "ccndstatus output")
123 cls._register_trace(log)
124 cls._register_trace(status)
126 def __init__(self, ec, guid):
127 super(LinuxCCND, self).__init__(ec, guid)
128 self._home = "ccnd-%s" % self.guid
129 self._version = "ccnx"
137 return "PATH=$PATH:${BIN}/%s/" % self.version
140 if not self.node or self.node.state < ResourceState.READY:
141 self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
143 # ccnd needs to wait until node is deployed and running
144 self.ec.schedule(reschedule_delay, self.deploy)
146 if not self.get("command"):
147 self.set("command", self._start_command)
149 if not self.get("depends"):
150 self.set("depends", self._dependencies)
152 if not self.get("sources"):
153 self.set("sources", self._sources)
155 sources = self.get("sources")
156 source = sources.split(" ")[0]
157 basename = os.path.basename(source)
158 self._version = ( basename.strip().replace(".tar.gz", "")
161 .replace(".zip","") )
163 if not self.get("build"):
164 self.set("build", self._build)
166 if not self.get("install"):
167 self.set("install", self._install)
169 if not self.get("env"):
170 self.set("env", self._environment)
172 command = self.get("command")
174 self.info("Deploying command '%s' " % command)
181 def upload_start_command(self):
182 command = self.get("command")
183 env = self.get("env")
185 # We want to make sure the ccnd is running
186 # before the experiment starts.
187 # Run the command as a bash script in background,
188 # in the host ( but wait until the command has
189 # finished to continue )
190 env = self.replace_paths(env)
191 command = self.replace_paths(command)
193 shfile = os.path.join(self.app_home, "start.sh")
194 self.node.run_and_wait(command, self.run_home,
200 if self.state == ResourceState.READY:
201 command = self.get("command")
202 self.info("Starting command '%s'" % command)
206 msg = " Failed to execute command '%s'" % command
207 self.error(msg, out, err)
208 raise RuntimeError, msg
211 command = self.get('command') or ''
213 if self.state == ResourceState.STARTED:
214 self.info("Stopping command '%s'" % command)
217 env = self.get("env")
219 # replace application specific paths in the command
220 command = self.replace_paths(command)
221 env = env and self.replace_paths(env)
223 # Upload the command to a file, and execute asynchronously
224 shfile = os.path.join(self.app_home, "stop.sh")
225 self.node.run_and_wait(command, self.run_home,
229 pidfile = "ccndstop_pidfile",
230 ecodefile = "ccndstop_exitcode",
231 stdout = "ccndstop_stdout",
232 stderr = "ccndstop_stderr")
238 # First check if the ccnd has failed
239 state_check_delay = 0.5
240 if self._state == ResourceState.STARTED and \
241 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
242 (out, err), proc = self._ccndstatus()
244 retcode = proc.poll()
246 if retcode == 1 and err.find("No such file or directory") > -1:
247 # ccnd is not running (socket not found)
251 msg = " Failed to execute command '%s'" % self.get("command")
252 self.error(msg, out, err)
255 self._last_state_check = tnow()
259 def _ccndstatus(self):
260 env = self.get('env') or ""
261 environ = self.node.format_environment(env, inline = True)
262 command = environ + " ccndstatus"
263 command = self.replace_paths(command)
265 return self.node.execute(command)
268 def _start_command(self):
272 def _dependencies(self):
273 if self.node.use_rpm:
274 return ( " autoconf openssl-devel expat-devel libpcap-devel "
275 " ecryptfs-utils-devel libxml2-devel automake gawk "
276 " gcc gcc-c++ git pcre-devel make ")
277 elif self.node.use_deb:
278 return ( " autoconf libssl-dev libexpat-dev libpcap-dev "
279 " libecryptfs0 libxml2-utils automake gawk gcc g++ "
280 " git-core pkg-config libpcre3-dev make ")
285 return "http://www.ccnx.org/releases/ccnx-0.8.1.tar.gz"
289 sources = self.get("sources").split(" ")[0]
290 sources = os.path.basename(sources)
293 # Evaluate if ccnx binaries are already installed
295 " test -f ${BIN}/%(version)s/ccnd && "
296 " echo 'binaries found, nothing to do' "
298 # If not, untar and build
300 " mkdir -p ${SRC}/%(version)s && "
301 " tar xf ${SRC}/%(sources)s --strip-components=1 -C ${SRC}/%(version)s "
303 "cd ${SRC}/%(version)s && "
304 # Just execute and silence warnings...
305 " ( ./configure && make ) "
306 " )") % ({ 'sources': sources,
307 'version': self.version
313 # Evaluate if ccnx binaries are already installed
315 " test -f ${BIN}/%(version)s/ccnd && "
316 " echo 'binaries found, nothing to do' "
319 " mkdir -p ${BIN}/%(version)s && "
320 " mv ${SRC}/%(version)s/bin/* ${BIN}/%(version)s/ "
322 ) % ({ 'version': self.version
326 def _environment(self):
328 "debug": "CCND_DEBUG",
329 "port": "CCN_LOCAL_PORT",
330 "sockname" : "CCN_LOCAL_SOCKNAME",
331 "capacity" : "CCND_CAP",
333 "dataPauseMicrosec" : "CCND_DATA_PAUSE_MICROSEC",
334 "defaultTimeToStale" : "CCND_DEFAULT_TIME_TO_STALE",
335 "maxTimeToStale" : "CCND_MAX_TIME_TO_STALE",
336 "maxRteMicrosec" : "CCND_MAX_RTE_MICROSEC",
337 "keyStoreDirectory" : "CCND_KEYSTORE_DIRECTORY",
338 "listenOn" : "CCND_LISTEN_ON",
339 "autoreg" : "CCND_AUTOREG",
340 "prefix" : "CCND_PREFIX",
344 env += " ".join(map(lambda k: "%s=%s" % (envs.get(k), str(self.get(k))) \
345 if self.get(k) else "", envs.keys()))
349 def valid_connection(self, guid):