- set API URL to www.planet-lab.org
[monitor.git] / plc.py
diff --git a/plc.py b/plc.py
index 6d5c3ca..c2b22b7 100644 (file)
--- a/plc.py
+++ b/plc.py
@@ -5,7 +5,7 @@
 # Faiyaz Ahmed <faiyaza@cs.princeton.edu>
 # Copyright (C) 2006, 2007 The Trustees of Princeton University
 #
-# $Id: plc.py,v 1.7 2007/02/08 22:43:11 mef Exp $
+# $Id: plc.py,v 1.11 2007/02/19 17:42:21 mef Exp $
 #
 
 from emailTxt import *
@@ -17,7 +17,7 @@ import getpass, getopt
 import sys
 
 logger = logging.getLogger("monitor")
-XMLRPC_SERVER = 'https://www2.planet-lab.org/PLCAPI/'
+XMLRPC_SERVER = 'https://www.planet-lab.org/PLCAPI/'
 api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none = True)
 auth = None
 
@@ -47,7 +47,7 @@ def siteId(argv):
 def slices(argv):
        """Returns list of slices for a site."""
 
-       global api, anon, auth
+       global api, auth
        if len(argv) < 1:
                printUsage("not enough arguments; please provide loginbase")
                sys.exit(1)
@@ -64,7 +64,7 @@ def slices(argv):
 def getpcu(argv):
        """Returns dict of PCU info of a given node."""
 
-       global api, anon, auth
+       global api, auth
        nodename = argv[0].lower()
        if auth is None:
                printUsage("requires admin privs")
@@ -122,7 +122,6 @@ def renewAllSlices (argv):
                for slice_attribute in slice_attributes:
                        if slice_attribute['name'] == "enabled":
                                print "%s is suspended" % name
-               continue
                if exp < newexp:
                        newdate = time.asctime(time.localtime(newexp))
                        ret = api.SliceRenew(auth,name,newexp)
@@ -132,7 +131,7 @@ def renewAllSlices (argv):
 def nodeBootState(argv):
        """Sets boot state of a node."""
 
-       global api, anon, auth
+       global api, auth
        if len(argv) < 1:
                printUsage("not enough arguments")
                sys.exit(1)
@@ -160,6 +159,7 @@ def nodeBootState(argv):
        else:
                logger.info("Cant find node %s to toggle boot state" % nodename)
 
+
 def nodePOD(argv):
        """Sends Ping Of Death to node."""
 
@@ -185,10 +185,24 @@ def nodePOD(argv):
        else:
                logger.info("Cant find node %s to send POD." % nodename)
 
+def suspendSlice(argv):
+       """Freeze specific slice."""
+       global api, auth
+       if auth is None:
+               printUsage("requires admin privs")
+               sys.exit(1)
+
+       slice = argv[0]
+       logger.info("Suspending slice %s" % slice)
+       try:
+               if not config.debug:
+                       api.AddSliceAttribute(auth, slice, "enabled", "0")
+       except Exception, exc:
+               logger.info("suspendSlices:  %s" % exc)
+
 def suspendSlices(argv):
        """Freeze all site slices."""
-
-       global api, anon, auth
+       global api, auth
        if auth is None:
                printUsage("requires admin privs")
                sys.exit(1)
@@ -197,89 +211,100 @@ def suspendSlices(argv):
        else: siteslices = slices(argv)
 
        for slice in siteslices:
-               logger.info("Suspending slice %s" % slice)
-               try:
-                       if not config.debug:
-                               api.SliceAttributeAdd(auth, slice, "plc_slice_state", 
-                               {"state" : "suspended"})
-               except Exception, exc:
-                       logger.info("suspendSlices:  %s" % exc)
+               suspendSlice([slice])
+
+def __enableSlice(slice):
+       logger.info("unfreezing slice %s" % slice['name'])
+       slice_attributes = api.GetSliceAttributes(auth,slice['slice_attribute_ids'])
+       for slice_attribute in slice_attributes:
+               if slice_attribute['name'] == "enabled":
+                       api.DeleteSliceAttribute(auth, slice_attribute['slice_attribute_id'])
+       
+def enableSlice(arg):
+       """Enable suspended slice."""
+       global api, auth
+       if auth is None:
+               printUsage("requires admin privs")
+               sys.exit(1)
 
+       slicename = arg[0]
+       gSlices = {'name':slicename}
+       slice = api.GetSlices(auth,gSlices)
+       if len(slice) == 1:
+               __enableSlice(slice[0])
+       else:
+               logger.info("slice %s not found" % slicename)
 
 def enableSlices(argv):
        """Enable suspended site slices."""
 
-       global api, anon, auth
+       global api, auth
        if auth is None:
                printUsage("requires admin privs")
                sys.exit(1)
 
-       if argv[0].find(".") <> -1: siteslices = slices([siteId(argv)])
-       else: siteslices = slices(argv)
-
-       for slice in siteslices:
-               logger.info("unfreezing slice %s" % slice)
-               api.SliceAttributeDelete(auth, slice, "plc_slice_state")
-
+       if argv[0].find(".") <> -1:
+               slices = api.GetSlices(auth,[siteId(argv)])
+       else:
+               gSlices = {'name':"%s_*"%argv[0]}
+               slices = api.GetSlices(auth,gSlices)
 
-def removeSliceCreation(argv):
-       """Removes ability to create slices. Returns previous max_slices"""
+       for slice in slices:
+               __enableSlice(slice)
 
-       global api, anon, auth
+def setSliceMax(argv):
+       """Set max_slices for Slice. Returns previous max_slices"""
+       global api, auth
        if auth is None:
                printUsage("requires admin privs")
                sys.exit(1)
 
        name = argv[0]
+       val = int(argv[1])
        if name.find(".") <> -1:
-               siteid = api.AnonAdmQuerySite (anon, {"node_hostname": name})
-               loginbase = siteId(name)
+               site_ids = api.GetNodes(auth, [name], ['site_id'])
+               if len(site_ids) == 1:
+                       site_id = [site_ids[0]['site_id']]
+                       loginbase = api.GetSites (auth, site_id, ["login_base"])
+               else:
+                       printUsage("invalid hostname %s" % name)
+                       sys.exit(1)
        else:
-               siteid = api.AnonAdmQuerySite (anon, {"site_loginbase": name})          
+               site_ids = api.GetSites(auth, {'login_base': "%s" % name}, ['site_id'])
+               if len(site_ids) == 1:
+                       siteid = site_ids[0]['site_id']
                loginbase = name
 
-       numslices = api.AdmGetSites(auth, siteid, ["max_slices"])[0]['max_slices']
-       if len(siteid) == 1:
-               logger.info("Removing slice creation for site %s" % loginbase)
-               try:
-                       if not config.debug:
-                               api.AdmUpdateSite(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." % loginbase)
+       numslices = api.GetSites(auth, [siteid], ["max_slices"])[0]['max_slices']
+       try:
+               api.UpdateSite(auth, siteid, {'max_slices': val})
+               logger.info("_SetSliceMax:  %s max_slices was %d set to %d" % (loginbase,numslices,val))
+               return numslices
+       except Exception, exc:
+               logger.info("_SetSliceMax:  %s" % exc)
 
-def enableSliceCreation(argv):
-       """QED"""
 
-       global api, anon, auth
+def authCheck(arg):
+       """Enable suspended slice."""
+       global api, auth
        if auth is None:
                printUsage("requires admin privs")
                sys.exit(1)
 
-       if len(argv) < 2:
-               printUsage("requires maxslice arg")
+       if len(arg) != 2:
+               printUsage("incorrect arguments")
                sys.exit(1)
-
-       maxslices = int(argv[1])
-       name = argv[0]
-       if name.find(".") <> -1:
-               siteid = api.AnonAdmQuerySite (anon, {"node_hostname": name})
-               loginbase = siteId(name)
-       else:
-               siteid = api.AnonAdmQuerySite (anon, {"site_loginbase": name})          
-               loginbase = name
-
-       if len(siteid) == 1:
-               logger.info("Enabling slice creation for site %s" % loginbase)
-               try:
-                       if not config.debug:
-                               api.AdmUpdateSite(auth, siteid[0], {"max_slices" : maxslices})
-               except Exception, exc:
-                       logger.info("API:  %s" % exc)
-       else:
-               logger.debug("Cant find site for %s.  Cannot enable creation." % loginbase)
+       user= arg[0]
+       pwd = arg[1]
+       
+       check = {}
+       check['Username'] = user
+       check['AuthMethod'] = "password"
+       check['AuthString'] = pwd
+       for role in ['user','tech','pi','admin']:
+               check['Role'] = role
+               res = api.AdmAuthCheck(check)
+               print "%s -> %s %d" % (user,role,res)
 
 
 
@@ -303,9 +328,8 @@ def printUsage(error = None):
                print "%20s\t%20s" % (name, function.__doc__)
        
 def main():
-       global api, auth, anon
+       global api, auth
 
-       anon = {"AuthMethod":"anonymous"}
        auth = None
        user = None
        password = None
@@ -362,10 +386,12 @@ funclist = (("nodesDbg",nodesDbg),
            ("siteNodes", getSiteNodes),
            ("nodeBootState", nodeBootState),
            ("nodePOD", nodePOD),
+           ("freezeSlice", suspendSlice),
+           ("unfreezeSlice", enableSlice),
            ("freezeSlices", suspendSlices),
            ("unfreezeSlices", enableSlices),
-           ("disableSliceCreation",removeSliceCreation),
-           ("enableSliceCreation", enableSliceCreation),
+           ("setSliceMax", setSliceMax),
+           ("authCheck", authCheck),
            ("renewAllSlices", renewAllSlices))
 
 functbl = {}