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)
49 def __init__(self, auth, url):
52 self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
54 def __getattr__(self, name):
55 method = getattr(self.api, name)
57 raise AssertionError("method does not exist")
59 return lambda *params : method(self.auth, *params)
62 return self.api.__repr__()
65 return xmlrpclib.Server(url, verbose=False, allow_none=True)
68 return PLC(auth.auth, auth.server)
71 def getTechEmails(loginbase):
73 For the given site, return all user email addresses that have the 'tech' role.
77 s = api.GetSites(loginbase)[0]
79 p = api.GetPersons(s['person_ids'])
80 # pull out those with the right role.
81 emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
84 def getPIEmails(loginbase):
86 For the given site, return all user email addresses that have the 'tech' role.
90 s = api.GetSites(loginbase)[0]
92 p = api.GetPersons(s['person_ids'])
93 # pull out those with the right role.
94 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
97 def getSliceUserEmails(loginbase):
99 For the given site, return all user email addresses that have the 'tech' role.
103 s = api.GetSites(loginbase)[0]
105 slices = api.GetSlices(s['slice_ids'])
108 people += api.GetPersons(slice['person_ids'])
109 # pull out those with the right role.
110 emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
111 unique_emails = [ x for x in set(emails) ]
115 Returns list of nodes in dbg as reported by PLC
119 api = xmlrpclib.Server(auth.server, verbose=False)
120 anon = {'AuthMethod': "anonymous"}
121 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
122 dbgNodes.append(node['hostname'])
123 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
128 Returns loginbase for given nodename
130 def siteId(nodename):
131 api = xmlrpclib.Server(auth.server, verbose=False)
132 anon = {'AuthMethod': "anonymous"}
133 site_id = api.GetNodes (anon, {"hostname": nodename}, ['site_id'])
134 if len(site_id) == 1:
135 loginbase = api.GetSites (anon, site_id[0], ["login_base"])
136 return loginbase[0]['login_base']
139 Returns list of slices for a site.
141 def slices(loginbase):
143 api = xmlrpclib.Server(auth.server, verbose=False)
144 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
145 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
146 siteslices.append(slice['name'])
150 Returns dict of PCU info of a given node.
152 def getpcu(nodename):
153 api = xmlrpclib.Server(auth.server, verbose=False)
154 anon = {'AuthMethod': "anonymous"}
155 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
156 if nodeinfo['pcu_ids']:
157 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
158 sitepcu[nodename] = nodeinfo["ports"][0]
161 logger.info("%s doesn't have PCU" % nodename)
164 def GetPCUs(filter=None, fields=None):
165 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
166 pcu_list = api.GetPCUs(auth.auth, filter, fields)
170 Returns all site nodes for site id (loginbase).
172 def getSiteNodes(loginbase, fields=None):
173 api = xmlrpclib.Server(auth.server, verbose=False)
175 anon = {'AuthMethod': "anonymous"}
177 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
178 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
179 nodelist.append(node['hostname'])
180 except Exception, exc:
181 logger.info("getSiteNodes: %s" % exc)
182 print "getSiteNodes: %s" % exc
186 def getPersons(filter=None, fields=None):
187 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
190 persons = api.GetPersons(auth.auth, filter, fields)
191 except Exception, exc:
192 print "getPersons: %s" % exc
193 logger.info("getPersons: %s" % exc)
196 def getSites(filter=None, fields=None):
197 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
199 anon = {'AuthMethod': "anonymous"}
201 #sites = api.GetSites(anon, filter, fields)
202 sites = api.GetSites(auth.auth, filter, fields)
203 except Exception, exc:
204 traceback.print_exc()
205 print "getSites: %s" % exc
206 logger.info("getSites: %s" % exc)
209 def getSiteNodes2(loginbase):
210 api = xmlrpclib.Server(auth.server, verbose=False)
212 anon = {'AuthMethod': "anonymous"}
214 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
215 nodelist += getNodes({'node_id':nodeids})
216 except Exception, exc:
217 logger.info("getSiteNodes2: %s" % exc)
220 def getNodeNetworks(filter=None):
221 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
222 nodenetworks = api.GetNodeNetworks(auth.auth, filter, None)
225 def getNodes(filter=None, fields=None):
226 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
227 nodes = api.GetNodes(auth.auth, filter, fields)
228 #['boot_state', 'hostname',
229 #'site_id', 'date_created', 'node_id', 'version', 'nodenetwork_ids',
230 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
234 Sets boot state of a node.
236 def nodeBootState(nodename, state):
237 api = xmlrpclib.Server(auth.server, verbose=False)
239 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
240 except Exception, exc:
241 logger.info("nodeBootState: %s" % exc)
243 def updateNodeKey(nodename, key):
244 api = xmlrpclib.Server(auth.server, verbose=False)
246 return api.UpdateNode(auth.auth, nodename, {'key': key})
247 except Exception, exc:
248 logger.info("updateNodeKey: %s" % exc)
251 Sends Ping Of Death to node.
253 def nodePOD(nodename):
254 api = xmlrpclib.Server(auth.server, verbose=False)
255 logger.info("Sending POD to %s" % nodename)
258 return api.RebootNode(auth.auth, nodename)
259 except Exception, exc:
260 logger.info("nodePOD: %s" % exc)
263 Freeze all site slices.
265 def suspendSlices(nodename):
266 api = xmlrpclib.Server(auth.server, verbose=False)
267 for slice in slices(siteId(nodename)):
268 logger.info("Suspending slice %s" % slice)
271 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
272 except Exception, exc:
273 logger.info("suspendSlices: %s" % exc)
275 def enableSlices(nodename):
276 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
277 for slice in slices(siteId(nodename)):
278 logger.info("Enabling slices %s" % slice)
281 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
282 if len(slice_list) == 0:
284 slice_id = slice_list[0]['slice_id']
285 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
287 if "enabled" == attr['name'] and attr['value'] == "0":
288 logger.info("Deleted enable=0 attribute from slice %s" % slice)
289 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
290 except Exception, exc:
291 logger.info("enableSlices: %s" % exc)
292 print "exception: %s" % exc
294 #I'm commenting this because this really should be a manual process.
296 #Enable suspended site slices.
298 #def enableSlices(nodename, slicelist):
299 # api = xmlrpclib.Server(auth.server, verbose=False)
300 # for slice in slices(siteId(nodename)):
301 # logger.info("Suspending slice %s" % slice)
302 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
304 def enableSliceCreation(nodename):
305 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
307 loginbase = siteId(nodename)
308 logger.info("Enabling slice creation for site %s" % loginbase)
310 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
311 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
312 except Exception, exc:
313 print "ERROR: enableSliceCreation: %s" % exc
314 logger.info("ERROR: enableSliceCreation: %s" % exc)
317 Removes ability to create slices. Returns previous max_slices
319 def removeSliceCreation(nodename):
320 print "removeSliceCreation(%s)" % nodename
321 api = xmlrpclib.Server(auth.server, verbose=False)
323 loginbase = siteId(nodename)
324 #numslices = api.GetSites(auth.auth, {"login_base": loginbase},
325 # ["max_slices"])[0]['max_slices']
326 logger.info("Removing slice creation for site %s" % loginbase)
328 #api.UpdateSite(auth.auth, loginbase, {'max_slices': 0})
329 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
330 except Exception, exc:
331 logger.info("removeSliceCreation: %s" % exc)
336 #def enableSliceCreation(nodename, maxslices):
337 # api = xmlrpclib.Server(auth.server, verbose=False)
338 # anon = {'AuthMethod': "anonymous"}
339 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
340 # if len(siteid) == 1:
341 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
344 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
345 # except Exception, exc:
346 # logger.info("API: %s" % exc)
348 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
351 logger.setLevel(logging.DEBUG)
352 ch = logging.StreamHandler()
353 ch.setLevel(logging.DEBUG)
354 formatter = logging.Formatter('logger - %(message)s')
355 ch.setFormatter(formatter)
356 logger.addHandler(ch)
357 #print getpcu("kupl2.ittc.ku.edu")
358 #print getpcu("planetlab1.cse.msu.edu")
359 #print getpcu("alice.cs.princeton.edu")
361 #nodeBootState("alice.cs.princeton.edu", "boot")
362 #freezeSite("alice.cs.princeton.edu")
363 print removeSliceCreation("alice.cs.princeton.edu")
364 #enableSliceCreation("alice.cs.princeton.edu", 1024)
365 #print getSiteNodes("princeton")
366 #print siteId("alice.cs.princeton.edu")
367 #print nodePOD("alice.cs.princeton.edu")
368 #print slices("princeton")
370 if __name__=="__main__":