4729e0153b7774d4973b4e4d22bbc6d0eed0a753
[nepi.git] / nepi / resources / all / collector.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 ResourceManager, clsinit_copy, \
22         ResourceState, ResourceAction
23 from nepi.util.sshfuncs import ProcStatus
24
25 import os
26 import tempfile
27
28 @clsinit_copy
29 class Collector(ResourceManager):
30     """ The collector entity is reponsible of collecting traces
31     of a same type associated to RMs into a local directory.
32
33     .. class:: Class Args :
34
35         :param ec: The Experiment controller
36         :type ec: ExperimentController
37         :param guid: guid of the RM
38         :type guid: int
39
40     """
41     _rtype = "Collector"
42     _help = "A Collector can be attached to a trace name on another " \
43         "ResourceManager and will retrieve and store the trace content " \
44         "in a local file at the end of the experiment"
45     _platform = "all"
46
47     @classmethod
48     def _register_attributes(cls):
49         trace_name = Attribute("traceName", 
50                 "Name of the trace to be collected", 
51                 flags = Flags.Design)
52
53         sub_dir = Attribute("subDir", 
54                 "Sub directory to collect traces into", 
55                 flags = Flags.Design)
56
57         rename = Attribute("rename", 
58                 "Name to give to the collected trace file", 
59                 flags = Flags.Design)
60
61         cls._register_attribute(trace_name)
62         cls._register_attribute(sub_dir)
63         cls._register_attribute(rename)
64
65     def __init__(self, ec, guid):
66         super(Collector, self).__init__(ec, guid)
67         self._store_path =  None
68
69     @property
70     def store_path(self):
71         return self._store_path
72    
73     def do_provision(self):
74         trace_name = self.get("traceName")
75         if not trace_name:
76             self.fail()
77             
78             msg = "No traceName was specified"
79             self.error(msg)
80             raise RuntimeError(msg)
81
82         self._store_path = self.ec.run_dir
83
84         subdir = self.get("subDir")
85         if subdir:
86             self._store_path = os.path.join(self.store_path, subdir)
87         
88         msg = "Creating local directory at %s to store %s traces " % (
89                 self.store_path, trace_name)
90         self.info(msg)
91
92         try:
93             os.makedirs(self.store_path)
94         except OSError:
95             pass
96
97         super(Collector, self).do_provision()
98
99     def do_deploy(self):
100         self.do_discover()
101         self.do_provision()
102
103         super(Collector, self).do_deploy()
104
105     def do_release(self):
106         trace_name = self.get("traceName")
107         rename = self.get("rename") or trace_name
108
109         msg = "Collecting '%s' traces to local directory %s" % (
110             trace_name, self.store_path)
111         self.info(msg)
112
113         rms = self.get_connected()
114         for rm in rms:
115             fpath = os.path.join(self.store_path, "%d.%s" % (rm.guid, 
116                  rename))
117
118             try:
119                 result = self.ec.trace(rm.guid, trace_name)
120                 with open(fpath, "w") as f:
121                     f.write(result)
122             except:
123                 import traceback
124                 err = traceback.format_exc()
125                 msg = "Couldn't retrieve trace %s for %d at %s " % (trace_name, 
126                         rm.guid, fpath)
127                 self.error(msg, out = "", err = err)
128                 continue
129
130         super(Collector, self).do_release()
131
132     def valid_connection(self, guid):
133         # TODO: Validate!
134         return True
135