4 # Helper functions that minipulate the PLC api.
6 # Faiyaz Ahmed <faiyaza@cs.princeton.edu
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 allnodes = api.AnonAdmGetNodes(anon, [], ['hostname','boot_state'])
31 if node['boot_state'] == 'dbg': dbgNodes.append(node['hostname'])
32 logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
37 Returns loginbase for given nodename
40 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
41 anon = {'AuthMethod': "anonymous"}
42 site_id = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
44 loginbase = api.AnonAdmGetSites (anon, site_id, ["login_base"])
45 return loginbase[0]['login_base']
48 Returns list of slices for a site.
50 def slices(loginbase):
51 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
52 return api.SliceListNames (auth.auth, loginbase)
55 Returns dict of PCU info of a given node.
58 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
59 anon = {'AuthMethod': "anonymous"}
61 site_id = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
65 sitepcu = api.AdmGetSitePowerControlUnits(auth.auth, site_id[0])[0]
66 # returns node_id and port
67 sitepcuports = api.AdmGetPowerControlUnitNodes(auth.auth, sitepcu['pcu_id'])
69 for nodeidports in sitepcuports:
70 nodeidports.update(api.AnonAdmGetNodes(anon,
71 [nodeidports['node_id']], ["node_id", "hostname"])[0])
72 nodes.append(nodeidports)
74 # WHY THE FUCK DOES EVERY XMl+RPC RETURN A FUCKING ARRAY?????
75 # FURTHER, WHY THE FUCK WOULD YOU RETURN A NODE-ID WHEN SANITY WOULD SUGGEST
78 sitepcu[node['hostname']] = node['port_number']
80 # Sanity Check. Make sure the node is in the return, if not, barf.
81 if nodename in sitepcu.keys():
85 except Exception, err:
86 logger.debug("getpcu: %s" % err)
89 logger.info("Cant find site for %s" % nodename)
93 Returns all site nodes for site id (loginbase).
95 def getSiteNodes(loginbase):
96 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
98 anon = {'AuthMethod': "anonymous"}
100 site_id = api.AnonAdmQuerySite(anon, {'site_loginbase': "%s" % loginbase})
101 node_ids = api.AnonAdmGetSiteNodes(anon, site_id)
102 for node in api.AnonAdmGetNodes(anon, node_ids["%s" % site_id[0]], ["hostname"]):
103 nodelist.append(node['hostname'])
104 except Exception, exc:
105 logger.info("getSiteNodes: %s" % exc)
109 Sets boot state of a node.
111 def nodeBootState(nodename, state):
112 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
113 anon = {'AuthMethod': "anonymous"}
114 node_id = api.AnonAdmQueryNode(anon, {'node_hostname' : nodename})
115 if len(node_id) == 1:
116 logger.info("Setting node %s to %s" %(nodename, state))
119 api.AdmUpdateNode(auth.auth, node_id[0], {'boot_state': state})
120 except Exception, exc:
121 logger.info("nodeBootState: %s" % exc)
123 logger.info("Cant find node %s to toggle boot state" % nodename)
126 Sends Ping Of Death to node.
128 def nodePOD(nodename):
129 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
130 anon = {'AuthMethod': "anonymous"}
131 node_id = api.AnonAdmQueryNode(anon, {'node_hostname' : nodename})
132 if len(node_id) == 1:
133 logger.info("Sending POD to %s" % nodename)
136 api.AdmRebootNode(auth.auth, node_id[0])
137 except Exception, exc:
138 logger.info("nodePOD: %s" % exc)
140 logger.info("Cant find node %s to send POD." % nodename)
143 Freeze all site slices.
145 def suspendSlices(nodename):
146 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
147 for slice in slices(siteId(nodename)):
148 logger.info("Suspending slice %s" % slice)
151 api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state",
152 {"state" : "suspended"})
153 except Exception, exc:
154 logger.info("suspendSlices: %s" % exc)
157 #I'm commenting this because this really should be a manual process.
159 #Enable suspended site slices.
161 #def enableSlices(nodename, slicelist):
162 # api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
163 # for slice in slices(siteId(nodename)):
164 # logger.info("Suspending slice %s" % slice)
165 # api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
169 Removes ability to create slices. Returns previous max_slices
171 def removeSliceCreation(nodename):
172 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
173 anon = {'AuthMethod': "anonymous"}
174 siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
175 numslices = api.AdmGetSites(auth.auth, siteid, ["max_slices"])[0]['max_slices']
177 logger.info("Removing slice creation for site %s" % siteId(nodename))
180 api.AdmUpdateSite(auth.auth, siteid[0], {'max_slices': 0})
182 except Exception, exc:
183 logger.info("removeSliceCreation: %s" % exc)
185 logger.debug("Cant find site for %s. Cannot revoke creation." % nodename)
190 def enableSliceCreation(nodename, maxslices):
191 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
192 anon = {'AuthMethod': "anonymous"}
193 siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
195 logger.info("Enabling slice creation for site %s" % siteId(nodename))
198 api.AdmUpdateSite(auth.auth, siteid[0], {"max_slices" : maxslices})
199 except Exception, exc:
200 logger.info("API: %s" % exc)
202 logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
205 logger.setLevel(logging.DEBUG)
206 ch = logging.StreamHandler()
207 ch.setLevel(logging.DEBUG)
208 formatter = logging.Formatter('logger - %(message)s')
209 ch.setFormatter(formatter)
210 logger.addHandler(ch)
211 #print getpcu("kupl2.ittc.ku.edu")
212 #print getpcu("planetlab1.cse.msu.edu")
213 #print getpcu("alice.cs.princeton.edu")
215 #nodeBootState("alice.cs.princaeton.edu", "boot")
216 #freezeSite("alice.cs.princeton.edu")
217 #removeSliceCreation("alice.cs.princeton.edu")
218 #enableSliceCreation("alice.cs.princeton.edu", 1024)
219 print getSiteNodes("princeton")
220 #print siteId("alice.cs.princeton.edu")
221 #print nodePOD("planetlab5.warsaw.rd.tp.pl")
223 if __name__=="__main__":