commented out unused value
[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         nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
252         if nodeinfo['pcu_ids']:
253                 print nodeinfo
254                 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
255                 print sitepcu
256                 print nodeinfo["ports"]
257                 sitepcu[nodename] = nodeinfo["ports"][0]
258                 return sitepcu
259         else:
260                 logger.info("%s doesn't have PCU" % nodename)
261                 return False
262
263 def GetPCUs(filter=None, fields=None):
264         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
265         pcu_list = api.GetPCUs(auth.auth, filter, fields)
266         return pcu_list 
267
268 '''
269 Returns all site nodes for site id (loginbase).
270 '''
271 def getSiteNodes(loginbase, fields=None):
272         api = xmlrpclib.Server(auth.server, verbose=False)
273         nodelist = []
274         anon = {'AuthMethod': "anonymous"}
275         try:
276                 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
277                 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
278                         nodelist.append(node['hostname'])
279         except Exception, exc:
280                 logger.info("getSiteNodes:  %s" % exc)
281                 print "getSiteNodes:  %s" % exc
282         return nodelist
283
284
285 def getPersons(filter=None, fields=None):
286         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
287         persons = []
288         try:
289                 persons = api.GetPersons(auth.auth, filter, fields)
290         except Exception, exc:
291                 print "getPersons:  %s" % exc
292                 logger.info("getPersons:  %s" % exc)
293         return persons
294
295 def getSites(filter=None, fields=None):
296         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
297         sites = []
298         anon = {'AuthMethod': "anonymous"}
299         try:
300                 #sites = api.GetSites(anon, filter, fields)
301                 sites = api.GetSites(auth.auth, filter, fields)
302         except Exception, exc:
303                 traceback.print_exc()
304                 print "getSites:  %s" % exc
305                 logger.info("getSites:  %s" % exc)
306         return sites
307
308 def getSiteNodes2(loginbase):
309         api = xmlrpclib.Server(auth.server, verbose=False)
310         nodelist = []
311         anon = {'AuthMethod': "anonymous"}
312         try:
313                 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
314                 nodelist += getNodes({'node_id':nodeids})
315         except Exception, exc:
316                 logger.info("getSiteNodes2:  %s" % exc)
317         return nodelist
318
319 def getNodeNetworks(filter=None):
320         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
321         nodenetworks = api.GetInterfaces(auth.auth, filter, None)
322         return nodenetworks
323
324 def getNodes(filter=None, fields=None):
325         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
326         nodes = api.GetNodes(auth.auth, filter, fields) 
327                         #['boot_state', 'hostname', 
328                         #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
329                         #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
330         return nodes
331
332
333 # Check if the site is a pending site that needs to be approved.
334 def isPendingSite(loginbase):
335         api = xmlrpclib.Server(auth.server, verbose=False)
336         try:
337                 site = api.GetSites(auth.auth, loginbase)[0]
338         except Exception, exc:
339                 login.info("ERROR: No site %s" % loginbase)
340                 return False
341
342         if not site['enabled'] and site['ext_consortium_id'] == PENDING_CONSORTIUM_ID:
343                 return True
344
345         return False
346
347
348 '''
349 Sets boot state of a node.
350 '''
351 def nodeBootState(nodename, state):
352         api = xmlrpclib.Server(auth.server, verbose=False)
353         try:
354                 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
355         except Exception, exc:
356                 logger.info("nodeBootState:  %s" % exc)
357
358 def updateNodeKey(nodename, key):
359         api = xmlrpclib.Server(auth.server, verbose=False)
360         try:
361                 return api.UpdateNode(auth.auth, nodename, {'key': key})
362         except Exception, exc:
363                 logger.info("updateNodeKey:  %s" % exc)
364
365 '''
366 Sends Ping Of Death to node.
367 '''
368 def nodePOD(nodename):
369         api = xmlrpclib.Server(auth.server, verbose=False)
370         logger.info("Sending POD to %s" % nodename)
371         try:
372                 if not debug:
373                         return api.RebootNode(auth.auth, nodename)
374         except Exception, exc:
375                         logger.info("nodePOD:  %s" % exc)
376
377 '''
378 Freeze all site slices.
379 '''
380 def suspendSiteSlices(loginbase):
381         api = xmlrpclib.Server(auth.server, verbose=False)
382         for slice in slices(loginbase):
383                 logger.info("Suspending slice %s" % slice)
384                 try:
385                         if not debug:
386                                 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
387                 except Exception, exc:
388                         logger.info("suspendSlices:  %s" % exc)
389
390 '''
391 Freeze all site slices.
392 '''
393 def suspendSlices(nodename):
394         api = xmlrpclib.Server(auth.server, verbose=False)
395         for slice in slices(siteId(nodename)):
396                 logger.info("Suspending slice %s" % slice)
397                 try:
398                         if not debug:
399                                 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
400                 except Exception, exc:
401                         logger.info("suspendSlices:  %s" % exc)
402
403 def enableSiteSlices(loginbase):
404         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
405         for slice in slices(loginbase):
406                 logger.info("Enabling slices %s" % slice)
407                 try:
408                         if not debug:
409                                 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
410                                 if len(slice_list) == 0:
411                                         return
412                                 slice_id = slice_list[0]['slice_id']
413                                 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
414                                 for attr in l_attr:
415                                         if "enabled" == attr['name'] and attr['value'] == "0":
416                                                 logger.info("Deleted enable=0 attribute from slice %s" % slice)
417                                                 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
418                 except Exception, exc:
419                         logger.info("enableSiteSlices: %s" % exc)
420                         print "exception: %s" % exc
421
422 def enableSlices(nodename):
423         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
424
425         for slice in slices(siteId(nodename)):
426                 logger.info("Enabling slices %s" % slice)
427                 try:
428                         if not debug:
429                                 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
430                                 if len(slice_list) == 0:
431                                         return
432                                 slice_id = slice_list[0]['slice_id']
433                                 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
434                                 for attr in l_attr:
435                                         if "enabled" == attr['name'] and attr['value'] == "0":
436                                                 logger.info("Deleted enable=0 attribute from slice %s" % slice)
437                                                 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
438                 except Exception, exc:
439                         logger.info("enableSlices: %s" % exc)
440                         print "exception: %s" % exc
441
442
443 #I'm commenting this because this really should be a manual process.  
444 #'''
445 #Enable suspended site slices.
446 #'''
447 #def enableSlices(nodename, slicelist):
448 #       api = xmlrpclib.Server(auth.server, verbose=False)
449 #       for slice in  slices(siteId(nodename)):
450 #               logger.info("Suspending slice %s" % slice)
451 #               api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
452 #
453 def enableSiteSliceCreation(loginbase):
454         if isPendingSite(loginbase):
455                 msg = "INFO: enableSiteSliceCreation: Pending Site (%s)" % loginbase
456                 print msg
457                 logger.info(msg)
458                 return
459
460         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
461         try:
462                 logger.info("Enabling slice creation for site %s" % loginbase)
463                 if not debug:
464                         logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
465                         api.UpdateSite(auth.auth, loginbase, {'enabled': True})
466         except Exception, exc:
467                 print "ERROR: enableSiteSliceCreation:  %s" % exc
468                 logger.info("ERROR: enableSiteSliceCreation:  %s" % exc)
469
470 def enableSliceCreation(nodename):
471         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
472         try:
473                 loginbase = siteId(nodename)
474                 enableSiteSliceCreation(loginbase)
475         except Exception, exc:
476                 print "ERROR: enableSliceCreation:  %s" % exc
477                 logger.info("ERROR: enableSliceCreation:  %s" % exc)
478
479 '''
480 Removes site's ability to create slices. Returns previous max_slices
481 '''
482 def removeSiteSliceCreation(loginbase):
483         print "removeSiteSliceCreation(%s)" % loginbase
484
485         if isPendingSite(loginbase):
486                 msg = "INFO: removeSiteSliceCreation: Pending Site (%s)" % loginbase
487                 print msg
488                 logger.info(msg)
489                 return
490
491         api = xmlrpclib.Server(auth.server, verbose=False)
492         try:
493                 logger.info("Removing slice creation for site %s" % loginbase)
494                 if not debug:
495                         api.UpdateSite(auth.auth, loginbase, {'enabled': False})
496         except Exception, exc:
497                 logger.info("removeSiteSliceCreation:  %s" % exc)
498
499 '''
500 Removes ability to create slices. Returns previous max_slices
501 '''
502 def removeSliceCreation(nodename):
503         print "removeSliceCreation(%s)" % nodename
504         api = xmlrpclib.Server(auth.server, verbose=False)
505         try:
506                 loginbase = siteId(nodename)
507                 removeSiteSliceCreation(loginbase)
508         except Exception, exc:
509                 logger.info("removeSliceCreation:  %s" % exc)
510
511 '''
512 QED
513 '''
514 #def enableSliceCreation(nodename, maxslices):
515 #       api = xmlrpclib.Server(auth.server, verbose=False)
516 #       anon = {'AuthMethod': "anonymous"}
517 #       siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
518 #       if len(siteid) == 1:
519 #               logger.info("Enabling slice creation for site %s" % siteId(nodename))
520 #               try:
521 #                       if not debug:
522 #                               api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
523 #               except Exception, exc:
524 #                       logger.info("API:  %s" % exc)
525 #       else:
526 #               logger.debug("Cant find site for %s.  Cannot enable creation." % nodename)
527
528 def main():
529         logger.setLevel(logging.DEBUG)
530         ch = logging.StreamHandler()
531         ch.setLevel(logging.DEBUG)
532         formatter = logging.Formatter('logger - %(message)s')
533         ch.setFormatter(formatter)
534         logger.addHandler(ch)
535         #print getpcu("kupl2.ittc.ku.edu")
536         #print getpcu("planetlab1.cse.msu.edu")
537         #print getpcu("alice.cs.princeton.edu")
538         #print nodesDbg()
539         #nodeBootState("alice.cs.princeton.edu", "boot")
540         #freezeSite("alice.cs.princeton.edu")
541         print removeSliceCreation("alice.cs.princeton.edu")
542         #enableSliceCreation("alice.cs.princeton.edu", 1024)
543         #print getSiteNodes("princeton")
544         #print siteId("alice.cs.princeton.edu")
545         #print nodePOD("alice.cs.princeton.edu")
546         #print slices("princeton")
547
548 if __name__=="__main__":
549         main()