Adding trace Collector RM
[nepi.git] / src / 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 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, ResourceState, \
23         ResourceAction
24 from nepi.util.sshfuncs import ProcStatus
25 from nepi.util.timefuncs import tsformat
26
27 import os
28 import tempfile
29
30 @clsinit
31 class Collector(ResourceManager):
32     """ The collector is reponsible of collecting traces
33     of a same type associated to RMs into a local directory.
34
35     .. class:: Class Args :
36
37         :param ec: The Experiment controller
38         :type ec: ExperimentController
39         :param guid: guid of the RM
40         :type guid: int
41         :param creds: Credentials to communicate with the rm (XmppClient)
42         :type creds: dict
43
44     """
45     _rtype = "Collector"
46
47     @classmethod
48     def _register_attributes(cls):
49         trace_name = Attribute("traceName", "Name of the trace to be collected", 
50                 flags = Flags.ExecReadOnly)
51         store_dir = Attribute("storeDir", "Path to local directory to store trace results", 
52                 default = tempfile.gettempdir(),
53                 flags = Flags.ExecReadOnly)
54
55         cls._register_attribute(trace_name)
56         cls._register_attribute(store_dir)
57
58     def __init__(self, ec, guid):
59         super(Collector, self).__init__(ec, guid)
60         self._store_path =  None
61
62     @property
63     def store_path(self):
64         return self._store_path
65     
66     def provision(self):
67         trace_name = self.get("traceName")
68         if not trace_name:
69             self.fail()
70             
71             msg = "No traceName was specified"
72             self.error(msg)
73             raise RuntimeError, msg
74
75         store_dir = self.get("storeDir")
76         timestamp = tsformat()
77         self._store_path = os.path.join(store_dir, self.ec.exp_id, timestamp)
78         
79         msg = "Creating local directory at %s to store %s traces " % (
80             store_dir, trace_name)
81         self.info(msg)
82
83         try:
84             os.makedirs(self.store_path)
85         except OSError:
86             pass
87
88         super(Collector, self).provision()
89
90     def deploy(self):
91         try:
92             self.discover()
93             self.provision()
94         except:
95             self.fail()
96             raise
97
98         super(Collector, self).deploy()
99
100     def release(self):
101         trace_name = self.get("traceName")
102
103         msg = "Collecting '%s' traces to local directory %s" % (
104             trace_name, self.store_path)
105         self.info(msg)
106
107         rms = self.get_connected()
108         for rm in rms:
109             result = self.ec.trace(rm.guid, trace_name)
110             fpath = os.path.join(self.store_path, "%d.%s" % (rm.guid, 
111                 trace_name))
112             f = open(fpath, "w")
113             f.write(result)
114             f.close()
115
116         super(Collector, self).release()
117
118     def valid_connection(self, guid):
119         # TODO: Validate!
120         return True
121