improved post server setup code in Monitor.spec
[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():
146         return PLC(auth.auth, auth.server)
147
148 def getCachedAuthAPI():
149         return CachedPLC(auth.auth, auth.server)
150
151 def getSessionAPI(session, server):
152         nodeauth = Auth(session=session)
153         return PLC(nodeauth.auth, server)
154 def getUserAPI(username, password, server):
155         auth = Auth(username,password)
156         return PLC(auth.auth, server)
157
158 def getTechEmails(loginbase):
159         """
160                 For the given site, return all user email addresses that have the 'tech' role.
161         """
162         api = getAuthAPI()
163         # get site details.
164         s = api.GetSites(loginbase)[0]
165         # get people at site
166         p = api.GetPersons(s['person_ids'])
167         # pull out those with the right role.
168         emails = [ person['email'] for person in filter(lambda x: 'tech' in x['roles'], p) ]
169         return emails
170
171 def getPIEmails(loginbase):
172         """
173                 For the given site, return all user email addresses that have the 'tech' role.
174         """
175         api = getAuthAPI()
176         # get site details.
177         s = api.GetSites(loginbase)[0]
178         # get people at site
179         p = api.GetPersons(s['person_ids'])
180         # pull out those with the right role.
181         emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], p) ]
182         return emails
183
184 def getSliceUserEmails(loginbase):
185         """
186                 For the given site, return all user email addresses that have the 'tech' role.
187         """
188         api = getAuthAPI()
189         # get site details.
190         s = api.GetSites(loginbase)[0]
191         # get people at site
192         slices = api.GetSlices(s['slice_ids'])
193         people = []
194         for slice in slices:
195                 people += api.GetPersons(slice['person_ids'])
196         # pull out those with the right role.
197         emails = [ person['email'] for person in filter(lambda x: 'pi' in x['roles'], people) ]
198         unique_emails = [ x for x in set(emails) ]
199         return unique_emails
200
201 '''
202 Returns list of nodes in dbg as reported by PLC
203 '''
204 def nodesDbg():
205         dbgNodes = []
206         api = xmlrpclib.Server(auth.server, verbose=False)
207         anon = {'AuthMethod': "anonymous"}
208         for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
209                 dbgNodes.append(node['hostname'])
210         logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
211         return dbgNodes
212
213
214 '''
215 Returns loginbase for given nodename
216 '''
217 def siteId(nodename):
218         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
219         site_id = api.GetNodes (auth.auth, {"hostname": nodename}, ['site_id'])
220         if len(site_id) == 1:
221                 loginbase = api.GetSites (auth.auth, site_id[0], ["login_base"])
222                 return loginbase[0]['login_base']
223         else:
224                 print "Not nodes returned!!!!"
225
226 '''
227 Returns list of slices for a site.
228 '''
229 def slices(loginbase):
230         siteslices = []
231         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
232         sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
233         for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
234                 siteslices.append(slice['name'])
235         return siteslices
236
237 '''
238 Returns dict of PCU info of a given node.
239 '''
240 def getpcu(nodename):
241         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
242         anon = {'AuthMethod': "anonymous"}
243         nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
244         if nodeinfo['pcu_ids']:
245                 print nodeinfo
246                 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
247                 print sitepcu
248                 print nodeinfo["ports"]
249                 sitepcu[nodename] = nodeinfo["ports"][0]
250                 return sitepcu
251         else:
252                 logger.info("%s doesn't have PCU" % nodename)
253                 return False
254
255 def GetPCUs(filter=None, fields=None):
256         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
257         pcu_list = api.GetPCUs(auth.auth, filter, fields)
258         return pcu_list 
259
260 '''
261 Returns all site nodes for site id (loginbase).
262 '''
263 def getSiteNodes(loginbase, fields=None):
264         api = xmlrpclib.Server(auth.server, verbose=False)
265         nodelist = []
266         anon = {'AuthMethod': "anonymous"}
267         try:
268                 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
269                 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
270                         nodelist.append(node['hostname'])
271         except Exception, exc:
272                 logger.info("getSiteNodes:  %s" % exc)
273                 print "getSiteNodes:  %s" % exc
274         return nodelist
275
276
277 def getPersons(filter=None, fields=None):
278         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
279         persons = []
280         try:
281                 persons = api.GetPersons(auth.auth, filter, fields)
282         except Exception, exc:
283                 print "getPersons:  %s" % exc
284                 logger.info("getPersons:  %s" % exc)
285         return persons
286
287 def getSites(filter=None, fields=None):
288         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
289         sites = []
290         anon = {'AuthMethod': "anonymous"}
291         try:
292                 #sites = api.GetSites(anon, filter, fields)
293                 sites = api.GetSites(auth.auth, filter, fields)
294         except Exception, exc:
295                 traceback.print_exc()
296                 print "getSites:  %s" % exc
297                 logger.info("getSites:  %s" % exc)
298         return sites
299
300 def getSiteNodes2(loginbase):
301         api = xmlrpclib.Server(auth.server, verbose=False)
302         nodelist = []
303         anon = {'AuthMethod': "anonymous"}
304         try:
305                 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
306                 nodelist += getNodes({'node_id':nodeids})
307         except Exception, exc:
308                 logger.info("getSiteNodes2:  %s" % exc)
309         return nodelist
310
311 def getNodeNetworks(filter=None):
312         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
313         nodenetworks = api.GetInterfaces(auth.auth, filter, None)
314         return nodenetworks
315
316 def getNodes(filter=None, fields=None):
317         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
318         nodes = api.GetNodes(auth.auth, filter, fields) 
319                         #['boot_state', 'hostname', 
320                         #'site_id', 'date_created', 'node_id', 'version', 'interface_ids',
321                         #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
322         return nodes
323
324 '''
325 Sets boot state of a node.
326 '''
327 def nodeBootState(nodename, state):
328         api = xmlrpclib.Server(auth.server, verbose=False)
329         try:
330                 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
331         except Exception, exc:
332                 logger.info("nodeBootState:  %s" % exc)
333
334 def updateNodeKey(nodename, key):
335         api = xmlrpclib.Server(auth.server, verbose=False)
336         try:
337                 return api.UpdateNode(auth.auth, nodename, {'key': key})
338         except Exception, exc:
339                 logger.info("updateNodeKey:  %s" % exc)
340
341 '''
342 Sends Ping Of Death to node.
343 '''
344 def nodePOD(nodename):
345         api = xmlrpclib.Server(auth.server, verbose=False)
346         logger.info("Sending POD to %s" % nodename)
347         try:
348                 if not debug:
349                         return api.RebootNode(auth.auth, nodename)
350         except Exception, exc:
351                         logger.info("nodePOD:  %s" % exc)
352
353 '''
354 Freeze all site slices.
355 '''
356 def suspendSiteSlices(loginbase):
357         api = xmlrpclib.Server(auth.server, verbose=False)
358         for slice in slices(loginbase):
359                 logger.info("Suspending slice %s" % slice)
360                 try:
361                         if not debug:
362                                 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
363                 except Exception, exc:
364                         logger.info("suspendSlices:  %s" % exc)
365
366 '''
367 Freeze all site slices.
368 '''
369 def suspendSlices(nodename):
370         api = xmlrpclib.Server(auth.server, verbose=False)
371         for slice in slices(siteId(nodename)):
372                 logger.info("Suspending slice %s" % slice)
373                 try:
374                         if not debug:
375                                 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
376                 except Exception, exc:
377                         logger.info("suspendSlices:  %s" % exc)
378
379 def enableSiteSlices(loginbase):
380         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
381         for slice in slices(loginbase):
382                 logger.info("Enabling slices %s" % slice)
383                 try:
384                         if not debug:
385                                 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
386                                 if len(slice_list) == 0:
387                                         return
388                                 slice_id = slice_list[0]['slice_id']
389                                 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
390                                 for attr in l_attr:
391                                         if "enabled" == attr['name'] and attr['value'] == "0":
392                                                 logger.info("Deleted enable=0 attribute from slice %s" % slice)
393                                                 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
394                 except Exception, exc:
395                         logger.info("enableSiteSlices: %s" % exc)
396                         print "exception: %s" % exc
397
398 def enableSlices(nodename):
399         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
400         for slice in slices(siteId(nodename)):
401                 logger.info("Enabling slices %s" % slice)
402                 try:
403                         if not debug:
404                                 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
405                                 if len(slice_list) == 0:
406                                         return
407                                 slice_id = slice_list[0]['slice_id']
408                                 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
409                                 for attr in l_attr:
410                                         if "enabled" == attr['name'] and attr['value'] == "0":
411                                                 logger.info("Deleted enable=0 attribute from slice %s" % slice)
412                                                 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
413                 except Exception, exc:
414                         logger.info("enableSlices: %s" % exc)
415                         print "exception: %s" % exc
416
417 #I'm commenting this because this really should be a manual process.  
418 #'''
419 #Enable suspended site slices.
420 #'''
421 #def enableSlices(nodename, slicelist):
422 #       api = xmlrpclib.Server(auth.server, verbose=False)
423 #       for slice in  slices(siteId(nodename)):
424 #               logger.info("Suspending slice %s" % slice)
425 #               api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
426 #
427 def enableSiteSliceCreation(loginbase):
428         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
429         try:
430                 logger.info("Enabling slice creation for site %s" % loginbase)
431                 if not debug:
432                         logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
433                         api.UpdateSite(auth.auth, loginbase, {'enabled': True})
434         except Exception, exc:
435                 print "ERROR: enableSiteSliceCreation:  %s" % exc
436                 logger.info("ERROR: enableSiteSliceCreation:  %s" % exc)
437
438 def enableSliceCreation(nodename):
439         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
440         try:
441                 loginbase = siteId(nodename)
442                 logger.info("Enabling slice creation for site %s" % loginbase)
443                 if not debug:
444                         logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
445                         api.UpdateSite(auth.auth, loginbase, {'enabled': True})
446         except Exception, exc:
447                 print "ERROR: enableSliceCreation:  %s" % exc
448                 logger.info("ERROR: enableSliceCreation:  %s" % exc)
449
450 '''
451 Removes site's ability to create slices. Returns previous max_slices
452 '''
453 def removeSiteSliceCreation(sitename):
454         print "removeSiteSliceCreation(%s)" % sitename
455         api = xmlrpclib.Server(auth.server, verbose=False)
456         try:
457                 logger.info("Removing slice creation for site %s" % sitename)
458                 if not debug:
459                         api.UpdateSite(auth.auth, sitename, {'enabled': False})
460         except Exception, exc:
461                 logger.info("removeSiteSliceCreation:  %s" % exc)
462
463 '''
464 Removes ability to create slices. Returns previous max_slices
465 '''
466 def removeSliceCreation(nodename):
467         print "removeSliceCreation(%s)" % nodename
468         api = xmlrpclib.Server(auth.server, verbose=False)
469         try:
470                 loginbase = siteId(nodename)
471                 #numslices = api.GetSites(auth.auth, {"login_base": loginbase}, 
472                 #               ["max_slices"])[0]['max_slices']
473                 logger.info("Removing slice creation for site %s" % loginbase)
474                 if not debug:
475                         #api.UpdateSite(auth.auth, loginbase, {'max_slices': 0})
476                         api.UpdateSite(auth.auth, loginbase, {'enabled': False})
477         except Exception, exc:
478                 logger.info("removeSliceCreation:  %s" % exc)
479
480 '''
481 QED
482 '''
483 #def enableSliceCreation(nodename, maxslices):
484 #       api = xmlrpclib.Server(auth.server, verbose=False)
485 #       anon = {'AuthMethod': "anonymous"}
486 #       siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
487 #       if len(siteid) == 1:
488 #               logger.info("Enabling slice creation for site %s" % siteId(nodename))
489 #               try:
490 #                       if not debug:
491 #                               api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
492 #               except Exception, exc:
493 #                       logger.info("API:  %s" % exc)
494 #       else:
495 #               logger.debug("Cant find site for %s.  Cannot enable creation." % nodename)
496
497 def main():
498         logger.setLevel(logging.DEBUG)
499         ch = logging.StreamHandler()
500         ch.setLevel(logging.DEBUG)
501         formatter = logging.Formatter('logger - %(message)s')
502         ch.setFormatter(formatter)
503         logger.addHandler(ch)
504         #print getpcu("kupl2.ittc.ku.edu")
505         #print getpcu("planetlab1.cse.msu.edu")
506         #print getpcu("alice.cs.princeton.edu")
507         #print nodesDbg()
508         #nodeBootState("alice.cs.princeton.edu", "boot")
509         #freezeSite("alice.cs.princeton.edu")
510         print removeSliceCreation("alice.cs.princeton.edu")
511         #enableSliceCreation("alice.cs.princeton.edu", 1024)
512         #print getSiteNodes("princeton")
513         #print siteId("alice.cs.princeton.edu")
514         #print nodePOD("alice.cs.princeton.edu")
515         #print slices("princeton")
516
517 if __name__=="__main__":
518         main()