2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
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.
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.
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/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19 # Lucia Guevgeozian <lucia.guevgeozian_odizzio@inria.fr>
21 from nepi.execution.attribute import Attribute, Flags, Types
22 from nepi.execution.resource import ResourceManager, clsinit_copy, ResourceState, \
24 from nepi.resources.linux.node import LinuxNode
25 from nepi.resources.planetlab.plcapi import PLCAPIFactory
26 from nepi.util.execfuncs import lexec
28 from random import randint
33 class PlanetlabNode(LinuxNode):
34 _rtype = "PlanetlabNode"
35 _help = "Controls a PlanetLab host accessible using a SSH key " \
36 "associated to a PlanetLab user account"
37 _backend = "planetlab"
40 provisionlist = list()
42 lock_blist = threading.Lock()
43 lock_plist = threading.Lock()
45 lock_slice = threading.Lock()
49 def _register_attributes(cls):
50 ip = Attribute("ip", "PlanetLab host public IP address",
51 flags = Flags.ReadOnly)
53 pl_url = Attribute("plcApiUrl", "URL of PlanetLab PLCAPI host \
54 (e.g. www.planet-lab.eu or www.planet-lab.org) ",
55 default = "www.planet-lab.eu",
56 flags = Flags.Credential)
58 pl_ptn = Attribute("plcApiPattern", "PLC API service regexp pattern \
59 (e.g. https://%(hostname)s:443/PLCAPI/ ) ",
60 default = "https://%(hostname)s:443/PLCAPI/",
61 flags = Flags.ExecReadOnly)
63 pl_user = Attribute("pluser", "PlanetLab account user, as the one to \
64 authenticate in the website) ",
65 flags = Flags.Credential)
67 pl_password = Attribute("password",
68 "PlanetLab account password, as \
69 the one to authenticate in the website) ",
70 flags = Flags.Credential)
72 city = Attribute("city", "Constrain location (city) during resource \
73 discovery. May use wildcards.",
76 country = Attribute("country", "Constrain location (country) during \
77 resource discovery. May use wildcards.",
80 region = Attribute("region", "Constrain location (region) during \
81 resource discovery. May use wildcards.",
84 architecture = Attribute("architecture", "Constrain architecture \
85 during resource discovery.",
86 type = Types.Enumerate,
91 operating_system = Attribute("operatingSystem", "Constrain operating \
92 system during resource discovery.",
93 type = Types.Enumerate,
101 site = Attribute("site", "Constrain the PlanetLab site this node \
103 type = Types.Enumerate,
107 flags = Flags.Filter)
109 min_reliability = Attribute("minReliability", "Constrain reliability \
110 while picking PlanetLab nodes. Specifies a lower \
114 flags = Flags.Filter)
116 max_reliability = Attribute("maxReliability", "Constrain reliability \
117 while picking PlanetLab nodes. Specifies an upper \
121 flags = Flags.Filter)
123 min_bandwidth = Attribute("minBandwidth", "Constrain available \
124 bandwidth while picking PlanetLab nodes. \
125 Specifies a lower acceptable bound.",
128 flags = Flags.Filter)
130 max_bandwidth = Attribute("maxBandwidth", "Constrain available \
131 bandwidth while picking PlanetLab nodes. \
132 Specifies an upper acceptable bound.",
135 flags = Flags.Filter)
137 min_load = Attribute("minLoad", "Constrain node load average while \
138 picking PlanetLab nodes. Specifies a lower acceptable \
142 flags = Flags.Filter)
144 max_load = Attribute("maxLoad", "Constrain node load average while \
145 picking PlanetLab nodes. Specifies an upper acceptable \
149 flags = Flags.Filter)
151 min_cpu = Attribute("minCpu", "Constrain available cpu time while \
152 picking PlanetLab nodes. Specifies a lower acceptable \
156 flags = Flags.Filter)
158 max_cpu = Attribute("maxCpu", "Constrain available cpu time while \
159 picking PlanetLab nodes. Specifies an upper acceptable \
163 flags = Flags.Filter)
165 timeframe = Attribute("timeframe", "Past time period in which to check\
166 information about the node. Values are year,month, \
169 type = Types.Enumerate,
174 flags = Flags.Filter)
176 cls._register_attribute(ip)
177 cls._register_attribute(pl_url)
178 cls._register_attribute(pl_ptn)
179 cls._register_attribute(pl_user)
180 cls._register_attribute(pl_password)
181 cls._register_attribute(site)
182 cls._register_attribute(city)
183 cls._register_attribute(country)
184 cls._register_attribute(region)
185 cls._register_attribute(architecture)
186 cls._register_attribute(operating_system)
187 cls._register_attribute(min_reliability)
188 cls._register_attribute(max_reliability)
189 cls._register_attribute(min_bandwidth)
190 cls._register_attribute(max_bandwidth)
191 cls._register_attribute(min_load)
192 cls._register_attribute(max_load)
193 cls._register_attribute(min_cpu)
194 cls._register_attribute(max_cpu)
195 cls._register_attribute(timeframe)
198 def __init__(self, ec, guid):
199 super(PlanetlabNode, self).__init__(ec, guid)
202 self._node_to_provision = None
207 pl_user = self.get("pluser")
208 pl_pass = self.get("password")
209 pl_url = self.get("plcApiUrl")
210 pl_ptn = self.get("plcApiPattern")
212 self._plapi = PLCAPIFactory.get_api(pl_user, pl_pass, pl_url,
219 Based on the attributes defined by the user, discover the suitable nodes
221 hostname = self.get("hostname")
223 # the user specified one particular node to be provisioned
224 # check with PLCAPI if it is alvive
225 node_id = self._query_if_alive(hostname=hostname)
226 node_id = node_id.pop()
228 # check that the node is not blacklisted or already being provision
230 blist = PlanetlabNode.blacklist
231 plist = PlanetlabNode.provisionlist
232 if node_id not in blist and node_id not in plist:
234 # check that is really alive, by performing ping
235 ping_ok = self._do_ping(node_id)
237 self._blacklist_node(node_id)
238 self.fail_node_not_alive(hostname)
240 self._node_to_provision = node_id
241 self._put_node_in_provision(node_id)
242 super(PlanetlabNode, self).discover()
245 self.fail_node_not_available(hostname)
248 # the user specifies constraints based on attributes, zero, one or
249 # more nodes can match these constraints
250 nodes = self._filter_based_on_attributes()
251 nodes_alive = self._query_if_alive(nodes)
253 # nodes that are already part of user's slice have the priority to
255 nodes_inslice = self._check_if_in_slice(nodes_alive)
256 nodes_not_inslice = list(set(nodes_alive) - set(nodes_inslice))
260 node_id = self._choose_random_node(nodes_inslice)
262 if not node_id and nodes_not_inslice:
263 # Either there were no matching nodes in the user's slice, or
264 # the nodes in the slice were blacklisted or being provisioned
265 # by other RM. Note nodes_not_inslice is never empty
266 node_id = self._choose_random_node(nodes_not_inslice)
268 self.fail_not_enough_nodes()
270 self._node_to_provision = node_id
271 super(PlanetlabNode, self).discover()
273 def provisionl(self):
275 Add node to user's slice after verifing that the node is functioning
283 while not provision_ok:
284 node = self._node_to_provision
285 self._set_hostname_attr(node)
286 self._add_node_to_slice(node)
288 # check ssh connection
290 while t < timeout and not ssh_ok:
292 cmd = 'echo \'GOOD NODE\''
293 ((out, err), proc) = self.execute(cmd)
294 if out.find("GOOD NODE") < 0:
303 # the timeout was reach without establishing ssh connection
304 # the node is blacklisted, deleted from the slice, and a new
305 # node to provision is discovered
306 self._blacklist_node(node)
307 self._delete_node_from_slice(node)
311 # check /proc directory is mounted (ssh_ok = True)
313 cmd = 'mount |grep proc'
314 ((out, err), proc) = self.execute(cmd)
315 if out.find("/proc type proc") < 0:
316 self._blacklist_node(node)
317 self._delete_node_from_slice(node)
324 ip = self._get_ip(node)
327 super(PlanetlabNode, self).provision()
329 def _filter_based_on_attributes(self):
331 Retrive the list of nodes ids that match user's constraints
333 # Map user's defined attributes with tagnames of PlanetLab
334 timeframe = self.get("timeframe")[0]
337 'country' : 'country',
339 'architecture' : 'arch',
340 'operatingSystem' : 'fcdistro',
341 #'site' : 'pldistro',
342 'minReliability' : 'reliability%s' % timeframe,
343 'maxReliability' : 'reliability%s' % timeframe,
344 'minBandwidth' : 'bw%s' % timeframe,
345 'maxBandwidth' : 'bw%s' % timeframe,
346 'minLoad' : 'load%s' % timeframe,
347 'maxLoad' : 'load%s' % timeframe,
348 'minCpu' : 'cpu%s' % timeframe,
349 'maxCpu' : 'cpu%s' % timeframe,
355 for attr_name, attr_obj in self._attrs.iteritems():
356 attr_value = self.get(attr_name)
358 if attr_value is not None and attr_obj.flags == 8 and \
359 attr_name != 'timeframe':
361 attr_tag = attr_to_tags[attr_name]
362 filters['tagname'] = attr_tag
364 # filter nodes by fixed constraints e.g. operating system
365 if not 'min' in attr_name and not 'max' in attr_name:
366 filters['value'] = attr_value
367 nodes_id = self._filter_by_fixed_attr(filters, nodes_id)
369 # filter nodes by range constraints e.g. max bandwidth
370 elif ('min' or 'max') in attr_name:
371 nodes_id = self._filter_by_range_attr(attr_name, attr_value, filters, nodes_id)
376 def _filter_by_fixed_attr(self, filters, nodes_id):
378 Query PLCAPI for nodes ids matching fixed attributes defined by the
381 node_tags = self.plapi.get_node_tags(filters)
382 if node_tags is not None:
384 if len(nodes_id) == 0:
385 # first attribute being matched
386 for node_tag in node_tags:
387 nodes_id.append(node_tag['node_id'])
389 # remove the nodes ids that don't match the new attribute
390 # that is being match
393 for node_tag in node_tags:
394 if node_tag['node_id'] in nodes_id:
395 nodes_id_tmp.append(node_tag['node_id'])
397 if len(nodes_id_tmp):
398 nodes_id = set(nodes_id) & set(nodes_id_tmp)
400 # no node from before match the new constraint
401 self.fail_discovery()
403 # no nodes match the filter applied
404 self.fail_discovery()
408 def _filter_by_range_attr(self, attr_name, attr_value, filters, nodes_id):
410 Query PLCAPI for nodes ids matching attributes defined in a certain
413 node_tags = self.plapi.get_node_tags(filters)
414 if node_tags is not None:
416 if len(nodes_id) == 0:
417 # first attribute being matched
418 for node_tag in node_tags:
420 # check that matches the min or max restriction
421 if 'min' in attr_name and node_tag['value'] != 'n/a' and \
422 float(node_tag['value']) > attr_value:
423 nodes_id.append(node_tag['node_id'])
425 elif 'max' in attr_name and node_tag['value'] != 'n/a' and \
426 float(node_tag['value']) < attr_value:
427 nodes_id.append(node_tag['node_id'])
430 # remove the nodes ids that don't match the new attribute
431 # that is being match
433 for node_tag in node_tags:
435 # check that matches the min or max restriction and was a
436 # matching previous filters
437 if 'min' in attr_name and node_tag['value'] != 'n/a' and \
438 float(node_tag['value']) > attr_value and \
439 node_tag['node_id'] in nodes_id:
440 nodes_id_tmp.append(node_tag['node_id'])
442 elif 'max' in attr_name and node_tag['value'] != 'n/a' and \
443 float(node_tag['value']) < attr_value and \
444 node_tag['node_id'] in nodes_id:
445 nodes_id_tmp.append(node_tag['node_id'])
447 if len(nodes_id_tmp):
448 nodes_id = set(nodes_id) & set(nodes_id_tmp)
450 # no node from before match the new constraint
451 self.fail_discovery()
454 # no nodes match the filter applied
455 self.fail_discovery()
459 def _query_if_alive(self, nodes_id=None, hostname=None):
461 Query PLCAPI for nodes that register activity recently, using filters
462 related to the state of the node, e.g. last time it was contacted
464 if nodes_id is None and hostname is None:
465 msg = "Specify nodes_id or hostname"
466 raise RuntimeError, msg
468 if nodes_id is not None and hostname is not None:
469 msg = "Specify either nodes_id or hostname"
470 raise RuntimeError, msg
472 # define PL filters to check the node is alive
474 filters['run_level'] = 'boot'
475 filters['boot_state'] = 'boot'
476 filters['node_type'] = 'regular'
477 filters['>last_contact'] = int(time.time()) - 2*3600
479 # adding node_id or hostname to the filters to check for the particular
482 filters['node_id'] = list(nodes_id)
483 alive_nodes_id = self._get_nodes_id(filters)
485 filters['hostname'] = hostname
486 alive_nodes_id = self._get_nodes_id(filters)
488 if len(alive_nodes_id) == 0:
489 self.fail_discovery()
492 for node_id in alive_nodes_id:
493 nid = node_id['node_id']
498 def _choose_random_node(self, nodes):
500 From the possible nodes for provision, choose randomly to decrese the
501 probability of different RMs choosing the same node for provision
503 blist = PlanetlabNode.blacklist
504 plist = PlanetlabNode.provisionlist
509 index = randint(0, size)
510 node_id = nodes[index]
511 nodes[index] = nodes[size]
513 # check the node is not blacklisted or being provision by other RM
514 # and perform ping to check that is really alive
515 if node_id not in blist and node_id not in plist:
516 ping_ok = self._do_ping(node_id)
518 self._blacklist_node(node_id)
520 # discovered node for provision, added to provision list
521 self._put_node_in_provision(node_id)
524 def _get_nodes_id(self, filters):
525 return self.plapi.get_nodes(filters, fields=['node_id'])
527 def _add_node_to_slice(self, node_id):
528 self.warn(" Adding node to slice ")
529 slicename = self.get("username")
530 with PlanetlabNode.lock_slice:
531 slice_nodes = self.plapi.get_slice_nodes(slicename)
532 slice_nodes.append(node_id)
533 self.plapi.add_slice_nodes(slicename, slice_nodes)
535 def _delete_node_from_slice(self, node):
536 self.warn(" Deleting node from slice ")
537 slicename = self.get("username")
538 self.plapi.delete_slice_node(slicename, [node])
540 def _set_hostname_attr(self, node):
542 Query PLCAPI for the hostname of a certain node id and sets the
543 attribute hostname, it will over write the previous value
545 hostname = self.plapi.get_nodes(node, ['hostname'])
546 self.set("hostname", hostname[0]['hostname'])
548 def _check_if_in_slice(self, nodes_id):
550 Query PLCAPI to find out if any node id from nodes_id is in the user's
553 slicename = self.get("username")
554 slice_nodes = self.plapi.get_slice_nodes(slicename)
555 nodes_inslice = list(set(nodes_id) & set(slice_nodes))
559 def _do_ping(self, node_id):
561 Perform ping command on node's IP matching node id
564 ip = self._get_ip(node_id)
565 command = "ping -c2 %s | echo \"PING OK\"" % ip
567 (out, err) = lexec(command)
568 if not out.find("PING OK") < 0:
573 def _blacklist_node(self, node):
575 Add node mal functioning node to blacklist
577 blist = PlanetlabNode.blacklist
579 self.warn(" Blacklisting malfunctioning node ")
580 with PlanetlabNode.lock_blist:
583 def _put_node_in_provision(self, node):
585 Add node to the list of nodes being provisioned, in order for other RMs
586 to not try to provision the same one again
588 plist = PlanetlabNode.provisionlist
590 self.warn(" Provisioning node ")
591 with PlanetlabNode.lock_plist:
594 def _get_ip(self, node_id):
596 Query PLCAPI for the IP of a node with certain node id
598 ip = self.plapi.get_interfaces({'node_id':node_id}, fields=['ip'])
602 def fail_discovery(self):
604 msg = "Discovery failed. No candidates found for node"
606 raise RuntimeError, msg
608 def fail_node_not_alive(self, hostname):
609 msg = "Node %s not alive, pick another node" % hostname
610 raise RuntimeError, msg
612 def fail_node_not_available(self, hostname):
613 msg = "Node %s not available for provisioning, pick another \
615 raise RuntimeError, msg
617 def fail_not_enough_nodes(self):
618 msg = "Not enough nodes available for provisioning"
619 raise RuntimeError, msg
621 def valid_connection(self, guid):