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 PENDING_CONSORTIUM_ID = 0
18 APPROVED_CONSORTIUM_ID = 999999
21 from monitor import config
23 XMLRPC_SERVER=config.API_SERVER
26 # NOTE: this host is used by default when there are no auth files.
27 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
29 logger = logging.getLogger("monitor")
32 def __init__(self, username=None, password=None, **kwargs):
33 if 'session' in kwargs:
34 self.auth= { 'AuthMethod' : 'session',
35 'session' : kwargs['session'] }
37 if username==None and password==None:
38 self.auth = {'AuthMethod': "anonymous"}
40 self.auth = {'Username' : username,
41 'AuthMethod' : 'password',
42 'AuthString' : password}
45 # NOTE: by default, use anonymous access, but if auth files are
46 # configured, use them, with their auth definitions.
49 from monitor import config
50 auth.auth = {'Username' : config.API_AUTH_USER,
51 'AuthMethod' : 'password',
52 'AuthString' : config.API_AUTH_PASSWORD}
53 auth.server = config.API_SERVER
57 auth.server = auth.plc
60 auth.server = XMLRPC_SERVER
62 global_error_count = 0
65 def __init__(self, auth, url):
68 self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
70 def __getattr__(self, name):
71 method = getattr(self.api, name)
73 raise AssertionError("method does not exist")
76 return lambda *params : method(self.auth, *params)
77 except xmlrpclib.ProtocolError:
79 global_error_count += 1
80 if global_error_count >= 10:
81 print "maximum error count exceeded; exiting..."
84 print "%s errors have occurred" % global_error_count
85 raise Exception("ProtocolError continuing")
88 return self.api.__repr__()
93 def _param_to_str(self, name, *params):
96 retstr += "%s-" % name
101 def __getattr__(self, name):
102 method = getattr(self.api, name)
104 raise AssertionError("method does not exist")
106 def run_or_returncached(*params):
107 cachename = self._param_to_str(name, *params)
108 #print "cachename is %s" % cachename
109 if hasattr(config, 'refresh'):
110 refresh = config.refresh
115 if not database.cachedRecently(cachename):
116 load_old_cache = False
118 values = method(self.auth, *params)
120 print "Call %s FAILED: Using old cached data" % cachename
121 load_old_cache = True
124 values = database.dbLoad(cachename)
126 database.dbDump(cachename, values)
130 values = database.dbLoad(cachename)
133 return method(self.auth, *params)
135 return run_or_returncached
137 api = PLC(auth.auth, auth.server)
138 cacheapi = CachedPLC(auth.auth, auth.server)
142 return xmlrpclib.Server(url, verbose=False, allow_none=True)
144 def getNodeAPI(session):
145 nodeauth = Auth(session=session)
146 return PLC(nodeauth.auth, auth.server)
148 def getAuthAPI(url=None):
150 return PLC(auth.auth, url)
152 return PLC(auth.auth, auth.server)
154 def getCachedAuthAPI():
155 return CachedPLC(auth.auth, auth.server)
157 def getSessionAPI(session, server):
158 nodeauth = Auth(session=session)
159 return PLC(nodeauth.auth, server)
160 def getUserAPI(username, password, server):
161 auth = Auth(username,password)
162 return PLC(auth.auth, server)
164 def getTechEmails(loginbase):
166 For the given site, return all user email addresses that have the 'tech' role.
170 s = api.GetSites(loginbase)[0]
172 p = api.GetPersons(s['person_ids'])
173 # pull out those with the right role.
174 emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
177 def getPIEmails(loginbase):
179 For the given site, return all user email addresses that have the 'tech' role.
183 s = api.GetSites(loginbase)[0]
185 p = api.GetPersons(s['person_ids'])
186 # pull out those with the right role.
187 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
190 def getSliceUserEmails(loginbase):
192 For the given site, return all user email addresses that have the 'tech' role.
196 s = api.GetSites(loginbase)[0]
198 slices = api.GetSlices(s['slice_ids'])
201 people += api.GetPersons(slice['person_ids'])
202 # pull out those with the right role.
203 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
204 unique_emails = [ x for x in set(emails) ]
208 Returns list of nodes in dbg as reported by PLC
212 api = xmlrpclib.Server(auth.server, verbose=False)
213 anon = {'AuthMethod': "anonymous"}
214 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
215 dbgNodes.append(node['hostname'])
216 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
221 Returns loginbase for given nodename
223 def siteId(nodename):
224 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
225 site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
226 if len(site_id) == 1:
227 loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
228 return loginbase[0]['login_base']
230 print "Not nodes returned!!!!"
233 Returns list of slices for a site.
235 def slices(loginbase):
237 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
238 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
239 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
240 siteslices.append(slice['name'])
244 Returns dict of PCU info of a given node.
246 def getpcu(nodename):
247 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
248 anon = {'AuthMethod': "anonymous"}
249 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
250 if nodeinfo['pcu_ids']:
252 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
254 print nodeinfo["ports"]
255 sitepcu[nodename] = nodeinfo["ports"][0]
258 logger.info("%s doesn't have PCU" % nodename)
261 def GetPCUs(filter=None, fields=None):
262 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
263 pcu_list = api.GetPCUs(auth.auth, filter, fields)
267 Returns all site nodes for site id (loginbase).
269 def getSiteNodes(loginbase, fields=None):
270 api = xmlrpclib.Server(auth.server, verbose=False)
272 anon = {'AuthMethod': "anonymous"}
274 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
275 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
276 nodelist.append(node['hostname'])
277 except Exception, exc:
278 logger.info("getSiteNodes: %s" % exc)
279 print "getSiteNodes: %s" % exc
283 def getPersons(filter=None, fields=None):
284 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
287 persons = api.GetPersons(auth.auth, filter, fields)
288 except Exception, exc:
289 print "getPersons: %s" % exc
290 logger.info("getPersons: %s" % exc)
293 def getSites(filter=None, fields=None):
294 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
296 anon = {'AuthMethod': "anonymous"}
298 #sites = api.GetSites(anon, filter, fields)
299 sites = api.GetSites(auth.auth, filter, fields)
300 except Exception, exc:
301 traceback.print_exc()
302 print "getSites: %s" % exc
303 logger.info("getSites: %s" % exc)
306 def getSiteNodes2(loginbase):
307 api = xmlrpclib.Server(auth.server, verbose=False)
309 anon = {'AuthMethod': "anonymous"}
311 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
312 nodelist += getNodes({'node_id':nodeids})
313 except Exception, exc:
314 logger.info("getSiteNodes2: %s" % exc)
317 def getNodeNetworks(filter=None):
318 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
319 nodenetworks = api.GetInterfaces(auth.auth, filter, None)
322 def getNodes(filter=None, fields=None):
323 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
324 nodes = api.GetNodes(auth.auth, filter, fields)
325 #['boot_state', 'hostname',
326 #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
327 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
331 # Check if the site is a pending site that needs to be approved.
332 def isPendingSite(loginbase):
333 api = xmlrpclib.Server(auth.server, verbose=False)
335 site = api.GetSites(auth.auth, loginbase)[0]
336 except Exception, exc:
337 login.info("ERROR: No site %s" % loginbase)
340 if not site['enabled'] and site['ext_consortium_id'] == PENDING_CONSORTIUM_ID:
347 Sets boot state of a node.
349 def nodeBootState(nodename, state):
350 api = xmlrpclib.Server(auth.server, verbose=False)
352 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
353 except Exception, exc:
354 logger.info("nodeBootState: %s" % exc)
356 def updateNodeKey(nodename, key):
357 api = xmlrpclib.Server(auth.server, verbose=False)
359 return api.UpdateNode(auth.auth, nodename, {'key': key})
360 except Exception, exc:
361 logger.info("updateNodeKey: %s" % exc)
364 Sends Ping Of Death to node.
366 def nodePOD(nodename):
367 api = xmlrpclib.Server(auth.server, verbose=False)
368 logger.info("Sending POD to %s" % nodename)
371 return api.RebootNode(auth.auth, nodename)
372 except Exception, exc:
373 logger.info("nodePOD: %s" % exc)
376 Freeze all site slices.
378 def suspendSiteSlices(loginbase):
379 api = xmlrpclib.Server(auth.server, verbose=False)
380 for slice in slices(loginbase):
381 logger.info("Suspending slice %s" % slice)
384 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
385 except Exception, exc:
386 logger.info("suspendSlices: %s" % exc)
389 Freeze all site slices.
391 def suspendSlices(nodename):
392 api = xmlrpclib.Server(auth.server, verbose=False)
393 for slice in slices(siteId(nodename)):
394 logger.info("Suspending slice %s" % slice)
397 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
398 except Exception, exc:
399 logger.info("suspendSlices: %s" % exc)
401 def enableSiteSlices(loginbase):
402 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
403 for slice in slices(loginbase):
404 logger.info("Enabling slices %s" % slice)
407 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
408 if len(slice_list) == 0:
410 slice_id = slice_list[0]['slice_id']
411 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
413 if "enabled" == attr['name'] and attr['value'] == "0":
414 logger.info("Deleted enable=0 attribute from slice %s" % slice)
415 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
416 except Exception, exc:
417 logger.info("enableSiteSlices: %s" % exc)
418 print "exception: %s" % exc
420 def enableSlices(nodename):
421 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
423 for slice in slices(siteId(nodename)):
424 logger.info("Enabling slices %s" % slice)
427 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
428 if len(slice_list) == 0:
430 slice_id = slice_list[0]['slice_id']
431 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
433 if "enabled" == attr['name'] and attr['value'] == "0":
434 logger.info("Deleted enable=0 attribute from slice %s" % slice)
435 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
436 except Exception, exc:
437 logger.info("enableSlices: %s" % exc)
438 print "exception: %s" % exc
441 #I'm commenting this because this really should be a manual process.
443 #Enable suspended site slices.
445 #def enableSlices(nodename, slicelist):
446 # api = xmlrpclib.Server(auth.server, verbose=False)
447 # for slice in slices(siteId(nodename)):
448 # logger.info("Suspending slice %s" % slice)
449 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
451 def enableSiteSliceCreation(loginbase):
452 if isPendingSite(loginbase):
453 msg = "INFO: enableSiteSliceCreation: Pending Site (%s)" % loginbase
458 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
460 logger.info("Enabling slice creation for site %s" % loginbase)
462 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
463 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
464 except Exception, exc:
465 print "ERROR: enableSiteSliceCreation: %s" % exc
466 logger.info("ERROR: enableSiteSliceCreation: %s" % exc)
468 def enableSliceCreation(nodename):
469 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
471 loginbase = siteId(nodename)
472 enableSiteSliceCreation(loginbase)
473 except Exception, exc:
474 print "ERROR: enableSliceCreation: %s" % exc
475 logger.info("ERROR: enableSliceCreation: %s" % exc)
478 Removes site's ability to create slices. Returns previous max_slices
480 def removeSiteSliceCreation(loginbase):
481 print "removeSiteSliceCreation(%s)" % loginbase
483 if isPendingSite(loginbase):
484 msg = "INFO: removeSiteSliceCreation: Pending Site (%s)" % loginbase
489 api = xmlrpclib.Server(auth.server, verbose=False)
491 logger.info("Removing slice creation for site %s" % loginbase)
493 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
494 except Exception, exc:
495 logger.info("removeSiteSliceCreation: %s" % exc)
498 Removes ability to create slices. Returns previous max_slices
500 def removeSliceCreation(nodename):
501 print "removeSliceCreation(%s)" % nodename
502 api = xmlrpclib.Server(auth.server, verbose=False)
504 loginbase = siteId(nodename)
505 removeSiteSliceCreation(loginbase)
506 except Exception, exc:
507 logger.info("removeSliceCreation: %s" % exc)
512 #def enableSliceCreation(nodename, maxslices):
513 # api = xmlrpclib.Server(auth.server, verbose=False)
514 # anon = {'AuthMethod': "anonymous"}
515 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
516 # if len(siteid) == 1:
517 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
520 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
521 # except Exception, exc:
522 # logger.info("API: %s" % exc)
524 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
527 logger.setLevel(logging.DEBUG)
528 ch = logging.StreamHandler()
529 ch.setLevel(logging.DEBUG)
530 formatter = logging.Formatter('logger - %(message)s')
531 ch.setFormatter(formatter)
532 logger.addHandler(ch)
533 #print getpcu("kupl2.ittc.ku.edu")
534 #print getpcu("planetlab1.cse.msu.edu")
535 #print getpcu("alice.cs.princeton.edu")
537 #nodeBootState("alice.cs.princeton.edu", "boot")
538 #freezeSite("alice.cs.princeton.edu")
539 print removeSliceCreation("alice.cs.princeton.edu")
540 #enableSliceCreation("alice.cs.princeton.edu", 1024)
541 #print getSiteNodes("princeton")
542 #print siteId("alice.cs.princeton.edu")
543 #print nodePOD("alice.cs.princeton.edu")
544 #print slices("princeton")
546 if __name__=="__main__":