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
28 XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
30 logger = logging.getLogger("monitor")
32 #XMLRPC_SERVER = config.XMLRPC_SERVER
35 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
38 api = xmlrpclib.Server(url, verbose=False, allow_none=True)
42 def __init__(self, auth, url):
45 self.api = xmlrpclib.Server(self.url, verbose=False, allow_none=True)
47 def __getattr__(self, name):
48 method = getattr(self.api, name)
50 raise AssertionError("method does not exist")
52 return lambda *params : method(self.auth, *params)
55 return self.api.__repr__()
58 Returns list of nodes in dbg as reported by PLC
62 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
63 anon = {'AuthMethod': "anonymous"}
64 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
65 dbgNodes.append(node['hostname'])
66 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
71 Returns loginbase for given nodename
74 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
75 anon = {'AuthMethod': "anonymous"}
76 site_id = api.GetNodes (anon, {"hostname": nodename}, ['site_id'])
78 loginbase = api.GetSites (anon, site_id[0], ["login_base"])
79 return loginbase[0]['login_base']
82 Returns list of slices for a site.
84 def slices(loginbase):
86 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
87 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
88 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
89 siteslices.append(slice['name'])
93 Returns dict of PCU info of a given node.
96 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
97 anon = {'AuthMethod': "anonymous"}
98 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
99 if nodeinfo['pcu_ids']:
100 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
101 sitepcu[nodename] = nodeinfo["ports"][0]
104 logger.info("%s doesn't have PCU" % nodename)
107 def GetPCUs(filter=None, fields=None):
108 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
109 pcu_list = api.GetPCUs(auth.auth, filter, fields)
113 Returns all site nodes for site id (loginbase).
115 def getSiteNodes(loginbase, fields=None):
116 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
118 anon = {'AuthMethod': "anonymous"}
120 nodeids = api.GetSites(anon, {"login_base": loginbase}, fields)[0]['node_ids']
121 for node in api.GetNodes(anon, {"node_id": nodeids}, ['hostname']):
122 nodelist.append(node['hostname'])
123 except Exception, exc:
124 logger.info("getSiteNodes: %s" % exc)
125 print "getSiteNodes: %s" % exc
128 def getPersons(filter=None, fields=None):
129 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
132 persons = api.GetPersons(auth.auth, filter, fields)
133 except Exception, exc:
134 print "getPersons: %s" % exc
135 logger.info("getPersons: %s" % exc)
138 def getSites(filter=None, fields=None):
139 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
141 anon = {'AuthMethod': "anonymous"}
143 sites = api.GetSites(anon, filter, fields)
144 except Exception, exc:
145 print "getSites: %s" % exc
146 logger.info("getSites: %s" % exc)
149 def getSiteNodes2(loginbase):
150 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
152 anon = {'AuthMethod': "anonymous"}
154 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
155 nodelist += getNodes({'node_id':nodeids})
156 except Exception, exc:
157 logger.info("getSiteNodes2: %s" % exc)
160 def getNodeNetworks(filter=None):
161 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
162 nodenetworks = api.GetNodeNetworks(auth.auth, filter, None)
165 def getNodes(filter=None, fields=None):
166 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
167 nodes = api.GetNodes(auth.auth, filter, fields)
168 #['boot_state', 'hostname',
169 #'site_id', 'date_created', 'node_id', 'version', 'nodenetwork_ids',
170 #'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
174 Sets boot state of a node.
176 def nodeBootState(nodename, state):
177 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
179 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
180 except Exception, exc:
181 logger.info("nodeBootState: %s" % exc)
183 def updateNodeKey(nodename, key):
184 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
186 return api.UpdateNode(auth.auth, nodename, {'key': key})
187 except Exception, exc:
188 logger.info("updateNodeKey: %s" % exc)
191 Sends Ping Of Death to node.
193 def nodePOD(nodename):
194 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
195 logger.info("Sending POD to %s" % nodename)
198 return api.RebootNode(auth.auth, nodename)
199 except Exception, exc:
200 logger.info("nodePOD: %s" % exc)
203 Freeze all site slices.
205 def suspendSlices(nodename):
206 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
207 for slice in slices(siteId(nodename)):
208 logger.info("Suspending slice %s" % slice)
211 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
212 except Exception, exc:
213 logger.info("suspendSlices: %s" % exc)
215 def enableSlices(nodename):
216 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
217 for slice in slices(siteId(nodename)):
218 logger.info("Enabling slices %s" % slice)
221 slice_list = api.GetSlices(auth.auth, {'name': slice}, None)
222 if len(slice_list) == 0:
224 slice_id = slice_list[0]['slice_id']
225 l_attr = api.GetSliceAttributes(auth.auth, {'slice_id': slice_id}, None)
227 if "enabled" == attr['name'] and attr['value'] == "0":
228 logger.info("Deleted enable=0 attribute from slice %s" % slice)
229 api.DeleteSliceAttribute(auth.auth, attr['slice_attribute_id'])
230 except Exception, exc:
231 logger.info("enableSlices: %s" % exc)
232 print "exception: %s" % exc
234 #I'm commenting this because this really should be a manual process.
236 #Enable suspended site slices.
238 #def enableSlices(nodename, slicelist):
239 # api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
240 # for slice in slices(siteId(nodename)):
241 # logger.info("Suspending slice %s" % slice)
242 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
244 def enableSliceCreation(nodename):
245 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
247 loginbase = siteId(nodename)
248 logger.info("Enabling slice creation for site %s" % loginbase)
250 logger.info("\tcalling UpdateSite(%s, enabled=True)" % loginbase)
251 api.UpdateSite(auth.auth, loginbase, {'enabled': True})
252 except Exception, exc:
253 print "ERROR: enableSliceCreation: %s" % exc
254 logger.info("ERROR: enableSliceCreation: %s" % exc)
257 Removes ability to create slices. Returns previous max_slices
259 def removeSliceCreation(nodename):
260 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
262 loginbase = siteId(nodename)
263 #numslices = api.GetSites(auth.auth, {"login_base": loginbase},
264 # ["max_slices"])[0]['max_slices']
265 logger.info("Removing slice creation for site %s" % loginbase)
267 #api.UpdateSite(auth.auth, loginbase, {'max_slices': 0})
268 api.UpdateSite(auth.auth, loginbase, {'enabled': False})
269 except Exception, exc:
270 logger.info("removeSliceCreation: %s" % exc)
275 #def enableSliceCreation(nodename, maxslices):
276 # api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
277 # anon = {'AuthMethod': "anonymous"}
278 # siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
279 # if len(siteid) == 1:
280 # logger.info("Enabling slice creation for site %s" % siteId(nodename))
282 # if not config.debug:
283 # api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
284 # except Exception, exc:
285 # logger.info("API: %s" % exc)
287 # logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
290 logger.setLevel(logging.DEBUG)
291 ch = logging.StreamHandler()
292 ch.setLevel(logging.DEBUG)
293 formatter = logging.Formatter('logger - %(message)s')
294 ch.setFormatter(formatter)
295 logger.addHandler(ch)
296 #print getpcu("kupl2.ittc.ku.edu")
297 #print getpcu("planetlab1.cse.msu.edu")
298 #print getpcu("alice.cs.princeton.edu")
300 #nodeBootState("alice.cs.princeton.edu", "boot")
301 #freezeSite("alice.cs.princeton.edu")
302 print removeSliceCreation("alice.cs.princeton.edu")
303 #enableSliceCreation("alice.cs.princeton.edu", 1024)
304 #print getSiteNodes("princeton")
305 #print siteId("alice.cs.princeton.edu")
306 #print nodePOD("alice.cs.princeton.edu")
307 #print slices("princeton")
309 if __name__=="__main__":