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 $
20 logger = logging.getLogger("monitor")
24 self.auth = {'AuthMethod': "anonymous"}
26 # NOTE: this host is used by default when there are no auth files.
27 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
29 # NOTE: by default, use anonymous access, but if auth files are
30 # configured, use them, with their auth definitions.
33 from monitor import config
34 auth.auth = {'Username' : config.API_AUTH_USER,
35 'AuthMethod' : 'password',
36 'AuthString' : config.API_AUTH_PASSWORD}
37 auth.server = config.API_SERVER
41 auth.server = auth.plc
44 auth.server = XMLRPC_SERVER
46 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
48 global_error_count = 0
51 def __init__(self, auth, url):
54 self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
56 def __getattr__(self, name):
57 method = getattr(self.api, name)
59 raise AssertionError("method does not exist")
62 return lambda *params : method(self.auth, *params)
65 global_error_count += 1
66 if global_error_count >= 10:
67 print "maximum error count exceeded; exiting..."
70 print "%s errors have occurred" % global_error_count
71 raise Exception("ProtocolError continuing")
74 return self.api.__repr__()
77 return xmlrpclib.Server(url, verbose=False, allow_none=True)
80 return PLC(auth.auth, auth.server)
83 def getTechEmails(loginbase):
85 For the given site, return all user email addresses that have the 'tech' role.
89 s = api.GetSites(loginbase)[0]
91 p = api.GetPersons(s['person_ids'])
92 # pull out those with the right role.
93 emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
96 def getPIEmails(loginbase):
98 For the given site, return all user email addresses that have the 'tech' role.
102 s = api.GetSites(loginbase)[0]
104 p = api.GetPersons(s['person_ids'])
105 # pull out those with the right role.
106 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
109 def getSliceUserEmails(loginbase):
111 For the given site, return all user email addresses that have the 'tech' role.
115 s = api.GetSites(loginbase)[0]
117 slices = api.GetSlices(s['slice_ids'])
120 people += api.GetPersons(slice['person_ids'])
121 # pull out those with the right role.
122 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
123 unique_emails = [ x for x in set(emails) ]
127 Returns list of nodes in dbg as reported by PLC
131 api = xmlrpclib.Server(auth.server, verbose=False)
132 anon = {'AuthMethod': "anonymous"}
133 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
134 dbgNodes.append(node['hostname'])
135 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
140 Returns loginbase for given nodename
142 def siteId(nodename):
143 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
144 site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
145 if len(site_id) == 1:
146 loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
147 return loginbase[0]['login_base']
149 print "Not nodes returned!!!!"
152 Returns list of slices for a site.
154 def slices(loginbase):
156 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
157 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
158 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
159 siteslices.append(slice['name'])
163 Returns dict of PCU info of a given node.
165 def getpcu(nodename):
166 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
167 anon = {'AuthMethod': "anonymous"}
168 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
169 if nodeinfo['pcu_ids']:
170 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
171 sitepcu[nodename] = nodeinfo["ports"][0]
174 logger.info("%s doesn't have PCU" % nodename)
177 def GetPCUs(filter=None, fields=None):
178 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
179 pcu_list = api.GetPCUs(auth.auth, filter, fields)
183 Returns all site nodes for site id (loginbase).
185 def getSiteNodes(loginbase, fields=None):
186 api = xmlrpclib.Server(auth.server, verbose=False)
188 anon = {'AuthMethod': "anonymous"}
190 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
191 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
192 nodelist.append(node['hostname'])
193 except Exception, exc:
194 logger.info("getSiteNodes: %s" % exc)
195 print "getSiteNodes: %s" % exc
199 def getPersons(filter=None, fields=None):
200 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
203 persons = api.GetPersons(auth.auth, filter, fields)
204 except Exception, exc:
205 print "getPersons: %s" % exc
206 logger.info("getPersons: %s" % exc)
209 def getSites(filter=None, fields=None):
210 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
212 anon = {'AuthMethod': "anonymous"}
214 #sites = api.GetSites(anon, filter, fields)
215 sites = api.GetSites(auth.auth, filter, fields)
216 except Exception, exc:
217 traceback.print_exc()
218 print "getSites: %s" % exc
219 logger.info("getSites: %s" % exc)
222 def getSiteNodes2(loginbase):
223 api = xmlrpclib.Server(auth.server, verbose=False)
225 anon = {'AuthMethod': "anonymous"}
227 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
228 nodelist += getNodes({'node_id':nodeids})
229 except Exception, exc:
230 logger.info("getSiteNodes2: %s" % exc)
233 def getNodeNetworks(filter=None):
234 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
235 nodenetworks = api.GetInterfaces(auth.auth, filter, None)
238 def getNodes(filter=None, fields=None):
239 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
240 nodes = api.GetNodes(auth.auth, filter, fields)
241 #['boot_state', 'hostname',
242 #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
243 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
247 Sets boot state of a node.
249 def nodeBootState(nodename, state):
250 api = xmlrpclib.Server(auth.server, verbose=False)
252 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
253 except Exception, exc:
254 logger.info("nodeBootState: %s" % exc)
256 def updateNodeKey(nodename, key):
257 api = xmlrpclib.Server(auth.server, verbose=False)
259 return api.UpdateNode(auth.auth, nodename, {'key': key})
260 except Exception, exc:
261 logger.info("updateNodeKey: %s" % exc)
264 Sends Ping Of Death to node.
266 def nodePOD(nodename):
267 api = xmlrpclib.Server(auth.server, verbose=False)
268 logger.info("Sending POD to %s" % nodename)
271 return api.RebootNode(auth.auth, nodename)
272 except Exception, exc:
273 logger.info("nodePOD: %s" % exc)
276 Freeze all site slices.
278 def suspendSlices(nodename):
279 api = xmlrpclib.Server(auth.server, verbose=False)
280 for slice in slices(siteId(nodename)):
281 logger.info("Suspending slice %s" % slice)
284 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
285 except Exception, exc:
286 logger.info("suspendSlices: %s" % exc)
288 def enableSlices(nodename):
289 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
290 for slice in slices(siteId(nodename)):
291 logger.info("Enabling slices %s" % slice)
294 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
295 if len(slice_list) == 0:
297 slice_id = slice_list[0]['slice_id']
298 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
300 if "enabled" == attr['name'] and attr['value'] == "0":
301 logger.info("Deleted enable=0 attribute from slice %s" % slice)
302 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
303 except Exception, exc:
304 logger.info("enableSlices: %s" % exc)
305 print "exception: %s" % exc
307 #I'm commenting this because this really should be a manual process.
309 #Enable suspended site slices.
311 #def enableSlices(nodename, slicelist):
312 # api = xmlrpclib.Server(auth.server, verbose=False)
313 # for slice in slices(siteId(nodename)):
314 # logger.info("Suspending slice %s" % slice)
315 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
317 def enableSliceCreation(nodename):
318 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
320 loginbase = siteId(nodename)
321 logger.info("Enabling slice creation for site %s" % loginbase)
323 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
324 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
325 except Exception, exc:
326 print "ERROR: enableSliceCreation: %s" % exc
327 logger.info("ERROR: enableSliceCreation: %s" % exc)
330 Removes ability to create slices. Returns previous max_slices
332 def removeSliceCreation(nodename):
333 print "removeSliceCreation(%s)" % nodename
334 api = xmlrpclib.Server(auth.server, verbose=False)
336 loginbase = siteId(nodename)
337 #numslices = api.GetSites(auth.auth, {"login_base": loginbase},
338 # ["max_slices"])[0]['max_slices']
339 logger.info("Removing slice creation for site %s" % loginbase)
341 #api.UpdateSite(auth.auth, loginbase, {'max_slices': 0})
342 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
343 except Exception, exc:
344 logger.info("removeSliceCreation: %s" % exc)
349 #def enableSliceCreation(nodename, maxslices):
350 # api = xmlrpclib.Server(auth.server, verbose=False)
351 # anon = {'AuthMethod': "anonymous"}
352 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
353 # if len(siteid) == 1:
354 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
357 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
358 # except Exception, exc:
359 # logger.info("API: %s" % exc)
361 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
364 logger.setLevel(logging.DEBUG)
365 ch = logging.StreamHandler()
366 ch.setLevel(logging.DEBUG)
367 formatter = logging.Formatter('logger - %(message)s')
368 ch.setFormatter(formatter)
369 logger.addHandler(ch)
370 #print getpcu("kupl2.ittc.ku.edu")
371 #print getpcu("planetlab1.cse.msu.edu")
372 #print getpcu("alice.cs.princeton.edu")
374 #nodeBootState("alice.cs.princeton.edu", "boot")
375 #freezeSite("alice.cs.princeton.edu")
376 print removeSliceCreation("alice.cs.princeton.edu")
377 #enableSliceCreation("alice.cs.princeton.edu", 1024)
378 #print getSiteNodes("princeton")
379 #print siteId("alice.cs.princeton.edu")
380 #print nodePOD("alice.cs.princeton.edu")
381 #print slices("princeton")
383 if __name__=="__main__":