Fix #123 [NS3] Upload a local ns-3 sources tar
[nepi.git] / src / nepi / resources / linux / ns3 / ns3simulation.py
1 #
2 #    NEPI, a framework to manage network experiments
3 #    Copyright (C) 2014 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.util.timefuncs import tnow, tdiffsec
26 from nepi.resources.ns3.ns3simulation import NS3Simulation
27 from nepi.resources.ns3.ns3wrapper import SIMULATOR_UUID, GLOBAL_VALUE_UUID
28 from nepi.resources.linux.ns3.ns3client import LinuxNS3Client
29
30 import os
31 import time
32
33 @clsinit_copy
34 class LinuxNS3Simulation(LinuxApplication, NS3Simulation):
35     _rtype = "LinuxNS3Simulation"
36
37     @classmethod
38     def _register_attributes(cls):
39         impl_type = Attribute("simulatorImplementationType",
40                 "The object class to use as the simulator implementation",
41             allowed = ["ns3::DefaultSimulatorImpl", "ns3::RealtimeSimulatorImpl"],
42             default = "ns3::DefaultSimulatorImpl",
43             type = Types.Enumerate,
44             flags = Flags.Design)
45
46         sched_type = Attribute("schedulerType",
47                 "The object class to use as the scheduler implementation",
48                 allowed = ["ns3::MapScheduler",
49                             "ns3::ListScheduler",
50                             "ns3::HeapScheduler",
51                             "ns3::MapScheduler",
52                             "ns3::CalendarScheduler"
53                     ],
54             default = "ns3::MapScheduler",
55             type = Types.Enumerate,
56             flags = Flags.Design)
57
58         check_sum = Attribute("checksumEnabled",
59                 "A global switch to enable all checksums for all protocols",
60             default = False,
61             type = Types.Bool,
62             flags = Flags.Design)
63
64         ns_log = Attribute("nsLog",
65             "NS_LOG environment variable. " \
66                     " Will only generate output if ns-3 is compiled in DEBUG mode. ",
67             flags = Flags.Design)
68
69         verbose = Attribute("verbose",
70             "True to output debugging info from the ns3 client-server communication",
71             type = Types.Bool,
72             flags = Flags.Design)
73
74         build_mode = Attribute("buildMode",
75             "Mode used to build ns-3 with waf. One if: debug, release, oprimized ",
76             default = "release", 
77             allowed = ["debug", "release", "optimized"],
78             type = Types.Enumerate,
79             flags = Flags.Design)
80
81         ns3_version = Attribute("ns3Version",
82             "Version of ns-3 to install from nsam repo",
83             default = "ns-3.19", 
84             flags = Flags.Design)
85
86         pybindgen_version = Attribute("pybindgenVersion",
87             "Version of pybindgen to install from bazar repo",
88             default = "834", 
89             flags = Flags.Design)
90
91         cls._register_attribute(impl_type)
92         cls._register_attribute(sched_type)
93         cls._register_attribute(check_sum)
94         cls._register_attribute(ns_log)
95         cls._register_attribute(verbose)
96         cls._register_attribute(build_mode)
97         cls._register_attribute(ns3_version)
98         cls._register_attribute(pybindgen_version)
99
100     def __init__(self, ec, guid):
101         LinuxApplication.__init__(self, ec, guid)
102         NS3Simulation.__init__(self)
103
104         self._client = None
105         self._home = "ns3-simu-%s" % self.guid
106         self._socket_name = "ns3simu-%s" % os.urandom(8).encode('hex')
107
108     @property
109     def socket_name(self):
110         return self._socket_name
111
112     @property
113     def remote_socket(self):
114         return os.path.join(self.run_home, self.socket_name)
115
116     @property
117     def local_socket(self):
118         if self.node.get('hostname') in ['localhost', '127.0.0.01']:
119             return self.remote_socket
120
121         return os.path.join("/", "tmp", self.socket_name)
122
123     def trace(self, name, attr = TraceAttr.ALL, block = 512, offset = 0):
124         self._client.flush() 
125         return LinuxApplication.trace(self, name, attr, block, offset)
126
127     def upload_sources(self):
128         self.node.mkdir(os.path.join(self.node.src_dir, "ns3wrapper"))
129
130         # upload ns3 wrapper python script
131         ns3_wrapper = os.path.join(os.path.dirname(__file__), "..", "..", "ns3", 
132                 "ns3wrapper.py")
133
134         self.node.upload(ns3_wrapper,
135                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3wrapper.py"),
136                 overwrite = False)
137
138         # upload ns3_server python script
139         ns3_server = os.path.join(os.path.dirname(__file__), "..", "..", "ns3",
140                 "ns3server.py")
141
142         self.node.upload(ns3_server,
143                 os.path.join(self.node.src_dir, "ns3wrapper", "ns3server.py"),
144                 overwrite = False)
145
146         if self.node.use_rpm:
147             # upload pygccxml sources
148             pygccxml_tar = os.path.join(os.path.dirname(__file__), "dependencies",
149                     "%s.tar.gz" % self.pygccxml_version)
150
151             self.node.upload(pygccxml_tar,
152                     os.path.join(self.node.src_dir, "%s.tar.gz" % self.pygccxml_version),
153                     overwrite = False)
154
155         # Upload user defined ns-3 sources
156         self.node.mkdir(os.path.join(self.node.src_dir, "ns-3"))
157         src_dir = os.path.join(self.node.src_dir, "ns-3")
158
159         super(LinuxNS3Simulation, self).upload_sources(src_dir = src_dir)
160
161     def upload_start_command(self):
162         command = self.get("command")
163         env = self.get("env")
164
165         # We want to make sure the ccnd is running
166         # before the experiment starts.
167         # Run the command as a bash script in background,
168         # in the host ( but wait until the command has
169         # finished to continue )
170         env = self.replace_paths(env)
171         command = self.replace_paths(command)
172
173         shfile = os.path.join(self.app_home, "start.sh")
174         self.node.upload_command(command, 
175                     shfile = shfile,
176                     env = env,
177                     overwrite = True)
178
179         # Run the ns3wrapper 
180         self._run_in_background()
181
182     def configure(self):
183         if self.has_changed("simulatorImplementationType"):
184             simu_type = self.get("simulatorImplementationType")
185             stype = self.create("StringValue", simu_type)
186             self.invoke(GLOBAL_VALUE_UUID, "Bind", "SimulatorImplementationType", stype)
187
188         if self.has_changed("checksumEnabled"):
189             check_sum = self.get("checksumEnabled")
190             btrue = self.create("BooleanValue", check_sum)    
191             self.invoke(GLOBAL_VALUE_UUID, "Bind", "ChecksumEnabled", btrue)
192         
193         if self.has_changed("schedulerType"):
194             sched_type = self.get("schedulerType")
195             stype = self.create("StringValue", sched_type)
196             self.invoke(GLOBAL_VALUE_UUID, "Bind", "SchedulerType", btrue)
197        
198     def do_deploy(self):
199         if not self.node or self.node.state < ResourceState.READY:
200             self.debug("---- RESCHEDULING DEPLOY ---- node state %s " % self.node.state )
201             
202             # ccnd needs to wait until node is deployed and running
203             self.ec.schedule(reschedule_delay, self.deploy)
204         else:
205             if not self.get("command"):
206                 self.set("command", self._start_command)
207             
208             if not self.get("depends"):
209                 self.set("depends", self._dependencies)
210
211             if self.get("sources"):
212                 sources = self.get("sources")
213                 source = sources.split(" ")[0]
214                 basename = os.path.basename(source)
215                 version = ( basename.strip().replace(".tar.gz", "")
216                     .replace(".tar","")
217                     .replace(".gz","")
218                     .replace(".zip","") )
219
220                 self.set("ns3Version", version)
221                 self.set("sources", source)
222
223             if not self.get("build"):
224                 self.set("build", self._build)
225
226             if not self.get("install"):
227                 self.set("install", self._install)
228
229             if not self.get("env"):
230                 self.set("env", self._environment)
231
232             self.do_discover()
233             self.do_provision()
234
235             # Create client
236             self._client = LinuxNS3Client(self)
237            
238             # Wait until local socket is created
239             for i in [1, 5, 15, 30, 60]:
240                 if os.path.exists(self.local_socket):
241                     break
242                 time.sleep(i)
243
244             if not os.path.exists(self.local_socket):
245                 raise RuntimeError("Problem starting socat")
246
247             self.configure()
248             
249             self.set_ready()
250
251     def do_start(self):
252         """ Starts simulation execution
253
254         """
255         self.info("Starting")
256
257         if self.state == ResourceState.READY:
258             self._client.start() 
259
260             self.set_started()
261         else:
262             msg = " Failed to execute command '%s'" % command
263             self.error(msg, out, err)
264             raise RuntimeError, msg
265
266     def do_stop(self):
267         """ Stops simulation execution
268
269         """
270         if self.state == ResourceState.STARTED:
271             self._client.stop() 
272             self.set_stopped()
273
274     def do_release(self):
275         self.info("Releasing resource")
276
277         tear_down = self.get("tearDown")
278         if tear_down:
279             self.node.execute(tear_down)
280
281         self.do_stop()
282         self._client.shutdown()
283         LinuxApplication.do_stop(self)
284         
285         super(LinuxApplication, self).do_release()
286
287     @property
288     def _start_command(self):
289         command = [] 
290         command.append("PYTHONPATH=$PYTHONPATH:${SRC}/ns3wrapper/")
291
292         command.append("python ${SRC}/ns3wrapper/ns3server.py -S %s" % self.remote_socket )
293
294         ns_log = self.get("nsLog")
295         if ns_log:
296             command.append("-L %s" % ns_log)
297
298         if self.get("verbose"):
299             command.append("-v")
300
301         command = " ".join(command)
302         return command
303
304     @property
305     def _dependencies(self):
306         if self.node.use_rpm:
307             return ( " gcc gcc-c++ python python-devel mercurial bzr tcpdump socat gccxml unzip")
308         elif self.node.use_deb:
309             return ( " gcc g++ python python-dev mercurial bzr tcpdump socat gccxml python-pygccxml unzip")
310         return ""
311
312     @property
313     def ns3_repo(self):
314         return "http://code.nsnam.org"
315
316     @property
317     def pygccxml_version(self):
318         return "pygccxml-1.0.0"
319
320     @property
321     def _build(self):
322         # If the user defined local sources for ns-3, we uncompress the sources
323         # on the remote sources directory. Else we clone ns-3 from the official repo.
324         source = self.get("sources")
325         if not source:
326             copy_ns3_cmd = "hg clone %(ns3_repo)s/%(ns3_version)s ${SRC}/ns-3/%(ns3_version)s" \
327                     % ({
328                         'ns3_version': self.get("ns3Version"),
329                         'ns3_repo':  self.ns3_repo,       
330                        })
331         else:
332             if source.find(".tar.gz") > -1:
333                 copy_ns3_cmd = ( 
334                             "tar xzf ${SRC}/ns-3/%(basename)s " 
335                             " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
336                             ) % ({
337                                 'basename': os.path.basename(source),
338                                 'ns3_version': self.get("ns3Version"),
339                                 })
340             elif source.find(".tar") > -1:
341                 copy_ns3_cmd = ( 
342                             "tar xf ${SRC}/ns-3/%(basename)s " 
343                             " --strip-components=1 -C ${SRC}/ns-3/%(ns3_version)s "
344                             ) % ({
345                                 'basename': os.path.basename(source),
346                                 'ns3_version': self.get("ns3Version"),
347                                 })
348             elif source.find(".zip") > -1:
349                 basename = os.path.basename(source)
350                 bare_basename = basename.replace(".zip", "") \
351                         .replace(".tar", "") \
352                         .replace(".tar.gz", "")
353
354                 copy_ns3_cmd = ( 
355                             "unzip ${SRC}/ns-3/%(basename)s && "
356                             "mv ${SRC}/ns-3/%(bare_basename)s ${SRC}/ns-3/%(ns3_version)s "
357                             ) % ({
358                                 'bare_basename': basename_name,
359                                 'basename': basename,
360                                 'ns3_version': self.get("ns3Version"),
361                                 })
362
363         return (
364                 # Test if ns-3 is alredy installed
365                 " ( "
366                 " (( "
367                 "    ( test -d ${SRC}/ns-3/%(ns3_version)s ) || "
368                 "    ( test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) "
369                 "  ) && echo 'binaries found, nothing to do' )"
370                 " ) "
371                 "  || " 
372                 # If not, install ns-3 and its dependencies
373                 " (   "
374                 # Install pygccxml
375                 "   (   "
376                 "     ( "
377                 "       python -c 'import pygccxml' && "
378                 "       echo 'pygccxml not found' "
379                 "     ) "
380                 "      || "
381                 "     ( "
382                 "       tar xf ${SRC}/%(pygccxml_version)s.tar.gz -C ${SRC} && "
383                 "       cd ${SRC}/%(pygccxml_version)s && "
384                 "       sudo -S python setup.py install "
385                 "     ) "
386                 "   ) " 
387                 # Install pybindgen
388                 "  && "
389                 "   (   "
390                 "     ( "
391                 "       test -d ${SRC}/pybindgen/%(pybindgen_version)s && "
392                 "       echo 'binaries found, nothing to do' "
393                 "     ) "
394                 "      || "
395                 # If not, clone and build
396                 "      ( cd ${SRC} && "
397                 "        mkdir -p ${SRC}/pybindgen && "
398                 "        bzr checkout lp:pybindgen -r %(pybindgen_version)s ${SRC}/pybindgen/%(pybindgen_version)s && "
399                 "        cd ${SRC}/pybindgen/%(pybindgen_version)s && "
400                 "        ./waf configure && "
401                 "        ./waf "
402                 "      ) "
403                 "   ) " 
404                 " && "
405                 # Get ns-3 source code
406                 "  ( "
407                 "     mkdir -p ${SRC}/ns-3/%(ns3_version)s && "
408                 "     %(copy_ns3_cmd)s "
409                 "  ) "
410                 " ) "
411              ) % ({ 
412                     'ns3_version': self.get("ns3Version"),
413                     'pybindgen_version': self.get("pybindgenVersion"),
414                     'pygccxml_version': self.pygccxml_version,
415                     'copy_ns3_cmd': copy_ns3_cmd,
416                  })
417
418     @property
419     def _install(self):
420         return (
421                  # Test if ns-3 is alredy cloned
422                 " ( "
423                 "  ( ( (test -d ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build ) || "
424                 "    (test -d ${NS3BINDINGS:='None'} && test -d ${NS3LIBRARIES:='None'}) ) && "
425                 "    echo 'binaries found, nothing to do' )"
426                 " ) "
427                 " ||" 
428                 " (   "
429                  # If not, copy ns-3 build to bin
430                 "  cd ${SRC}/ns-3/%(ns3_version)s && "
431                 "  ./waf configure -d %(build_mode)s --with-pybindgen=${SRC}/pybindgen/%(pybindgen_version)s && "
432                 "  ./waf && "
433                 "  mkdir -p ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s && "
434                 "  mv ${SRC}/ns-3/%(ns3_version)s/build ${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build "
435                 " )"
436              ) % ({ 
437                     'ns3_version': self.get("ns3Version"),
438                     'pybindgen_version': self.get("pybindgenVersion"),
439                     'build_mode': self.get("buildMode"),
440                  })
441
442     @property
443     def _environment(self):
444         env = []
445         env.append("NS3BINDINGS=${NS3BINDINGS:=${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build/bindings/python/}" % ({ 
446                     'ns3_version': self.get("ns3Version"),
447                     'build_mode': self.get("buildMode")
448                  }))
449         env.append("NS3LIBRARIES=${NS3LIBRARIES:=${BIN}/ns-3/%(ns3_version)s/%(build_mode)s/build/}" % ({ 
450                     'ns3_version': self.get("ns3Version"),
451                     'build_mode': self.get("buildMode")
452                  }))
453
454         return " ".join(env) 
455
456     def valid_connection(self, guid):
457         # TODO: Validate!
458         return True
459