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 $
16 # note: this needs to be consistent with the value in PLEWWW/planetlab/includes/plc_functions.php
17 PENDING_CONSORTIUM_ID = 0
19 #APPROVED_CONSORTIUM_ID = 999999
22 from monitor import config
24 XMLRPC_SERVER=config.API_SERVER
27 # NOTE: this host is used by default when there are no auth files.
28 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
30 logger = logging.getLogger("monitor")
33 def __init__(self, username=None, password=None, **kwargs):
34 if 'session' in kwargs:
35 self.auth= { 'AuthMethod' : 'session',
36 'session' : kwargs['session'] }
38 if username==None and password==None:
39 self.auth = {'AuthMethod': "anonymous"}
41 self.auth = {'Username' : username,
42 'AuthMethod' : 'password',
43 'AuthString' : password}
46 # NOTE: by default, use anonymous access, but if auth files are
47 # configured, use them, with their auth definitions.
50 from monitor import config
51 auth.auth = {'Username' : config.API_AUTH_USER,
52 'AuthMethod' : 'password',
53 'AuthString' : config.API_AUTH_PASSWORD}
54 auth.server = config.API_SERVER
58 auth.server = auth.plc
61 auth.server = XMLRPC_SERVER
63 global_error_count = 0
66 def __init__(self, auth, url):
69 self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
71 def __getattr__(self, name):
72 method = getattr(self.api, name)
74 raise AssertionError("method does not exist")
77 return lambda *params : method(self.auth, *params)
78 except xmlrpclib.ProtocolError:
80 global_error_count += 1
81 if global_error_count >= 10:
82 print "maximum error count exceeded; exiting..."
85 print "%s errors have occurred" % global_error_count
86 raise Exception("ProtocolError continuing")
89 return self.api.__repr__()
92 api = PLC(auth.auth, auth.server)
96 return xmlrpclib.Server(url, verbose=False, allow_none=True)
98 def getNodeAPI(session):
99 nodeauth = Auth(session=session)
100 return PLC(nodeauth.auth, auth.server)
102 def getAuthAPI(url=None):
104 return PLC(auth.auth, url)
106 return PLC(auth.auth, auth.server)
108 def getCachedAuthAPI():
109 return CachedPLC(auth.auth, auth.server)
111 def getSessionAPI(session, server):
112 nodeauth = Auth(session=session)
113 return PLC(nodeauth.auth, server)
114 def getUserAPI(username, password, server):
115 auth = Auth(username,password)
116 return PLC(auth.auth, server)
118 def getTechEmails(loginbase):
120 For the given site, return all user email addresses that have the 'tech' role.
124 s = api.GetSites(loginbase)[0]
126 p = api.GetPersons(s['person_ids'])
127 # pull out those with the right role.
128 emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
131 def getPIEmails(loginbase):
133 For the given site, return all user email addresses that have the 'tech' role.
137 s = api.GetSites(loginbase)[0]
139 p = api.GetPersons(s['person_ids'])
140 # pull out those with the right role.
141 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
144 def getSliceUserEmails(loginbase):
146 For the given site, return all user email addresses that have the 'tech' role.
150 s = api.GetSites(loginbase)[0]
152 slices = api.GetSlices(s['slice_ids'])
155 people += api.GetPersons(slice['person_ids'])
156 # pull out those with the right role.
157 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
158 unique_emails = [ x for x in set(emails) ]
162 Returns list of nodes in dbg as reported by PLC
166 api = xmlrpclib.Server(auth.server, verbose=False)
167 anon = {'AuthMethod': "anonymous"}
168 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
169 dbgNodes.append(node['hostname'])
170 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
175 Returns loginbase for given nodename
177 def siteId(nodename):
178 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
179 site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
180 if len(site_id) == 1:
181 loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
182 return loginbase[0]['login_base']
184 print "Not nodes returned!!!!"
187 Returns list of slices for a site.
189 def slices(loginbase):
191 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
192 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
193 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
194 siteslices.append(slice['name'])
198 Returns dict of PCU info of a given node.
200 def getpcu(nodename):
201 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
202 anon = {'AuthMethod': "anonymous"}
204 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
206 logger.info("Can not find node: %s" % nodename)
208 if nodeinfo['pcu_ids']:
210 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
212 print nodeinfo["ports"]
213 sitepcu[nodename] = nodeinfo["ports"][0]
216 logger.info("%s doesn't have PCU" % nodename)
219 def GetPCUs(filter=None, fields=None):
220 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
221 pcu_list = api.GetPCUs(auth.auth, filter, fields)
225 Returns all site nodes for site id (loginbase).
227 def getSiteNodes(loginbase, fields=None):
228 api = xmlrpclib.Server(auth.server, verbose=False)
230 anon = {'AuthMethod': "anonymous"}
232 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
233 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
234 nodelist.append(node['hostname'])
235 except Exception, exc:
236 logger.info("getSiteNodes: %s" % exc)
237 print "getSiteNodes: %s" % exc
241 def getPersons(filter=None, fields=None):
242 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
245 persons = api.GetPersons(auth.auth, filter, fields)
246 except Exception, exc:
247 print "getPersons: %s" % exc
248 logger.info("getPersons: %s" % exc)
251 def getSites(filter=None, fields=None):
252 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
254 anon = {'AuthMethod': "anonymous"}
256 #sites = api.GetSites(anon, filter, fields)
257 sites = api.GetSites(auth.auth, filter, fields)
258 except Exception, exc:
259 traceback.print_exc()
260 print "getSites: %s" % exc
261 logger.info("getSites: %s" % exc)
264 def getSiteNodes2(loginbase):
265 api = xmlrpclib.Server(auth.server, verbose=False)
267 anon = {'AuthMethod': "anonymous"}
269 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
270 nodelist += getNodes({'node_id':nodeids})
271 except Exception, exc:
272 logger.info("getSiteNodes2: %s" % exc)
275 def getNodeNetworks(filter=None):
276 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
277 nodenetworks = api.GetInterfaces(auth.auth, filter, None)
280 def getNodes(filter=None, fields=None):
281 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
282 nodes = api.GetNodes(auth.auth, filter, fields)
283 #['boot_state', 'hostname',
284 #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
285 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
289 # Check if the site is a pending site that needs to be approved.
290 def isPendingSite(loginbase):
291 api = xmlrpclib.Server(auth.server, verbose=False)
293 site = api.GetSites(auth.auth, loginbase)[0]
294 except Exception, exc:
295 logger.info("ERROR: No site %s" % loginbase)
298 if not site['enabled'] and site['ext_consortium_id'] == PENDING_CONSORTIUM_ID:
305 Sets boot state of a node.
307 def nodeBootState(nodename, state):
308 api = xmlrpclib.Server(auth.server, verbose=False)
310 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
311 except Exception, exc:
312 logger.info("nodeBootState: %s" % exc)
314 def updateNodeKey(nodename, key):
315 api = xmlrpclib.Server(auth.server, verbose=False)
317 return api.UpdateNode(auth.auth, nodename, {'key': key})
318 except Exception, exc:
319 logger.info("updateNodeKey: %s" % exc)
322 Sends Ping Of Death to node.
324 def nodePOD(nodename):
325 api = xmlrpclib.Server(auth.server, verbose=False)
326 logger.info("Sending POD to %s" % nodename)
329 return api.RebootNode(auth.auth, nodename)
330 except Exception, exc:
331 logger.info("nodePOD: %s" % exc)
334 Freeze all site slices.
336 def suspendSiteSlices(loginbase):
337 if isPendingSite(loginbase):
338 msg = "INFO: suspendSiteSlices: Pending Site (%s)" % loginbase
343 api = xmlrpclib.Server(auth.server, verbose=False)
344 for slice in slices(loginbase):
345 logger.info("Suspending slice %s" % slice)
348 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
349 except Exception, exc:
350 logger.info("suspendSlices: %s" % exc)
353 Freeze all site slices.
355 def suspendSlices(nodename):
356 loginbase = siteId(nodename)
357 suspendSiteSlices(loginbase)
360 def enableSiteSlices(loginbase):
361 if isPendingSite(loginbase):
362 msg = "INFO: enableSiteSlices: Pending Site (%s)" % loginbase
367 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
368 for slice in slices(loginbase):
369 logger.info("Enabling slices %s" % slice)
372 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
373 if len(slice_list) == 0:
375 slice_id = slice_list[0]['slice_id']
376 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
378 if "enabled" == attr['name'] and attr['value'] == "0":
379 logger.info("Deleted enable=0 attribute from slice %s" % slice)
380 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
381 except Exception, exc:
382 logger.info("enableSiteSlices: %s" % exc)
383 print "exception: %s" % exc
385 def enableSlices(nodename):
386 loginbase = siteId(nodename)
387 enableSiteSlices(loginbase)
390 #I'm commenting this because this really should be a manual process.
392 #Enable suspended site slices.
394 #def enableSlices(nodename, slicelist):
395 # api = xmlrpclib.Server(auth.server, verbose=False)
396 # for slice in slices(siteId(nodename)):
397 # logger.info("Suspending slice %s" % slice)
398 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
400 def enableSiteSliceCreation(loginbase):
401 if isPendingSite(loginbase):
402 msg = "INFO: enableSiteSliceCreation: Pending Site (%s)" % loginbase
407 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
409 logger.info("Enabling slice creation for site %s" % loginbase)
411 site = api.GetSites(auth.auth, loginbase)[0]
412 if site['enabled'] == False:
413 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
414 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
415 except Exception, exc:
416 print "ERROR: enableSiteSliceCreation: %s" % exc
417 logger.info("ERROR: enableSiteSliceCreation: %s" % exc)
419 def enableSliceCreation(nodename):
420 loginbase = siteId(nodename)
421 enableSiteSliceCreation(loginbase)
424 Removes site's ability to create slices. Returns previous max_slices
426 def removeSiteSliceCreation(loginbase):
427 print "removeSiteSliceCreation(%s)" % loginbase
429 if isPendingSite(loginbase):
430 msg = "INFO: removeSiteSliceCreation: Pending Site (%s)" % loginbase
435 api = xmlrpclib.Server(auth.server, verbose=False)
437 logger.info("Removing slice creation for site %s" % loginbase)
439 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
440 except Exception, exc:
441 logger.info("removeSiteSliceCreation: %s" % exc)
444 Removes ability to create slices. Returns previous max_slices
446 def removeSliceCreation(nodename):
447 loginbase = siteId(nodename)
448 removeSiteSliceCreation(loginbase)
454 #def enableSliceCreation(nodename, maxslices):
455 # api = xmlrpclib.Server(auth.server, verbose=False)
456 # anon = {'AuthMethod': "anonymous"}
457 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
458 # if len(siteid) == 1:
459 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
462 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
463 # except Exception, exc:
464 # logger.info("API: %s" % exc)
466 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
469 logger.setLevel(logging.DEBUG)
470 ch = logging.StreamHandler()
471 ch.setLevel(logging.DEBUG)
472 formatter = logging.Formatter('logger - %(message)s')
473 ch.setFormatter(formatter)
474 logger.addHandler(ch)
475 #print getpcu("kupl2.ittc.ku.edu")
476 #print getpcu("planetlab1.cse.msu.edu")
477 #print getpcu("alice.cs.princeton.edu")
479 #nodeBootState("alice.cs.princeton.edu", "boot")
480 #freezeSite("alice.cs.princeton.edu")
481 print removeSliceCreation("alice.cs.princeton.edu")
482 #enableSliceCreation("alice.cs.princeton.edu", 1024)
483 #print getSiteNodes("princeton")
484 #print siteId("alice.cs.princeton.edu")
485 #print nodePOD("alice.cs.princeton.edu")
486 #print slices("princeton")
488 if __name__=="__main__":