2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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;
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.
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/>.
17 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 from nepi.execution.attribute import Attribute, Flags, Types
20 from nepi.execution.trace import Trace, TraceAttr
21 from nepi.execution.resource import ResourceManager, clsinit_copy, \
22 ResourceState, ResourceAction
28 class Collector(ResourceManager):
29 """ The collector entity is reponsible of collecting traces
30 of a same type associated to RMs into a local directory.
32 .. class:: Class Args :
34 :param ec: The Experiment controller
35 :type ec: ExperimentController
36 :param guid: guid of the RM
41 _help = "A Collector can be attached to a trace name on another " \
42 "ResourceManager and will retrieve and store the trace content " \
43 "in a local file at the end of the experiment"
47 def _register_attributes(cls):
48 cls._register_attribute(
49 Attribute("traceName",
50 "Name of the trace to be collected",
51 flags = Flags.Design))
52 cls._register_attribute(
54 "Sub directory to collect traces into",
55 flags = Flags.Design))
56 cls._register_attribute(
58 "Name to give to the collected trace file",
59 flags = Flags.Design))
61 def __init__(self, ec, guid):
62 super(Collector, self).__init__(ec, guid)
63 self._store_path = None
67 return self._store_path
69 def do_provision(self):
70 trace_name = self.get("traceName")
74 msg = "No traceName was specified"
76 raise RuntimeError(msg)
78 self._store_path = self.ec.run_dir
80 subdir = self.get("subDir")
82 self._store_path = os.path.join(self.store_path, subdir)
84 msg = "Creating local directory at {} to store {} traces "\
85 .format(self.store_path, trace_name)
89 os.makedirs(self.store_path)
93 super(Collector, self).do_provision()
99 super(Collector, self).do_deploy()
101 def do_release(self):
102 trace_name = self.get("traceName")
103 rename = self.get("rename") or trace_name
105 msg = "Collecting '{}' traces to local directory {}"\
106 .format(trace_name, self.store_path)
109 rms = self.get_connected()
111 fpath = os.path.join(self.store_path, "{}.{}"\
112 .format(rm.guid, rename))
115 result = self.ec.trace(rm.guid, trace_name)
116 print("collector.do_release ..")
117 with open(fpath, "w") as f:
121 err = traceback.format_exc()
122 msg = "Couldn't retrieve trace {} for {} at {} "\
123 .format(trace_name, rm.guid, fpath)
124 self.error(msg, out = "", err = err)
127 super(Collector, self).do_release()
129 def valid_connection(self, guid):