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