From c51ad794e8dc07072d705b508e79ba06849aa408 Mon Sep 17 00:00:00 2001
From: Stephen Soltesz <soltesz@cs.princeton.edu>
Date: Mon, 4 Aug 2008 16:18:20 +0000
Subject: [PATCH] Changed 'import auth' statements to use plc.py or
 monitorconfig.py

---
 bootman.py          |  3 +-
 findbad.py          |  3 +-
 getconf.py          |  3 +-
 grouprins.py        |  3 +-
 monitor.py          |  3 +-
 nodeaction.py       |  3 +-
 nodebad.py          |  3 +-
 nodeconfig.py       |  3 +-
 nodegroups.py       |  3 +-
 nodehistory.py      |  3 +-
 nodeinfo.py         |  3 +-
 nodenetwork.py      |  3 +-
 nodequery.py        |  3 +-
 pcubad.py           |  3 +-
 plc.py              | 81 ++++++++++++++++++++++++---------------------
 prep_power_users.py |  6 ++--
 showlatlon.py       |  3 +-
 sitebad.py          |  3 +-
 siteinfo.py         |  3 +-
 todo                | 20 ++++++-----
 unified_model.py    |  3 +-
 21 files changed, 76 insertions(+), 85 deletions(-)

diff --git a/bootman.py b/bootman.py
index 0a75fac..c3116bc 100755
--- a/bootman.py
+++ b/bootman.py
@@ -3,8 +3,7 @@
 # Attempt to reboot a node in debug state.
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 import sys
 import os
diff --git a/findbad.py b/findbad.py
index 7efa52c..2b8fe00 100755
--- a/findbad.py
+++ b/findbad.py
@@ -29,8 +29,7 @@ import syncplcdb
 from nodequery import verify,query_to_dict,node_select
 import traceback
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 def collectPingAndSSH(nodename, cohash):
 	### RUN PING ######################
diff --git a/getconf.py b/getconf.py
index 155c26f..56cf1b8 100755
--- a/getconf.py
+++ b/getconf.py
@@ -1,12 +1,11 @@
 #!/usr/bin/python
 
-import auth
 import plc
+api = plc.getAuthAPI()
 import sys
 import os
 
 def getconf(hostname, force=False, media=None):
-	api = plc.PLC(auth.auth, auth.plc)
 	n = api.GetNodes(hostname)
 	filename = "bootcd/" + hostname + ".txt"
 	if not os.path.exists(filename) or force:
diff --git a/grouprins.py b/grouprins.py
index 14be85f..ffb4f85 100755
--- a/grouprins.py
+++ b/grouprins.py
@@ -13,8 +13,7 @@
 # 
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 import policy
 import traceback
diff --git a/monitor.py b/monitor.py
index b9e3ece..48fa514 100644
--- a/monitor.py
+++ b/monitor.py
@@ -13,8 +13,7 @@ import rt
 import sys
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 from clean_policy import *
 
diff --git a/nodeaction.py b/nodeaction.py
index 00d2810..62095d1 100755
--- a/nodeaction.py
+++ b/nodeaction.py
@@ -1,8 +1,7 @@
 #!/usr/bin/python
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 import reboot
 
diff --git a/nodebad.py b/nodebad.py
index 96720fb..c274f49 100755
--- a/nodebad.py
+++ b/nodebad.py
@@ -13,8 +13,7 @@ import syncplcdb
 from nodequery import verify,query_to_dict,node_select
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 from unified_model import *
 from monitor_policy import MINUP
 
diff --git a/nodeconfig.py b/nodeconfig.py
index fa4a3eb..d8a8c12 100755
--- a/nodeconfig.py
+++ b/nodeconfig.py
@@ -2,8 +2,7 @@
 
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 from optparse import OptionParser
 from sets import Set
diff --git a/nodegroups.py b/nodegroups.py
index 207efae..be4ed6f 100755
--- a/nodegroups.py
+++ b/nodegroups.py
@@ -14,8 +14,7 @@
 #  * 
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 from optparse import OptionParser
 from sets import Set
diff --git a/nodehistory.py b/nodehistory.py
index d09f01f..520037c 100755
--- a/nodehistory.py
+++ b/nodehistory.py
@@ -1,8 +1,7 @@
 #!/usr/bin/python
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 import database
 import reboot
diff --git a/nodeinfo.py b/nodeinfo.py
index 23afab9..f50f846 100755
--- a/nodeinfo.py
+++ b/nodeinfo.py
@@ -1,8 +1,7 @@
 #!/usr/bin/python
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 import database
 import reboot
diff --git a/nodenetwork.py b/nodenetwork.py
index bf80e00..ec8d869 100755
--- a/nodenetwork.py
+++ b/nodenetwork.py
@@ -2,8 +2,7 @@
 
 import sys
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 import config
 
 if len(sys.argv[1:]) > 0:
diff --git a/nodequery.py b/nodequery.py
index 3ee4236..a982e20 100755
--- a/nodequery.py
+++ b/nodequery.py
@@ -1,8 +1,7 @@
 #!/usr/bin/python
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 import sys
 import database
diff --git a/pcubad.py b/pcubad.py
index 5b71845..fbcdb17 100755
--- a/pcubad.py
+++ b/pcubad.py
@@ -14,8 +14,7 @@ import syncplcdb
 from nodequery import verify,query_to_dict,node_select
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 from unified_model import *
 from monitor_policy import MINUP
 
diff --git a/plc.py b/plc.py
index 0506ad5..0970eaa 100644
--- a/plc.py
+++ b/plc.py
@@ -10,31 +10,38 @@
 
 import xml, xmlrpclib
 import logging
-try:
-    import auth
-except:
-	class Anon:
-		def __init__(self):
-			self.auth = {'AuthMethod': "anonymous"}
-	auth = Anon()
-
 import time
+import traceback
 try:
 	from config import config
 	config = config()
 	debug = config.debug
 except:
 	debug = False
+logger = logging.getLogger("monitor")
 	
-XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
+class Auth:
+	def __init__(self):
+		self.auth = {'AuthMethod': "anonymous"}
 
-logger = logging.getLogger("monitor")
+# NOTE: this host is used by default when there are no auth files.
+XMLRPC_SERVER="https://boot.planet-lab.org/PLCAPI/"
 
-api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+# NOTE: by default, use anonymous access, but if auth files are 
+#       configured, use them, with their auth definitions.
+auth = Auth()
+try:
+	import monitorconfig
+	auth.auth = monitorconfig.API_AUTH
+	auth.server = monitorconfig.API_SERVER
+except:
+	try:
+		import auth
+		auth.server = auth.plc
+	except:
+		auth.server = XMLRPC_SERVER
 
-def getAPI(url):
-	api = xmlrpclib.Server(url, verbose=False, allow_none=True)
-	return api
+api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
 
 class PLC:
 	def __init__(self, auth, url):
@@ -52,8 +59,10 @@ class PLC:
 	def __repr__(self):
 		return self.api.__repr__()
 
+def getAPI(url):
+	return xmlrpclib.Server(url, verbose=False, allow_none=True)
+
 def getAuthAPI():
-	import monitorconfig
 	return PLC(monitorconfig.API_AUTH, monitorconfig.API_SERVER)
 
 '''
@@ -61,7 +70,7 @@ Returns list of nodes in dbg as reported by PLC
 '''
 def nodesDbg():
 	dbgNodes = []
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.server, verbose=False)
 	anon = {'AuthMethod': "anonymous"}
 	for node in api.GetNodes(anon, {"boot_state":"dbg"},["hostname"]):
 		dbgNodes.append(node['hostname'])
@@ -73,7 +82,7 @@ def nodesDbg():
 Returns loginbase for given nodename
 '''
 def siteId(nodename):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.server, verbose=False)
 	anon = {'AuthMethod': "anonymous"}
 	site_id = api.GetNodes (anon, {"hostname": nodename}, ['site_id'])
 	if len(site_id) == 1:
@@ -85,7 +94,7 @@ Returns list of slices for a site.
 '''
 def slices(loginbase):
 	siteslices = []
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.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'])
@@ -95,7 +104,7 @@ def slices(loginbase):
 Returns dict of PCU info of a given node.
 '''
 def getpcu(nodename):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.server, verbose=False)
 	anon = {'AuthMethod': "anonymous"}
 	nodeinfo = api.GetNodes(auth.auth, {"hostname": nodename}, ["pcu_ids", "ports"])[0]
 	if nodeinfo['pcu_ids']:
@@ -107,7 +116,7 @@ def getpcu(nodename):
 		return False
 
 def GetPCUs(filter=None, fields=None):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+	api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
 	pcu_list = api.GetPCUs(auth.auth, filter, fields)
 	return pcu_list 
 
@@ -115,7 +124,7 @@ def GetPCUs(filter=None, fields=None):
 Returns all site nodes for site id (loginbase).
 '''
 def getSiteNodes(loginbase, fields=None):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.server, verbose=False)
 	nodelist = []
 	anon = {'AuthMethod': "anonymous"}
 	try:
@@ -128,7 +137,7 @@ def getSiteNodes(loginbase, fields=None):
 	return nodelist
 
 def getPersons(filter=None, fields=None):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+	api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
 	persons = []
 	try:
 		persons = api.GetPersons(auth.auth, filter, fields)
@@ -138,21 +147,20 @@ def getPersons(filter=None, fields=None):
 	return persons
 
 def getSites(filter=None, fields=None):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+	api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
 	sites = []
 	anon = {'AuthMethod': "anonymous"}
 	try:
 		#sites = api.GetSites(anon, filter, fields)
 		sites = api.GetSites(auth.auth, filter, fields)
 	except Exception, exc:
-		import traceback
 		traceback.print_exc()
 		print "getSites:  %s" % exc
 		logger.info("getSites:  %s" % exc)
 	return sites
 
 def getSiteNodes2(loginbase):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.server, verbose=False)
 	nodelist = []
 	anon = {'AuthMethod': "anonymous"}
 	try:
@@ -163,12 +171,12 @@ def getSiteNodes2(loginbase):
 	return nodelist
 
 def getNodeNetworks(filter=None):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+	api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
 	nodenetworks = api.GetNodeNetworks(auth.auth, filter, None)
 	return nodenetworks
 
 def getNodes(filter=None, fields=None):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+	api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
 	nodes = api.GetNodes(auth.auth, filter, fields) 
 			#['boot_state', 'hostname', 
 			#'site_id', 'date_created', 'node_id', 'version', 'nodenetwork_ids',
@@ -179,14 +187,14 @@ def getNodes(filter=None, fields=None):
 Sets boot state of a node.
 '''
 def nodeBootState(nodename, state):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.server, verbose=False)
 	try:
 		return api.UpdateNode(auth.auth, nodename, {'boot_state': state})
 	except Exception, exc:
 		logger.info("nodeBootState:  %s" % exc)
 
 def updateNodeKey(nodename, key):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.server, verbose=False)
 	try:
 		return api.UpdateNode(auth.auth, nodename, {'key': key})
 	except Exception, exc:
@@ -196,7 +204,7 @@ def updateNodeKey(nodename, key):
 Sends Ping Of Death to node.
 '''
 def nodePOD(nodename):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.server, verbose=False)
 	logger.info("Sending POD to %s" % nodename)
 	try:
 		if not debug:
@@ -208,7 +216,7 @@ def nodePOD(nodename):
 Freeze all site slices.
 '''
 def suspendSlices(nodename):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.server, verbose=False)
 	for slice in slices(siteId(nodename)):
 		logger.info("Suspending slice %s" % slice)
 		try:
@@ -218,7 +226,7 @@ def suspendSlices(nodename):
 			logger.info("suspendSlices:  %s" % exc)
 
 def enableSlices(nodename):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+	api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
 	for slice in slices(siteId(nodename)):
 		logger.info("Enabling slices %s" % slice)
 		try:
@@ -241,13 +249,13 @@ def enableSlices(nodename):
 #Enable suspended site slices.
 #'''
 #def enableSlices(nodename, slicelist):
-#	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+#	api = xmlrpclib.Server(auth.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"})
 #
 def enableSliceCreation(nodename):
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False, allow_none=True)
+	api = xmlrpclib.Server(auth.server, verbose=False, allow_none=True)
 	try:
 		loginbase = siteId(nodename)
 		logger.info("Enabling slice creation for site %s" % loginbase)
@@ -263,7 +271,7 @@ Removes ability to create slices. Returns previous max_slices
 '''
 def removeSliceCreation(nodename):
 	print "removeSliceCreation(%s)" % nodename
-	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+	api = xmlrpclib.Server(auth.server, verbose=False)
 	try:
 		loginbase = siteId(nodename)
 		#numslices = api.GetSites(auth.auth, {"login_base": loginbase}, 
@@ -279,7 +287,7 @@ def removeSliceCreation(nodename):
 QED
 '''
 #def enableSliceCreation(nodename, maxslices):
-#	api = xmlrpclib.Server(XMLRPC_SERVER, verbose=False)
+#	api = xmlrpclib.Server(auth.server, verbose=False)
 #	anon = {'AuthMethod': "anonymous"}
 #	siteid = api.AnonAdmQuerySite (anon, {"node_hostname": nodename})
 #	if len(siteid) == 1:
@@ -313,5 +321,4 @@ def main():
 	#print slices("princeton")
 
 if __name__=="__main__":
-	import reboot
 	main() 
diff --git a/prep_power_users.py b/prep_power_users.py
index 3f354c0..8566a6d 100755
--- a/prep_power_users.py
+++ b/prep_power_users.py
@@ -1,7 +1,6 @@
 #!/usr/bin/python
 
 import plc
-import auth
 import sys
 import time
 
@@ -16,8 +15,9 @@ email_list = [ 'monitor@planet-lab.org',
         	 # 'vivek@cs.princeton.edu',
 		]
 
-api   = plc.PLC(auth.auth, auth.plc)
-api06 = plc.PLC(auth.auth06, auth.plc06)
+import monitorconfig
+api   = plc.PLC(monitorconfig.API_AUTH, monitorconfig.API_SERVER)
+api06 = plc.PLC(monitorconfig.API_AUTH06, monitorconfig.API_SERVER06)
 
 
 # add planetlab-15.cs.princeton.edu, and use the key on the CD.
diff --git a/showlatlon.py b/showlatlon.py
index fbed374..10367e4 100755
--- a/showlatlon.py
+++ b/showlatlon.py
@@ -1,8 +1,7 @@
 #!/usr/bin/python
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 import sys
 import reboot
diff --git a/sitebad.py b/sitebad.py
index c9e0033..6b12ef3 100755
--- a/sitebad.py
+++ b/sitebad.py
@@ -13,8 +13,7 @@ import syncplcdb
 from nodequery import verify,query_to_dict,node_select
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 from unified_model import *
 from monitor_policy import MINUP
 
diff --git a/siteinfo.py b/siteinfo.py
index d248b99..f3692bc 100755
--- a/siteinfo.py
+++ b/siteinfo.py
@@ -1,8 +1,7 @@
 #!/usr/bin/python
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 import database
 import reboot
diff --git a/todo b/todo
index 066e683..cedd0aa 100644
--- a/todo
+++ b/todo
@@ -4,25 +4,27 @@ TODO:
    to share very similar argument or argument sets, as well as have some
    common config options.  I'm not sure the best way to do this.
 
- * Find a better location to place and pull the PLK files currently in the pdb
+ * Find a better location to place and pull the PKL files currently in the pdb
    directory.  Ultimately, these should be stored in a real DB.  Until then,
    they should sit in a location that is accessible from the www scripts,
    backend scripts, and user utilities.
 
- * pull out global configuration information from various files, like rt_db,
-   mailer.py,  auth.py, and any others.  Create a single configuration file
-   from which all others pull.
-
-   - convert plc and other files to use the new monitorconfig.py rather than
-     auth, or plc.*
-   - need to alter all import 'auth' statements.
+ * clean up plc.py; there's a lot of redundent code.
 
 Lower priority:
  * Add a more structured, 'automate' library of scripts and means of making
    batch calls, etc.
 
  * add a third package for user tools that will interact with the Monitor
-   service.  Mostly, I'm guessing this would be queries for the live status
+   service.  Mostly, I'm guessing this would be queries for the live status of
    nodes and a more reliable 'reboot' and 'reinstall' mechanism than currently
    availble with PLC.
 
+Done:
+ * pull out global configuration information from various files, like rt_db,
+   mailer.py,  auth.py, and any others.  Create a single configuration file
+   from which all others pull.
+
+   - convert plc and other files to use the new monitorconfig.py rather than
+     auth, or plc.*
+   - need to alter all import 'auth' statements.
diff --git a/unified_model.py b/unified_model.py
index 602c902..01c99a2 100755
--- a/unified_model.py
+++ b/unified_model.py
@@ -3,8 +3,7 @@
 import database
 
 import plc
-import auth
-api = plc.PLC(auth.auth, auth.plc)
+api = plc.getAuthAPI()
 
 import mailer
 import time
-- 
2.47.0