4 # Helper functions that minipulate the PLC api.
6 # Faiyaz Ahmed <faiyaza@cs.princeton.edu
8 # $Id: plc.py,v 1.18 2007/08/29 17:26:50 soltesz Exp $
15 from datetime import datetime
17 # note: this needs to be consistent with the value in PLEWWW/planetlab/includes/plc_functions.php
18 PENDING_CONSORTIUM_ID = 0
20 #APPROVED_CONSORTIUM_ID = 999999
23 from monitor import config
25 XMLRPC_SERVER=config.API_SERVER
28 # NOTE: this host is used by default when there are no auth files.
29 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
31 logger = logging.getLogger("monitor")
34 def __init__(self, username=None, password=None, **kwargs):
35 if 'session' in kwargs:
36 self.auth= { 'AuthMethod' : 'session',
37 'session' : kwargs['session'] }
39 if username==None and password==None:
40 self.auth = {'AuthMethod': "anonymous"}
42 self.auth = {'Username' : username,
43 'AuthMethod' : 'password',
44 'AuthString' : password}
47 # NOTE: by default, use anonymous access, but if auth files are
48 # configured, use them, with their auth definitions.
51 from monitor import config
52 auth.auth = {'Username' : config.API_AUTH_USER,
53 'AuthMethod' : 'password',
54 'AuthString' : config.API_AUTH_PASSWORD}
55 auth.server = config.API_SERVER
59 auth.server = auth.plc
62 auth.server = XMLRPC_SERVER
64 global_error_count = 0
67 def __init__(self, auth, url):
70 self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
72 def __getattr__(self, name):
73 method = getattr(self.api, name)
75 raise AssertionError("method does not exist")
78 return lambda *params : method(self.auth, *params)
79 except xmlrpclib.ProtocolError:
81 global_error_count += 1
82 if global_error_count >= 10:
83 print "maximum error count exceeded; exiting..."
86 print "%s errors have occurred" % global_error_count
87 raise Exception("ProtocolError continuing")
90 return self.api.__repr__()
93 api = PLC(auth.auth, auth.server)
97 return xmlrpclib.Server(url, verbose=False, allow_none=True)
99 def getNodeAPI(session):
100 nodeauth = Auth(session=session)
101 return PLC(nodeauth.auth, auth.server)
103 def getAuthAPI(url=None):
105 return PLC(auth.auth, url)
107 return PLC(auth.auth, auth.server)
109 def getCachedAuthAPI():
110 return CachedPLC(auth.auth, auth.server)
112 def getSessionAPI(session, server):
113 nodeauth = Auth(session=session)
114 return PLC(nodeauth.auth, server)
115 def getUserAPI(username, password, server):
116 auth = Auth(username,password)
117 return PLC(auth.auth, server)
119 def getTechEmails(loginbase):
121 For the given site, return all user email addresses that have the 'tech' role.
125 s = api.GetSites(loginbase)[0]
127 p = api.GetPersons(s['person_ids'])
128 # pull out those with the right role.
130 for person in filter(lambda x: 'tech' in x['roles'], p):
131 if not isPersonExempt(person['email']):
132 emails.append(person['email'])
133 #emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
136 def getPIEmails(loginbase):
138 For the given site, return all user email addresses that have the 'tech' role.
142 s = api.GetSites(loginbase)[0]
144 p = api.GetPersons(s['person_ids'])
145 # pull out those with the right role.
146 #emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
148 for person in filter(lambda x: 'pi' in x['roles'], p):
149 if not isPersonExempt(person['email']):
150 emails.append(person['email'])
153 def getSliceUserEmails(loginbase):
155 For the given site, return all user email addresses that have the 'tech' role.
159 s = api.GetSites(loginbase)[0]
161 slices = api.GetSlices(s['slice_ids'])
164 people += api.GetPersons(slice['person_ids'])
165 # pull out those with the right role.
166 #emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
169 for person in people:
170 if not isPersonExempt(person['email']):
171 emails.append(person['email'])
173 unique_emails = [ x for x in set(emails) ]
177 Returns list of nodes in dbg as reported by PLC
181 api = xmlrpclib.Server(auth.server, verbose=False)
182 anon = {'AuthMethod': "anonymous"}
183 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
184 dbgNodes.append(node['hostname'])
185 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
190 Returns loginbase for given nodename
192 def siteId(nodename):
193 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
194 site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
195 if len(site_id) == 1:
196 loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
197 return loginbase[0]['login_base']
199 print "Not nodes returned!!!!"
202 Returns list of slices for a site.
204 def slices(loginbase):
206 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
207 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
208 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
209 siteslices.append(slice['name'])
213 Returns dict of PCU info of a given node.
215 def getpcu(nodename):
216 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
217 anon = {'AuthMethod': "anonymous"}
219 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
221 logger.info("Can not find node: %s" % nodename)
223 if nodeinfo['pcu_ids']:
225 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
227 print nodeinfo["ports"]
228 sitepcu[nodename] = nodeinfo["ports"][0]
231 logger.info("%s doesn't have PCU" % nodename)
234 def GetPCUs(filter=None, fields=None):
235 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
236 pcu_list = api.GetPCUs(auth.auth, filter, fields)
240 Returns all site nodes for site id (loginbase).
242 def getSiteNodes(loginbase, fields=None):
243 api = xmlrpclib.Server(auth.server, verbose=False)
245 anon = {'AuthMethod': "anonymous"}
247 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
248 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
249 nodelist.append(node['hostname'])
250 except Exception, exc:
251 logger.info("getSiteNodes: %s" % exc)
252 print "getSiteNodes: %s" % exc
256 def getPersons(filter=None, fields=None):
257 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
260 persons = api.GetPersons(auth.auth, filter, fields)
261 except Exception, exc:
262 print "getPersons: %s" % exc
263 logger.info("getPersons: %s" % exc)
266 def getSites(filter=None, fields=None):
267 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
269 anon = {'AuthMethod': "anonymous"}
271 #sites = api.GetSites(anon, filter, fields)
272 sites = api.GetSites(auth.auth, filter, fields)
273 except Exception, exc:
274 traceback.print_exc()
275 print "getSites: %s" % exc
276 logger.info("getSites: %s" % exc)
279 def getSiteNodes2(loginbase):
280 api = xmlrpclib.Server(auth.server, verbose=False)
282 anon = {'AuthMethod': "anonymous"}
284 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
285 nodelist += getNodes({'node_id':nodeids})
286 except Exception, exc:
287 logger.info("getSiteNodes2: %s" % exc)
290 def getNodeNetworks(filter=None):
291 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
292 nodenetworks = api.GetInterfaces(auth.auth, filter, None)
295 def getNodes(filter=None, fields=None):
296 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
297 nodes = api.GetNodes(auth.auth, filter, fields)
298 #['boot_state', 'hostname',
299 #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
300 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
304 # Check if the site is a pending site that needs to be approved.
305 def isPendingSite(loginbase):
306 api = xmlrpclib.Server(auth.server, verbose=False)
308 site = api.GetSites(auth.auth, loginbase)[0]
309 except Exception, exc:
310 logger.info("ERROR: No site %s" % loginbase)
313 if not site['enabled'] and site['ext_consortium_id'] == PENDING_CONSORTIUM_ID:
320 Sets boot state of a node.
322 def nodeBootState(nodename, state):
323 api = xmlrpclib.Server(auth.server, verbose=False)
325 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
326 except Exception, exc:
327 logger.info("nodeBootState: %s" % exc)
329 def updateNodeKey(nodename, key):
330 api = xmlrpclib.Server(auth.server, verbose=False)
332 return api.UpdateNode(auth.auth, nodename, {'key': key})
333 except Exception, exc:
334 logger.info("updateNodeKey: %s" % exc)
337 Sends Ping Of Death to node.
339 def nodePOD(nodename):
340 api = xmlrpclib.Server(auth.server, verbose=False)
341 logger.info("Sending POD to %s" % nodename)
344 return api.RebootNode(auth.auth, nodename)
345 except Exception, exc:
346 logger.info("nodePOD: %s" % exc)
349 Freeze all site slices.
351 def suspendSiteSlices(loginbase):
352 if isPendingSite(loginbase):
353 msg = "INFO: suspendSiteSlices: Pending Site (%s)" % loginbase
358 api = xmlrpclib.Server(auth.server, verbose=False)
359 for slice in slices(loginbase):
360 logger.info("Suspending slice %s" % slice)
363 if not isSliceExempt(slice):
364 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
365 except Exception, exc:
366 logger.info("suspendSlices: %s" % exc)
369 Freeze all site slices.
371 def suspendSlices(nodename):
372 loginbase = siteId(nodename)
373 suspendSiteSlices(loginbase)
376 def enableSiteSlices(loginbase):
377 if isPendingSite(loginbase):
378 msg = "INFO: enableSiteSlices: Pending Site (%s)" % loginbase
383 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
384 for slice in slices(loginbase):
385 logger.info("Enabling slices %s" % slice)
388 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
389 if len(slice_list) == 0:
391 slice_id = slice_list[0]['slice_id']
392 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
394 if "enabled" == attr['name'] and attr['value'] == "0":
395 logger.info("Deleted enable=0 attribute from slice %s" % slice)
396 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
397 except Exception, exc:
398 logger.info("enableSiteSlices: %s" % exc)
399 print "exception: %s" % exc
401 def enableSlices(nodename):
402 loginbase = siteId(nodename)
403 enableSiteSlices(loginbase)
406 #I'm commenting this because this really should be a manual process.
408 #Enable suspended site slices.
410 #def enableSlices(nodename, slicelist):
411 # api = xmlrpclib.Server(auth.server, verbose=False)
412 # for slice in slices(siteId(nodename)):
413 # logger.info("Suspending slice %s" % slice)
414 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
416 def enableSiteSliceCreation(loginbase):
417 if isPendingSite(loginbase):
418 msg = "INFO: enableSiteSliceCreation: Pending Site (%s)" % loginbase
423 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
425 logger.info("Enabling slice creation for site %s" % loginbase)
427 site = api.GetSites(auth.auth, loginbase)[0]
428 if site['enabled'] == False:
429 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
430 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
431 except Exception, exc:
432 print "ERROR: enableSiteSliceCreation: %s" % exc
433 logger.info("ERROR: enableSiteSliceCreation: %s" % exc)
435 def enableSliceCreation(nodename):
436 loginbase = siteId(nodename)
437 enableSiteSliceCreation(loginbase)
439 def isTagCurrent(tags):
443 if datetime.strptime(until, "%Y%m%d") > datetime.now():
444 # NOTE: the 'exempt_until' time is beyond current time
448 def isPersonExempt(email):
449 tags = api.GetPersonTags({'email' : email, 'tagname' : 'exempt_person_until'})
450 return isTagCurrent(tags)
452 def isNodeExempt(hostname):
453 tags = api.GetNodeTags({'hostname' : hostname, 'tagname' : 'exempt_node_until'})
454 return isTagCurrent(tags)
456 def isSliceExempt(slicename):
457 tags = api.GetSliceTags({'name' : slicename, 'tagname' : 'exempt_slice_until'})
458 return isTagCurrent(tags)
460 def isSiteExempt(loginbase):
461 tags = api.GetSiteTags({'login_base' : loginbase, 'tagname' : 'exempt_site_until'})
462 return isTagCurrent(tags)
465 Removes site's ability to create slices. Returns previous max_slices
467 def removeSiteSliceCreation(loginbase):
468 #print "removeSiteSliceCreation(%s)" % loginbase
470 if isPendingSite(loginbase):
471 msg = "INFO: removeSiteSliceCreation: Pending Site (%s)" % loginbase
476 api = xmlrpclib.Server(auth.server, verbose=False)
478 logger.info("Removing slice creation for site %s" % loginbase)
480 if not isSiteExempt(loginbase):
481 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
482 except Exception, exc:
483 logger.info("removeSiteSliceCreation: %s" % exc)
486 Removes ability to create slices. Returns previous max_slices
488 def removeSliceCreation(nodename):
489 loginbase = siteId(nodename)
490 removeSiteSliceCreation(loginbase)
496 #def enableSliceCreation(nodename, maxslices):
497 # api = xmlrpclib.Server(auth.server, verbose=False)
498 # anon = {'AuthMethod': "anonymous"}
499 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
500 # if len(siteid) == 1:
501 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
504 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
505 # except Exception, exc:
506 # logger.info("API: %s" % exc)
508 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
511 logger.setLevel(logging.DEBUG)
512 ch = logging.StreamHandler()
513 ch.setLevel(logging.DEBUG)
514 formatter = logging.Formatter('logger - %(message)s')
515 ch.setFormatter(formatter)
516 logger.addHandler(ch)
517 #print getpcu("kupl2.ittc.ku.edu")
518 #print getpcu("planetlab1.cse.msu.edu")
519 #print getpcu("alice.cs.princeton.edu")
521 #nodeBootState("alice.cs.princeton.edu", "boot")
522 #freezeSite("alice.cs.princeton.edu")
523 print removeSliceCreation("alice.cs.princeton.edu")
524 #enableSliceCreation("alice.cs.princeton.edu", 1024)
525 #print getSiteNodes("princeton")
526 #print siteId("alice.cs.princeton.edu")
527 #print nodePOD("alice.cs.princeton.edu")
528 #print slices("princeton")
530 if __name__=="__main__":