+ add additional support for RT tickets, closing, changing Subject, and CCs.
[monitor.git] / plc.py
diff --git a/plc.py b/plc.py
index 42335aa..ff5efd3 100644 (file)
--- a/plc.py
+++ b/plc.py
@@ -5,7 +5,7 @@
 # 
 # Faiyaz Ahmed <faiyaza@cs.princeton.edu
 #
-# $Id: $
+# $Id: plc.py,v 1.15 2007/06/29 12:42:22 soltesz Exp $
 #
 
 from emailTxt import *
@@ -13,11 +13,13 @@ import xml, xmlrpclib
 import logging
 import auth
 import time
-import config
+from config import config,XMLRPC_SERVER
 
 logger = logging.getLogger("monitor")
 
-XMLRPC_SERVER = 'https://www.planet-lab.org/PLCAPI/'
+#XMLRPC_SERVER = config.XMLRPC_SERVER
+
+config = config()
 
 '''
 Returns list of nodes in dbg as reported by PLC
@@ -26,9 +28,8 @@ def nodesDbg():
        dbgNodes = []
        api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
        anon = {'AuthMethod': "anonymous"}
-       allnodes = api.AnonAdmGetNodes(anon, [], ['hostname','boot_state'])
-       for node in allnodes:
-               if node['boot_state'] == 'dbg': dbgNodes.append(node['hostname'])
+       for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
+               dbgNodes.append(node['hostname'])
        logger.info("%s nodes in debug according to PLC." %len(dbgNodes))
        return dbgNodes
 
@@ -39,17 +40,21 @@ Returns loginbase for given nodename
 def siteId(nodename):
        api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
        anon = {'AuthMethod': "anonymous"}
-       site_id = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
+       site_id = api.GetNodes (anon, {"hostname": nodename}, ['site_id'])
        if len(site_id) == 1:
-               loginbase = api.AnonAdmGetSites (anon, site_id, ["login_base"])
+               loginbase = api.GetSites (anon, site_id[0], ["login_base"])
                return loginbase[0]['login_base']
 
 '''
 Returns list of slices for a site.
 '''
 def slices(loginbase):
+       siteslices = []
        api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
-       return api.SliceListNames (auth.auth, loginbase)
+       sliceids = api.GetSites (auth.auth, {"login_base" : loginbase}, ["slice_ids"])[0]['slice_ids']
+       for slice in api.GetSlices(auth.auth, {"slice_id" : sliceids}, ["name"]):
+               siteslices.append(slice['name'])
+       return siteslices
 
 '''
 Returns dict of PCU info of a given node.
@@ -57,37 +62,14 @@ Returns dict of PCU info of a given node.
 def getpcu(nodename):
        api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
        anon = {'AuthMethod': "anonymous"}
-       nodes = []
-       site_id = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
-       if len(site_id) == 1:
-               # PCU uname, pw, etc
-               try:
-                       sitepcu = api.AdmGetSitePowerControlUnits(auth.auth, site_id[0])[0]
-                       # returns node_id and port
-                       sitepcuports = api.AdmGetPowerControlUnitNodes(auth.auth, sitepcu['pcu_id'])
-                       # Joining feilds
-                       for nodeidports in sitepcuports:
-                               nodeidports.update(api.AnonAdmGetNodes(anon, 
-                               [nodeidports['node_id']], ["node_id", "hostname"])[0])
-                               nodes.append(nodeidports)
-
-                       # WHY THE FUCK DOES EVERY XMl+RPC RETURN A FUCKING ARRAY?????
-                       # FURTHER, WHY THE FUCK WOULD YOU RETURN A NODE-ID WHEN SANITY WOULD SUGGEST
-                       # FQDN???? /RANT
-                       for node in nodes:
-                               sitepcu[node['hostname']] = node['port_number']
-
-                       # Sanity Check.  Make sure the node is in the return, if not, barf.
-                       if nodename in sitepcu.keys():
-                               return sitepcu
-                       else:
-                               raise Exception
-               except Exception, err:
-                       logger.debug("getpcu: %s" % err)
-                       return
+       nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
+       if nodeinfo['pcu_ids']:
+               sitepcu = api.GetPCUs(auth.auth, nodeinfo['pcu_ids'])[0]
+               sitepcu[nodename] = nodeinfo["ports"][0]
+               return sitepcu
        else:
-               logger.info("Cant find site for %s" % nodename)
-
+               logger.info("%s doesn't have PCU" % nodename)
+               return False
 
 '''
 Returns all site nodes for site id (loginbase).
@@ -97,47 +79,68 @@ def getSiteNodes(loginbase):
        nodelist = []
        anon = {'AuthMethod': "anonymous"}
        try:
-               site_id = api.AnonAdmQuerySite(anon, {'site_loginbase': "%s" % loginbase})
-               node_ids = api.AnonAdmGetSiteNodes(anon, site_id)
-               for node in api.AnonAdmGetNodes(anon, node_ids["%s" % site_id[0]], ["hostname"]):
+               nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
+               for node in api.GetNodes(anon, {"node_id": nodeids}):
                        nodelist.append(node['hostname'])
        except Exception, exc:
                logger.info("getSiteNodes:  %s" % exc)
        return nodelist
 
+def getSites(filter=None):
+       api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+       sites = []
+       anon = {'AuthMethod': "anonymous"}
+       try:
+               sites = api.GetSites(anon, filter, None)
+       except Exception, exc:
+               print "getSiteNodes2:  %s" % exc
+               logger.info("getSiteNodes2:  %s" % exc)
+       return sites
+
+def getSiteNodes2(loginbase):
+       api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+       nodelist = []
+       anon = {'AuthMethod': "anonymous"}
+       try:
+               nodeids = api.GetSites(anon, {"login_base": loginbase})[0]['node_ids']
+               nodelist += getNodes({'node_id':nodeids})
+       except Exception, exc:
+               logger.info("getSiteNodes2:  %s" % exc)
+       return nodelist
+
+def getNodeNetworks(filter=None):
+       api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+       nodenetworks = api.GetNodeNetworks(auth.auth, filter, None)
+       return nodenetworks
+
+def getNodes(filter=None):
+       api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+       nodes = api.GetNodes(auth.auth, filter, ['boot_state', 'hostname', 
+                       'site_id', 'date_created', 'node_id', 'version', 'nodenetwork_ids',
+                       'last_updated', 'peer_node_id', 'ssh_rsa_key' ])
+       return nodes
+
 '''
 Sets boot state of a node.
 '''
 def nodeBootState(nodename, state):
        api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
-       anon = {'AuthMethod': "anonymous"}
-       node_id = api.AnonAdmQueryNode(anon, {'node_hostname' : nodename})
-       if len(node_id) == 1:
-               logger.info("Setting node %s to %s" %(nodename, state))
-               try:
-                       if not config.debug:
-                               api.AdmUpdateNode(auth.auth, node_id[0], {'boot_state': state})
-               except Exception, exc:
-                       logger.info("nodeBootState:  %s" % exc)
-       else:
-               logger.info("Cant find node %s to toggle boot state" % nodename)
+       try:
+               return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
+       except Exception, exc:
+               logger.info("nodeBootState:  %s" % exc)
 
 '''
 Sends Ping Of Death to node.
 '''
 def nodePOD(nodename):
        api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
-       anon = {'AuthMethod': "anonymous"}
-       node_id = api.AnonAdmQueryNode(anon, {'node_hostname' : nodename})
-       if len(node_id) == 1:
-               logger.info("Sending POD to %s" % nodename)
-               try:
-                       if not config.debug:
-                               api.AdmRebootNode(auth.auth, node_id[0])
-               except Exception, exc:
+       logger.info("Sending POD to %s" % nodename)
+       try:
+               if not config.debug:
+                       return api.RebootNode(auth.auth, nodename)
+       except Exception, exc:
                        logger.info("nodePOD:  %s" % exc)
-       else:
-               logger.info("Cant find node %s to send POD." % nodename)
 
 '''
 Freeze all site slices.
@@ -148,8 +151,7 @@ def suspendSlices(nodename):
                logger.info("Suspending slice %s" % slice)
                try:
                        if not config.debug:
-                               api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", 
-                               {"state" : "suspended"})
+                               api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
                except Exception, exc:
                        logger.info("suspendSlices:  %s" % exc)
 
@@ -170,19 +172,16 @@ Removes ability to create slices. Returns previous max_slices
 '''
 def removeSliceCreation(nodename):
        api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
-       anon = {'AuthMethod': "anonymous"}
-       siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
-       numslices = api.AdmGetSites(auth.auth, siteid, ["max_slices"])[0]['max_slices']
-       if len(siteid) == 1:
-               logger.info("Removing slice creation for site %s" % siteId(nodename))
-               try:
-                       if not config.debug:
-                               api.AdmUpdateSite(auth.auth, siteid[0], {'max_slices': 0})
-                       return numslices
-               except Exception, exc:
-                       logger.info("removeSliceCreation:  %s" % exc)
-       else:
-               logger.debug("Cant find site for %s.  Cannot revoke creation." % nodename)
+       try:
+               loginbase = siteId(nodename)
+               #numslices = api.GetSites(auth.auth, {"login_base": loginbase}, 
+               #               ["max_slices"])[0]['max_slices']
+               logger.info("Removing slice creation for site %s" % loginbase)
+               if not config.debug:
+                       #api.UpdateSite(auth.auth, loginbase, {'max_slices': 0})
+                       api.UpdateSite(auth.auth, loginbase, {'enabled': False})
+       except Exception, exc:
+               logger.info("removeSliceCreation:  %s" % exc)
 
 '''
 QED
@@ -212,13 +211,14 @@ def main():
        #print getpcu("planetlab1.cse.msu.edu")
        #print getpcu("alice.cs.princeton.edu")
        #print nodesDbg()
-       #nodeBootState("alice.cs.princaeton.edu", "boot")
+       #nodeBootState("alice.cs.princeton.edu", "boot")
        #freezeSite("alice.cs.princeton.edu")
-       #removeSliceCreation("alice.cs.princeton.edu")
+       print removeSliceCreation("alice.cs.princeton.edu")
        #enableSliceCreation("alice.cs.princeton.edu", 1024)
-       print getSiteNodes("princeton")
+       #print getSiteNodes("princeton")
        #print siteId("alice.cs.princeton.edu")
-       #print nodePOD("planetlab5.warsaw.rd.tp.pl")
+       #print nodePOD("alice.cs.princeton.edu")
+       #print slices("princeton")
 
 if __name__=="__main__":
        import reboot