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