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__()
87 api = PLC(auth.auth, auth.server)
91 def _param_to_str(self, name, *params):
94 retstr += "%s-" % name
99 def __getattr__(self, name):
100 method = getattr(self.api, name)
102 raise AssertionError("method does not exist")
104 def run_or_returncached(*params):
105 cachename = self._param_to_str(name, *params)
106 #print "cachename is %s" % cachename
107 if hasattr(config, 'refresh'):
108 refresh = config.refresh
113 if not database.cachedRecently(cachename):
114 load_old_cache = False
116 values = method(self.auth, *params)
118 print "Call %s FAILED: Using old cached data" % cachename
119 load_old_cache = True
122 values = database.dbLoad(cachename)
124 database.dbDump(cachename, values)
128 values = database.dbLoad(cachename)
131 return method(self.auth, *params)
133 return run_or_returncached
137 return xmlrpclib.Server(url, verbose=False, allow_none=True)
139 def getNodeAPI(session):
140 nodeauth = Auth(session=session)
141 return PLC(nodeauth.auth, auth.server)
144 return PLC(auth.auth, auth.server)
146 def getCachedAuthAPI():
147 return CachedPLC(auth.auth, auth.server)
149 def getSessionAPI(session, server):
150 nodeauth = Auth(session=session)
151 return PLC(nodeauth.auth, server)
152 def getUserAPI(username, password, server):
153 auth = Auth(username,password)
154 return PLC(auth.auth, server)
156 def getTechEmails(loginbase):
158 For the given site, return all user email addresses that have the 'tech' role.
162 s = api.GetSites(loginbase)[0]
164 p = api.GetPersons(s['person_ids'])
165 # pull out those with the right role.
166 emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
169 def getPIEmails(loginbase):
171 For the given site, return all user email addresses that have the 'tech' role.
175 s = api.GetSites(loginbase)[0]
177 p = api.GetPersons(s['person_ids'])
178 # pull out those with the right role.
179 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
182 def getSliceUserEmails(loginbase):
184 For the given site, return all user email addresses that have the 'tech' role.
188 s = api.GetSites(loginbase)[0]
190 slices = api.GetSlices(s['slice_ids'])
193 people += api.GetPersons(slice['person_ids'])
194 # pull out those with the right role.
195 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
196 unique_emails = [ x for x in set(emails) ]
200 Returns list of nodes in dbg as reported by PLC
204 api = xmlrpclib.Server(auth.server, verbose=False)
205 anon = {'AuthMethod': "anonymous"}
206 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
207 dbgNodes.append(node['hostname'])
208 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
213 Returns loginbase for given nodename
215 def siteId(nodename):
216 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
217 site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
218 if len(site_id) == 1:
219 loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
220 return loginbase[0]['login_base']
222 print "Not nodes returned!!!!"
225 Returns list of slices for a site.
227 def slices(loginbase):
229 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
230 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
231 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
232 siteslices.append(slice['name'])
236 Returns dict of PCU info of a given node.
238 def getpcu(nodename):
239 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
240 anon = {'AuthMethod': "anonymous"}
241 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
242 if nodeinfo['pcu_ids']:
244 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
246 print nodeinfo["ports"]
247 sitepcu[nodename] = nodeinfo["ports"][0]
250 logger.info("%s doesn't have PCU" % nodename)
253 def GetPCUs(filter=None, fields=None):
254 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
255 pcu_list = api.GetPCUs(auth.auth, filter, fields)
259 Returns all site nodes for site id (loginbase).
261 def getSiteNodes(loginbase, fields=None):
262 api = xmlrpclib.Server(auth.server, verbose=False)
264 anon = {'AuthMethod': "anonymous"}
266 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
267 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
268 nodelist.append(node['hostname'])
269 except Exception, exc:
270 logger.info("getSiteNodes: %s" % exc)
271 print "getSiteNodes: %s" % exc
275 def getPersons(filter=None, fields=None):
276 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
279 persons = api.GetPersons(auth.auth, filter, fields)
280 except Exception, exc:
281 print "getPersons: %s" % exc
282 logger.info("getPersons: %s" % exc)
285 def getSites(filter=None, fields=None):
286 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
288 anon = {'AuthMethod': "anonymous"}
290 #sites = api.GetSites(anon, filter, fields)
291 sites = api.GetSites(auth.auth, filter, fields)
292 except Exception, exc:
293 traceback.print_exc()
294 print "getSites: %s" % exc
295 logger.info("getSites: %s" % exc)
298 def getSiteNodes2(loginbase):
299 api = xmlrpclib.Server(auth.server, verbose=False)
301 anon = {'AuthMethod': "anonymous"}
303 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
304 nodelist += getNodes({'node_id':nodeids})
305 except Exception, exc:
306 logger.info("getSiteNodes2: %s" % exc)
309 def getNodeNetworks(filter=None):
310 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
311 nodenetworks = api.GetInterfaces(auth.auth, filter, None)
314 def getNodes(filter=None, fields=None):
315 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
316 nodes = api.GetNodes(auth.auth, filter, fields)
317 #['boot_state', 'hostname',
318 #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
319 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
323 Sets boot state of a node.
325 def nodeBootState(nodename, state):
326 api = xmlrpclib.Server(auth.server, verbose=False)
328 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
329 except Exception, exc:
330 logger.info("nodeBootState: %s" % exc)
332 def updateNodeKey(nodename, key):
333 api = xmlrpclib.Server(auth.server, verbose=False)
335 return api.UpdateNode(auth.auth, nodename, {'key': key})
336 except Exception, exc:
337 logger.info("updateNodeKey: %s" % exc)
340 Sends Ping Of Death to node.
342 def nodePOD(nodename):
343 api = xmlrpclib.Server(auth.server, verbose=False)
344 logger.info("Sending POD to %s" % nodename)
347 return api.RebootNode(auth.auth, nodename)
348 except Exception, exc:
349 logger.info("nodePOD: %s" % exc)
352 Freeze all site slices.
354 def suspendSiteSlices(loginbase):
355 api = xmlrpclib.Server(auth.server, verbose=False)
356 for slice in slices(loginbase):
357 logger.info("Suspending slice %s" % slice)
360 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
361 except Exception, exc:
362 logger.info("suspendSlices: %s" % exc)
365 Freeze all site slices.
367 def suspendSlices(nodename):
368 api = xmlrpclib.Server(auth.server, verbose=False)
369 for slice in slices(siteId(nodename)):
370 logger.info("Suspending slice %s" % slice)
373 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
374 except Exception, exc:
375 logger.info("suspendSlices: %s" % exc)
377 def enableSiteSlices(loginbase):
378 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
379 for slice in slices(loginbase):
380 logger.info("Enabling slices %s" % slice)
383 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
384 if len(slice_list) == 0:
386 slice_id = slice_list[0]['slice_id']
387 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
389 if "enabled" == attr['name'] and attr['value'] == "0":
390 logger.info("Deleted enable=0 attribute from slice %s" % slice)
391 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
392 except Exception, exc:
393 logger.info("enableSiteSlices: %s" % exc)
394 print "exception: %s" % exc
396 def enableSlices(nodename):
397 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
398 for slice in slices(siteId(nodename)):
399 logger.info("Enabling slices %s" % slice)
402 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
403 if len(slice_list) == 0:
405 slice_id = slice_list[0]['slice_id']
406 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
408 if "enabled" == attr['name'] and attr['value'] == "0":
409 logger.info("Deleted enable=0 attribute from slice %s" % slice)
410 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
411 except Exception, exc:
412 logger.info("enableSlices: %s" % exc)
413 print "exception: %s" % exc
415 #I'm commenting this because this really should be a manual process.
417 #Enable suspended site slices.
419 #def enableSlices(nodename, slicelist):
420 # api = xmlrpclib.Server(auth.server, verbose=False)
421 # for slice in slices(siteId(nodename)):
422 # logger.info("Suspending slice %s" % slice)
423 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
425 def enableSiteSliceCreation(loginbase):
426 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
428 logger.info("Enabling slice creation for site %s" % loginbase)
430 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
431 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
432 except Exception, exc:
433 print "ERROR: enableSiteSliceCreation: %s" % exc
434 logger.info("ERROR: enableSiteSliceCreation: %s" % exc)
436 def enableSliceCreation(nodename):
437 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
439 loginbase = siteId(nodename)
440 logger.info("Enabling slice creation for site %s" % loginbase)
442 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
443 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
444 except Exception, exc:
445 print "ERROR: enableSliceCreation: %s" % exc
446 logger.info("ERROR: enableSliceCreation: %s" % exc)
449 Removes site's ability to create slices. Returns previous max_slices
451 def removeSiteSliceCreation(sitename):
452 print "removeSiteSliceCreation(%s)" % sitename
453 api = xmlrpclib.Server(auth.server, verbose=False)
455 logger.info("Removing slice creation for site %s" % sitename)
457 api.UpdateSite(auth.auth, sitename, {'enabled': False})
458 except Exception, exc:
459 logger.info("removeSiteSliceCreation: %s" % exc)
462 Removes ability to create slices. Returns previous max_slices
464 def removeSliceCreation(nodename):
465 print "removeSliceCreation(%s)" % nodename
466 api = xmlrpclib.Server(auth.server, verbose=False)
468 loginbase = siteId(nodename)
469 #numslices = api.GetSites(auth.auth, {"login_base": loginbase},
470 # ["max_slices"])[0]['max_slices']
471 logger.info("Removing slice creation for site %s" % loginbase)
473 #api.UpdateSite(auth.auth, loginbase, {'max_slices': 0})
474 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
475 except Exception, exc:
476 logger.info("removeSliceCreation: %s" % exc)
481 #def enableSliceCreation(nodename, maxslices):
482 # api = xmlrpclib.Server(auth.server, verbose=False)
483 # anon = {'AuthMethod': "anonymous"}
484 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
485 # if len(siteid) == 1:
486 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
489 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
490 # except Exception, exc:
491 # logger.info("API: %s" % exc)
493 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
496 logger.setLevel(logging.DEBUG)
497 ch = logging.StreamHandler()
498 ch.setLevel(logging.DEBUG)
499 formatter = logging.Formatter('logger - %(message)s')
500 ch.setFormatter(formatter)
501 logger.addHandler(ch)
502 #print getpcu("kupl2.ittc.ku.edu")
503 #print getpcu("planetlab1.cse.msu.edu")
504 #print getpcu("alice.cs.princeton.edu")
506 #nodeBootState("alice.cs.princeton.edu", "boot")
507 #freezeSite("alice.cs.princeton.edu")
508 print removeSliceCreation("alice.cs.princeton.edu")
509 #enableSliceCreation("alice.cs.princeton.edu", 1024)
510 #print getSiteNodes("princeton")
511 #print siteId("alice.cs.princeton.edu")
512 #print nodePOD("alice.cs.princeton.edu")
513 #print slices("princeton")
515 if __name__=="__main__":