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)
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)
146 return PLC(auth.auth, auth.server)
148 def getCachedAuthAPI():
149 return CachedPLC(auth.auth, auth.server)
151 def getSessionAPI(session, server):
152 nodeauth = Auth(session=session)
153 return PLC(nodeauth.auth, server)
154 def getUserAPI(username, password, server):
155 auth = Auth(username,password)
156 return PLC(auth.auth, server)
158 def getTechEmails(loginbase):
160 For the given site, return all user email addresses that have the 'tech' role.
164 s = api.GetSites(loginbase)[0]
166 p = api.GetPersons(s['person_ids'])
167 # pull out those with the right role.
168 emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
171 def getPIEmails(loginbase):
173 For the given site, return all user email addresses that have the 'tech' role.
177 s = api.GetSites(loginbase)[0]
179 p = api.GetPersons(s['person_ids'])
180 # pull out those with the right role.
181 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
184 def getSliceUserEmails(loginbase):
186 For the given site, return all user email addresses that have the 'tech' role.
190 s = api.GetSites(loginbase)[0]
192 slices = api.GetSlices(s['slice_ids'])
195 people += api.GetPersons(slice['person_ids'])
196 # pull out those with the right role.
197 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
198 unique_emails = [ x for x in set(emails) ]
202 Returns list of nodes in dbg as reported by PLC
206 api = xmlrpclib.Server(auth.server, verbose=False)
207 anon = {'AuthMethod': "anonymous"}
208 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
209 dbgNodes.append(node['hostname'])
210 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
215 Returns loginbase for given nodename
217 def siteId(nodename):
218 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
219 site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
220 if len(site_id) == 1:
221 loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
222 return loginbase[0]['login_base']
224 print "Not nodes returned!!!!"
227 Returns list of slices for a site.
229 def slices(loginbase):
231 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
232 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
233 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
234 siteslices.append(slice['name'])
238 Returns dict of PCU info of a given node.
240 def getpcu(nodename):
241 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
242 anon = {'AuthMethod': "anonymous"}
243 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
244 if nodeinfo['pcu_ids']:
246 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
248 print nodeinfo["ports"]
249 sitepcu[nodename] = nodeinfo["ports"][0]
252 logger.info("%s doesn't have PCU" % nodename)
255 def GetPCUs(filter=None, fields=None):
256 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
257 pcu_list = api.GetPCUs(auth.auth, filter, fields)
261 Returns all site nodes for site id (loginbase).
263 def getSiteNodes(loginbase, fields=None):
264 api = xmlrpclib.Server(auth.server, verbose=False)
266 anon = {'AuthMethod': "anonymous"}
268 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
269 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
270 nodelist.append(node['hostname'])
271 except Exception, exc:
272 logger.info("getSiteNodes: %s" % exc)
273 print "getSiteNodes: %s" % exc
277 def getPersons(filter=None, fields=None):
278 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
281 persons = api.GetPersons(auth.auth, filter, fields)
282 except Exception, exc:
283 print "getPersons: %s" % exc
284 logger.info("getPersons: %s" % exc)
287 def getSites(filter=None, fields=None):
288 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
290 anon = {'AuthMethod': "anonymous"}
292 #sites = api.GetSites(anon, filter, fields)
293 sites = api.GetSites(auth.auth, filter, fields)
294 except Exception, exc:
295 traceback.print_exc()
296 print "getSites: %s" % exc
297 logger.info("getSites: %s" % exc)
300 def getSiteNodes2(loginbase):
301 api = xmlrpclib.Server(auth.server, verbose=False)
303 anon = {'AuthMethod': "anonymous"}
305 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
306 nodelist += getNodes({'node_id':nodeids})
307 except Exception, exc:
308 logger.info("getSiteNodes2: %s" % exc)
311 def getNodeNetworks(filter=None):
312 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
313 nodenetworks = api.GetInterfaces(auth.auth, filter, None)
316 def getNodes(filter=None, fields=None):
317 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
318 nodes = api.GetNodes(auth.auth, filter, fields)
319 #['boot_state', 'hostname',
320 #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
321 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
325 Sets boot state of a node.
327 def nodeBootState(nodename, state):
328 api = xmlrpclib.Server(auth.server, verbose=False)
330 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
331 except Exception, exc:
332 logger.info("nodeBootState: %s" % exc)
334 def updateNodeKey(nodename, key):
335 api = xmlrpclib.Server(auth.server, verbose=False)
337 return api.UpdateNode(auth.auth, nodename, {'key': key})
338 except Exception, exc:
339 logger.info("updateNodeKey: %s" % exc)
342 Sends Ping Of Death to node.
344 def nodePOD(nodename):
345 api = xmlrpclib.Server(auth.server, verbose=False)
346 logger.info("Sending POD to %s" % nodename)
349 return api.RebootNode(auth.auth, nodename)
350 except Exception, exc:
351 logger.info("nodePOD: %s" % exc)
354 Freeze all site slices.
356 def suspendSiteSlices(loginbase):
357 api = xmlrpclib.Server(auth.server, verbose=False)
358 for slice in slices(loginbase):
359 logger.info("Suspending slice %s" % slice)
362 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
363 except Exception, exc:
364 logger.info("suspendSlices: %s" % exc)
367 Freeze all site slices.
369 def suspendSlices(nodename):
370 api = xmlrpclib.Server(auth.server, verbose=False)
371 for slice in slices(siteId(nodename)):
372 logger.info("Suspending slice %s" % slice)
375 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
376 except Exception, exc:
377 logger.info("suspendSlices: %s" % exc)
379 def enableSiteSlices(loginbase):
380 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
381 for slice in slices(loginbase):
382 logger.info("Enabling slices %s" % slice)
385 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
386 if len(slice_list) == 0:
388 slice_id = slice_list[0]['slice_id']
389 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
391 if "enabled" == attr['name'] and attr['value'] == "0":
392 logger.info("Deleted enable=0 attribute from slice %s" % slice)
393 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
394 except Exception, exc:
395 logger.info("enableSiteSlices: %s" % exc)
396 print "exception: %s" % exc
398 def enableSlices(nodename):
399 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
400 for slice in slices(siteId(nodename)):
401 logger.info("Enabling slices %s" % slice)
404 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
405 if len(slice_list) == 0:
407 slice_id = slice_list[0]['slice_id']
408 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
410 if "enabled" == attr['name'] and attr['value'] == "0":
411 logger.info("Deleted enable=0 attribute from slice %s" % slice)
412 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
413 except Exception, exc:
414 logger.info("enableSlices: %s" % exc)
415 print "exception: %s" % exc
417 #I'm commenting this because this really should be a manual process.
419 #Enable suspended site slices.
421 #def enableSlices(nodename, slicelist):
422 # api = xmlrpclib.Server(auth.server, verbose=False)
423 # for slice in slices(siteId(nodename)):
424 # logger.info("Suspending slice %s" % slice)
425 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
427 def enableSiteSliceCreation(loginbase):
428 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
430 logger.info("Enabling slice creation for site %s" % loginbase)
432 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
433 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
434 except Exception, exc:
435 print "ERROR: enableSiteSliceCreation: %s" % exc
436 logger.info("ERROR: enableSiteSliceCreation: %s" % exc)
438 def enableSliceCreation(nodename):
439 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
441 loginbase = siteId(nodename)
442 logger.info("Enabling slice creation for site %s" % loginbase)
444 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
445 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
446 except Exception, exc:
447 print "ERROR: enableSliceCreation: %s" % exc
448 logger.info("ERROR: enableSliceCreation: %s" % exc)
451 Removes site's ability to create slices. Returns previous max_slices
453 def removeSiteSliceCreation(sitename):
454 print "removeSiteSliceCreation(%s)" % sitename
455 api = xmlrpclib.Server(auth.server, verbose=False)
457 logger.info("Removing slice creation for site %s" % sitename)
459 api.UpdateSite(auth.auth, sitename, {'enabled': False})
460 except Exception, exc:
461 logger.info("removeSiteSliceCreation: %s" % exc)
464 Removes ability to create slices. Returns previous max_slices
466 def removeSliceCreation(nodename):
467 print "removeSliceCreation(%s)" % nodename
468 api = xmlrpclib.Server(auth.server, verbose=False)
470 loginbase = siteId(nodename)
471 #numslices = api.GetSites(auth.auth, {"login_base": loginbase},
472 # ["max_slices"])[0]['max_slices']
473 logger.info("Removing slice creation for site %s" % loginbase)
475 #api.UpdateSite(auth.auth, loginbase, {'max_slices': 0})
476 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
477 except Exception, exc:
478 logger.info("removeSliceCreation: %s" % exc)
483 #def enableSliceCreation(nodename, maxslices):
484 # api = xmlrpclib.Server(auth.server, verbose=False)
485 # anon = {'AuthMethod': "anonymous"}
486 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
487 # if len(siteid) == 1:
488 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
491 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
492 # except Exception, exc:
493 # logger.info("API: %s" % exc)
495 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
498 logger.setLevel(logging.DEBUG)
499 ch = logging.StreamHandler()
500 ch.setLevel(logging.DEBUG)
501 formatter = logging.Formatter('logger - %(message)s')
502 ch.setFormatter(formatter)
503 logger.addHandler(ch)
504 #print getpcu("kupl2.ittc.ku.edu")
505 #print getpcu("planetlab1.cse.msu.edu")
506 #print getpcu("alice.cs.princeton.edu")
508 #nodeBootState("alice.cs.princeton.edu", "boot")
509 #freezeSite("alice.cs.princeton.edu")
510 print removeSliceCreation("alice.cs.princeton.edu")
511 #enableSliceCreation("alice.cs.princeton.edu", 1024)
512 #print getSiteNodes("princeton")
513 #print siteId("alice.cs.princeton.edu")
514 #print nodePOD("alice.cs.princeton.edu")
515 #print slices("princeton")
517 if __name__=="__main__":