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