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 monitor import database
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__()
95 def _param_to_str(self, name, *params):
98 retstr += "%s-" % name
103 def __getattr__(self, name):
104 method = getattr(self.api, name)
106 raise AssertionError("method does not exist")
108 def run_or_returncached(*params):
109 cachename = self._param_to_str(name, *params)
110 #print "cachename is %s" % cachename
111 if hasattr(config, 'refresh'):
112 refresh = config.refresh
117 if not database.cachedRecently(cachename):
118 load_old_cache = False
120 values = method(self.auth, *params)
122 print "Call %s FAILED: Using old cached data" % cachename
123 load_old_cache = True
126 values = database.dbLoad(cachename)
128 database.dbDump(cachename, values)
132 values = database.dbLoad(cachename)
135 return method(self.auth, *params)
137 return run_or_returncached
139 api = PLC(auth.auth, auth.server)
140 cacheapi = CachedPLC(auth.auth, auth.server)
144 return xmlrpclib.Server(url, verbose=False, allow_none=True)
146 def getNodeAPI(session):
147 nodeauth = Auth(session=session)
148 return PLC(nodeauth.auth, auth.server)
150 def getAuthAPI(url=None):
152 return PLC(auth.auth, url)
154 return PLC(auth.auth, auth.server)
156 def getCachedAuthAPI():
157 return CachedPLC(auth.auth, auth.server)
159 def getSessionAPI(session, server):
160 nodeauth = Auth(session=session)
161 return PLC(nodeauth.auth, server)
162 def getUserAPI(username, password, server):
163 auth = Auth(username,password)
164 return PLC(auth.auth, server)
166 def getTechEmails(loginbase):
168 For the given site, return all user email addresses that have the 'tech' role.
172 s = api.GetSites(loginbase)[0]
174 p = api.GetPersons(s['person_ids'])
175 # pull out those with the right role.
176 emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
179 def getPIEmails(loginbase):
181 For the given site, return all user email addresses that have the 'tech' role.
185 s = api.GetSites(loginbase)[0]
187 p = api.GetPersons(s['person_ids'])
188 # pull out those with the right role.
189 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
192 def getSliceUserEmails(loginbase):
194 For the given site, return all user email addresses that have the 'tech' role.
198 s = api.GetSites(loginbase)[0]
200 slices = api.GetSlices(s['slice_ids'])
203 people += api.GetPersons(slice['person_ids'])
204 # pull out those with the right role.
205 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
206 unique_emails = [ x for x in set(emails) ]
210 Returns list of nodes in dbg as reported by PLC
214 api = xmlrpclib.Server(auth.server, verbose=False)
215 anon = {'AuthMethod': "anonymous"}
216 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
217 dbgNodes.append(node['hostname'])
218 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
223 Returns loginbase for given nodename
225 def siteId(nodename):
226 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
227 site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
228 if len(site_id) == 1:
229 loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
230 return loginbase[0]['login_base']
232 print "Not nodes returned!!!!"
235 Returns list of slices for a site.
237 def slices(loginbase):
239 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
240 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
241 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
242 siteslices.append(slice['name'])
246 Returns dict of PCU info of a given node.
248 def getpcu(nodename):
249 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
250 anon = {'AuthMethod': "anonymous"}
251 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
252 if nodeinfo['pcu_ids']:
254 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
256 print nodeinfo["ports"]
257 sitepcu[nodename] = nodeinfo["ports"][0]
260 logger.info("%s doesn't have PCU" % nodename)
263 def GetPCUs(filter=None, fields=None):
264 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
265 pcu_list = api.GetPCUs(auth.auth, filter, fields)
269 Returns all site nodes for site id (loginbase).
271 def getSiteNodes(loginbase, fields=None):
272 api = xmlrpclib.Server(auth.server, verbose=False)
274 anon = {'AuthMethod': "anonymous"}
276 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
277 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
278 nodelist.append(node['hostname'])
279 except Exception, exc:
280 logger.info("getSiteNodes: %s" % exc)
281 print "getSiteNodes: %s" % exc
285 def getPersons(filter=None, fields=None):
286 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
289 persons = api.GetPersons(auth.auth, filter, fields)
290 except Exception, exc:
291 print "getPersons: %s" % exc
292 logger.info("getPersons: %s" % exc)
295 def getSites(filter=None, fields=None):
296 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
298 anon = {'AuthMethod': "anonymous"}
300 #sites = api.GetSites(anon, filter, fields)
301 sites = api.GetSites(auth.auth, filter, fields)
302 except Exception, exc:
303 traceback.print_exc()
304 print "getSites: %s" % exc
305 logger.info("getSites: %s" % exc)
308 def getSiteNodes2(loginbase):
309 api = xmlrpclib.Server(auth.server, verbose=False)
311 anon = {'AuthMethod': "anonymous"}
313 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
314 nodelist += getNodes({'node_id':nodeids})
315 except Exception, exc:
316 logger.info("getSiteNodes2: %s" % exc)
319 def getNodeNetworks(filter=None):
320 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
321 nodenetworks = api.GetInterfaces(auth.auth, filter, None)
324 def getNodes(filter=None, fields=None):
325 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
326 nodes = api.GetNodes(auth.auth, filter, fields)
327 #['boot_state', 'hostname',
328 #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
329 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
333 # Check if the site is a pending site that needs to be approved.
334 def isPendingSite(loginbase):
335 api = xmlrpclib.Server(auth.server, verbose=False)
337 site = api.GetSites(auth.auth, loginbase)[0]
338 except Exception, exc:
339 login.info("ERROR: No site %s" % loginbase)
342 if not site['enabled'] and site['ext_consortium_id'] == PENDING_CONSORTIUM_ID:
349 Sets boot state of a node.
351 def nodeBootState(nodename, state):
352 api = xmlrpclib.Server(auth.server, verbose=False)
354 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
355 except Exception, exc:
356 logger.info("nodeBootState: %s" % exc)
358 def updateNodeKey(nodename, key):
359 api = xmlrpclib.Server(auth.server, verbose=False)
361 return api.UpdateNode(auth.auth, nodename, {'key': key})
362 except Exception, exc:
363 logger.info("updateNodeKey: %s" % exc)
366 Sends Ping Of Death to node.
368 def nodePOD(nodename):
369 api = xmlrpclib.Server(auth.server, verbose=False)
370 logger.info("Sending POD to %s" % nodename)
373 return api.RebootNode(auth.auth, nodename)
374 except Exception, exc:
375 logger.info("nodePOD: %s" % exc)
378 Freeze all site slices.
380 def suspendSiteSlices(loginbase):
381 api = xmlrpclib.Server(auth.server, verbose=False)
382 for slice in slices(loginbase):
383 logger.info("Suspending slice %s" % slice)
386 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
387 except Exception, exc:
388 logger.info("suspendSlices: %s" % exc)
391 Freeze all site slices.
393 def suspendSlices(nodename):
394 api = xmlrpclib.Server(auth.server, verbose=False)
395 for slice in slices(siteId(nodename)):
396 logger.info("Suspending slice %s" % slice)
399 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
400 except Exception, exc:
401 logger.info("suspendSlices: %s" % exc)
403 def enableSiteSlices(loginbase):
404 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
405 for slice in slices(loginbase):
406 logger.info("Enabling slices %s" % slice)
409 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
410 if len(slice_list) == 0:
412 slice_id = slice_list[0]['slice_id']
413 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
415 if "enabled" == attr['name'] and attr['value'] == "0":
416 logger.info("Deleted enable=0 attribute from slice %s" % slice)
417 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
418 except Exception, exc:
419 logger.info("enableSiteSlices: %s" % exc)
420 print "exception: %s" % exc
422 def enableSlices(nodename):
423 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
425 for slice in slices(siteId(nodename)):
426 logger.info("Enabling slices %s" % slice)
429 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
430 if len(slice_list) == 0:
432 slice_id = slice_list[0]['slice_id']
433 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
435 if "enabled" == attr['name'] and attr['value'] == "0":
436 logger.info("Deleted enable=0 attribute from slice %s" % slice)
437 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
438 except Exception, exc:
439 logger.info("enableSlices: %s" % exc)
440 print "exception: %s" % exc
443 #I'm commenting this because this really should be a manual process.
445 #Enable suspended site slices.
447 #def enableSlices(nodename, slicelist):
448 # api = xmlrpclib.Server(auth.server, verbose=False)
449 # for slice in slices(siteId(nodename)):
450 # logger.info("Suspending slice %s" % slice)
451 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
453 def enableSiteSliceCreation(loginbase):
454 if isPendingSite(loginbase):
455 msg = "INFO: enableSiteSliceCreation: Pending Site (%s)" % loginbase
460 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
462 logger.info("Enabling slice creation for site %s" % loginbase)
464 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
465 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
466 except Exception, exc:
467 print "ERROR: enableSiteSliceCreation: %s" % exc
468 logger.info("ERROR: enableSiteSliceCreation: %s" % exc)
470 def enableSliceCreation(nodename):
471 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
473 loginbase = siteId(nodename)
474 enableSiteSliceCreation(loginbase)
475 except Exception, exc:
476 print "ERROR: enableSliceCreation: %s" % exc
477 logger.info("ERROR: enableSliceCreation: %s" % exc)
480 Removes site's ability to create slices. Returns previous max_slices
482 def removeSiteSliceCreation(loginbase):
483 print "removeSiteSliceCreation(%s)" % loginbase
485 if isPendingSite(loginbase):
486 msg = "INFO: removeSiteSliceCreation: Pending Site (%s)" % loginbase
491 api = xmlrpclib.Server(auth.server, verbose=False)
493 logger.info("Removing slice creation for site %s" % loginbase)
495 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
496 except Exception, exc:
497 logger.info("removeSiteSliceCreation: %s" % exc)
500 Removes ability to create slices. Returns previous max_slices
502 def removeSliceCreation(nodename):
503 print "removeSliceCreation(%s)" % nodename
504 api = xmlrpclib.Server(auth.server, verbose=False)
506 loginbase = siteId(nodename)
507 removeSiteSliceCreation(loginbase)
508 except Exception, exc:
509 logger.info("removeSliceCreation: %s" % exc)
514 #def enableSliceCreation(nodename, maxslices):
515 # api = xmlrpclib.Server(auth.server, verbose=False)
516 # anon = {'AuthMethod': "anonymous"}
517 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
518 # if len(siteid) == 1:
519 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
522 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
523 # except Exception, exc:
524 # logger.info("API: %s" % exc)
526 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
529 logger.setLevel(logging.DEBUG)
530 ch = logging.StreamHandler()
531 ch.setLevel(logging.DEBUG)
532 formatter = logging.Formatter('logger - %(message)s')
533 ch.setFormatter(formatter)
534 logger.addHandler(ch)
535 #print getpcu("kupl2.ittc.ku.edu")
536 #print getpcu("planetlab1.cse.msu.edu")
537 #print getpcu("alice.cs.princeton.edu")
539 #nodeBootState("alice.cs.princeton.edu", "boot")
540 #freezeSite("alice.cs.princeton.edu")
541 print removeSliceCreation("alice.cs.princeton.edu")
542 #enableSliceCreation("alice.cs.princeton.edu", 1024)
543 #print getSiteNodes("princeton")
544 #print siteId("alice.cs.princeton.edu")
545 #print nodePOD("alice.cs.princeton.edu")
546 #print slices("princeton")
548 if __name__=="__main__":