-#!/bin/env python
+#
+# plc.py
#
# Helper functions that minipulate the PLC api.
#
-# Faiyaz Ahmed <faiyaza@cs.princeton.edu>
-# Copyright (C) 2006, 2007 The Trustees of Princeton University
+# Faiyaz Ahmed <faiyaza@cs.princeton.edu
#
-# $Id: plc.py,v 1.4 2007/02/01 14:25:56 mef Exp $
+# $Id: plc.py,v 1.14 2007/04/06 16:16:54 faiyaza Exp $
#
from emailTxt import *
import xml, xmlrpclib
import logging
+import auth
import time
-import config
-import getpass, getopt
-import sys
+from config import config
+
+config = config()
logger = logging.getLogger("monitor")
-XMLRPC_SERVER = 'https://www.planet-lab.org/PLCAPI/'
-api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none = True)
-auth = None
-def nodesDbg(argv):
- """Returns list of nodes in dbg as reported by PLC"""
+XMLRPC_SERVER = 'https://www.planet-lab.org/PLCAPI/'
- global api, auth
+'''
+Returns list of nodes in dbg as reported by PLC
+'''
+def nodesDbg():
dbgNodes = []
- allnodes = api.GetNodes(auth, None, ['hostname','boot_state'])
- for node in allnodes:
- if node['boot_state'] == 'dbg': dbgNodes.append(node['hostname'])
- logger.info("%d nodes in debug according to PLC." %len(dbgNodes))
+ api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+ anon = {'AuthMethod': "anonymous"}
+ 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
-def siteId(argv):
- """Returns loginbase for given nodename"""
-
- global api, anon, auth
- nodename = argv[0]
- site_id = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
+'''
+Returns loginbase for given nodename
+'''
+def siteId(nodename):
+ api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+ anon = {'AuthMethod': "anonymous"}
+ 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']
-def slices(argv):
- """Returns list of slices for a site."""
-
- global api, anon, auth
- loginbase = argv[0]
- if auth is None:
- printUsage("requires admin privs")
- sys.exit(1)
- return api.SliceListNames (auth, loginbase)
-
-def getpcu(argv):
- """Returns dict of PCU info of a given node."""
-
- global api, anon, auth
- nodename = argv[0].lower()
- if auth is None:
- printUsage("requires admin privs")
- sys.exit(1)
-
- nodes = []
- site_id = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
- if len(site_id) == 1:
- try:
- sitepcus = api.AdmGetSitePowerControlUnits(auth, site_id[0])
- for sitepcu in sitepcus:
- sitepcuports = api.AdmGetPowerControlUnitNodes(auth, sitepcu['pcu_id'])
- for sitepcuport in sitepcuports:
- node_id = [sitepcuport['node_id']]
- node = api.AnonAdmGetNodes(anon,node_id,["hostname"])
- if len(node)==0:
- continue
- node = node[0]
- hostname = node['hostname'].lower()
- if hostname == nodename:
- sitepcu['port_number']=sitepcuport['port_number']
- return sitepcu
-
- except Exception, err:
- logger.debug("getpcu: %s" % err)
- return
+'''
+Returns list of slices for a site.
+'''
+def slices(loginbase):
+ siteslices = []
+ api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+ 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.
+'''
+def getpcu(nodename):
+ api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+ anon = {'AuthMethod': "anonymous"}
+ 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)
-
-
-def getSiteNodes(argv):
- """Returns all site nodes for site id (loginbase)."""
- global api, anon, auth
- loginbase = argv[0]
+ logger.info("%s doesn't have PCU" % nodename)
+ return False
+
+'''
+Returns all site nodes for site id (loginbase).
+'''
+def getSiteNodes(loginbase):
+ api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
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)
- nodelist.sort()
return nodelist
-def renewAllSlices (argv):
- """Sets the expiration date of all slices to given date"""
- global api, anon, auth
-
- newexp = argv[0]
- # convert time string using fmt "%B %d %Y" to epoch integer
+def getSites(filter=None):
+ api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+ sites = []
+ anon = {'AuthMethod': "anonymous"}
try:
- newexp = int(time.mktime(time.strptime(newexp,"%B %d %Y")))
- except ValueError, e:
- errormsg = """Expecting date to be in Month Day Year
- e.g., April 7 2007
- new expiration date provided %s""" % newexp
- printUsage(errormsg)
- sys.exit(1)
-
- slices = api.GetSlices(auth)
- for slice in slices:
- name = slice['name']
- exp = int(slice['expires'])
- olddate = time.asctime(time.localtime(exp))
- slice_attributes = api.GetSliceAttributes(auth,slice['slice_attribute_ids'])
- 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)
- if ret == 0:
- print "failed to renew %s" %name
-
-def nodeBootState(argv):
- """Sets boot state of a node."""
-
- global api, anon, auth
- if len(argv) <> 2:
- printUsage("not enough arguments")
- sys.exit(1)
-
- nodename = argv[0]
- state = argv[1]
-
- if auth is None:
- printUsage("requires admin privs")
- sys.exit(1)
-
- 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, 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)
-
-def nodePOD(argv):
- """Sends Ping Of Death to node."""
+ sites = api.GetSites(anon, filter, None)
+ except Exception, exc:
+ print "getSiteNodes2: %s" % exc
+ logger.info("getSiteNodes2: %s" % exc)
+ return sites
- global api, anon, auth
- nodename = argv[0]
- if auth is None:
- printUsage("requires admin privs")
- sys.exit(1)
+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
- 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, node_id[0])
- except Exception, exc:
+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)
+ 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)
+ 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)
-
-def suspendSlices(argv):
- """Freeze all site slices."""
- global api, anon, 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:
+'''
+Freeze all site slices.
+'''
+def suspendSlices(nodename):
+ api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+ for slice in slices(siteId(nodename)):
logger.info("Suspending slice %s" % slice)
try:
if not config.debug:
- api.SliceAttributeAdd(auth, slice, "plc_slice_state",
- {"state" : "suspended"})
+ api.AddSliceAttribute(auth.auth, slice, "enabled", "0")
except Exception, exc:
logger.info("suspendSlices: %s" % exc)
-def enableSlices(argv):
- """Enable suspended site slices."""
-
- global api, anon, 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")
-
-
-def removeSliceCreation(argv):
- """Removes ability to create slices. Returns previous max_slices"""
-
- global api, anon, auth
- if auth is None:
- printUsage("requires admin privs")
- sys.exit(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
+#I'm commenting this because this really should be a manual process.
+#'''
+#Enable suspended site slices.
+#'''
+#def enableSlices(nodename, slicelist):
+# api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+# for slice in slices(siteId(nodename)):
+# logger.info("Suspending slice %s" % slice)
+# api.SliceAttributeAdd(auth.auth, slice, "plc_slice_state", {"state" : "suspended"})
+#
- numslices = api.AdmGetSites(auth, siteid, ["max_slices"])[0]['max_slices']
- if len(siteid) == 1:
+'''
+Removes ability to create slices. Returns previous max_slices
+'''
+def removeSliceCreation(nodename):
+ api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+ 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)
- 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)
-
-def enableSliceCreation(argv):
- """QED"""
-
- global api, anon, auth
- if auth is None:
- printUsage("requires admin privs")
- sys.exit(1)
-
- if len(argv) < 2:
- printUsage("requires maxslice arg")
- 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 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
+'''
+def enableSliceCreation(nodename, maxslices):
+ api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+ anon = {'AuthMethod': "anonymous"}
+ siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
if len(siteid) == 1:
- logger.info("Enabling slice creation for site %s" % loginbase)
+ logger.info("Enabling slice creation for site %s" % siteId(nodename))
try:
if not config.debug:
- api.AdmUpdateSite(auth, siteid[0], {"max_slices" : maxslices})
+ api.AdmUpdateSite(auth.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)
-
-
-
-USAGE = """
-Usage: %s [-u user] [-p password] [-r role] CMD
+ logger.debug("Cant find site for %s. Cannot enable creation." % nodename)
-Options:
--u PLC account username
--p PLC account password
--r PLC account role
--h This message
-""" % sys.argv[0]
-
-def printUsage(error = None):
- global funclist
- if error <> None:
- print "%s %s" %(sys.argv[0],error)
- print USAGE
- print "CMD:"
- for name,function in funclist:
- print "%20s\t%20s" % (name, function.__doc__)
-
def main():
- global api, auth, anon
-
- anon = {"AuthMethod":"anonymous"}
- auth = None
- user = None
- password = None
- role = 'admin'
-
- (opts, argv) = getopt.getopt(sys.argv[1:], "u:p:r:h")
- if len(argv)==0:
- printUsage()
- sys.exit(1)
-
- for (opt, optval) in opts:
- if opt == '-u':
- user = optval
- elif opt == '-p':
- password = optval
- elif opt == '-r':
- role = optval
- elif opt == '-h':
- print USAGE
- sys.exit(0)
-
- if user <> None:
- if password is None:
- try:
- password = getpass.getpass()
- except (EOFError, KeyboardInterrupt):
- print( "" )
- sys.exit(1)
- auth = {}
- auth['Username'] = user
- auth['AuthMethod'] = "password"
- auth['AuthString'] = password
- auth['Role'] = role
-
- cmd = functbl.get(argv[0], None)
- if cmd is None:
- printUsage()
- sys.exit(1)
-
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('logger - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
- result = cmd(argv[1:])
- if result <> None:
- print result
-
-funclist = (("nodesDbg",nodesDbg),
- ("siteId", siteId),
- ("slices", slices),
- ("pcu", getpcu),
- ("siteNodes", getSiteNodes),
- ("nodeBootState", nodeBootState),
- ("nodePOD", nodePOD),
- ("freezeSlices", suspendSlices),
- ("unfreezeSlices", enableSlices),
- ("disableSliceCreation",removeSliceCreation),
- ("enableSliceCreation", enableSliceCreation),
- ("renewAllSlices", renewAllSlices))
-
-functbl = {}
-for f in funclist:
- functbl[f[0]]=f[1]
+ #print getpcu("kupl2.ittc.ku.edu")
+ #print getpcu("planetlab1.cse.msu.edu")
+ #print getpcu("alice.cs.princeton.edu")
+ #print nodesDbg()
+ #nodeBootState("alice.cs.princeton.edu", "boot")
+ #freezeSite("alice.cs.princeton.edu")
+ print removeSliceCreation("alice.cs.princeton.edu")
+ #enableSliceCreation("alice.cs.princeton.edu", 1024)
+ #print getSiteNodes("princeton")
+ #print siteId("alice.cs.princeton.edu")
+ #print nodePOD("alice.cs.princeton.edu")
+ #print slices("princeton")
if __name__=="__main__":
import reboot