correct message
[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         if isPendingSite(loginbase):
382                 msg = "INFO: suspendSiteSlices: Pending Site (%s)" % loginbase
383                 print msg
384                 logger.info(msg)
385                 return
386
387         api = xmlrpclib.Server(auth.server, verbose=False)
388         for slice in slices(loginbase):
389                 logger.info("Suspending slice %s" % slice)
390                 try:
391                         if not debug:
392                                 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
393                 except Exception, exc:
394                         logger.info("suspendSlices:  %s" % exc)
395
396 '''
397 Freeze all site slices.
398 '''
399 def suspendSlices(nodename):
400         loginbase = siteId(nodename)
401         suspendSiteSlices(loginbase)
402
403
404 def enableSiteSlices(loginbase):
405         if isPendingSite(loginbase):
406                 msg = "INFO: enableSiteSlices: Pending Site (%s)" % loginbase
407                 print msg
408                 logger.info(msg)
409                 return
410
411         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
412         for slice in slices(loginbase):
413                 logger.info("Enabling slices %s" % slice)
414                 try:
415                         if not debug:
416                                 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
417                                 if len(slice_list) == 0:
418                                         return
419                                 slice_id = slice_list[0]['slice_id']
420                                 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
421                                 for attr in l_attr:
422                                         if "enabled" == attr['name'] and attr['value'] == "0":
423                                                 logger.info("Deleted enable=0 attribute from slice %s" % slice)
424                                                 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
425                 except Exception, exc:
426                         logger.info("enableSiteSlices: %s" % exc)
427                         print "exception: %s" % exc
428
429 def enableSlices(nodename):
430         loginbase = siteId(nodename)
431         enableSiteSlices(loginbase)
432
433
434 #I'm commenting this because this really should be a manual process.  
435 #'''
436 #Enable suspended site slices.
437 #'''
438 #def enableSlices(nodename, slicelist):
439 #       api = xmlrpclib.Server(auth.server, verbose=False)
440 #       for slice in  slices(siteId(nodename)):
441 #               logger.info("Suspending slice %s" % slice)
442 #               api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
443 #
444 def enableSiteSliceCreation(loginbase):
445         if isPendingSite(loginbase):
446                 msg = "INFO: enableSiteSliceCreation: Pending Site (%s)" % loginbase
447                 print msg
448                 logger.info(msg)
449                 return
450
451         api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
452         try:
453                 logger.info("Enabling slice creation for site %s" % loginbase)
454                 if not debug:
455                         logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
456                         api.UpdateSite(auth.auth, loginbase, {'enabled': True})
457         except Exception, exc:
458                 print "ERROR: enableSiteSliceCreation:  %s" % exc
459                 logger.info("ERROR: enableSiteSliceCreation:  %s" % exc)
460
461 def enableSliceCreation(nodename):
462         loginbase = siteId(nodename)
463         enableSiteSliceCreation(loginbase)
464
465 '''
466 Removes site's ability to create slices. Returns previous max_slices
467 '''
468 def removeSiteSliceCreation(loginbase):
469         print "removeSiteSliceCreation(%s)" % loginbase
470
471         if isPendingSite(loginbase):
472                 msg = "INFO: removeSiteSliceCreation: Pending Site (%s)" % loginbase
473                 print msg
474                 logger.info(msg)
475                 return
476
477         api = xmlrpclib.Server(auth.server, verbose=False)
478         try:
479                 logger.info("Removing slice creation for site %s" % loginbase)
480                 if not debug:
481                         api.UpdateSite(auth.auth, loginbase, {'enabled': False})
482         except Exception, exc:
483                 logger.info("removeSiteSliceCreation:  %s" % exc)
484
485 '''
486 Removes ability to create slices. Returns previous max_slices
487 '''
488 def removeSliceCreation(nodename):
489         loginbase = siteId(nodename)
490         removeSiteSliceCreation(loginbase)
491
492
493 '''
494 QED
495 '''
496 #def enableSliceCreation(nodename, maxslices):
497 #       api = xmlrpclib.Server(auth.server, verbose=False)
498 #       anon = {'AuthMethod': "anonymous"}
499 #       siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
500 #       if len(siteid) == 1:
501 #               logger.info("Enabling slice creation for site %s" % siteId(nodename))
502 #               try:
503 #                       if not debug:
504 #                               api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
505 #               except Exception, exc:
506 #                       logger.info("API:  %s" % exc)
507 #       else:
508 #               logger.debug("Cant find site for %s.  Cannot enable creation." % nodename)
509
510 def main():
511         logger.setLevel(logging.DEBUG)
512         ch = logging.StreamHandler()
513         ch.setLevel(logging.DEBUG)
514         formatter = logging.Formatter('logger - %(message)s')
515         ch.setFormatter(formatter)
516         logger.addHandler(ch)
517         #print getpcu("kupl2.ittc.ku.edu")
518         #print getpcu("planetlab1.cse.msu.edu")
519         #print getpcu("alice.cs.princeton.edu")
520         #print nodesDbg()
521         #nodeBootState("alice.cs.princeton.edu", "boot")
522         #freezeSite("alice.cs.princeton.edu")
523         print removeSliceCreation("alice.cs.princeton.edu")
524         #enableSliceCreation("alice.cs.princeton.edu", 1024)
525         #print getSiteNodes("princeton")
526         #print siteId("alice.cs.princeton.edu")
527         #print nodePOD("alice.cs.princeton.edu")
528         #print slices("princeton")
529
530 if __name__=="__main__":
531         main()