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