Ticket #29: Implement dependencies to support testbed-in-PL
[nepi.git] / src / nepi / testbeds / planetlab / execute.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 from constants import TESTBED_ID
5 from nepi.core import testbed_impl
6 import os
7 import time
8
9 class TestbedController(testbed_impl.TestbedController):
10     def __init__(self, testbed_version):
11         super(TestbedController, self).__init__(TESTBED_ID, testbed_version)
12         self._home_directory = None
13         self.slicename = None
14         self._traces = dict()
15
16         import node, interfaces, application
17         self._node = node
18         self._interfaces = interfaces
19         self._app = application
20
21     @property
22     def home_directory(self):
23         return self._home_directory
24
25     @property
26     def plapi(self):
27         if not hasattr(self, '_plapi'):
28             import plcapi
29
30             if self.authUser:
31                 self._plapi = plcapi.PLCAPI(
32                     username = self.authUser,
33                     password = self.authString)
34             else:
35                 # anonymous access - may not be enough for much
36                 self._plapi = plcapi.PLCAPI()
37         return self._plapi
38
39     @property
40     def slice_id(self):
41         if not hasattr(self, '_slice_id'):
42             slices = self.plapi.GetSlices(self.slicename, fields=('slice_id',))
43             if slices:
44                 self._slice_id = slices[0]['slice_id']
45             else:
46                 # If it wasn't found, don't remember this failure, keep trying
47                 return None
48         return self._slice_id
49
50     def do_setup(self):
51         self._home_directory = self._attributes.\
52             get_attribute_value("homeDirectory")
53         self.slicename = self._attributes.\
54             get_attribute_value("slice")
55         self.authUser = self._attributes.\
56             get_attribute_value("authUser")
57         self.authString = self._attributes.\
58             get_attribute_value("authPass")
59         self.sliceSSHKey = self._attributes.\
60             get_attribute_value("sliceSSHKey")
61
62     def do_preconfigure(self):
63         # Perform resource discovery if we don't have
64         # specific resources assigned yet
65         self.do_resource_discovery()
66
67         # Create PlanetLab slivers
68         self.do_provisioning()
69
70         # Configure elements per XML data
71         super(TestbedController, self).do_preconfigure()
72
73     def do_resource_discovery(self):
74         # Do what?
75
76         # Provisional algo:
77         #   look for perfectly defined nodes
78         #   (ie: those with only one candidate)
79         to_provision = self._to_provision = set()
80         for guid, node in self._elements.iteritems():
81             if isinstance(node, self._node.Node) and node._node_id is None:
82                 # Try existing nodes first
83                 # If we have only one candidate, simply use it
84                 candidates = node.find_candidates(
85                     filter_slice_id = self.slice_id)
86                 if len(candidates) == 1:
87                     node.assign_node_id(iter(candidates).next())
88                 else:
89                     # Try again including unassigned nodes
90                     candidates = node.find_candidates()
91                     if len(candidates) > 1:
92                         raise RuntimeError, "Cannot assign resources for node %s, too many candidates" % (guid,)
93                     if len(candidates) == 1:
94                         node_id = iter(candidates).next()
95                         node.assign_node_id(node_id)
96                         to_provision.add(node_id)
97                     elif not candidates:
98                         raise RuntimeError, "Cannot assign resources for node %s, no candidates" % (guid,)
99
100     def do_provisioning(self):
101         if self._to_provision:
102             # Add new nodes to the slice
103             cur_nodes = self.plapi.GetSlices(self.slicename, ['node_ids'])[0]['node_ids']
104             new_nodes = list(set(cur_nodes) | self._to_provision)
105             self.plapi.UpdateSlice(self.slicename, nodes=new_nodes)
106
107         # cleanup
108         del self._to_provision
109
110
111     def set(self, time, guid, name, value):
112         super(TestbedController, self).set(time, guid, name, value)
113         # TODO: take on account schedule time for the task
114         element = self._elements[guid]
115         if element:
116             setattr(element, name, value)
117
118             if hasattr(element, 'refresh'):
119                 # invoke attribute refresh hook
120                 element.refresh()
121
122     def get(self, time, guid, name):
123         # TODO: take on account schedule time for the task
124         element = self._elements.get(guid)
125         if element:
126             try:
127                 if hasattr(element, name):
128                     # Runtime attribute
129                     return getattr(element, name)
130                 else:
131                     # Try design-time attributes
132                     return self.box_get(time, guid, name)
133             except KeyError, AttributeError:
134                 return None
135
136     def get_route(self, guid, index, attribute):
137         # TODO: fetch real data from planetlab
138         try:
139             return self.box_get_route(guid, int(index), attribute)
140         except KeyError, AttributeError:
141             return None
142
143     def get_address(self, guid, index, attribute='Address'):
144         index = int(index)
145
146         # try the real stuff
147         iface = self._elements.get(guid)
148         if iface and index == 0:
149             if attribute == 'Address':
150                 return iface.address
151             elif attribute == 'NetPrefix':
152                 return iface.netprefix
153             elif attribute == 'Broadcast':
154                 return iface.broadcast
155
156         # if all else fails, query box
157         try:
158             return self.box_get_address(guid, index, attribute)
159         except KeyError, AttributeError:
160             return None
161
162
163     def action(self, time, guid, action):
164         raise NotImplementedError
165
166     def shutdown(self):
167         for trace in self._traces.values():
168             trace.close()
169         for element in self._elements.values():
170             # invoke cleanup hooks
171             if hasattr(element, 'cleanup'):
172                 element.cleanup()
173
174     def trace(self, guid, trace_id, attribute='value'):
175         app = self._elements[guid]
176
177         if attribute == 'value':
178             path = app.sync_trace(self.home_directory, trace_id)
179             if path:
180                 fd = open(path, "r")
181                 content = fd.read()
182                 fd.close()
183             else:
184                 content = None
185         elif attribute == 'path':
186             content = app.remote_trace_path(trace_id)
187         else:
188             content = None
189         return content
190
191     def follow_trace(self, trace_id, trace):
192         self._traces[trace_id] = trace
193     
194     def _make_generic(self, parameters, kind):
195         app = kind(self.plapi)
196
197         # Note: there is 1-to-1 correspondence between attribute names
198         #   If that changes, this has to change as well
199         for attr,val in parameters.iteritems():
200             setattr(app, attr, val)
201
202         return app
203
204     def _make_node(self, parameters):
205         node = self._make_generic(parameters, self._node.Node)
206
207         # If emulation is enabled, we automatically need
208         # some vsys interfaces and packages
209         if node.emulation:
210             node.required_vsys.add('ipfw-be')
211             node.required_packages.add('ipfwslice')
212
213         return node
214
215     def _make_node_iface(self, parameters):
216         return self._make_generic(parameters, self._interfaces.NodeIface)
217
218     def _make_tun_iface(self, parameters):
219         return self._make_generic(parameters, self._interfaces.TunIface)
220
221     def _make_netpipe(self, parameters):
222         return self._make_generic(parameters, self._interfaces.NetPipe)
223
224     def _make_internet(self, parameters):
225         return self._make_generic(parameters, self._interfaces.Internet)
226
227     def _make_application(self, parameters):
228         return self._make_generic(parameters, self._app.Application)
229
230     def _make_dependency(self, parameters):
231         return self._make_generic(parameters, self._app.Dependency)
232
233     def _make_nepi_dependency(self, parameters):
234         return self._make_generic(parameters, self._app.NepiDependency)
235
236