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