applied the except and raise fixers to the master branch to close the gap with py3
[nepi.git] / src / nepi / resources / linux / ccn / ccnr.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 version 2 as
7 #    published by the Free Software Foundation;
8 #
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.
13 #
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/>.
16 #
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
18
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.trace import Trace, TraceAttr
21 from nepi.execution.resource import clsinit_copy, ResourceState, \
22     ResourceAction
23 from nepi.resources.linux.application import LinuxApplication
24 from nepi.resources.linux.ccn.ccnd import LinuxCCND
25 from nepi.util.timefuncs import tnow
26
27 import os
28
29 @clsinit_copy
30 class LinuxCCNR(LinuxApplication):
31     _rtype = "linux::CCNR"
32
33     @classmethod
34     def _register_attributes(cls):
35         max_fanout = Attribute("maxFanout",
36             "Sets the CCNR_BTREE_MAX_FANOUT environmental variable. ",
37             flags = Flags.Design)
38
39         max_leaf_entries = Attribute("maxLeafEntries",
40             "Sets the CCNR_BTREE_MAX_LEAF_ENTRIES environmental variable. ",
41             flags = Flags.Design)
42
43         max_node_bytes = Attribute("maxNodeBytes",
44             "Sets the CCNR_BTREE_MAX_NODE_BYTES environmental variable. ",
45             flags = Flags.Design)
46
47         max_node_pool = Attribute("maxNodePool",
48             "Sets the CCNR_BTREE_MAX_NODE_POOL environmental variable. ",
49             flags = Flags.Design)
50
51         content_cache = Attribute("contentCache",
52             "Sets the CCNR_CONTENT_CACHE environmental variable. ",
53             flags = Flags.Design)
54
55         debug = Attribute("debug",
56             "Sets the CCNR_DEBUG environmental variable. "
57             "Logging level for ccnr. Defaults to WARNING.",
58             type = Types.Enumerate,
59             allowed = [
60                     "NONE",
61                     "SEVERE",
62                     "ERROR",
63                     "WARNING",
64                     "INFO",
65                     "FINE, FINER, FINEST"],
66             flags = Flags.Design)
67
68         directory = Attribute("directory",
69             "Sets the CCNR_DIRECTORY environmental variable. ",
70             flags = Flags.Design)
71
72         global_prefix = Attribute("globalPrefix",
73             "Sets the CCNR_GLOBAL_PREFIX environmental variable. ",
74             flags = Flags.Design)
75
76         listen_on = Attribute("listenOn",
77             "Sets the CCNR_LISTEN_ON environmental variable. ",
78             flags = Flags.Design)
79
80         min_send_bufsize = Attribute("minSendBufsize",
81             "Sets the CCNR_MIN_SEND_BUFSIZE environmental variable. ",
82             flags = Flags.Design)
83
84         proto = Attribute("proto",
85             "Sets the CCNR_PROTO environmental variable. ",
86             flags = Flags.Design)
87
88         status_port = Attribute("statusPort",
89             "Sets the CCNR_STATUS_PORT environmental variable. ",
90             flags = Flags.Design)
91
92         start_write_scope_limit = Attribute("startWriteScopeLimit",
93             "Sets the CCNR_START_WRITE_SCOPE_LIMIT environmental variable. ",
94             flags = Flags.Design)
95
96         ccns_debug = Attribute("ccnsDebug",
97             "Sets the CCNS_DEBUG environmental variable. ",
98             flags = Flags.Design)
99
100         ccns_enable = Attribute("ccnsEnable",
101             "Sets the CCNS_ENABLE environmental variable. ",
102             flags = Flags.Design)
103
104         ccns_faux_error = Attribute("ccnsFauxError",
105             "Sets the CCNS_FAUX_ERROR environmental variable. ",
106             flags = Flags.Design)
107
108         ccns_heartbeat_micros = Attribute("ccnsHeartBeatMicros",
109             "Sets the CCNS_HEART_BEAT_MICROS environmental variable. ",
110             flags = Flags.Design)
111
112         ccns_max_compares_busy = Attribute("ccnsMaxComparesBusy",
113             "Sets the CCNS_MAX_COMPARES_BUSY environmental variable. ",
114             flags = Flags.Design)
115
116         ccns_max_fetch_busy = Attribute("ccnsMaxFetchBusy",
117             "Sets the CCNS_MAX_FETCH_BUSY environmental variable. ",
118             flags = Flags.Design)
119
120         ccns_node_fetch_lifetime = Attribute("ccnsNodeFetchLifetime",
121             "Sets the CCNS_NODE_FETCH_LIFETIME environmental variable. ",
122             flags = Flags.Design)
123
124         ccns_note_err = Attribute("ccnsNoteErr",
125             "Sets the CCNS_NOTE_ERR environmental variable. ",
126             flags = Flags.Design)
127
128         ccns_repo_store = Attribute("ccnsRepoStore",
129             "Sets the CCNS_REPO_STORE environmental variable. ",
130             flags = Flags.Design)
131
132         ccns_root_advise_fresh = Attribute("ccnsRootAdviseFresh",
133             "Sets the CCNS_ROOT_ADVISE_FRESH environmental variable. ",
134             flags = Flags.Design)
135
136         ccns_root_advise_lifetime = Attribute("ccnsRootAdviseLifetime",
137             "Sets the CCNS_ROOT_ADVISE_LIFETIME environmental variable. ",
138             flags = Flags.Design)
139
140         ccns_stable_enabled = Attribute("ccnsStableEnabled",
141             "Sets the CCNS_STABLE_ENABLED environmental variable. ",
142             flags = Flags.Design)
143
144         ccns_sync_scope = Attribute("ccnsSyncScope",
145             "Sets the CCNS_SYNC_SCOPE environmental variable. ",
146             flags = Flags.Design)
147
148         repo_file = Attribute("repoFile1",
149             "The Repository uses $CCNR_DIRECTORY/repoFile1 for "
150             "persistent storage of CCN Content Objects",
151             flags = Flags.Design)
152
153         cls._register_attribute(max_fanout)
154         cls._register_attribute(max_leaf_entries)
155         cls._register_attribute(max_node_bytes)
156         cls._register_attribute(max_node_pool)
157         cls._register_attribute(content_cache)
158         cls._register_attribute(debug)
159         cls._register_attribute(directory)
160         cls._register_attribute(global_prefix)
161         cls._register_attribute(listen_on)
162         cls._register_attribute(min_send_bufsize)
163         cls._register_attribute(proto)
164         cls._register_attribute(status_port)
165         cls._register_attribute(start_write_scope_limit)
166         cls._register_attribute(ccns_debug)
167         cls._register_attribute(ccns_enable)
168         cls._register_attribute(ccns_faux_error)
169         cls._register_attribute(ccns_heartbeat_micros)
170         cls._register_attribute(ccns_max_compares_busy)
171         cls._register_attribute(ccns_max_fetch_busy)
172         cls._register_attribute(ccns_node_fetch_lifetime)
173         cls._register_attribute(ccns_note_err)
174         cls._register_attribute(ccns_repo_store)
175         cls._register_attribute(ccns_root_advise_fresh)
176         cls._register_attribute(ccns_root_advise_lifetime)
177         cls._register_attribute(ccns_stable_enabled)
178         cls._register_attribute(ccns_sync_scope)
179         cls._register_attribute(repo_file)
180
181     @classmethod
182     def _register_traces(cls):
183         log = Trace("log", "CCND log output")
184
185         cls._register_trace(log)
186
187     def __init__(self, ec, guid):
188         super(LinuxCCNR, self).__init__(ec, guid)
189         self._home = "ccnr-%s" % self.guid
190
191     @property
192     def ccnd(self):
193         ccnd = self.get_connected(LinuxCCND.get_rtype())
194         if ccnd: return ccnd[0]
195         return None
196
197     @property
198     def node(self):
199         if self.ccnd: return self.ccnd.node
200         return None
201
202     def do_deploy(self):
203         if not self.ccnd or self.ccnd.state < ResourceState.READY:
204             self.debug("---- RESCHEDULING DEPLOY ---- CCND state %s " % self.ccnd.state )
205             
206             # ccnr needs to wait until ccnd is deployed and running
207             self.ec.schedule(self.reschedule_delay, self.deploy)
208         else:
209             if not self.get("command"):
210                 self.set("command", self._start_command)
211
212             if not self.get("env"):
213                 self.set("env", self._environment)
214
215             command = self.get("command")
216
217             self.info("Deploying command '%s' " % command)
218
219             self.do_discover()
220             self.do_provision()
221
222             self.set_ready()
223
224     def upload_start_command(self):
225         command = self.get("command")
226         env = self.get("env")
227
228         if self.get("repoFile1"):
229             # upload repoFile1
230             local_file = self.get("repoFile1")
231             remote_file = "${RUN_HOME}/repoFile1"
232             remote_file = self.replace_paths(remote_file)
233             self.node.upload(local_file,
234                     remote_file,
235                     overwrite = False)
236
237         # We want to make sure the repository is running
238         # before the experiment starts.
239         # Run the command as a bash script in background,
240         # in the host ( but wait until the command has
241         # finished to continue )
242         env = self.replace_paths(env)
243         command = self.replace_paths(command)
244
245         shfile = os.path.join(self.app_home, "start.sh")
246         self.node.run_and_wait(command, self.run_home,
247                 shfile = shfile,
248                 overwrite = False,
249                 env = env)
250
251     def do_start(self):
252         if self.state == ResourceState.READY:
253             command = self.get("command")
254             self.info("Starting command '%s'" % command)
255
256             self.set_started()
257         else:
258             msg = " Failed to execute command '%s'" % command
259             self.error(msg, out, err)
260             raise RuntimeError(msg)
261
262     @property
263     def _start_command(self):
264         return "ccnr &"
265
266     @property
267     def _environment(self):
268         envs = dict({
269             "maxFanout": "CCNR_BTREE_MAX_FANOUT",
270             "maxLeafEntries": "CCNR_BTREE_MAX_LEAF_ENTRIES",
271             "maxNodeBytes": "CCNR_BTREE_MAX_NODE_BYTES",
272             "maxNodePool": "CCNR_BTREE_MAX_NODE_POOL",
273             "contentCache": "CCNR_CONTENT_CACHE",
274             "debug": "CCNR_DEBUG",
275             "directory": "CCNR_DIRECTORY",
276             "globalPrefix": "CCNR_GLOBAL_PREFIX",
277             "listenOn": "CCNR_LISTEN_ON",
278             "minSendBufsize": "CCNR_MIN_SEND_BUFSIZE",
279             "proto": "CCNR_PROTO",
280             "statusPort": "CCNR_STATUS_PORT",
281             "startWriteScopeLimit": "CCNR_START_WRITE_SCOPE_LIMIT",
282             "ccnsDebug": "CCNS_DEBUG",
283             "ccnsEnable": "CCNS_ENABLE",
284             "ccnsFauxError": "CCNS_FAUX_ERROR",
285             "ccnsHeartBeatMicros": "CCNS_HEART_BEAT_MICROS",
286             "ccnsMaxComparesBusy": "CCNS_MAX_COMPARES_BUSY",
287             "ccnsMaxFetchBusy": "CCNS_MAX_FETCH_BUSY",
288             "ccnsNodeFetchLifetime": "CCNS_NODE_FETCH_LIFETIME",
289             "ccnsNoteErr": "CCNS_NOTE_ERR",
290             "ccnsRepoStore": "CCNS_REPO_STORE",
291             "ccnsRootAdviseFresh": "CCNS_ROOT_ADVISE_FRESH",
292             "ccnsRootAdviseLifetime": "CCNS_ROOT_ADVISE_LIFETIME",
293             "ccnsStableEnabled": "CCNS_STABLE_ENABLED",
294             "ccnsSyncScope": "CCNS_SYNC_SCOPE",
295             })
296
297         env = self.ccnd.path
298         env += " ".join(map(lambda k: "%s=%s" % (envs.get(k), self.get(k)) \
299             if self.get(k) else "", envs.keys()))
300        
301         return env            
302         
303     def valid_connection(self, guid):
304         # TODO: Validate!
305         return True
306