2 # -*- coding: utf-8 -*-
4 from constants import TESTBED_ID
5 from nepi.core import metadata
6 from nepi.core.attributes import Attribute
7 from nepi.util import validation
8 from nepi.util.constants import STATUS_NOT_STARTED, STATUS_RUNNING, \
12 NODEIFACE = "NodeInterface"
13 TUNIFACE = "TunInterface"
14 APPLICATION = "Application"
17 PL_TESTBED_ID = "planetlab"
19 ### Connection functions ####
21 def connect_node_iface_node(testbed_instance, node, iface):
24 def connect_node_iface_inet(testbed_instance, iface, inet):
25 iface.has_internet = True
27 def connect_tun_iface_node(testbed_instance, node, iface):
30 def connect_app(testbed_instance, node, app):
34 node.required_packages.update(set(
35 app.depends.split() ))
37 ### Creation functions ###
39 def create_node(testbed_instance, guid):
40 parameters = testbed_instance._get_parameters(guid)
42 # create element with basic attributes
43 element = testbed_instance._make_node(parameters)
45 # add constraint on number of (real) interfaces
46 # by counting connected devices
47 dev_guids = testbed_instance.get_connected(guid, "node", "devs")
48 num_open_ifaces = sum( # count True values
49 TUNEIFACE == testbed_instance._get_factory_id(guid)
50 for guid in dev_guids )
51 element.min_num_external_ifaces = num_open_ifaces
53 testbed_instance.elements[guid] = element
55 def create_nodeiface(testbed_instance, guid):
56 parameters = testbed_instance._get_parameters(guid)
57 element = testbed_instance._make_node_iface(parameters)
58 testbed_instance.elements[guid] = element
60 def create_tuniface(testbed_instance, guid):
61 parameters = testbed_instance._get_parameters(guid)
62 element = testbed_instance._make_tun_iface(parameters)
63 testbed_instance.elements[guid] = element
65 def create_application(testbed_instance, guid):
66 parameters = testbed_instance._get_parameters(guid)
67 element = testbed_instance._make_application(parameters)
68 testbed_instance.elements[guid] = element
70 def create_internet(testbed_instance, guid):
71 parameters = testbed_instance._get_parameters(guid)
72 element = testbed_instance._make_internet(parameters)
73 testbed_instance.elements[guid] = element
75 ### Start/Stop functions ###
77 def start_application(testbed_instance, guid):
78 parameters = testbed_instance._get_parameters(guid)
79 traces = testbed_instance._get_traces(guid)
80 app = testbed_instance.elements[guid]
82 app.stdout = "stdout" in traces
83 app.stderr = "stderr" in traces
84 app.buildlog = "buildlog" in traces
88 def stop_application(testbed_instance, guid):
89 app = testbed_instance.elements[guid]
92 ### Status functions ###
94 def status_application(testbed_instance, guid):
95 if guid not in testbed_instance.elements.keys():
96 return STATUS_NOT_STARTED
98 app = testbed_instance.elements[guid]
101 ### Configure functions ###
103 def configure_nodeiface(testbed_instance, guid):
104 element = testbed_instance._elements[guid]
106 # Cannot explicitly configure addresses
107 if guid in testbed_instance._add_address:
108 del testbed_instance._add_address[guid]
111 node_guid = testbed_instance.get_connected(guid, "node", "devs")[0]
112 dev_guids = testbed_instance.get_connected(node_guid, "node", "devs")
113 siblings = [ self._element[dev_guid]
114 for dev_guid in dev_guids
115 if dev_guid != guid ]
117 # Fetch address from PLC api
118 element.pick_iface(siblings)
120 # Do some validations
123 def configure_tuniface(testbed_instance, guid):
124 element = testbed_instance._elements[guid]
125 if not guid in testbed_instance._add_address:
128 addresses = testbed_instance._add_address[guid]
129 for address in addresses:
130 (address, netprefix, broadcast) = address
131 raise NotImplementedError, "C'mon... TUNs are hard..."
133 # Do some validations
136 def configure_node(testbed_instance, guid):
137 node = testbed_instance._elements[guid]
139 # If we have only one candidate, simply use it
140 candidates = node.find_candidates(
141 filter_slice_id = testbed_instance.slice_id)
142 if len(candidates) == 1:
143 node.assign_node_id(iter(candidates).next())
145 # Do some validations
148 # TODO: this should be done in parallel in all nodes
149 node.install_dependencies()
151 def configure_application(testbed_instance, guid):
152 app = testbed_instance._elements[guid]
154 # Just inject configuration stuff
155 app.home_path = "nepi-app-%s" % (guid,)
156 app.ident_path = testbed_instance.sliceSSHKey
157 app.slicename = testbed_instance.slicename
159 # Do some validations
162 # Wait for dependencies
163 app.node.wait_dependencies()
168 ### Factory information ###
170 connector_types = dict({
172 "help": "Connector from node to applications",
178 "help": "Connector from node to network interfaces",
184 "help": "Connector from network interfaces to the internet",
190 "help": "Connector to a Node",
199 "from": (TESTBED_ID, NODE, "devs"),
200 "to": (TESTBED_ID, NODEIFACE, "node"),
201 "code": connect_node_iface_node,
205 "from": (TESTBED_ID, NODE, "devs"),
206 "to": (TESTBED_ID, TUNIFACE, "node"),
207 "code": connect_tun_iface_node,
211 "from": (TESTBED_ID, NODEIFACE, "inet"),
212 "to": (TESTBED_ID, INTERNET, "devs"),
213 "code": connect_node_iface_inet,
217 "from": (TESTBED_ID, NODE, "apps"),
218 "to": (TESTBED_ID, APPLICATION, "node"),
225 "forward_X11": dict({
226 "name": "forward_X11",
227 "help": "Forward x11 from main namespace to the node",
228 "type": Attribute.BOOL,
230 "flags": Attribute.DesignOnly,
231 "validation_function": validation.is_bool,
235 "help": "Constrain hostname during resource discovery. May use wildcards.",
236 "type": Attribute.STRING,
237 "flags": Attribute.DesignOnly,
238 "validation_function": validation.is_string,
240 "architecture": dict({
241 "name": "architecture",
242 "help": "Constrain architexture during resource discovery.",
243 "type": Attribute.ENUM,
244 "flags": Attribute.DesignOnly,
245 "allowed": ["x86_64",
247 "validation_function": validation.is_enum,
249 "operating_system": dict({
250 "name": "operatingSystem",
251 "help": "Constrain operating system during resource discovery.",
252 "type": Attribute.ENUM,
253 "flags": Attribute.DesignOnly,
259 "validation_function": validation.is_enum,
263 "help": "Constrain the PlanetLab site this node should reside on.",
264 "type": Attribute.ENUM,
265 "flags": Attribute.DesignOnly,
269 "validation_function": validation.is_enum,
273 "help": "Enable emulation on this node. Enables NetfilterRoutes, bridges, and a host of other functionality.",
274 "type": Attribute.BOOL,
276 "flags": Attribute.DesignOnly,
277 "validation_function": validation.is_bool,
279 "min_reliability": dict({
280 "name": "minReliability",
281 "help": "Constrain reliability while picking PlanetLab nodes. Specifies a lower acceptable bound.",
282 "type": Attribute.DOUBLE,
284 "flags": Attribute.DesignOnly,
285 "validation_function": validation.is_double,
287 "max_reliability": dict({
288 "name": "maxReliability",
289 "help": "Constrain reliability while picking PlanetLab nodes. Specifies an upper acceptable bound.",
290 "type": Attribute.DOUBLE,
292 "flags": Attribute.DesignOnly,
293 "validation_function": validation.is_double,
295 "min_bandwidth": dict({
296 "name": "minBandwidth",
297 "help": "Constrain available bandwidth while picking PlanetLab nodes. Specifies a lower acceptable bound.",
298 "type": Attribute.DOUBLE,
300 "flags": Attribute.DesignOnly,
301 "validation_function": validation.is_double,
303 "max_bandwidth": dict({
304 "name": "maxBandwidth",
305 "help": "Constrain available bandwidth while picking PlanetLab nodes. Specifies an upper acceptable bound.",
306 "type": Attribute.DOUBLE,
308 "flags": Attribute.DesignOnly,
309 "validation_function": validation.is_double,
315 "type": Attribute.BOOL,
317 "validation_function": validation.is_bool
321 "help": "This is the primary interface for the attached node",
322 "type": Attribute.BOOL,
324 "validation_function": validation.is_bool
326 "device_name": dict({
328 "help": "Device name",
329 "type": Attribute.STRING,
330 "flags": Attribute.DesignOnly,
331 "validation_function": validation.is_string
335 "help": "Maximum transmition unit for device",
336 "type": Attribute.INTEGER,
338 "validation_function": validation.is_integer_range(0,1500)
342 "help": "Network mask for the device (eg: 24 for /24 network)",
343 "type": Attribute.INTEGER,
344 "validation_function": validation.is_integer_range(8,24)
348 "help": "Enable SNAT (source NAT to the internet) no this device",
349 "type": Attribute.BOOL,
351 "validation_function": validation.is_bool
356 "help": "Command line string",
357 "type": Attribute.STRING,
358 "flags": Attribute.DesignOnly,
359 "validation_function": validation.is_string
363 "help": "System user",
364 "type": Attribute.BOOL,
365 "flags": Attribute.DesignOnly,
367 "validation_function": validation.is_bool
371 "help": "Standard input",
372 "type": Attribute.STRING,
373 "flags": Attribute.DesignOnly,
374 "validation_function": validation.is_string
379 "help": "Space-separated list of packages required to run the application",
380 "type": Attribute.STRING,
381 "flags": Attribute.DesignOnly,
382 "validation_function": validation.is_string
384 "build-depends": dict({
385 "name": "buildDepends",
386 "help": "Space-separated list of packages required to build the application",
387 "type": Attribute.STRING,
388 "flags": Attribute.DesignOnly,
389 "validation_function": validation.is_string
393 "help": "Space-separated list of regular files to be deployed in the working path prior to building. "
394 "Archives won't be expanded automatically.",
395 "type": Attribute.STRING,
396 "flags": Attribute.DesignOnly,
397 "validation_function": validation.is_string
401 "help": "Build commands to execute after deploying the sources. "
402 "Sources will be in the initial working folder. "
403 "Example: cd my-app && ./configure && make && make install.\n"
404 "Try to make the commands return with a nonzero exit code on error.",
405 "type": Attribute.STRING,
406 "flags": Attribute.DesignOnly,
407 "validation_function": validation.is_string
414 "help": "Standard output stream"
418 "help": "Application standard error",
422 "help": "Output of the build process",
426 create_order = [ INTERNET, NODE, NODEIFACE, TUNIFACE, APPLICATION ]
428 configure_order = [ INTERNET, NODE, NODEIFACE, TUNIFACE, APPLICATION ]
430 factories_info = dict({
432 "allow_routes": False,
433 "help": "Virtualized Node (V-Server style)",
434 "category": "topology",
435 "create_function": create_node,
436 "preconfigure_function": configure_node,
449 "connector_types": ["devs", "apps"]
452 "allow_addresses": True,
453 "help": "External network interface - they cannot be brought up or down, and they MUST be connected to the internet.",
454 "category": "devices",
455 "create_function": create_nodeiface,
456 "preconfigure_function": configure_nodeiface,
457 "box_attributes": [ ],
458 "connector_types": ["node", "inet"]
461 "allow_addresses": True,
462 "help": "Virtual TUN network interface",
463 "category": "devices",
464 "create_function": create_tuniface,
465 "preconfigure_function": configure_tuniface,
467 "up", "device_name", "mtu", "snat",
469 "connector_types": ["node"]
472 "help": "Generic executable command line application",
473 "category": "applications",
474 "create_function": create_application,
475 "start_function": start_application,
476 "status_function": status_application,
477 "stop_function": stop_application,
478 "configure_function": configure_application,
479 "box_attributes": ["command", "sudo", "stdin"],
480 "connector_types": ["node"],
481 "traces": ["stdout", "stderr"]
484 "help": "Internet routing",
485 "category": "topology",
486 "create_function": create_internet,
487 "connector_types": ["devs"],
491 testbed_attributes = dict({
494 "help": "The name of the PlanetLab slice to use",
495 "type": Attribute.STRING,
496 "flags": Attribute.DesignOnly | Attribute.HasNoDefaultValue,
497 "validation_function": validation.is_string
501 "help": "The name of the PlanetLab user to use for API calls - it must have at least a User role.",
502 "type": Attribute.STRING,
503 "flags": Attribute.DesignOnly | Attribute.HasNoDefaultValue,
504 "validation_function": validation.is_string
508 "help": "The PlanetLab user's password.",
509 "type": Attribute.STRING,
510 "flags": Attribute.DesignOnly | Attribute.HasNoDefaultValue,
511 "validation_function": validation.is_string
513 "slice_ssh_key": dict({
514 "name": "sliceSSHKey",
515 "help": "The controller-local path to the slice user's ssh private key. "
516 "It is the user's responsability to deploy this file where the controller "
517 "will run, it won't be done automatically because it's sensitive information. "
518 "It is recommended that a NEPI-specific user be created for this purpose and "
519 "this purpose alone.",
520 "type": Attribute.STRING,
521 "flags": Attribute.DesignOnly | Attribute.HasNoDefaultValue,
522 "validation_function": validation.is_string
526 class VersionedMetadataInfo(metadata.VersionedMetadataInfo):
528 def connector_types(self):
529 return connector_types
532 def connections(self):
536 def attributes(self):
544 def create_order(self):
548 def configure_order(self):
549 return configure_order
552 def factories_info(self):
553 return factories_info
556 def testbed_attributes(self):
557 return testbed_attributes