more convenience functions to create an api interface as a user or node.
[monitor.git] / monitor / wrapper / plc.py
1 #
2 # plc.py
3 #
4 # Helper functions that minipulate the PLC api.
5
6 # Faiyaz Ahmed <faiyaza@cs.princeton.edu
7 #
8 # $Id: plc.py,v 1.18 2007/08/29 17:26:50 soltesz Exp $
9 #
10
11 import xml, xmlrpclib
12 import logging
13 import time
14 import traceback
15 from monitor import database
16
17 try:
18         from monitor import config
19         debug = config.debug
20         XMLRPC_SERVER=config.API_SERVER
21 except:
22         debug = False
23         # NOTE: this host is used by default when there are no auth files.
24         XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
25
26 logger = logging.getLogger("monitor")
27         
28 class Auth:
29         def __init__(self, username=None, password=None, **kwargs):
30                 if 'session' in kwargs:
31                         self.auth= { 'AuthMethod' : 'session',
32                                         'session' : kwargs['session'] }
33                 else:
34                         if username==None and password==None:
35                                 self.auth = {'AuthMethod': "anonymous"}
36                         else:
37                                 self.auth = {'Username' : username,
38                                                         'AuthMethod' : 'password',
39                                                         'AuthString' : password}
40
41
42 # NOTE: by default, use anonymous access, but if auth files are 
43 #       configured, use them, with their auth definitions.
44 auth = Auth()
45 try:
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
51 except:
52         try:
53                 import auth
54                 auth.server = auth.plc
55         except:
56                 auth = Auth()
57                 auth.server = XMLRPC_SERVER
58
59 global_error_count = 0
60
61 class PLC:
62         def __init__(self, auth, url):
63                 self.auth = auth
64                 self.url = url
65                 self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
66
67         def __getattr__(self, name):
68                 method = getattr(self.api, name)
69                 if method is None:
70                         raise AssertionError("method does not exist")
71
72                 try:
73                         return lambda *params : method(self.auth, *params)
74                 except ProtocolError:
75                         traceback.print_exc()
76                         global_error_count += 1
77                         if global_error_count >= 10:
78                                 print "maximum error count exceeded; exiting..."
79                                 sys.exit(1)
80                         else:
81                                 print "%s errors have occurred" % global_error_count
82                         raise Exception("ProtocolError continuing")
83
84         def __repr__(self):
85                 return self.api.__repr__()
86
87 api = PLC(auth.auth, auth.server)
88
89 class CachedPLC(PLC):
90
91         def _param_to_str(self, name, *params):
92                 fields = len(params)
93                 retstr = ""
94                 retstr += "%s-" % name
95                 for x in params:
96                         retstr += "%s-" % x
97                 return retstr[:-1]
98
99         def __getattr__(self, name):
100                 method = getattr(self.api, name)
101                 if method is None:
102                         raise AssertionError("method does not exist")
103
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
109                         else:
110                                 refresh = False
111
112                         if 'Get' in name:
113                                 if not database.cachedRecently(cachename):
114                                         load_old_cache = False
115                                         try:
116                                                 values = method(self.auth, *params)
117                                         except:
118                                                 print "Call %s FAILED: Using old cached data" % cachename
119                                                 load_old_cache = True
120                                                 
121                                         if load_old_cache:
122                                                 values = database.dbLoad(cachename)
123                                         else:
124                                                 database.dbDump(cachename, values)
125                                                 
126                                         return values
127                                 else:
128                                         values = database.dbLoad(cachename)
129                                         return values
130                         else:
131                                 return method(self.auth, *params)
132
133                 return run_or_returncached
134
135
136 def getAPI(url):
137         return xmlrpclib.Server(url, verbose=False, allow_none=True)
138
139 def getNodeAPI(session):
140         nodeauth = Auth(session=session)
141         return PLC(nodeauth.auth, auth.server)
142
143 def getAuthAPI():
144         return PLC(auth.auth, auth.server)
145
146 def getCachedAuthAPI():
147         return CachedPLC(auth.auth, auth.server)
148
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)
155
156 def getTechEmails(loginbase):
157         """
158                 For the given site, return all user email addresses that have the 'tech' role.
159         """
160         api = getAuthAPI()
161         # get site details.
162         s = api.GetSites(loginbase)[0]
163         # get people at site
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) ]
167         return emails
168
169 def getPIEmails(loginbase):
170         """
171                 For the given site, return all user email addresses that have the 'tech' role.
172         """
173         api = getAuthAPI()
174         # get site details.
175         s = api.GetSites(loginbase)[0]
176         # get people at site
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) ]
180         return emails
181
182 def getSliceUserEmails(loginbase):
183         """
184                 For the given site, return all user email addresses that have the 'tech' role.
185         """
186         api = getAuthAPI()
187         # get site details.
188         s = api.GetSites(loginbase)[0]
189         # get people at site
190         slices = api.GetSlices(s['slice_ids'])
191         people = []
192         for slice in slices:
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) ]
197         return unique_emails
198
199 '''
200 Returns list of nodes in dbg as reported by PLC
201 '''
202 def nodesDbg():
203         dbgNodes = []
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))
209         return dbgNodes
210
211
212 '''
213 Returns loginbase for given nodename
214 '''
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']
221         else:
222                 print "Not nodes returned!!!!"
223
224 '''
225 Returns list of slices for a site.
226 '''
227 def slices(loginbase):
228         siteslices = []
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'])
233         return siteslices
234
235 '''
236 Returns dict of PCU info of a given node.
237 '''
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']:
243                 print nodeinfo
244                 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
245                 print sitepcu
246                 print nodeinfo["ports"]
247                 sitepcu[nodename] = nodeinfo["ports"][0]
248                 return sitepcu
249         else:
250                 logger.info("%s doesn't have PCU" % nodename)
251                 return False
252
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)
256         return pcu_list 
257
258 '''
259 Returns all site nodes for site id (loginbase).
260 '''
261 def getSiteNodes(loginbase, fields=None):
262         api = xmlrpclib.Server(auth.server, verbose=False)
263         nodelist = []
264         anon = {'AuthMethod': "anonymous"}
265         try:
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
272         return nodelist
273
274
275 def getPersons(filter=None, fields=None):
276         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
277         persons = []
278         try:
279                 persons = api.GetPersons(auth.auth, filter, fields)
280         except Exception, exc:
281                 print "getPersons:  %s" % exc
282                 logger.info("getPersons:  %s" % exc)
283         return persons
284
285 def getSites(filter=None, fields=None):
286         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
287         sites = []
288         anon = {'AuthMethod': "anonymous"}
289         try:
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)
296         return sites
297
298 def getSiteNodes2(loginbase):
299         api = xmlrpclib.Server(auth.server, verbose=False)
300         nodelist = []
301         anon = {'AuthMethod': "anonymous"}
302         try:
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)
307         return nodelist
308
309 def getNodeNetworks(filter=None):
310         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
311         nodenetworks = api.GetInterfaces(auth.auth, filter, None)
312         return nodenetworks
313
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' ])
320         return nodes
321
322 '''
323 Sets boot state of a node.
324 '''
325 def nodeBootState(nodename, state):
326         api = xmlrpclib.Server(auth.server, verbose=False)
327         try:
328                 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
329         except Exception, exc:
330                 logger.info("nodeBootState:  %s" % exc)
331
332 def updateNodeKey(nodename, key):
333         api = xmlrpclib.Server(auth.server, verbose=False)
334         try:
335                 return api.UpdateNode(auth.auth, nodename, {'key': key})
336         except Exception, exc:
337                 logger.info("updateNodeKey:  %s" % exc)
338
339 '''
340 Sends Ping Of Death to node.
341 '''
342 def nodePOD(nodename):
343         api = xmlrpclib.Server(auth.server, verbose=False)
344         logger.info("Sending POD to %s" % nodename)
345         try:
346                 if not debug:
347                         return api.RebootNode(auth.auth, nodename)
348         except Exception, exc:
349                         logger.info("nodePOD:  %s" % exc)
350
351 '''
352 Freeze all site slices.
353 '''
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)
358                 try:
359                         if not debug:
360                                 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
361                 except Exception, exc:
362                         logger.info("suspendSlices:  %s" % exc)
363
364 '''
365 Freeze all site slices.
366 '''
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)
371                 try:
372                         if not debug:
373                                 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
374                 except Exception, exc:
375                         logger.info("suspendSlices:  %s" % exc)
376
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)
381                 try:
382                         if not debug:
383                                 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
384                                 if len(slice_list) == 0:
385                                         return
386                                 slice_id = slice_list[0]['slice_id']
387                                 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
388                                 for attr in l_attr:
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
395
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)
400                 try:
401                         if not debug:
402                                 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
403                                 if len(slice_list) == 0:
404                                         return
405                                 slice_id = slice_list[0]['slice_id']
406                                 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
407                                 for attr in l_attr:
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
414
415 #I'm commenting this because this really should be a manual process.  
416 #'''
417 #Enable suspended site slices.
418 #'''
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"})
424 #
425 def enableSiteSliceCreation(loginbase):
426         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
427         try:
428                 logger.info("Enabling slice creation for site %s" % loginbase)
429                 if not debug:
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)
435
436 def enableSliceCreation(nodename):
437         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
438         try:
439                 loginbase = siteId(nodename)
440                 logger.info("Enabling slice creation for site %s" % loginbase)
441                 if not debug:
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)
447
448 '''
449 Removes site's ability to create slices. Returns previous max_slices
450 '''
451 def removeSiteSliceCreation(sitename):
452         print "removeSiteSliceCreation(%s)" % sitename
453         api = xmlrpclib.Server(auth.server, verbose=False)
454         try:
455                 logger.info("Removing slice creation for site %s" % sitename)
456                 if not debug:
457                         api.UpdateSite(auth.auth, sitename, {'enabled': False})
458         except Exception, exc:
459                 logger.info("removeSiteSliceCreation:  %s" % exc)
460
461 '''
462 Removes ability to create slices. Returns previous max_slices
463 '''
464 def removeSliceCreation(nodename):
465         print "removeSliceCreation(%s)" % nodename
466         api = xmlrpclib.Server(auth.server, verbose=False)
467         try:
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)
472                 if not debug:
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)
477
478 '''
479 QED
480 '''
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))
487 #               try:
488 #                       if not debug:
489 #                               api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
490 #               except Exception, exc:
491 #                       logger.info("API:  %s" % exc)
492 #       else:
493 #               logger.debug("Cant find site for %s.  Cannot enable creation." % nodename)
494
495 def main():
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")
505         #print nodesDbg()
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")
514
515 if __name__=="__main__":
516         main()