4 # Helper functions that minipulate the PLC api.
6 # Faiyaz Ahmed <faiyaza@cs.princeton.edu
8 # $Id: plc.py,v 1.18 2007/08/29 17:26:50 soltesz Exp $
18 self.auth = {'AuthMethod': "anonymous"}
23 from config import config
29 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
31 logger = logging.getLogger("monitor")
33 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
36 api = xmlrpclib.Server(url, verbose=False, allow_none=True)
40 def __init__(self, auth, url):
43 self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
45 def __getattr__(self, name):
46 method = getattr(self.api, name)
48 raise AssertionError("method does not exist")
50 return lambda *params : method(self.auth, *params)
53 return self.api.__repr__()
56 Returns list of nodes in dbg as reported by PLC
60 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
61 anon = {'AuthMethod': "anonymous"}
62 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
63 dbgNodes.append(node['hostname'])
64 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
69 Returns loginbase for given nodename
72 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
73 anon = {'AuthMethod': "anonymous"}
74 site_id = api.GetNodes (anon, {"hostname": nodename}, ['site_id'])
76 loginbase = api.GetSites (anon, site_id[0], ["login_base"])
77 return loginbase[0]['login_base']
80 Returns list of slices for a site.
82 def slices(loginbase):
84 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
85 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
86 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
87 siteslices.append(slice['name'])
91 Returns dict of PCU info of a given node.
94 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
95 anon = {'AuthMethod': "anonymous"}
96 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
97 if nodeinfo['pcu_ids']:
98 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
99 sitepcu[nodename] = nodeinfo["ports"][0]
102 logger.info("%s doesn't have PCU" % nodename)
105 def GetPCUs(filter=None, fields=None):
106 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
107 pcu_list = api.GetPCUs(auth.auth, filter, fields)
111 Returns all site nodes for site id (loginbase).
113 def getSiteNodes(loginbase, fields=None):
114 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
116 anon = {'AuthMethod': "anonymous"}
118 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
119 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
120 nodelist.append(node['hostname'])
121 except Exception, exc:
122 logger.info("getSiteNodes: %s" % exc)
123 print "getSiteNodes: %s" % exc
126 def getPersons(filter=None, fields=None):
127 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
130 persons = api.GetPersons(auth.auth, filter, fields)
131 except Exception, exc:
132 print "getPersons: %s" % exc
133 logger.info("getPersons: %s" % exc)
136 def getSites(filter=None, fields=None):
137 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
139 anon = {'AuthMethod': "anonymous"}
141 sites = api.GetSites(anon, filter, fields)
142 except Exception, exc:
143 print "getSites: %s" % exc
144 logger.info("getSites: %s" % exc)
147 def getSiteNodes2(loginbase):
148 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
150 anon = {'AuthMethod': "anonymous"}
152 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
153 nodelist += getNodes({'node_id':nodeids})
154 except Exception, exc:
155 logger.info("getSiteNodes2: %s" % exc)
158 def getNodeNetworks(filter=None):
159 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
160 nodenetworks = api.GetNodeNetworks(auth.auth, filter, None)
163 def getNodes(filter=None, fields=None):
164 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
165 nodes = api.GetNodes(auth.auth, filter, fields)
166 #['boot_state', 'hostname',
167 #'site_id', 'date_created', 'node_id', 'version', 'nodenetwork_ids',
168 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
172 Sets boot state of a node.
174 def nodeBootState(nodename, state):
175 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
177 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
178 except Exception, exc:
179 logger.info("nodeBootState: %s" % exc)
181 def updateNodeKey(nodename, key):
182 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
184 return api.UpdateNode(auth.auth, nodename, {'key': key})
185 except Exception, exc:
186 logger.info("updateNodeKey: %s" % exc)
189 Sends Ping Of Death to node.
191 def nodePOD(nodename):
192 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
193 logger.info("Sending POD to %s" % nodename)
196 return api.RebootNode(auth.auth, nodename)
197 except Exception, exc:
198 logger.info("nodePOD: %s" % exc)
201 Freeze all site slices.
203 def suspendSlices(nodename):
204 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
205 for slice in slices(siteId(nodename)):
206 logger.info("Suspending slice %s" % slice)
209 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
210 except Exception, exc:
211 logger.info("suspendSlices: %s" % exc)
213 def enableSlices(nodename):
214 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
215 for slice in slices(siteId(nodename)):
216 logger.info("Enabling slices %s" % slice)
219 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
220 if len(slice_list) == 0:
222 slice_id = slice_list[0]['slice_id']
223 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
225 if "enabled" == attr['name'] and attr['value'] == "0":
226 logger.info("Deleted enable=0 attribute from slice %s" % slice)
227 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
228 except Exception, exc:
229 logger.info("enableSlices: %s" % exc)
230 print "exception: %s" % exc
232 #I'm commenting this because this really should be a manual process.
234 #Enable suspended site slices.
236 #def enableSlices(nodename, slicelist):
237 # api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
238 # for slice in slices(siteId(nodename)):
239 # logger.info("Suspending slice %s" % slice)
240 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
242 def enableSliceCreation(nodename):
243 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
245 loginbase = siteId(nodename)
246 logger.info("Enabling slice creation for site %s" % loginbase)
248 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
249 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
250 except Exception, exc:
251 print "ERROR: enableSliceCreation: %s" % exc
252 logger.info("ERROR: enableSliceCreation: %s" % exc)
255 Removes ability to create slices. Returns previous max_slices
257 def removeSliceCreation(nodename):
258 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
260 loginbase = siteId(nodename)
261 #numslices = api.GetSites(auth.auth, {"login_base": loginbase},
262 # ["max_slices"])[0]['max_slices']
263 logger.info("Removing slice creation for site %s" % loginbase)
265 #api.UpdateSite(auth.auth, loginbase, {'max_slices': 0})
266 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
267 except Exception, exc:
268 logger.info("removeSliceCreation: %s" % exc)
273 #def enableSliceCreation(nodename, maxslices):
274 # api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
275 # anon = {'AuthMethod': "anonymous"}
276 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
277 # if len(siteid) == 1:
278 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
281 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
282 # except Exception, exc:
283 # logger.info("API: %s" % exc)
285 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
288 logger.setLevel(logging.DEBUG)
289 ch = logging.StreamHandler()
290 ch.setLevel(logging.DEBUG)
291 formatter = logging.Formatter('logger - %(message)s')
292 ch.setFormatter(formatter)
293 logger.addHandler(ch)
294 #print getpcu("kupl2.ittc.ku.edu")
295 #print getpcu("planetlab1.cse.msu.edu")
296 #print getpcu("alice.cs.princeton.edu")
298 #nodeBootState("alice.cs.princeton.edu", "boot")
299 #freezeSite("alice.cs.princeton.edu")
300 print removeSliceCreation("alice.cs.princeton.edu")
301 #enableSliceCreation("alice.cs.princeton.edu", 1024)
302 #print getSiteNodes("princeton")
303 #print siteId("alice.cs.princeton.edu")
304 #print nodePOD("alice.cs.princeton.edu")
305 #print slices("princeton")
307 if __name__=="__main__":