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