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"}
252 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
254 logger.info("Can not find node: %s" % nodename)
256 if nodeinfo['pcu_ids']:
258 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
260 print nodeinfo["ports"]
261 sitepcu[nodename] = nodeinfo["ports"][0]
264 logger.info("%s doesn't have PCU" % nodename)
267 def GetPCUs(filter=None, fields=None):
268 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
269 pcu_list = api.GetPCUs(auth.auth, filter, fields)
273 Returns all site nodes for site id (loginbase).
275 def getSiteNodes(loginbase, fields=None):
276 api = xmlrpclib.Server(auth.server, verbose=False)
278 anon = {'AuthMethod': "anonymous"}
280 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
281 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
282 nodelist.append(node['hostname'])
283 except Exception, exc:
284 logger.info("getSiteNodes: %s" % exc)
285 print "getSiteNodes: %s" % exc
289 def getPersons(filter=None, fields=None):
290 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
293 persons = api.GetPersons(auth.auth, filter, fields)
294 except Exception, exc:
295 print "getPersons: %s" % exc
296 logger.info("getPersons: %s" % exc)
299 def getSites(filter=None, fields=None):
300 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
302 anon = {'AuthMethod': "anonymous"}
304 #sites = api.GetSites(anon, filter, fields)
305 sites = api.GetSites(auth.auth, filter, fields)
306 except Exception, exc:
307 traceback.print_exc()
308 print "getSites: %s" % exc
309 logger.info("getSites: %s" % exc)
312 def getSiteNodes2(loginbase):
313 api = xmlrpclib.Server(auth.server, verbose=False)
315 anon = {'AuthMethod': "anonymous"}
317 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
318 nodelist += getNodes({'node_id':nodeids})
319 except Exception, exc:
320 logger.info("getSiteNodes2: %s" % exc)
323 def getNodeNetworks(filter=None):
324 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
325 nodenetworks = api.GetInterfaces(auth.auth, filter, None)
328 def getNodes(filter=None, fields=None):
329 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
330 nodes = api.GetNodes(auth.auth, filter, fields)
331 #['boot_state', 'hostname',
332 #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
333 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
337 # Check if the site is a pending site that needs to be approved.
338 def isPendingSite(loginbase):
339 api = xmlrpclib.Server(auth.server, verbose=False)
341 site = api.GetSites(auth.auth, loginbase)[0]
342 except Exception, exc:
343 logger.info("ERROR: No site %s" % loginbase)
346 if not site['enabled'] and site['ext_consortium_id'] == PENDING_CONSORTIUM_ID:
353 Sets boot state of a node.
355 def nodeBootState(nodename, state):
356 api = xmlrpclib.Server(auth.server, verbose=False)
358 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
359 except Exception, exc:
360 logger.info("nodeBootState: %s" % exc)
362 def updateNodeKey(nodename, key):
363 api = xmlrpclib.Server(auth.server, verbose=False)
365 return api.UpdateNode(auth.auth, nodename, {'key': key})
366 except Exception, exc:
367 logger.info("updateNodeKey: %s" % exc)
370 Sends Ping Of Death to node.
372 def nodePOD(nodename):
373 api = xmlrpclib.Server(auth.server, verbose=False)
374 logger.info("Sending POD to %s" % nodename)
377 return api.RebootNode(auth.auth, nodename)
378 except Exception, exc:
379 logger.info("nodePOD: %s" % exc)
382 Freeze all site slices.
384 def suspendSiteSlices(loginbase):
385 if isPendingSite(loginbase):
386 msg = "INFO: suspendSiteSlices: Pending Site (%s)" % loginbase
391 api = xmlrpclib.Server(auth.server, verbose=False)
392 for slice in slices(loginbase):
393 logger.info("Suspending slice %s" % slice)
396 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
397 except Exception, exc:
398 logger.info("suspendSlices: %s" % exc)
401 Freeze all site slices.
403 def suspendSlices(nodename):
404 loginbase = siteId(nodename)
405 suspendSiteSlices(loginbase)
408 def enableSiteSlices(loginbase):
409 if isPendingSite(loginbase):
410 msg = "INFO: enableSiteSlices: Pending Site (%s)" % loginbase
415 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
416 for slice in slices(loginbase):
417 logger.info("Enabling slices %s" % slice)
420 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
421 if len(slice_list) == 0:
423 slice_id = slice_list[0]['slice_id']
424 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
426 if "enabled" == attr['name'] and attr['value'] == "0":
427 logger.info("Deleted enable=0 attribute from slice %s" % slice)
428 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
429 except Exception, exc:
430 logger.info("enableSiteSlices: %s" % exc)
431 print "exception: %s" % exc
433 def enableSlices(nodename):
434 loginbase = siteId(nodename)
435 enableSiteSlices(loginbase)
438 #I'm commenting this because this really should be a manual process.
440 #Enable suspended site slices.
442 #def enableSlices(nodename, slicelist):
443 # api = xmlrpclib.Server(auth.server, verbose=False)
444 # for slice in slices(siteId(nodename)):
445 # logger.info("Suspending slice %s" % slice)
446 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
448 def enableSiteSliceCreation(loginbase):
449 if isPendingSite(loginbase):
450 msg = "INFO: enableSiteSliceCreation: Pending Site (%s)" % loginbase
455 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
457 logger.info("Enabling slice creation for site %s" % loginbase)
459 site = api.GetSites(auth.auth, loginbase)[0]
460 if site['enabled'] == False:
461 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
462 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
463 except Exception, exc:
464 print "ERROR: enableSiteSliceCreation: %s" % exc
465 logger.info("ERROR: enableSiteSliceCreation: %s" % exc)
467 def enableSliceCreation(nodename):
468 loginbase = siteId(nodename)
469 enableSiteSliceCreation(loginbase)
472 Removes site's ability to create slices. Returns previous max_slices
474 def removeSiteSliceCreation(loginbase):
475 print "removeSiteSliceCreation(%s)" % loginbase
477 if isPendingSite(loginbase):
478 msg = "INFO: removeSiteSliceCreation: Pending Site (%s)" % loginbase
483 api = xmlrpclib.Server(auth.server, verbose=False)
485 logger.info("Removing slice creation for site %s" % loginbase)
487 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
488 except Exception, exc:
489 logger.info("removeSiteSliceCreation: %s" % exc)
492 Removes ability to create slices. Returns previous max_slices
494 def removeSliceCreation(nodename):
495 loginbase = siteId(nodename)
496 removeSiteSliceCreation(loginbase)
502 #def enableSliceCreation(nodename, maxslices):
503 # api = xmlrpclib.Server(auth.server, verbose=False)
504 # anon = {'AuthMethod': "anonymous"}
505 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
506 # if len(siteid) == 1:
507 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
510 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
511 # except Exception, exc:
512 # logger.info("API: %s" % exc)
514 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
517 logger.setLevel(logging.DEBUG)
518 ch = logging.StreamHandler()
519 ch.setLevel(logging.DEBUG)
520 formatter = logging.Formatter('logger - %(message)s')
521 ch.setFormatter(formatter)
522 logger.addHandler(ch)
523 #print getpcu("kupl2.ittc.ku.edu")
524 #print getpcu("planetlab1.cse.msu.edu")
525 #print getpcu("alice.cs.princeton.edu")
527 #nodeBootState("alice.cs.princeton.edu", "boot")
528 #freezeSite("alice.cs.princeton.edu")
529 print removeSliceCreation("alice.cs.princeton.edu")
530 #enableSliceCreation("alice.cs.princeton.edu", 1024)
531 #print getSiteNodes("princeton")
532 #print siteId("alice.cs.princeton.edu")
533 #print nodePOD("alice.cs.princeton.edu")
534 #print slices("princeton")
536 if __name__=="__main__":