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