2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID, TESTBED_VERSION
5 from nepi.core import testbed_impl
6 from nepi.util.constants import TIME_NOW
7 from nepi.util.graphtools import mst
8 from nepi.util import ipaddr2
9 from nepi.util import environ
10 from nepi.util.parallel import ParallelRun
27 class TempKeyError(Exception):
30 class TestbedController(testbed_impl.TestbedController):
32 super(TestbedController, self).__init__(TESTBED_ID, TESTBED_VERSION)
33 self._home_directory = None
37 import node, interfaces, application
39 self._interfaces = interfaces
40 self._app = application
42 self._blacklist = set()
43 self._just_provisioned = set()
45 self._load_blacklist()
47 self._logger = logging.getLogger('nepi.testbeds.planetlab')
50 def home_directory(self):
51 return self._home_directory
55 if not hasattr(self, '_plapi'):
59 self._plapi = plcapi.PLCAPI(
60 username = self.authUser,
61 password = self.authString,
62 hostname = self.plcHost,
63 urlpattern = self.plcUrl
66 # anonymous access - may not be enough for much
67 self._plapi = plcapi.PLCAPI()
72 if not hasattr(self, '_slice_id'):
73 slices = self.plapi.GetSlices(self.slicename, fields=('slice_id',))
75 self._slice_id = slices[0]['slice_id']
77 # If it wasn't found, don't remember this failure, keep trying
81 def _load_blacklist(self):
82 blpath = environ.homepath('plblacklist')
85 bl = open(blpath, "r")
87 self._blacklist = set()
91 self._blacklist = set(
93 map(str.strip, bl.readlines())
99 def _save_blacklist(self):
100 blpath = environ.homepath('plblacklist')
101 bl = open(blpath, "w")
104 map('%s\n'.__mod__, self._blacklist))
109 self._home_directory = self._attributes.\
110 get_attribute_value("homeDirectory")
111 self.slicename = self._attributes.\
112 get_attribute_value("slice")
113 self.authUser = self._attributes.\
114 get_attribute_value("authUser")
115 self.authString = self._attributes.\
116 get_attribute_value("authPass")
117 self.sliceSSHKey = self._attributes.\
118 get_attribute_value("sliceSSHKey")
119 self.sliceSSHKeyPass = None
120 self.plcHost = self._attributes.\
121 get_attribute_value("plcHost")
122 self.plcUrl = self._attributes.\
123 get_attribute_value("plcUrl")
124 self.logLevel = self._attributes.\
125 get_attribute_value("plLogLevel")
126 self.tapPortBase = self._attributes.\
127 get_attribute_value("tapPortBase")
129 self._logger.setLevel(getattr(logging,self.logLevel))
131 super(TestbedController, self).do_setup()
133 def do_post_asynclaunch(self, guid):
134 # Dependencies were launched asynchronously,
136 dep = self._elements[guid]
137 if isinstance(dep, self._app.Dependency):
138 dep.async_setup_wait()
140 # Two-phase configuration for asynchronous launch
141 do_poststep_preconfigure = staticmethod(do_post_asynclaunch)
142 do_poststep_configure = staticmethod(do_post_asynclaunch)
144 def do_preconfigure(self):
146 # Perform resource discovery if we don't have
147 # specific resources assigned yet
148 self.do_resource_discovery()
150 # Create PlanetLab slivers
151 self.do_provisioning()
154 # Wait for provisioning
159 except self._node.UnresponsiveNodeError:
163 # Plan application deployment
164 self.do_spanning_deployment_plan()
166 # Configure elements per XML data
167 super(TestbedController, self).do_preconfigure()
169 def do_resource_discovery(self):
170 to_provision = self._to_provision = set()
172 reserved = set(self._blacklist)
173 for guid, node in self._elements.iteritems():
174 if isinstance(node, self._node.Node) and node._node_id is not None:
175 reserved.add(node._node_id)
178 # look for perfectly defined nodes
179 # (ie: those with only one candidate)
180 for guid, node in self._elements.iteritems():
181 if isinstance(node, self._node.Node) and node._node_id is None:
182 # Try existing nodes first
183 # If we have only one candidate, simply use it
184 candidates = node.find_candidates(
185 filter_slice_id = self.slice_id)
186 candidates -= reserved
187 if len(candidates) == 1:
188 node_id = iter(candidates).next()
189 node.assign_node_id(node_id)
190 reserved.add(node_id)
192 # Try again including unassigned nodes
193 candidates = node.find_candidates()
194 candidates -= reserved
195 if len(candidates) > 1:
197 if len(candidates) == 1:
198 node_id = iter(candidates).next()
199 node.assign_node_id(node_id)
200 to_provision.add(node_id)
201 reserved.add(node_id)
203 raise RuntimeError, "Cannot assign resources for node %s, no candidates sith %s" % (guid,
204 node.make_filter_description())
206 # Now do the backtracking search for a suitable solution
207 # First with existing slice nodes
210 for guid, node in self._elements.iteritems():
211 if isinstance(node, self._node.Node) and node._node_id is None:
212 # Try existing nodes first
213 # If we have only one candidate, simply use it
214 candidates = node.find_candidates(
215 filter_slice_id = self.slice_id)
216 candidates -= reserved
217 reqs.append(candidates)
222 solution = resourcealloc.alloc(reqs)
223 except resourcealloc.ResourceAllocationError:
224 # Failed, try again with all nodes
227 candidates = node.find_candidates()
228 candidates -= reserved
229 reqs.append(candidates)
231 solution = resourcealloc.alloc(reqs)
232 to_provision.update(solution)
235 for node, node_id in zip(nodes, solution):
236 node.assign_node_id(node_id)
238 def do_provisioning(self):
239 if self._to_provision:
240 # Add new nodes to the slice
241 cur_nodes = self.plapi.GetSlices(self.slicename, ['node_ids'])[0]['node_ids']
242 new_nodes = list(set(cur_nodes) | self._to_provision)
243 self.plapi.UpdateSlice(self.slicename, nodes=new_nodes)
246 self._just_provisioned = self._to_provision
247 del self._to_provision
249 def do_wait_nodes(self):
250 for guid, node in self._elements.iteritems():
251 if isinstance(node, self._node.Node):
252 # Just inject configuration stuff
253 node.home_path = "nepi-node-%s" % (guid,)
254 node.ident_path = self.sliceSSHKey
255 node.slicename = self.slicename
258 self._logger.info("PlanetLab Node %s configured at %s", guid, node.hostname)
261 for guid, node in self._elements.iteritems():
262 if isinstance(node, self._node.Node):
263 self._logger.info("Waiting for Node %s configured at %s", guid, node.hostname)
265 node.wait_provisioning(
266 (20*60 if node._node_id in self._just_provisioned else 60)
269 self._logger.info("READY Node %s at %s", guid, node.hostname)
270 except self._node.UnresponsiveNodeError:
272 self._logger.warn("UNRESPONSIVE Node %s", node.hostname)
274 # Mark all dead nodes (which are unresponsive) on the blacklist
276 for guid, node in self._elements.iteritems():
277 if isinstance(node, self._node.Node):
278 if not node.is_alive():
279 self._logger.warn("Blacklisting %s for unresponsiveness", node.hostname)
280 self._blacklist.add(node._node_id)
284 self._save_blacklist()
288 traceback.print_exc()
292 def do_spanning_deployment_plan(self):
293 # Create application groups by collecting all applications
294 # based on their hash - the hash should contain everything that
295 # defines them and the platform they're built
299 frozenset((app.depends or "").split(' ')),
300 frozenset((app.sources or "").split(' ')),
303 app.node.architecture,
304 app.node.operatingSystem,
308 depgroups = collections.defaultdict(list)
310 for element in self._elements.itervalues():
311 if isinstance(element, self._app.Dependency):
312 depgroups[dephash(element)].append(element)
314 # Set up spanning deployment for those applications that
315 # have been deployed in several nodes.
316 for dh, group in depgroups.iteritems():
318 # Pick root (deterministically)
319 root = min(group, key=lambda app:app.node.hostname)
321 # Obtain all IPs in numeric format
322 # (which means faster distance computations)
324 dep._ip = socket.gethostbyname(dep.node.hostname)
325 dep._ip_n = struct.unpack('!L', socket.inet_aton(dep._ip))[0]
328 # NOTE: the plan is an iterator
331 lambda a,b : ipaddr2.ipdistn(a._ip_n, b._ip_n),
335 # Re-sign private key
337 tempprk, temppuk, tmppass = self._make_temp_private_key()
343 for slave, master in plan:
344 slave.set_master(master)
345 slave.install_keys(tempprk, temppuk, tmppass)
347 # We don't need the user's passphrase anymore
348 self.sliceSSHKeyPass = None
350 def _make_temp_private_key(self):
351 # Get the user's key's passphrase
352 if not self.sliceSSHKeyPass:
353 if 'SSH_ASKPASS' in os.environ:
354 proc = subprocess.Popen(
355 [ os.environ['SSH_ASKPASS'],
356 "Please type the passphrase for the %s SSH identity file. "
357 "The passphrase will be used to re-cipher the identity file with "
358 "a random 256-bit key for automated chain deployment on the "
359 "%s PlanetLab slice" % (
360 os.path.basename(self.sliceSSHKey),
363 stdin = open("/dev/null"),
364 stdout = subprocess.PIPE,
365 stderr = subprocess.PIPE)
366 out,err = proc.communicate()
367 self.sliceSSHKeyPass = out.strip()
369 if not self.sliceSSHKeyPass:
372 # Create temporary key files
373 prk = tempfile.NamedTemporaryFile(
374 dir = self.root_directory,
375 prefix = "pl_deploy_tmpk_",
378 puk = tempfile.NamedTemporaryFile(
379 dir = self.root_directory,
380 prefix = "pl_deploy_tmpk_",
383 # Create secure 256-bits temporary passphrase
384 passphrase = ''.join(map(chr,[rng.randint(0,255)
385 for rng in (random.SystemRandom(),)
386 for i in xrange(32)] )).encode("hex")
389 oprk = open(self.sliceSSHKey, "rb")
390 opuk = open(self.sliceSSHKey+".pub", "rb")
391 shutil.copymode(oprk.name, prk.name)
392 shutil.copymode(opuk.name, puk.name)
393 shutil.copyfileobj(oprk, prk)
394 shutil.copyfileobj(opuk, puk)
400 # A descriptive comment
401 comment = "%s#NEPI_INTERNAL@%s" % (self.authUser, self.slicename)
404 proc = subprocess.Popen(
407 "-P", self.sliceSSHKeyPass,
410 stdout = subprocess.PIPE,
411 stderr = subprocess.PIPE,
412 stdin = subprocess.PIPE
414 out, err = proc.communicate()
417 raise RuntimeError, "Problem generating keys: \n%s\n%r" % (
423 # Change comment on public key
424 puklines = puk.readlines()
425 puklines[0] = puklines[0].split(' ')
426 puklines[0][-1] = comment+'\n'
427 puklines[0] = ' '.join(puklines[0])
430 puk.writelines(puklines)
434 return prk, puk, passphrase
436 def set(self, guid, name, value, time = TIME_NOW):
437 super(TestbedController, self).set(guid, name, value, time)
438 # TODO: take on account schedule time for the task
439 element = self._elements[guid]
441 setattr(element, name, value)
443 if hasattr(element, 'refresh'):
444 # invoke attribute refresh hook
447 def get(self, guid, name, time = TIME_NOW):
448 value = super(TestbedController, self).get(guid, name, time)
449 # TODO: take on account schedule time for the task
450 factory_id = self._create[guid]
451 factory = self._factories[factory_id]
452 element = self._elements.get(guid)
454 return getattr(element, name)
455 except (KeyError, AttributeError):
458 def get_address(self, guid, index, attribute='Address'):
462 iface = self._elements.get(guid)
463 if iface and index == 0:
464 if attribute == 'Address':
466 elif attribute == 'NetPrefix':
467 return iface.netprefix
468 elif attribute == 'Broadcast':
469 return iface.broadcast
471 # if all else fails, query box
472 return super(TestbedController, self).get_address(guid, index, attribute)
474 def action(self, time, guid, action):
475 raise NotImplementedError
478 for trace in self._traces.itervalues():
481 runner = ParallelRun(16)
483 for element in self._elements.itervalues():
484 # invoke cleanup hooks
485 if hasattr(element, 'cleanup'):
486 runner.put(element.cleanup)
489 runner = ParallelRun(16)
491 for element in self._elements.itervalues():
492 # invoke destroy hooks
493 if hasattr(element, 'destroy'):
494 runner.put(element.destroy)
497 self._elements.clear()
500 def trace(self, guid, trace_id, attribute='value'):
501 app = self._elements[guid]
503 if attribute == 'value':
504 path = app.sync_trace(self.home_directory, trace_id)
511 elif attribute == 'path':
512 content = app.remote_trace_path(trace_id)
517 def follow_trace(self, trace_id, trace):
518 self._traces[trace_id] = trace
520 def _make_generic(self, parameters, kind):
521 app = kind(self.plapi)
523 # Note: there is 1-to-1 correspondence between attribute names
524 # If that changes, this has to change as well
525 for attr,val in parameters.iteritems():
526 setattr(app, attr, val)
530 def _make_node(self, parameters):
531 return self._make_generic(parameters, self._node.Node)
533 def _make_node_iface(self, parameters):
534 return self._make_generic(parameters, self._interfaces.NodeIface)
536 def _make_tun_iface(self, parameters):
537 return self._make_generic(parameters, self._interfaces.TunIface)
539 def _make_tap_iface(self, parameters):
540 return self._make_generic(parameters, self._interfaces.TapIface)
542 def _make_netpipe(self, parameters):
543 return self._make_generic(parameters, self._interfaces.NetPipe)
545 def _make_internet(self, parameters):
546 return self._make_generic(parameters, self._interfaces.Internet)
548 def _make_application(self, parameters):
549 return self._make_generic(parameters, self._app.Application)
551 def _make_dependency(self, parameters):
552 return self._make_generic(parameters, self._app.Dependency)
554 def _make_nepi_dependency(self, parameters):
555 return self._make_generic(parameters, self._app.NepiDependency)
557 def _make_ns3_dependency(self, parameters):
558 return self._make_generic(parameters, self._app.NS3Dependency)