Code cleanup. Setting resource state through specific functions
[nepi.git] / src / nepi / resources / linux / ccn / ccnd.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2013 INRIA
4 #
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.
9 #
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.
14 #
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/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
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
27
28 import os
29
30 # TODO: use ccndlogging to dynamically change the logging level
31
32
33 @clsinit_copy
34 class LinuxCCND(LinuxApplication):
35     _rtype = "LinuxCCND"
36
37     @classmethod
38     def _register_attributes(cls):
39         debug = Attribute("debug", "Sets the CCND_DEBUG environmental variable. "
40             " Allowed values are : \n"
41             "  0 - no messages \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",
52             type = Types.Integer,
53             flags = Flags.ExecReadOnly)
54
55         port = Attribute("port", "Sets the CCN_LOCAL_PORT environmental variable. "
56             "Defaults to 9695 ", 
57             flags = Flags.ExecReadOnly)
58  
59         sockname = Attribute("sockname",
60             "Sets the CCN_LOCAL_SCOKNAME environmental variable. "
61             "Defaults to /tmp/.ccnd.sock", 
62             flags = Flags.ExecReadOnly)
63
64         capacity = Attribute("capacity",
65             "Sets the CCND_CAP environmental variable. "
66             "Capacity limit in terms of ContentObjects",
67             flags = Flags.ExecReadOnly)
68
69         mtu = Attribute("mtu", "Sets the CCND_MTU environmental variable. ",
70             flags = Flags.ExecReadOnly)
71   
72         data_pause = Attribute("dataPauseMicrosec",
73             "Sets the CCND_DATA_PAUSE_MICROSEC environmental variable. ",
74             flags = Flags.ExecReadOnly)
75
76         default_stale = Attribute("defaultTimeToStale",
77              "Sets the CCND_DEFAULT_TIME_TO_STALE environmental variable. ",
78             flags = Flags.ExecReadOnly)
79
80         max_stale = Attribute("maxTimeToStale",
81             "Sets the CCND_MAX_TIME_TO_STALE environmental variable. ",
82             flags = Flags.ExecReadOnly)
83
84         max_rte = Attribute("maxRteMicrosec",
85             "Sets the CCND_MAX_RTE_MICROSEC environmental variable. ",
86             flags = Flags.ExecReadOnly)
87
88         keystore = Attribute("keyStoreDirectory",
89             "Sets the CCND_KEYSTORE_DIRECTORY environmental variable. ",
90             flags = Flags.ExecReadOnly)
91
92         listen_on = Attribute("listenOn",
93             "Sets the CCND_LISTEN_ON environmental variable. ",
94             flags = Flags.ExecReadOnly)
95
96         autoreg = Attribute("autoreg",
97             "Sets the CCND_AUTOREG environmental variable. ",
98             flags = Flags.ExecReadOnly)
99
100         prefix = Attribute("prefix",
101             "Sets the CCND_PREFIX environmental variable. ",
102             flags = Flags.ExecReadOnly)
103
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)
117
118     @classmethod
119     def _register_traces(cls):
120         log = Trace("log", "CCND log output")
121         status = Trace("status", "ccndstatus output")
122
123         cls._register_trace(log)
124         cls._register_trace(status)
125
126     def __init__(self, ec, guid):
127         super(LinuxCCND, self).__init__(ec, guid)
128         self._home = "ccnd-%s" % self.guid
129         self._version = "ccnx"
130
131     @property
132     def version(self):
133         return self._version
134
135     @property
136     def path(self):
137         return "PATH=$PATH:${BIN}/%s/" % self.version 
138
139     def deploy(self):
140         if not self.node or self.node.state < ResourceState.READY:
141             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
142             
143             # ccnd needs to wait until node is deployed and running
144             self.ec.schedule(reschedule_delay, self.deploy)
145         else:
146             try:
147                 if not self.get("command"):
148                     self.set("command", self._start_command)
149                 
150                 if not self.get("depends"):
151                     self.set("depends", self._dependencies)
152
153                 if not self.get("sources"):
154                     self.set("sources", self._sources)
155
156                 sources = self.get("sources")
157                 source = sources.split(" ")[0]
158                 basename = os.path.basename(source)
159                 self._version = ( basename.strip().replace(".tar.gz", "")
160                         .replace(".tar","")
161                         .replace(".gz","")
162                         .replace(".zip","") )
163
164                 if not self.get("build"):
165                     self.set("build", self._build)
166
167                 if not self.get("install"):
168                     self.set("install", self._install)
169
170                 if not self.get("env"):
171                     self.set("env", self._environment)
172
173                 command = self.get("command")
174
175                 self.info("Deploying command '%s' " % command)
176
177                 self.discover()
178                 self.provision()
179             except:
180                 self.fail()
181                 raise
182  
183             self.debug("----- READY ---- ")
184             self.set_ready()
185
186     def upload_start_command(self):
187         command = self.get("command")
188         env = self.get("env")
189
190         # We want to make sure the ccnd is running
191         # before the experiment starts.
192         # Run the command as a bash script in background,
193         # in the host ( but wait until the command has
194         # finished to continue )
195         env = self.replace_paths(env)
196         command = self.replace_paths(command)
197
198         shfile = os.path.join(self.app_home, "start.sh")
199         self.node.run_and_wait(command, self.run_home,
200                 shfile = shfile,
201                 overwrite = False,
202                 env = env,
203                 raise_on_error = True)
204
205     def start(self):
206         if self.state == ResourceState.READY:
207             command = self.get("command")
208             self.info("Starting command '%s'" % command)
209
210             self.set_started()
211         else:
212             msg = " Failed to execute command '%s'" % command
213             self.error(msg, out, err)
214             self.set_failed()
215             raise RuntimeError, msg
216
217     def stop(self):
218         command = self.get('command') or ''
219         
220         if self.state == ResourceState.STARTED:
221             self.info("Stopping command '%s'" % command)
222
223             command = "ccndstop"
224             env = self.get("env") 
225
226             # replace application specific paths in the command
227             command = self.replace_paths(command)
228             env = env and self.replace_paths(env)
229
230             # Upload the command to a file, and execute asynchronously
231             shfile = os.path.join(self.app_home, "stop.sh")
232             self.node.run_and_wait(command, self.run_home,
233                         shfile = shfile,
234                         overwrite = False,
235                         env = env,
236                         pidfile = "ccndstop_pidfile", 
237                         ecodefile = "ccndstop_exitcode", 
238                         stdout = "ccndstop_stdout", 
239                         stderr = "ccndstop_stderr")
240
241             self.set_stopped()
242     
243     @property
244     def state(self):
245         # First check if the ccnd has failed
246         state_check_delay = 0.5
247         if self._state == ResourceState.STARTED and \
248                 tdiffsec(tnow(), self._last_state_check) > state_check_delay:
249             (out, err), proc = self._ccndstatus
250
251             retcode = proc.poll()
252
253             if retcode == 1 and err.find("No such file or directory") > -1:
254                 # ccnd is not running (socket not found)
255                 self.set_finished()
256             elif retcode:
257                 # other errors ...
258                 msg = " Failed to execute command '%s'" % self.get("command")
259                 self.error(msg, out, err)
260                 self.fail()
261
262             self._last_state_check = tnow()
263
264         return self._state
265
266     @property
267     def _ccndstatus(self):
268         env = self.get('env') or ""
269         environ = self.node.format_environment(env, inline = True)
270         command = environ + " ccndstatus"
271         command = self.replace_paths(command)
272     
273         return self.node.execute(command)
274
275     @property
276     def _start_command(self):
277         return "ccndstart"
278
279     @property
280     def _dependencies(self):
281         if self.node.use_rpm:
282             return ( " autoconf openssl-devel  expat-devel libpcap-devel "
283                 " ecryptfs-utils-devel libxml2-devel automake gawk " 
284                 " gcc gcc-c++ git pcre-devel make ")
285         elif self.node.use_deb:
286             return ( " autoconf libssl-dev libexpat-dev libpcap-dev "
287                 " libecryptfs0 libxml2-utils automake gawk gcc g++ "
288                 " git-core pkg-config libpcre3-dev make ")
289         return ""
290
291     @property
292     def _sources(self):
293         return "http://www.ccnx.org/releases/ccnx-0.7.2.tar.gz"
294
295     @property
296     def _build(self):
297         sources = self.get("sources").split(" ")[0]
298         sources = os.path.basename(sources)
299
300         return (
301             # Evaluate if ccnx binaries are already installed
302             " ( "
303                 " test -f ${BIN}/%(version)s/ccnd && "
304                 " echo 'binaries found, nothing to do' "
305             " ) || ( "
306             # If not, untar and build
307                 " ( "
308                     " mkdir -p ${SRC}/%(version)s && "
309                     " tar xf ${SRC}/%(sources)s --strip-components=1 -C ${SRC}/%(version)s "
310                  " ) && "
311                     "cd ${SRC}/%(version)s && "
312                     # Just execute and silence warnings...
313                     " ( ./configure && make ) "
314              " )") % ({ 'sources': sources,
315                         'version': self.version
316                  })
317
318     @property
319     def _install(self):
320         return (
321             # Evaluate if ccnx binaries are already installed
322             " ( "
323                 " test -f ${BIN}/%(version)s/ccnd && "
324                 " echo 'binaries found, nothing to do' "
325             " ) || ( "
326             # If not, install
327                 "  mkdir -p ${BIN}/%(version)s && "
328                 "  mv ${SRC}/%(version)s/bin/* ${BIN}/%(version)s/ "
329             " )"
330             ) % ({ 'version': self.version
331                  })
332
333     @property
334     def _environment(self):
335         envs = dict({
336             "debug": "CCND_DEBUG",
337             "port": "CCN_LOCAL_PORT",
338             "sockname" : "CCN_LOCAL_SOCKNAME",
339             "capacity" : "CCND_CAP",
340             "mtu" : "CCND_MTU",
341             "dataPauseMicrosec" : "CCND_DATA_PAUSE_MICROSEC",
342             "defaultTimeToStale" : "CCND_DEFAULT_TIME_TO_STALE",
343             "maxTimeToStale" : "CCND_MAX_TIME_TO_STALE",
344             "maxRteMicrosec" : "CCND_MAX_RTE_MICROSEC",
345             "keyStoreDirectory" : "CCND_KEYSTORE_DIRECTORY",
346             "listenOn" : "CCND_LISTEN_ON",
347             "autoreg" : "CCND_AUTOREG",
348             "prefix" : "CCND_PREFIX",
349             })
350
351         env = self.path 
352         env += " ".join(map(lambda k: "%s=%s" % (envs.get(k), str(self.get(k))) \
353             if self.get(k) else "", envs.keys()))
354         
355         return env            
356
357     def valid_connection(self, guid):
358         # TODO: Validate!
359         return True
360