Bugfixes for LinuxApplication and LinuxNode
[nepi.git] / src / nepi / resources / linux / 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, ResourceState
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.resources.linux.node import OSType
25
26 import os
27
28 @clsinit_copy
29 class LinuxCCND(LinuxApplication):
30     _rtype = "LinuxCCND"
31
32     @classmethod
33     def _register_attributes(cls):
34         debug = Attribute("debug", "Sets the CCND_DEBUG environmental variable. "
35             " Allowed values are : \n"
36             "  0 - no messages \n"
37             "  1 - basic messages (any non-zero value gets these) \n"
38             "  2 - interest messages \n"
39             "  4 - content messages \n"
40             "  8 - matching details \n"
41             "  16 - interest details \n"
42             "  32 - gory interest details \n"
43             "  64 - log occasional human-readable timestamps \n"
44             "  128 - face registration debugging \n"
45             "  -1 - max logging \n"
46             "  Or apply bitwise OR to these values to get combinations of them",
47             flags = Flags.ExecReadOnly)
48
49         port = Attribute("port", "Sets the CCN_LOCAL_PORT environmental variable. "
50             "Defaults to 9695 ", 
51             flags = Flags.ExecReadOnly)
52  
53         sockname = Attribute("sockname",
54             "Sets the CCN_LOCAL_SCOKNAME environmental variable. "
55             "Defaults to /tmp/.ccnd.sock", 
56             flags = Flags.ExecReadOnly)
57
58         capacity = Attribute("capacity",
59             "Sets the CCND_CAP environmental variable. "
60             "Capacity limit in terms of ContentObjects",
61             flags = Flags.ExecReadOnly)
62
63         mtu = Attribute("mtu", "Sets the CCND_MTU environmental variable. ",
64             flags = Flags.ExecReadOnly)
65   
66         data_pause = Attribute("dataPauseMicrosec",
67             "Sets the CCND_DATA_PAUSE_MICROSEC environmental variable. ",
68             flags = Flags.ExecReadOnly)
69
70         default_stale = Attribute("defaultTimeToStale",
71              "Sets the CCND_DEFAULT_TIME_TO_STALE environmental variable. ",
72             flags = Flags.ExecReadOnly)
73
74         max_stale = Attribute("maxTimeToStale",
75             "Sets the CCND_MAX_TIME_TO_STALE environmental variable. ",
76             flags = Flags.ExecReadOnly)
77
78         max_rte = Attribute("maxRteMicrosec",
79             "Sets the CCND_MAX_RTE_MICROSEC environmental variable. ",
80             flags = Flags.ExecReadOnly)
81
82         keystore = Attribute("keyStoreDirectory",
83             "Sets the CCND_KEYSTORE_DIRECTORY environmental variable. ",
84             flags = Flags.ExecReadOnly)
85
86         listen_on = Attribute("listenOn",
87             "Sets the CCND_LISTEN_ON environmental variable. ",
88             flags = Flags.ExecReadOnly)
89
90         autoreg = Attribute("autoreg",
91             "Sets the CCND_AUTOREG environmental variable. ",
92             flags = Flags.ExecReadOnly)
93
94         prefix = Attribute("prefix",
95             "Sets the CCND_PREFIX environmental variable. ",
96             flags = Flags.ExecReadOnly)
97
98         cls._register_attribute(debug)
99         cls._register_attribute(port)
100         cls._register_attribute(sockname)
101         cls._register_attribute(capacity)
102         cls._register_attribute(mtu)
103         cls._register_attribute(data_pause)
104         cls._register_attribute(default_stale)
105         cls._register_attribute(max_stale)
106         cls._register_attribute(max_rte)
107         cls._register_attribute(keystore)
108         cls._register_attribute(listen_on)
109         cls._register_attribute(autoreg)
110         cls._register_attribute(prefix)
111
112     @classmethod
113     def _register_traces(cls):
114         log = Trace("log", "CCND log output")
115         status = Trace("status", "ccndstatus output")
116
117         cls._register_trace(log)
118         cls._register_trace(status)
119
120     def __init__(self, ec, guid):
121         super(LinuxCCND, self).__init__(ec, guid)
122
123     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
124         self.info("Retrieving '%s' trace %s " % (name, attr))
125
126         path = os.path.join(self.app_home, name)
127         
128         command = "(test -f %s && echo 'success') || echo 'error'" % path
129         (out, err), proc = self.node.execute(command)
130
131         if (err and proc.poll()) or out.find("error") != -1:
132             msg = " Couldn't find trace %s " % name
133             self.error(msg, out, err)
134             return None
135     
136         if attr == TraceAttr.PATH:
137             return path
138
139         if attr == TraceAttr.ALL:
140             (out, err), proc = self.node.check_output(self.app_home, name)
141             
142             if err and proc.poll():
143                 msg = " Couldn't read trace %s " % name
144                 self.error(msg, out, err)
145                 return None
146
147             return out
148
149         if attr == TraceAttr.STREAM:
150             cmd = "dd if=%s bs=%d count=1 skip=%d" % (path, block, offset)
151         elif attr == TraceAttr.SIZE:
152             cmd = "stat -c%%s %s " % path
153
154         (out, err), proc = self.node.execute(cmd)
155
156         if err and proc.poll():
157             msg = " Couldn't find trace %s " % name
158             self.error(msg, out, err)
159             return None
160         
161         if attr == TraceAttr.SIZE:
162             out = int(out.strip())
163
164         return out
165             
166     def deploy(self):
167         if not self.get("command"):
168             self.set("command", self._default_command)
169         
170         if not self.get("depends"):
171             self.set("depends", self._default_dependencies)
172
173         if not self.get("sources"):
174             self.set("sources", self._default_sources)
175
176         if not self.get("build"):
177             self.set("build", self._default_build)
178
179         if not self.get("install"):
180             self.set("install", self._default_install)
181
182         if not self.get("env"):
183             self.set("env", self._default_environment)
184
185         super(LinuxCCND, self).deploy()
186
187     def stop(self):
188         command = self.get('command') or ''
189         state = self.state
190         
191         if state == ResourceState.STARTED:
192             self.info("Stopping command '%s'" % command)
193
194             (out, err), proc = self.node.kill(self.pid, self.ppid)
195
196             if out or err:
197                 # check if execution errors occurred
198                 msg = " Failed to STOP command '%s' " % self.get("command")
199                 self.error(msg, out, err)
200                 self._state = ResourceState.FAILED
201                 stopped = False
202             else:
203                 super(LinuxApplication, self).stop()
204
205
206     @property
207     def _default_command(self):
208         return "ccndstart"
209
210     @property
211     def _default_dependencies(self):
212         if self.node.os in [ OSType.FEDORA_12 , OSType.FEDORA_14 ]:
213             return ( " autoconf openssl-devel  expat-devel libpcap-devel "
214                 " ecryptfs-utils-devel libxml2-devel automake gawk " 
215                 " gcc gcc-c++ git pcre-devel make ")
216         elif self.node.os in [ OSType.UBUNTU , OSType.DEBIAN]:
217             return ( " autoconf libssl-dev libexpat-dev libpcap-dev "
218                 " libecryptfs0 libxml2-utils automake gawk gcc g++ "
219                 " git-core pkg-config libpcre3-dev make ")
220         return ""
221
222     @property
223     def _default_sources(self):
224         return "http://www.ccnx.org/releases/ccnx-0.7.1.tar.gz"
225
226     @property
227     def _default_build(self):
228         sources = self.get("sources").split(" ")[0]
229         sources = os.path.basename(sources)
230
231         return (
232             # Evaluate if ccnx binaries are already installed
233             " ( "
234                 "  test -f ${EXP_HOME}/ccnx/bin/ccnd"
235             " ) || ( "
236             # If not, untar and build
237                 " ( "
238                     " mkdir -p ${SOURCES}/ccnx && "
239                     " tar xf ${SOURCES}/%(sources)s --strip-components=1 -C ${SOURCES}/ccnx "
240                  " ) && "
241                     "cd ${SOURCES}/ccnx && "
242                     # Just execute and silence warnings...
243                     " ( ./configure && make ) "
244              " )") % ({ 'sources': sources })
245
246     @property
247     def _default_install(self):
248         return (
249             # Evaluate if ccnx binaries are already installed
250             " ( "
251                 "  test -f ${EXP_HOME}/ccnx/bin/ccnd"
252             " ) || ( "
253                 "  mkdir -p ${EXP_HOME}/ccnx/bin && "
254                 "  cp -r ${SOURCES}/ccnx ${EXP_HOME}"
255             " )"
256             )
257
258     @property
259     def _default_environment(self):
260         envs = dict({
261             "debug": "CCND_DEBUG",
262             "port": "CCN_LOCAL_PORT",
263             "sockname" : "CCN_LOCAL_SOCKNAME",
264             "capacity" : "CCND_CAP",
265             "mtu" : "CCND_MTU",
266             "dataPauseMicrosec" : "CCND_DATA_PAUSE_MICROSEC",
267             "defaultTimeToStale" : "CCND_DEFAULT_TIME_TO_STALE",
268             "maxTimeToStale" : "CCND_MAX_TIME_TO_STALE",
269             "maxRteMicrosec" : "CCND_MAX_RTE_MICROSEC",
270             "keyStoreDirectory" : "CCND_KEYSTORE_DIRECTORY",
271             "listenOn" : "CCND_LISTEN_ON",
272             "autoreg" : "CCND_AUTOREG",
273             "prefix" : "CCND_PREFIX",
274             })
275
276         env = "PATH=$PATH:${EXP_HOME}/ccnx/bin"
277         for key in envs.keys():
278             val = self.get(key)
279             if val:
280                 env += " %s=%s" % (key, val)
281         
282         return env            
283         
284     def valid_connection(self, guid):
285         # TODO: Validate!
286         return True
287