4 # Helper functions that minipulate the PLC api.
6 # Faiyaz Ahmed <faiyaza@cs.princeton.edu
8 # $Id: plc.py,v 1.1 2006/11/14 19:27:09 faiyaza Exp $
11 from emailTxt import *
18 logger = logging.getLogger("monitor")
20 XMLRPC_SERVER = 'https://www.planet-lab.org/PLCAPI/'
23 Returns list of nodes in dbg as reported by PLC
27 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
28 anon = {'AuthMethod': "anonymous"}
29 for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
30 dbgNodes.append(node['hostname'])
31 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
36 Returns loginbase for given nodename
39 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
40 anon = {'AuthMethod': "anonymous"}
41 site_id = api.GetNodes (anon, {"hostname": nodename}, ['site_id'])
43 loginbase = api.GetSites (anon, site_id[0], ["login_base"])
44 return loginbase[0]['login_base']
47 Returns list of slices for a site.
49 def slices(loginbase):
51 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
52 sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
53 for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
54 siteslices.append(slice['name'])
58 Returns dict of PCU info of a given node.
61 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
62 anon = {'AuthMethod': "anonymous"}
63 nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
64 if nodeinfo['pcu_ids']:
65 sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
66 sitepcu[nodename] = nodeinfo["ports"][0]
69 logger.info("%s doesn't have PCU" % nodename)
74 Returns all site nodes for site id (loginbase).
76 def getSiteNodes(loginbase):
77 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
79 anon = {'AuthMethod': "anonymous"}
81 nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
82 for node in api.GetNodes(anon, {"node_id": nodeids}):
83 nodelist.append(node['hostname'])
84 except Exception, exc:
85 logger.info("getSiteNodes: %s" % exc)
90 Sets boot state of a node.
92 def nodeBootState(nodename, state):
93 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
95 return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
96 except Exception, exc:
97 logger.info("nodeBootState: %s" % exc)
100 Sends Ping Of Death to node.
102 def nodePOD(nodename):
103 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
104 logger.info("Sending POD to %s" % nodename)
107 return api.RebootNode(auth.auth, nodename)
108 except Exception, exc:
109 logger.info("nodePOD: %s" % exc)
112 Freeze all site slices.
114 def suspendSlices(nodename):
115 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
116 for slice in slices(siteId(nodename)):
117 logger.info("Suspending slice %s" % slice)
120 api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
121 except Exception, exc:
122 logger.info("suspendSlices: %s" % exc)
125 #I'm commenting this because this really should be a manual process.
127 #Enable suspended site slices.
129 #def enableSlices(nodename, slicelist):
130 # api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
131 # for slice in slices(siteId(nodename)):
132 # logger.info("Suspending slice %s" % slice)
133 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
137 Removes ability to create slices. Returns previous max_slices
139 def removeSliceCreation(nodename):
140 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
142 loginbase = siteId(nodename)
143 numslices = api.GetSites(auth.auth, {"login_base": loginbase},
144 ["max_slices"])[0]['max_slices']
145 logger.info("Removing slice creation for site %s" % loginbase)
147 api.UpdateSite(auth.auth, siteid, {'max_slices': 0})
148 except Exception, exc:
149 logger.info("removeSliceCreation: %s" % exc)
154 def enableSliceCreation(nodename, maxslices):
155 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
156 anon = {'AuthMethod': "anonymous"}
157 siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
159 logger.info("Enabling slice creation for site %s" % siteId(nodename))
162 api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
163 except Exception, exc:
164 logger.info("API: %s" % exc)
166 logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
169 logger.setLevel(logging.DEBUG)
170 ch = logging.StreamHandler()
171 ch.setLevel(logging.DEBUG)
172 formatter = logging.Formatter('logger - %(message)s')
173 ch.setFormatter(formatter)
174 logger.addHandler(ch)
175 #print getpcu("kupl2.ittc.ku.edu")
176 #print getpcu("planetlab1.cse.msu.edu")
177 #print getpcu("alice.cs.princeton.edu")
179 #nodeBootState("alice.cs.princeton.edu", "boot")
180 #freezeSite("alice.cs.princeton.edu")
181 print removeSliceCreation("alice.cs.princeton.edu")
182 #enableSliceCreation("alice.cs.princeton.edu", 1024)
183 #print getSiteNodes("princeton")
184 #print siteId("alice.cs.princeton.edu")
185 #print nodePOD("alice.cs.princeton.edu")
186 #print slices("princeton")
188 if __name__=="__main__":