1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
#!/usr/bin/env python
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
import getopt
import sys
import socket
import qpid
import os
from qpid.management import managementClient
from qpid.managementdata import Broker
from qpid.peer import Closed
from qpid.connection import Connection, ConnectionFailed
from qpid.util import connect
def Usage ():
print "Usage: qpid-route [OPTIONS] link add <dest-broker> <src-broker>"
print " qpid-route [OPTIONS] link del <dest-broker> <src-broker>"
print " qpid-route [OPTIONS] link list <dest-broker>"
print
print " qpid-route [OPTIONS] route add <dest-broker> <src-broker> <exchange> <routing-key> [id] [exclude-list]"
print " qpid-route [OPTIONS] route del <dest-broker> <src-broker> <exchange> <routing-key>"
print " qpid-route [OPTIONS] route list <dest-broker>"
print " qpid-route [OPTIONS] route flush <dest-broker>"
print
print "Options:"
print " -v [ --verbose ] Verbose output"
print " -q [ --quiet ] Quiet output, don't print duplicate warnings"
print " -d [ --durable ] Added configuration shall be durable"
print " -e [ --del-empty-link ] Delete link after deleting last route on the link"
print
print " dest-broker and src-broker are in the form: [username/password@] hostname | ip-address [:<port>]"
print " ex: localhost, 10.1.1.7:10000, broker-host:10000, guest/guest@localhost"
print
sys.exit (1)
_verbose = False
_quiet = False
_durable = False
_dellink = False
class RouteManager:
def __init__ (self, destBroker):
self.dest = Broker (destBroker)
self.src = None
def ConnectToBroker (self):
broker = self.dest
if _verbose:
print "Connecting to broker: %s:%d" % (broker.host, broker.port)
try:
self.sessionId = "%s.%d" % (os.uname()[1], os.getpid())
self.conn = Connection (connect (broker.host, broker.port), \
username=broker.username, password=broker.password)
self.conn.start ()
self.session = self.conn.session(self.sessionId)
self.mclient = managementClient (self.conn.spec)
self.mch = self.mclient.addChannel (self.session)
self.mclient.syncWaitForStable (self.mch)
except socket.error, e:
print "Socket Error %s - %s" % (e[0], e[1])
sys.exit (1)
except Closed, e:
print "Connect Failed %d - %s" % (e[0], e[1])
sys.exit (1)
except ConnectionFailed, e:
print "Connect Failed %d - %s" % (e[0], e[1])
sys.exit(1)
def Disconnect (self):
self.mclient.removeChannel (self.mch)
self.session.close(timeout=10)
self.conn.close(timeout=10)
def getLink (self):
links = self.mclient.syncGetObjects (self.mch, "link")
for link in links:
if "%s:%d" % (link.host, link.port) == self.src.name ():
return link
return None
def AddLink (self, srcBroker):
self.src = Broker (srcBroker)
mc = self.mclient
brokers = mc.syncGetObjects (self.mch, "broker")
broker = brokers[0]
link = self.getLink()
if link != None:
print "Link already exists"
sys.exit(1)
connectArgs = {}
connectArgs["host"] = self.src.host
connectArgs["port"] = self.src.port
connectArgs["useSsl"] = False
connectArgs["durable"] = _durable
connectArgs["authMechanism"] = "PLAIN"
connectArgs["username"] = self.src.username
connectArgs["password"] = self.src.password
res = mc.syncCallMethod (self.mch, broker.id, broker.classKey, "connect", connectArgs)
if _verbose:
print "Connect method returned:", res.status, res.statusText
link = self.getLink ()
def DelLink (self, srcBroker):
self.src = Broker (srcBroker)
mc = self.mclient
brokers = mc.syncGetObjects (self.mch, "broker")
broker = brokers[0]
link = self.getLink()
if link == None:
print "Link not found"
sys.exit(1)
res = mc.syncCallMethod (self.mch, link.id, link.classKey, "close")
if _verbose:
print "Close method returned:", res.status, res.statusText
def ListLinks (self):
mc = self.mclient
links = mc.syncGetObjects (self.mch, "link")
if len(links) == 0:
print "No Links Found"
else:
print
print "Host Port Durable State Last Error"
print "==================================================================="
for link in links:
print "%-16s%-8d %c %-18s%s" % (link.host, link.port, YN(link.durable), link.state, link.lastError)
def AddRoute (self, srcBroker, exchange, routingKey, id, excludes):
self.src = Broker (srcBroker)
mc = self.mclient
brokers = mc.syncGetObjects (self.mch, "broker")
broker = brokers[0]
link = self.getLink ()
if link == None:
if _verbose:
print "Inter-broker link not found, creating..."
connectArgs = {}
connectArgs["host"] = self.src.host
connectArgs["port"] = self.src.port
connectArgs["useSsl"] = False
connectArgs["durable"] = _durable
connectArgs["authMechanism"] = "PLAIN"
connectArgs["username"] = self.src.username
connectArgs["password"] = self.src.password
res = mc.syncCallMethod (self.mch, broker.id, broker.classKey, "connect", connectArgs)
if _verbose:
print "Connect method returned:", res.status, res.statusText
link = self.getLink ()
if link == None:
print "Protocol Error - Missing link ID"
sys.exit (1)
bridges = mc.syncGetObjects (self.mch, "bridge")
for bridge in bridges:
if bridge.linkRef == link.id and bridge.dest == exchange and bridge.key == routingKey:
if not _quiet:
print "Duplicate Route - ignoring: %s(%s)" % (exchange, routingKey)
sys.exit (1)
sys.exit (0)
if _verbose:
print "Creating inter-broker binding..."
bridgeArgs = {}
bridgeArgs["durable"] = _durable
bridgeArgs["src"] = exchange
bridgeArgs["dest"] = exchange
bridgeArgs["key"] = routingKey
bridgeArgs["tag"] = id
bridgeArgs["excludes"] = excludes
bridgeArgs["src_is_queue"] = 0
bridgeArgs["src_is_local"] = 0
res = mc.syncCallMethod (self.mch, link.id, link.classKey, "bridge", bridgeArgs)
if res.status == 4:
print "Can't create a durable route on a non-durable link"
sys.exit(1)
if _verbose:
print "Bridge method returned:", res.status, res.statusText
def DelRoute (self, srcBroker, exchange, routingKey):
self.src = Broker (srcBroker)
mc = self.mclient
link = self.getLink ()
if link == None:
if not _quiet:
print "No link found from %s to %s" % (self.src.name(), self.dest.name())
sys.exit (1)
sys.exit (0)
bridges = mc.syncGetObjects (self.mch, "bridge")
for bridge in bridges:
if bridge.linkRef == link.id and bridge.dest == exchange and bridge.key == routingKey:
if _verbose:
print "Closing bridge..."
res = mc.syncCallMethod (self.mch, bridge.id, bridge.classKey, "close")
if res.status != 0:
print "Error closing bridge: %d - %s" % (res.status, res.statusText)
sys.exit (1)
if len (bridges) == 1 and _dellink:
link = self.getLink ()
if link == None:
sys.exit (0)
if _verbose:
print "Last bridge on link, closing link..."
res = mc.syncCallMethod (self.mch, link.id, link.classKey, "close")
if res.status != 0:
print "Error closing link: %d - %s" % (res.status, res.statusText)
sys.exit (1)
sys.exit (0)
if not _quiet:
print "Route not found"
sys.exit (1)
def ListRoutes (self):
mc = self.mclient
links = mc.syncGetObjects (self.mch, "link")
bridges = mc.syncGetObjects (self.mch, "bridge")
for bridge in bridges:
myLink = None
for link in links:
if bridge.linkRef == link.id:
myLink = link
break
if myLink != None:
print "%s %s:%d %s %s" % (self.dest.name(), myLink.host, myLink.port, bridge.dest, bridge.key)
def ClearAllRoutes (self):
mc = self.mclient
links = mc.syncGetObjects (self.mch, "link")
bridges = mc.syncGetObjects (self.mch, "bridge")
for bridge in bridges:
if _verbose:
myLink = None
for link in links:
if bridge.linkRef == link.id:
myLink = link
break
if myLink != None:
print "Deleting Bridge: %s:%d %s %s... " % (myLink.host, myLink.port, bridge.dest, bridge.key),
res = mc.syncCallMethod (self.mch, bridge.id, bridge.classKey, "close")
if res.status != 0:
print "Error: %d - %s" % (res.status, res.statusText)
elif _verbose:
print "Ok"
if _dellink:
links = mc.syncGetObjects (self.mch, "link")
for link in links:
if _verbose:
print "Deleting Link: %s:%d... " % (link.host, link.port),
res = mc.syncCallMethod (self.mch, link.id, link.classKey, "close")
if res.status != 0:
print "Error: %d - %s" % (res.status, res.statusText)
elif _verbose:
print "Ok"
def YN(val):
if val == 1:
return 'Y'
return 'N'
##
## Main Program
##
try:
longOpts = ("verbose", "quiet", "durable", "del-empty-link")
(optlist, cargs) = getopt.gnu_getopt (sys.argv[1:], "vqde", longOpts)
except:
Usage ()
for opt in optlist:
if opt[0] == "-v" or opt[0] == "--verbose":
_verbose = True
if opt[0] == "-q" or opt[0] == "--quiet":
_quiet = True
if opt[0] == "-d" or opt[0] == "--durable":
_durable = True
if opt[0] == "-e" or opt[0] == "--del-empty-link":
_dellink = True
nargs = len (cargs)
if nargs < 3:
Usage ()
group = cargs[0]
cmd = cargs[1]
rm = RouteManager (cargs[2])
rm.ConnectToBroker ()
if group == "link":
if cmd == "add":
if nargs != 4:
Usage()
rm.AddLink (cargs[3])
elif cmd == "del":
if nargs != 4:
Usage()
rm.DelLink (cargs[3])
elif cmd == "list":
if nargs != 3:
Usage()
rm.ListLinks ()
elif group == "route":
if cmd == "add":
if nargs < 6 or nargs > 8:
Usage ()
id = ""
excludes = ""
if nargs > 6: id = cargs[6]
if nargs > 7: excludes = cargs[7]
rm.AddRoute (cargs[3], cargs[4], cargs[5], id, excludes)
elif cmd == "del":
if nargs != 6:
Usage ()
else:
rm.DelRoute (cargs[3], cargs[4], cargs[5])
else:
if nargs != 3:
Usage ()
if cmd == "list":
rm.ListRoutes ()
elif cmd == "flush":
rm.ClearAllRoutes ()
else:
Usage ()
rm.Disconnect ()
|