incubator-tashi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From strou...@apache.org
Subject svn commit: r1295322 - /incubator/tashi/branches/stroucki-accounting/src/tashi/util.py
Date Wed, 29 Feb 2012 22:24:35 GMT
Author: stroucki
Date: Wed Feb 29 22:24:35 2012
New Revision: 1295322

URL: http://svn.apache.org/viewvc?rev=1295322&view=rev
Log:
util: Create new Connection object to deal with monitoring a client on the client side, handling
connection errors by failing and reconnecting on next invocation.

Modified:
    incubator/tashi/branches/stroucki-accounting/src/tashi/util.py

Modified: incubator/tashi/branches/stroucki-accounting/src/tashi/util.py
URL: http://svn.apache.org/viewvc/incubator/tashi/branches/stroucki-accounting/src/tashi/util.py?rev=1295322&r1=1295321&r2=1295322&view=diff
==============================================================================
--- incubator/tashi/branches/stroucki-accounting/src/tashi/util.py (original)
+++ incubator/tashi/branches/stroucki-accounting/src/tashi/util.py Wed Feb 29 22:24:35 2012
@@ -27,6 +27,7 @@ import time
 import traceback
 import types
 import getpass
+import functools
 
 from tashi.rpycservices import rpycservices
 from tashi.rpycservices.rpyctypes import TashiException, Errors, InstanceState, HostState
@@ -268,6 +269,64 @@ def scrubString(s, allowed="ABCDEFGHIJKL
 			ns = ns + c
 	return ns
 
+class Connection:
+	def __init__(self, host, port, authAndEncrypt=False, credentials=None):
+		self.host = host
+		self.port = port
+		self.credentials = credentials
+		self.authAndEncrypt = authAndEncrypt
+		self.connection = None
+
+	def __connect(self):
+		# create new connection
+
+		username = None
+		password = None
+
+		if credentials is not None:
+			username = self.credentials[0]
+			password = self.credentials[1]
+
+		if authAndEncrypt:
+			if username is None:
+				username = raw_input("Enter Username:")
+
+			if password is None:
+				password = raw_input("Enter Password:")
+
+			if self.credentials != (username, password):
+				self.credentials = (username, password)
+
+			client = rpycservices.client(self.host, self.port, username=username, password=password)
+		else:
+			client = rpycservices.client(self.host, self.port)
+
+		self.connection = client
+
+
+	def __do(self, name, *args, **kwargs):
+		if self.connection is None:
+			self.__connect()
+
+		remotefn = getattr(self.connection, name, None)
+
+		try:
+			if callable(remotefn):
+				remotefn(args, kwargs)
+
+			else:
+				raise TashiException
+
+		except:
+			self.connection = None
+			raise TashiException
+
+		# ENDING
+
+	def __getattr__(self, name):
+		return functools.partial(self.__do, name)
+
+
 def createClient(config):
 	cfgHost = config.get('Client', 'clusterManagerHost')
 	cfgPort = config.get('Client', 'clusterManagerPort')
@@ -281,14 +340,12 @@ def createClient(config):
 	authAndEncrypt = boolean(config.get('Security', 'authAndEncrypt'))
 	if authAndEncrypt:
 		username = config.get('AccessClusterManager', 'username')
-		if username == '':
-			username = raw_input('Enter Username:')
 		password = config.get('AccessClusterManager', 'password')
-		if password == '':
-			password = getpass.getpass('Enter Password:')
-		client = rpycservices.client(host, port, username=username, password=password)
+		client = Connection(host, port, authAndEncrypt, (username, password)
+
 	else:
-		client = rpycservices.client(host, port)
+		client = Connection(host, port)
+
 	return client
 
 def enumToStringDict(cls):



Mime
View raw message