You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2009/11/18 16:47:28 UTC
svn commit: r881806 - /qpid/trunk/qpid/python/commands/qpid-cluster
Author: aconway
Date: Wed Nov 18 15:47:28 2009
New Revision: 881806
URL: http://svn.apache.org/viewvc?rev=881806&view=rev
Log:
Refactor qpid-cluster script with main() to be callable from python.
Modified:
qpid/trunk/qpid/python/commands/qpid-cluster
Modified: qpid/trunk/qpid/python/commands/qpid-cluster
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/python/commands/qpid-cluster?rev=881806&r1=881805&r2=881806&view=diff
==============================================================================
--- qpid/trunk/qpid/python/commands/qpid-cluster (original)
+++ qpid/trunk/qpid/python/commands/qpid-cluster Wed Nov 18 15:47:28 2009
@@ -27,16 +27,18 @@
import re
from qmf.console import Session
-_host = "localhost"
-_connTimeout = 10
-_stopId = None
-_stopAll = False
-_force = False
-_numeric = False
-_showConn = False
-_delConn = None
+class Config:
+ def __init__(self):
+ self._host = "localhost"
+ self._connTimeout = 10
+ self._stopId = None
+ self._stopAll = False
+ self._force = False
+ self._numeric = False
+ self._showConn = False
+ self._delConn = None
-def Usage ():
+def usage ():
print "Usage: qpid-cluster [OPTIONS] [broker-addr]"
print
print " broker-addr is in the form: [username/password@] hostname | ip-address [:<port>]"
@@ -53,8 +55,6 @@
print " -f [--force] Suppress the 'are-you-sure?' prompt"
print " -n [--numeric] Don't resolve names"
print
- sys.exit (1)
-
class IpAddr:
def __init__(self, text):
@@ -82,7 +82,8 @@
return bestAddr
class BrokerManager:
- def __init__(self):
+ def __init__(self, config):
+ self.config = config
self.brokerName = None
self.qmf = None
self.broker = None
@@ -90,7 +91,7 @@
def SetBroker(self, brokerUrl):
self.url = brokerUrl
self.qmf = Session()
- self.broker = self.qmf.addBroker(brokerUrl, _connTimeout)
+ self.broker = self.qmf.addBroker(brokerUrl, self.config._connTimeout)
agents = self.qmf.getAgents()
for a in agents:
if a.getAgentBank() == 0:
@@ -103,19 +104,17 @@
def _getClusters(self):
packages = self.qmf.getPackages()
if "org.apache.qpid.cluster" not in packages:
- print "Clustering is not installed on the broker."
- sys.exit(0)
+ raise Exception("Clustering is not installed on the broker.")
clusters = self.qmf.getObjects(_class="cluster", _agent=self.brokerAgent)
if len(clusters) == 0:
- print "Clustering is installed but not enabled on the broker."
- sys.exit(0)
+ raise Exception("Clustering is installed but not enabled on the broker.")
return clusters
def _getHostList(self, urlList):
hosts = []
- hostAddr = IpAddr(_host)
+ hostAddr = IpAddr(self.config._host)
for url in urlList:
if url.find("amqp:") != 0:
raise Exception("Invalid URL 1")
@@ -155,10 +154,9 @@
cluster = clusters[0]
idList = cluster.memberIDs.split(";")
if id not in idList:
- print "No member with matching ID found"
- sys.exit(1)
+ raise Exception("No member with matching ID found")
- if not _force:
+ if not self.config._force:
prompt = "Warning: "
if len(idList) == 1:
prompt += "This command will shut down the last running cluster member."
@@ -168,21 +166,19 @@
confirm = raw_input(prompt)
if len(confirm) == 0 or confirm[0].upper() != 'Y':
- print "Operation canceled"
- sys.exit(1)
+ raise Exception("Operation canceled")
cluster.stopClusterNode(id)
def stopAll(self):
clusters = self._getClusters()
- if not _force:
+ if not self.config._force:
prompt = "Warning: This command will shut down the entire cluster."
prompt += " Are you sure? [N]: "
confirm = raw_input(prompt)
if len(confirm) == 0 or confirm[0].upper() != 'Y':
- print "Operation canceled"
- sys.exit(1)
+ raise Exception("Operation canceled")
cluster = clusters[0]
cluster.stopFullCluster()
@@ -201,20 +197,20 @@
idx = 0
for host in hostList:
- if _showConn == "all" or _showConn == idList[idx] or _delConn:
- self.brokers.append(self.qmf.addBroker(host, _connTimeout))
+ if self.config._showConn == "all" or self.config._showConn == idList[idx] or self.config._delConn:
+ self.brokers.append(self.qmf.addBroker(host, self.config._connTimeout))
displayList.append(idList[idx])
idx += 1
idx = 0
found = False
for broker in self.brokers:
- if not _delConn:
+ if not self.config._delConn:
print "Clients on Member: ID=%s:" % displayList[idx]
connList = self.qmf.getObjects(_class="connection", _package="org.apache.qpid.broker", _broker=broker)
for conn in connList:
if pattern.match(conn.address):
- if _numeric or _delConn:
+ if self.config._numeric or self.config._delConn:
a = conn.address
else:
tokens = conn.address.split(":")
@@ -224,8 +220,8 @@
except:
host = tokens[0]
a = host + ":" + tokens[1]
- if _delConn:
- tokens = _delConn.split(":")
+ if self.config._delConn:
+ tokens = self.config._delConn.split(":")
ip = socket.gethostbyname(tokens[0])
toDelete = ip + ":" + tokens[1]
if a == toDelete:
@@ -235,94 +231,98 @@
else:
print " %s" % a
idx += 1
- if not _delConn:
+ if not self.config._delConn:
print
- if _delConn and not found:
- print "Client connection '%s' not found" % _delConn
+ if self.config._delConn and not found:
+ print "Client connection '%s' not found" % self.config._delConn
for broker in self.brokers:
self.qmf.delBroker(broker)
-##
-## Main Program
-##
-
-try:
- longOpts = ("stop=", "all-stop", "force", "connections=", "all-connections" "del-connection=", "numeric", "timeout=")
- (optlist, encArgs) = getopt.gnu_getopt(sys.argv[1:], "s:kfCc:d:n", longOpts)
-except:
- Usage()
-
-try:
- encoding = locale.getpreferredencoding()
- cargs = [a.decode(encoding) for a in encArgs]
-except:
- cargs = encArgs
-
-count = 0
-for opt in optlist:
- if opt[0] == "--timeout":
- _connTimeout = int(opt[1])
- if _connTimeout == 0:
- _connTimeout = None
- if opt[0] == "-s" or opt[0] == "--stop":
- _stopId = opt[1]
- if len(_stopId.split(":")) != 2:
- print "Member ID must be of form: <host or ip>:<number>"
- sys.exit(1)
- count += 1
- if opt[0] == "-k" or opt[0] == "--all-stop":
- _stopAll = True
- count += 1
- if opt[0] == "-f" or opt[0] == "--force":
- _force = True
- if opt[0] == "-n" or opt[0] == "--numeric":
- _numeric = True
- if opt[0] == "-C" or opt[0] == "--all-connections":
- _showConn = "all"
- count += 1
- if opt[0] == "-c" or opt[0] == "--connections":
- _showConn = opt[1]
- if len(_showConn.split(":")) != 2:
- print "Member ID must be of form: <host or ip>:<number>"
- sys.exit(1)
- count += 1
- if opt[0] == "-d" or opt[0] == "--del-connection":
- _delConn = opt[1]
- if len(_delConn.split(":")) != 2:
- print "Connection must be of form: <host or ip>:<port>"
- sys.exit(1)
- count += 1
-
-if count > 1:
- print "Only one command option may be supplied"
- print
- Usage()
-
-nargs = len(cargs)
-bm = BrokerManager()
-
-if nargs == 1:
- _host = cargs[0]
-
-try:
- bm.SetBroker(_host)
- if _stopId:
- bm.stopMember(_stopId)
- elif _stopAll:
- bm.stopAll()
- elif _showConn or _delConn:
- bm.showConnections()
- else:
- bm.overview()
-except KeyboardInterrupt:
- print
-except Exception,e:
- if e.__repr__().find("connection aborted") > 0:
- # we expect this when asking the connected broker to shut down
- sys.exit(0)
- print "Failed: %s - %s" % (e.__class__.__name__, e)
- sys.exit(1)
+def main(argv=None):
+ if argv is None: argv = sys.argv
+ try:
+ config = Config()
+ try:
+ longOpts = ("stop=", "all-stop", "force", "connections=", "all-connections" "del-connection=", "numeric", "timeout=")
+ (optlist, encArgs) = getopt.gnu_getopt(argv[1:], "s:kfCc:d:n", longOpts)
+ except:
+ usage()
+ return 1
+
+ try:
+ encoding = locale.getpreferredencoding()
+ cargs = [a.decode(encoding) for a in encArgs]
+ except:
+ cargs = encArgs
+
+ count = 0
+ for opt in optlist:
+ if opt[0] == "--timeout":
+ config._connTimeout = int(opt[1])
+ if config._connTimeout == 0:
+ config._connTimeout = None
+ if opt[0] == "-s" or opt[0] == "--stop":
+ config._stopId = opt[1]
+ if len(config._stopId.split(":")) != 2:
+ raise Exception("Member ID must be of form: <host or ip>:<number>")
+ count += 1
+ if opt[0] == "-k" or opt[0] == "--all-stop":
+ config._stopAll = True
+ count += 1
+ if opt[0] == "-f" or opt[0] == "--force":
+ config._force = True
+ if opt[0] == "-n" or opt[0] == "--numeric":
+ config._numeric = True
+ if opt[0] == "-C" or opt[0] == "--all-connections":
+ config._showConn = "all"
+ count += 1
+ if opt[0] == "-c" or opt[0] == "--connections":
+ config._showConn = opt[1]
+ if len(config._showConn.split(":")) != 2:
+ raise Exception("Member ID must be of form: <host or ip>:<number>")
+ count += 1
+ if opt[0] == "-d" or opt[0] == "--del-connection":
+ config._delConn = opt[1]
+ if len(config._delConn.split(":")) != 2:
+ raise Exception("Connection must be of form: <host or ip>:<port>")
+ count += 1
+
+ if count > 1:
+ print "Only one command option may be supplied"
+ print
+ usage()
+ return 1
+
+ nargs = len(cargs)
+ bm = BrokerManager(config)
+
+ if nargs == 1:
+ config._host = cargs[0]
+
+ try:
+ bm.SetBroker(config._host)
+ if config._stopId:
+ bm.stopMember(config._stopId)
+ elif config._stopAll:
+ bm.stopAll()
+ elif config._showConn or config._delConn:
+ bm.showConnections()
+ else:
+ bm.overview()
+ except KeyboardInterrupt:
+ print
+ except Exception,e:
+ if str(e).find("connection aborted") > 0:
+ # we expect this when asking the connected broker to shut down
+ return 0
+ raise Exception("Failed: %s - %s" % (e.__class__.__name__, e))
+
+ bm.Disconnect()
+ except Exception, e:
+ print str(e)
+ return 1
-bm.Disconnect()
+if __name__ == "__main__":
+ sys.exit(main())
---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project: http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org