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
18 from monitor import config
20 XMLRPC_SERVER=config.API_SERVER
23 # NOTE: this host is used by default when there are no auth files.
24 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
26 logger = logging.getLogger("monitor")
29 def __init__(self, username=None, password=None, **kwargs):
30 if 'session' in kwargs:
31 self.auth= { 'AuthMethod' : 'session',
32 'session' : kwargs['session'] }
34 if username==None and password==None:
35 self.auth = {'AuthMethod': "anonymous"}
37 self.auth = {'Username' : username,
38 'AuthMethod' : 'password',
39 'AuthString' : password}
42 # NOTE: by default, use anonymous access, but if auth files are
43 # configured, use them, with their auth definitions.
46 from monitor import config
47 auth.auth = {'Username' : config.API_AUTH_USER,
48 'AuthMethod' : 'password',
49 'AuthString' : config.API_AUTH_PASSWORD}
50 auth.server = config.API_SERVER
54 auth.server = auth.plc
57 auth.server = XMLRPC_SERVER
59 global_error_count = 0
62 def __init__(self, auth, url):
65 self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
67 def __getattr__(self, name):
68 method = getattr(self.api, name)
70 raise AssertionError("method does not exist")
73 return lambda *params : method(self.auth, *params)
74 except xmlrpclib.ProtocolError:
76 global_error_count += 1
77 if global_error_count >= 10:
78 print "maximum error count exceeded; exiting..."
81 print "%s errors have occurred" % global_error_count
82 raise Exception("ProtocolError continuing")
85 return self.api.__repr__()
90 def _param_to_str(self, name, *params):
93 retstr += "%s-" % name
98 def __getattr__(self, name):
99 method = getattr(self.api, name)
101 raise AssertionError("method does not exist")
103 def run_or_returncached(*params):
104 cachename = self._param_to_str(name, *params)
105 #print "cachename is %s" % cachename
106 if hasattr(config, 'refresh'):
107 refresh = config.refresh
112 if not database.cachedRecently(cachename):
113 load_old_cache = False
115 values = method(self.auth, *params)
117 print "Call %s FAILED: Using old cached data" % cachename
118 load_old_cache = True
121 values = database.dbLoad(cachename)
123 database.dbDump(cachename, values)
127 values = database.dbLoad(cachename)
130 return method(self.auth, *params)
132 return run_or_returncached
134 api = PLC(auth.auth, auth.server)
135 cacheapi = CachedPLC(auth.auth, auth.server)
139 return xmlrpclib.Server(url, verbose=False, allow_none=True)
141 def getNodeAPI(session):
142 nodeauth = Auth(session=session)
143 return PLC(nodeauth.auth, auth.server)
145 def getAuthAPI(url=None):
147 return PLC(auth.auth, url)
149 return PLC(auth.auth, auth.server)
151 def getCachedAuthAPI():
152 return CachedPLC(auth.auth, auth.server)
154 def getSessionAPI(session, server):
155 nodeauth = Auth(session=session)
156 return PLC(nodeauth.auth, server)
157 def getUserAPI(username, password, server):
158 auth = Auth(username,password)
159 return PLC(auth.auth, server)
161 def getTechEmails(loginbase):
163 For the given site, return all user email addresses that have the 'tech' role.
167 s = api.GetSites(loginbase)[0]
169 p = api.GetPersons(s['person_ids'])
170 # pull out those with the right role.
171 emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
174 def getPIEmails(loginbase):
176 For the given site, return all user email addresses that have the 'tech' role.
180 s = api.GetSites(loginbase)[0]
182 p = api.GetPersons(s['person_ids'])
183 # pull out those with the right role.
184 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
187 def getSliceUserEmails(loginbase):
189 For the given site, return all user email addresses that have the 'tech' role.
193 s = api.GetSites(loginbase)[0]
195 slices = api.GetSlices(s['slice_ids'])
198 people += api.GetPersons(slice['person_ids'])
199 # pull out those with the right role.
200 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
201 unique_emails = [ x for x in set(emails) ]
205 Returns list of nodes in dbg as reported by PLC
209 api = xmlrpclib.Server(auth.server, verbose=False)
210 anon = {'AuthMethod': "anonymous"}
211 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
212 dbgNodes.append(node['hostname'])
213 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
218 Returns loginbase for given nodename
220 def siteId(nodename):
221 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
222 site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
223 if len(site_id) == 1:
224 loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
225 return loginbase[0]['login_base']
227 print "Not nodes returned!!!!"
230 Returns list of slices for a site.
232 def slices(loginbase):
234 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
235 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
236 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
237 siteslices.append(slice['name'])
241 Returns dict of PCU info of a given node.
243 def getpcu(nodename):
244 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
245 anon = {'AuthMethod': "anonymous"}
246 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
247 if nodeinfo['pcu_ids']:
249 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
251 print nodeinfo["ports"]
252 sitepcu[nodename] = nodeinfo["ports"][0]
255 logger.info("%s doesn't have PCU" % nodename)
258 def GetPCUs(filter=None, fields=None):
259 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
260 pcu_list = api.GetPCUs(auth.auth, filter, fields)
264 Returns all site nodes for site id (loginbase).
266 def getSiteNodes(loginbase, fields=None):
267 api = xmlrpclib.Server(auth.server, verbose=False)
269 anon = {'AuthMethod': "anonymous"}
271 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
272 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
273 nodelist.append(node['hostname'])
274 except Exception, exc:
275 logger.info("getSiteNodes: %s" % exc)
276 print "getSiteNodes: %s" % exc
280 def getPersons(filter=None, fields=None):
281 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
284 persons = api.GetPersons(auth.auth, filter, fields)
285 except Exception, exc:
286 print "getPersons: %s" % exc
287 logger.info("getPersons: %s" % exc)
290 def getSites(filter=None, fields=None):
291 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
293 anon = {'AuthMethod': "anonymous"}
295 #sites = api.GetSites(anon, filter, fields)
296 sites = api.GetSites(auth.auth, filter, fields)
297 except Exception, exc:
298 traceback.print_exc()
299 print "getSites: %s" % exc
300 logger.info("getSites: %s" % exc)
303 def getSiteNodes2(loginbase):
304 api = xmlrpclib.Server(auth.server, verbose=False)
306 anon = {'AuthMethod': "anonymous"}
308 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
309 nodelist += getNodes({'node_id':nodeids})
310 except Exception, exc:
311 logger.info("getSiteNodes2: %s" % exc)
314 def getNodeNetworks(filter=None):
315 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
316 nodenetworks = api.GetInterfaces(auth.auth, filter, None)
319 def getNodes(filter=None, fields=None):
320 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
321 nodes = api.GetNodes(auth.auth, filter, fields)
322 #['boot_state', 'hostname',
323 #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
324 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
328 # Check if the site is a pending site that needs to be approved.
329 def isPendingSite(loginbase):
330 api = xmlrpclib.Server(auth.server, verbose=False)
332 site = api.GetSites(auth.auth, loginbase)[0]
333 except Exception, exc:
334 login.info("ERROR: No site %s" % loginbase)
337 def all_disabled(person_ids):
338 persons = api.GetPersons(auth.auth, person_ids)
339 for person in persons:
340 if person['enabled']:
344 if not site['max_slices'] and not site['node_ids'] and all_disabled(site['person_ids']):
351 Sets boot state of a node.
353 def nodeBootState(nodename, state):
354 api = xmlrpclib.Server(auth.server, verbose=False)
356 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
357 except Exception, exc:
358 logger.info("nodeBootState: %s" % exc)
360 def updateNodeKey(nodename, key):
361 api = xmlrpclib.Server(auth.server, verbose=False)
363 return api.UpdateNode(auth.auth, nodename, {'key': key})
364 except Exception, exc:
365 logger.info("updateNodeKey: %s" % exc)
368 Sends Ping Of Death to node.
370 def nodePOD(nodename):
371 api = xmlrpclib.Server(auth.server, verbose=False)
372 logger.info("Sending POD to %s" % nodename)
375 return api.RebootNode(auth.auth, nodename)
376 except Exception, exc:
377 logger.info("nodePOD: %s" % exc)
380 Freeze all site slices.
382 def suspendSiteSlices(loginbase):
383 api = xmlrpclib.Server(auth.server, verbose=False)
384 for slice in slices(loginbase):
385 logger.info("Suspending slice %s" % slice)
388 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
389 except Exception, exc:
390 logger.info("suspendSlices: %s" % exc)
393 Freeze all site slices.
395 def suspendSlices(nodename):
396 api = xmlrpclib.Server(auth.server, verbose=False)
397 for slice in slices(siteId(nodename)):
398 logger.info("Suspending slice %s" % slice)
401 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
402 except Exception, exc:
403 logger.info("suspendSlices: %s" % exc)
405 def enableSiteSlices(loginbase):
406 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
407 for slice in slices(loginbase):
408 logger.info("Enabling slices %s" % slice)
411 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
412 if len(slice_list) == 0:
414 slice_id = slice_list[0]['slice_id']
415 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
417 if "enabled" == attr['name'] and attr['value'] == "0":
418 logger.info("Deleted enable=0 attribute from slice %s" % slice)
419 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
420 except Exception, exc:
421 logger.info("enableSiteSlices: %s" % exc)
422 print "exception: %s" % exc
424 def enableSlices(nodename):
425 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
427 for slice in slices(siteId(nodename)):
428 logger.info("Enabling slices %s" % slice)
431 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
432 if len(slice_list) == 0:
434 slice_id = slice_list[0]['slice_id']
435 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
437 if "enabled" == attr['name'] and attr['value'] == "0":
438 logger.info("Deleted enable=0 attribute from slice %s" % slice)
439 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
440 except Exception, exc:
441 logger.info("enableSlices: %s" % exc)
442 print "exception: %s" % exc
445 #I'm commenting this because this really should be a manual process.
447 #Enable suspended site slices.
449 #def enableSlices(nodename, slicelist):
450 # api = xmlrpclib.Server(auth.server, verbose=False)
451 # for slice in slices(siteId(nodename)):
452 # logger.info("Suspending slice %s" % slice)
453 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
455 def enableSiteSliceCreation(loginbase):
456 if isPendingSite(loginbase):
457 msg = "INFO: enableSiteSliceCreation: Pending Site (%s)" % loginbase
462 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
464 logger.info("Enabling slice creation for site %s" % loginbase)
466 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
467 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
468 except Exception, exc:
469 print "ERROR: enableSiteSliceCreation: %s" % exc
470 logger.info("ERROR: enableSiteSliceCreation: %s" % exc)
472 def enableSliceCreation(nodename):
473 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
475 loginbase = siteId(nodename)
476 enableSiteSliceCreation(loginbase)
477 except Exception, exc:
478 print "ERROR: enableSliceCreation: %s" % exc
479 logger.info("ERROR: enableSliceCreation: %s" % exc)
482 Removes site's ability to create slices. Returns previous max_slices
484 def removeSiteSliceCreation(sitename):
485 print "removeSiteSliceCreation(%s)" % sitename
486 api = xmlrpclib.Server(auth.server, verbose=False)
488 logger.info("Removing slice creation for site %s" % sitename)
490 api.UpdateSite(auth.auth, sitename, {'enabled': False})
491 except Exception, exc:
492 logger.info("removeSiteSliceCreation: %s" % exc)
495 Removes ability to create slices. Returns previous max_slices
497 def removeSliceCreation(nodename):
498 print "removeSliceCreation(%s)" % nodename
499 api = xmlrpclib.Server(auth.server, verbose=False)
501 loginbase = siteId(nodename)
502 #numslices = api.GetSites(auth.auth, {"login_base": loginbase},
503 # ["max_slices"])[0]['max_slices']
504 logger.info("Removing slice creation for site %s" % loginbase)
506 #api.UpdateSite(auth.auth, loginbase, {'max_slices': 0})
507 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
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__":