added blacklist to action.py
[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 except:
21         debug = False
22 logger = logging.getLogger("monitor")
23         
24 class Auth:
25         def __init__(self, username=None, password=None, **kwargs):
26                 if 'session' in kwargs:
27                         self.auth= { 'AuthMethod' : 'session',
28                                         'session' : kwargs['session'] }
29                 else:
30                         if username==None and password==None:
31                                 self.auth = {'AuthMethod': "anonymous"}
32                         else:
33                                 self.auth = {'Username' : username,
34                                                         'AuthMethod' : 'password',
35                                                         'AuthString' : password}
36
37 # NOTE: this host is used by default when there are no auth files.
38 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
39
40 # NOTE: by default, use anonymous access, but if auth files are 
41 #       configured, use them, with their auth definitions.
42 auth = Auth()
43 try:
44         from monitor import config
45         auth.auth = {'Username' : config.API_AUTH_USER,
46                      'AuthMethod' : 'password',
47                                  'AuthString' : config.API_AUTH_PASSWORD}
48         auth.server = config.API_SERVER
49 except:
50         try:
51                 import auth
52                 auth.server = auth.plc
53         except:
54                 auth = Auth()
55                 auth.server = XMLRPC_SERVER
56
57 api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
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 class CachedPLC(PLC):
88
89         def _param_to_str(self, name, *params):
90                 fields = len(params)
91                 retstr = ""
92                 retstr += "%s-" % name
93                 for x in params:
94                         retstr += "%s-" % x
95                 return retstr[:-1]
96
97         def __getattr__(self, name):
98                 method = getattr(self.api, name)
99                 if method is None:
100                         raise AssertionError("method does not exist")
101
102                 def run_or_returncached(*params):
103                         cachename = self._param_to_str(name, *params)
104                         #print "cachename is %s" % cachename
105                         if hasattr(config, 'refresh'):
106                                 refresh = config.refresh
107                         else:
108                                 refresh = False
109
110                         if 'Get' in name:
111                                 if not database.cachedRecently(cachename):
112                                         load_old_cache = False
113                                         try:
114                                                 values = method(self.auth, *params)
115                                         except:
116                                                 print "Call %s FAILED: Using old cached data" % cachename
117                                                 load_old_cache = True
118                                                 
119                                         if load_old_cache:
120                                                 values = database.dbLoad(cachename)
121                                         else:
122                                                 database.dbDump(cachename, values)
123                                                 
124                                         return values
125                                 else:
126                                         values = database.dbLoad(cachename)
127                                         return values
128                         else:
129                                 return method(self.auth, *params)
130
131                 return run_or_returncached
132
133
134 def getAPI(url):
135         return xmlrpclib.Server(url, verbose=False, allow_none=True)
136
137 def getNodeAPI(session):
138         nodeauth = Auth(session=session)
139         return PLC(nodeauth.auth, auth.server)
140
141 def getAuthAPI():
142         return PLC(auth.auth, auth.server)
143
144 def getCachedAuthAPI():
145         return CachedPLC(auth.auth, auth.server)
146
147 def getTechEmails(loginbase):
148         """
149                 For the given site, return all user email addresses that have the 'tech' role.
150         """
151         api = getAuthAPI()
152         # get site details.
153         s = api.GetSites(loginbase)[0]
154         # get people at site
155         p = api.GetPersons(s['person_ids'])
156         # pull out those with the right role.
157         emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
158         return emails
159
160 def getPIEmails(loginbase):
161         """
162                 For the given site, return all user email addresses that have the 'tech' role.
163         """
164         api = getAuthAPI()
165         # get site details.
166         s = api.GetSites(loginbase)[0]
167         # get people at site
168         p = api.GetPersons(s['person_ids'])
169         # pull out those with the right role.
170         emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
171         return emails
172
173 def getSliceUserEmails(loginbase):
174         """
175                 For the given site, return all user email addresses that have the 'tech' role.
176         """
177         api = getAuthAPI()
178         # get site details.
179         s = api.GetSites(loginbase)[0]
180         # get people at site
181         slices = api.GetSlices(s['slice_ids'])
182         people = []
183         for slice in slices:
184                 people += api.GetPersons(slice['person_ids'])
185         # pull out those with the right role.
186         emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
187         unique_emails = [ x for x in set(emails) ]
188         return unique_emails
189
190 '''
191 Returns list of nodes in dbg as reported by PLC
192 '''
193 def nodesDbg():
194         dbgNodes = []
195         api = xmlrpclib.Server(auth.server, verbose=False)
196         anon = {'AuthMethod': "anonymous"}
197         for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
198                 dbgNodes.append(node['hostname'])
199         logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
200         return dbgNodes
201
202
203 '''
204 Returns loginbase for given nodename
205 '''
206 def siteId(nodename):
207         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
208         site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
209         if len(site_id) == 1:
210                 loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
211                 return loginbase[0]['login_base']
212         else:
213                 print "Not nodes returned!!!!"
214
215 '''
216 Returns list of slices for a site.
217 '''
218 def slices(loginbase):
219         siteslices = []
220         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
221         sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
222         for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
223                 siteslices.append(slice['name'])
224         return siteslices
225
226 '''
227 Returns dict of PCU info of a given node.
228 '''
229 def getpcu(nodename):
230         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
231         anon = {'AuthMethod': "anonymous"}
232         nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
233         if nodeinfo['pcu_ids']:
234                 print nodeinfo
235                 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
236                 print sitepcu
237                 print nodeinfo["ports"]
238                 sitepcu[nodename] = nodeinfo["ports"][0]
239                 return sitepcu
240         else:
241                 logger.info("%s doesn't have PCU" % nodename)
242                 return False
243
244 def GetPCUs(filter=None, fields=None):
245         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
246         pcu_list = api.GetPCUs(auth.auth, filter, fields)
247         return pcu_list 
248
249 '''
250 Returns all site nodes for site id (loginbase).
251 '''
252 def getSiteNodes(loginbase, fields=None):
253         api = xmlrpclib.Server(auth.server, verbose=False)
254         nodelist = []
255         anon = {'AuthMethod': "anonymous"}
256         try:
257                 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
258                 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
259                         nodelist.append(node['hostname'])
260         except Exception, exc:
261                 logger.info("getSiteNodes:  %s" % exc)
262                 print "getSiteNodes:  %s" % exc
263         return nodelist
264
265
266 def getPersons(filter=None, fields=None):
267         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
268         persons = []
269         try:
270                 persons = api.GetPersons(auth.auth, filter, fields)
271         except Exception, exc:
272                 print "getPersons:  %s" % exc
273                 logger.info("getPersons:  %s" % exc)
274         return persons
275
276 def getSites(filter=None, fields=None):
277         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
278         sites = []
279         anon = {'AuthMethod': "anonymous"}
280         try:
281                 #sites = api.GetSites(anon, filter, fields)
282                 sites = api.GetSites(auth.auth, filter, fields)
283         except Exception, exc:
284                 traceback.print_exc()
285                 print "getSites:  %s" % exc
286                 logger.info("getSites:  %s" % exc)
287         return sites
288
289 def getSiteNodes2(loginbase):
290         api = xmlrpclib.Server(auth.server, verbose=False)
291         nodelist = []
292         anon = {'AuthMethod': "anonymous"}
293         try:
294                 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
295                 nodelist += getNodes({'node_id':nodeids})
296         except Exception, exc:
297                 logger.info("getSiteNodes2:  %s" % exc)
298         return nodelist
299
300 def getNodeNetworks(filter=None):
301         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
302         nodenetworks = api.GetNodeNetworks(auth.auth, filter, None)
303         return nodenetworks
304
305 def getNodes(filter=None, fields=None):
306         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
307         nodes = api.GetNodes(auth.auth, filter, fields) 
308                         #['boot_state', 'hostname', 
309                         #'site_id', 'date_created', 'node_id', 'version', 'nodenetwork_ids',
310                         #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
311         return nodes
312
313 '''
314 Sets boot state of a node.
315 '''
316 def nodeBootState(nodename, state):
317         api = xmlrpclib.Server(auth.server, verbose=False)
318         try:
319                 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
320         except Exception, exc:
321                 logger.info("nodeBootState:  %s" % exc)
322
323 def updateNodeKey(nodename, key):
324         api = xmlrpclib.Server(auth.server, verbose=False)
325         try:
326                 return api.UpdateNode(auth.auth, nodename, {'key': key})
327         except Exception, exc:
328                 logger.info("updateNodeKey:  %s" % exc)
329
330 '''
331 Sends Ping Of Death to node.
332 '''
333 def nodePOD(nodename):
334         api = xmlrpclib.Server(auth.server, verbose=False)
335         logger.info("Sending POD to %s" % nodename)
336         try:
337                 if not debug:
338                         return api.RebootNode(auth.auth, nodename)
339         except Exception, exc:
340                         logger.info("nodePOD:  %s" % exc)
341
342 '''
343 Freeze all site slices.
344 '''
345 def suspendSiteSlices(loginbase):
346         api = xmlrpclib.Server(auth.server, verbose=False)
347         for slice in slices(loginbase):
348                 logger.info("Suspending slice %s" % slice)
349                 try:
350                         if not debug:
351                                 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
352                 except Exception, exc:
353                         logger.info("suspendSlices:  %s" % exc)
354
355 '''
356 Freeze all site slices.
357 '''
358 def suspendSlices(nodename):
359         api = xmlrpclib.Server(auth.server, verbose=False)
360         for slice in slices(siteId(nodename)):
361                 logger.info("Suspending slice %s" % slice)
362                 try:
363                         if not debug:
364                                 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
365                 except Exception, exc:
366                         logger.info("suspendSlices:  %s" % exc)
367
368 def enableSiteSlices(loginbase):
369         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
370         for slice in slices(loginbase):
371                 logger.info("Enabling slices %s" % slice)
372                 try:
373                         if not debug:
374                                 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
375                                 if len(slice_list) == 0:
376                                         return
377                                 slice_id = slice_list[0]['slice_id']
378                                 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
379                                 for attr in l_attr:
380                                         if "enabled" == attr['name'] and attr['value'] == "0":
381                                                 logger.info("Deleted enable=0 attribute from slice %s" % slice)
382                                                 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
383                 except Exception, exc:
384                         logger.info("enableSiteSlices: %s" % exc)
385                         print "exception: %s" % exc
386
387 def enableSlices(nodename):
388         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
389         for slice in slices(siteId(nodename)):
390                 logger.info("Enabling slices %s" % slice)
391                 try:
392                         if not debug:
393                                 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
394                                 if len(slice_list) == 0:
395                                         return
396                                 slice_id = slice_list[0]['slice_id']
397                                 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
398                                 for attr in l_attr:
399                                         if "enabled" == attr['name'] and attr['value'] == "0":
400                                                 logger.info("Deleted enable=0 attribute from slice %s" % slice)
401                                                 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
402                 except Exception, exc:
403                         logger.info("enableSlices: %s" % exc)
404                         print "exception: %s" % exc
405
406 #I'm commenting this because this really should be a manual process.  
407 #'''
408 #Enable suspended site slices.
409 #'''
410 #def enableSlices(nodename, slicelist):
411 #       api = xmlrpclib.Server(auth.server, verbose=False)
412 #       for slice in  slices(siteId(nodename)):
413 #               logger.info("Suspending slice %s" % slice)
414 #               api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
415 #
416 def enableSiteSliceCreation(loginbase):
417         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
418         try:
419                 logger.info("Enabling slice creation for site %s" % loginbase)
420                 if not debug:
421                         logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
422                         api.UpdateSite(auth.auth, loginbase, {'enabled': True})
423         except Exception, exc:
424                 print "ERROR: enableSiteSliceCreation:  %s" % exc
425                 logger.info("ERROR: enableSiteSliceCreation:  %s" % exc)
426
427 def enableSliceCreation(nodename):
428         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
429         try:
430                 loginbase = siteId(nodename)
431                 logger.info("Enabling slice creation for site %s" % loginbase)
432                 if not debug:
433                         logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
434                         api.UpdateSite(auth.auth, loginbase, {'enabled': True})
435         except Exception, exc:
436                 print "ERROR: enableSliceCreation:  %s" % exc
437                 logger.info("ERROR: enableSliceCreation:  %s" % exc)
438
439 '''
440 Removes site's ability to create slices. Returns previous max_slices
441 '''
442 def removeSiteSliceCreation(sitename):
443         print "removeSiteSliceCreation(%s)" % sitename
444         api = xmlrpclib.Server(auth.server, verbose=False)
445         try:
446                 logger.info("Removing slice creation for site %s" % sitename)
447                 if not debug:
448                         api.UpdateSite(auth.auth, sitename, {'enabled': False})
449         except Exception, exc:
450                 logger.info("removeSiteSliceCreation:  %s" % exc)
451
452 '''
453 Removes ability to create slices. Returns previous max_slices
454 '''
455 def removeSliceCreation(nodename):
456         print "removeSliceCreation(%s)" % nodename
457         api = xmlrpclib.Server(auth.server, verbose=False)
458         try:
459                 loginbase = siteId(nodename)
460                 #numslices = api.GetSites(auth.auth, {"login_base": loginbase}, 
461                 #               ["max_slices"])[0]['max_slices']
462                 logger.info("Removing slice creation for site %s" % loginbase)
463                 if not debug:
464                         #api.UpdateSite(auth.auth, loginbase, {'max_slices': 0})
465                         api.UpdateSite(auth.auth, loginbase, {'enabled': False})
466         except Exception, exc:
467                 logger.info("removeSliceCreation:  %s" % exc)
468
469 '''
470 QED
471 '''
472 #def enableSliceCreation(nodename, maxslices):
473 #       api = xmlrpclib.Server(auth.server, verbose=False)
474 #       anon = {'AuthMethod': "anonymous"}
475 #       siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
476 #       if len(siteid) == 1:
477 #               logger.info("Enabling slice creation for site %s" % siteId(nodename))
478 #               try:
479 #                       if not debug:
480 #                               api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
481 #               except Exception, exc:
482 #                       logger.info("API:  %s" % exc)
483 #       else:
484 #               logger.debug("Cant find site for %s.  Cannot enable creation." % nodename)
485
486 def main():
487         logger.setLevel(logging.DEBUG)
488         ch = logging.StreamHandler()
489         ch.setLevel(logging.DEBUG)
490         formatter = logging.Formatter('logger - %(message)s')
491         ch.setFormatter(formatter)
492         logger.addHandler(ch)
493         #print getpcu("kupl2.ittc.ku.edu")
494         #print getpcu("planetlab1.cse.msu.edu")
495         #print getpcu("alice.cs.princeton.edu")
496         #print nodesDbg()
497         #nodeBootState("alice.cs.princeton.edu", "boot")
498         #freezeSite("alice.cs.princeton.edu")
499         print removeSliceCreation("alice.cs.princeton.edu")
500         #enableSliceCreation("alice.cs.princeton.edu", 1024)
501         #print getSiteNodes("princeton")
502         #print siteId("alice.cs.princeton.edu")
503         #print nodePOD("alice.cs.princeton.edu")
504         #print slices("princeton")
505
506 if __name__=="__main__":
507         main()