This repository has been archived by the owner. It is now read-only.
/
RabbitMqManager.py
439 lines (350 loc) · 15.8 KB
/
RabbitMqManager.py
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# -*- coding: utf-8 -*-
from robot.api import logger
from robot.utils import ConnectionCache
import httplib
import base64
import json
import socket
import urlparse
import urllib
class RabbitMqManager(object):
"""
Библиотека для управления сервером RabbitMq.
Реализована на основе:
- [ http://hg.rabbitmq.com/rabbitmq-management/raw-file/3646dee55e02/priv/www-api/help.html | RabbitMQ Management HTTP API ]
- [ https://github.com/rabbitmq/rabbitmq-management/blob/master/bin/rabbitmqadmin | rabbitmqadmin ]
== Зависимости ==
| robot framework | http://robotframework.org |
== Example ==
| *Settings* | *Value* |
| Library | RabbitMqManager |
| Library | Collections |
| *Test Cases* | *Action* | *Argument* | *Argument* | *Argument* | *Argument* | *Argument* |
| Simple |
| | Connect To Rabbitmq | my_host_name | 15672 | guest | guest | alias=rmq |
| | ${overview}= | Overview |
| | Log Dictionary | ${overview} |
| | Close All Rabbitmq Connections |
"""
ROBOT_LIBRARY_SCOPE='GLOBAL'
def __init__(self):
self._connection=None
self.headers=None
self._cache=ConnectionCache()
def connect_to_rabbitmq (self, host, port, username = 'guest', password = 'guest', timeout = 15, alias = None):
"""
Подключение к серверу RabbitMq.
*Args:*\n
_host_ - имя сервера;\n
_port_ - номер порта;\n
_username_ - имя пользователя;\n
_password_ - пароль пользователя;\n
_timeout_ - время ожидания соединения;\n
_alias_ - псевдоним соединения;\n
*Returns:*\n
Индекс текущего соединения.
*Raises:*\n
socket.error в том случае, если невозможно создать соединение.
*Example:*\n
| Connect To Rabbitmq | my_host_name | 15672 | guest | guest | alias=rmq |
"""
port=int (port)
timeout=int (timeout)
logger.debug ('Connecting using : host=%s, port=%d, username=%s, password=%s, timeout=%d, alias=%s '%(host, port, username, password, timeout, alias))
self.headers={"Authorization":"Basic "+base64.b64encode(username+":"+password)}
try:
self._connection=httplib.HTTPConnection (host, port, timeout)
self._connection.connect()
return self._cache.register(self._connection, alias)
except socket.error, e:
raise Exception ("Could not connect to RabbitMq", str(e))
def switch_rabbitmq_connection (self, index_or_alias):
"""
Переключение между активными соединениями с RabbitMq, используя их индекс или псевдоним.
Псевдоним задается в keyword [#Connect To Rabbitmq|Connect To Rabbitmq], который также возвращает индекс соединения.
*Args:*\n
_index_or_alias_ -индекс соединения или его псевдоним;
*Returns:*\n
Индекс предыдущего соединения.
*Example:*\n
| Connect To Rabbitmq | my_host_name_1 | 15672 | guest | guest | alias=rmq1 |
| Connect To Rabbitmq | my_host_name_2 | 15672 | guest | guest | alias=rmq2 |
| Switch Rabbitmq Connection | rmq1 |
| ${live}= | Is alive |
| Switch Rabbitmq Connection | rmq2 |
| ${live}= | Is alive |
| Close All Rabbitmq Connections |
"""
old_index=self._cache.current_index
self._connection=self._cache.switch(index_or_alias)
return old_index
def disconnect_from_rabbitmq(self):
"""
Закрытие текущего соединения с RabbitMq.
*Example:*\n
| Connect To Rabbitmq | my_host_name | 15672 | guest | guest | alias=rmq |
| Disconnect From Rabbitmq |
"""
logger.debug ('Close connection with : host=%s, port=%d '%(self._connection.host, self._connection.port))
self._connection.close()
def close_all_rabbitmq_connections (self):
"""
Закрытие всех соединений с RabbitMq.
Данный keyword используется для закрытия всех соединений в том случае, если их было открыто несколько штук.
Использовать [#Disconnect From Rabbitmq|Disconnect From Rabbitmq] и [#Close All Rabbitmq Connections|Close All Rabbitmq Connections]
вместе нельзя.
После выполнения этого keyword индекс, возвращаемый [#Connect To Rabbitmq|Connect To Rabbitmq], начинается с 1.
*Example:*\n
| Connect To Rabbitmq | my_host_name | 15672 | guest | guest | alias=rmq |
| Close All Rabbitmq Connections |
"""
self._connection=self._cache.close_all()
def _http_request (self, method, path, body):
"""
Выполнение запросов к RabbitMq
*Args:*\n
_method_ - метод запроса;\n
_path_ - uri запроса;\n
_body_ - тело POST-запроса;\n
"""
if body!="":
self.headers["Content-Type"]="application/json"
logger.debug ('Prepared request with metod '+method+' to '+'http://'+self._connection.host+':'+str(self._connection.port)+path+' and body\n'+body)
try:
self._connection.request(method, path, body, self.headers)
except socket.error, e:
raise Exception("Could not send request: {0}".format(e))
resp=self._connection.getresponse()
if resp.status==400:
raise Exception (json.loads(resp.read())['reason'])
if resp.status==401:
raise Exception("Access refused: {0}".format('http://'+self._connection.host+':'+str(self._connection.port)+path))
if resp.status==404:
raise Exception("Not found: {0}".format('http://'+self._connection.host+':'+str(self._connection.port)+path))
if resp.status==301:
url=urlparse.urlparse(resp.getheader('location'))
[host, port]=url.netloc.split(':')
self.options.hostname=host
self.options.port=int(port)
return self.http(method, url.path+'?'+url.query, body)
if resp.status<200 or resp.status>400:
raise Exception("Received %d %s for request %s\n%s"
%(resp.status, resp.reason, 'http://'+self._connection.host+':'+str(self._connection.port)+path, resp.read()))
return resp.read()
def _get (self, path):
return self._http_request('GET', '/api%s'%path, '')
def _put (self, path, body):
return self._http_request("PUT", "/api%s"%path, body)
def _post (self, path, body):
return self._http_request("POST", "/api%s"%path, body)
def _delete (self, path):
return self._http_request("DELETE", "/api%s"%path, "")
def _quote_vhost (self, vhost):
"""
Декодирование vhost.
"""
if vhost=='/':
vhost='%2F'
if vhost!='%2F':
vhost=urllib.quote(vhost)
return vhost
def is_alive(self):
"""
Проверка работоспособности RabbitMq.
Отправляется GET-запрос следующего вида: 'http://<host>:<port>/api/' и проверяется код возврата.
*Returns:*\n
bool True, если код возврата равен 200.\n
bool False во всех остальных случаях.
*Raises:*\n
socket.error в том случае, если невозмодно отправить GET-запрос.
*Example:*\n
| ${live}= | Is Alive |
=>\n
True
"""
try:
self._connection.request('GET', '/api/')
except socket.error, e:
raise Exception("Could not send request: {0}".format(e))
resp=self._connection.getresponse()
resp.read()
logger.debug ('Response status=%d'%resp.status)
if resp.status==200 :
return True
else:
return False
def overview (self):
"""
Информация о сервере RabbitMq.
*Returns:*\n
Словарь с информацией о сервере.
*Example:*\n
| ${overview}= | Overview |
| Log Dictionary | ${overview} |
| ${version}= | Get From Dictionary | ${overview} | rabbitmq_version |
=>\n
Dictionary size is 13 and it contains following items:
| erlang_full_version | Erlang R16B02 (erts-5.10.3) [source] [64-bit] [smp:2:2] [async-threads:30] [hipe] [kernel-poll:true] |
| erlang_version | R16B02 |
| listeners | [{u'node': u'rabbit@srv2-rs582b-m', u'ip_address': u'0.0.0.0', u'protocol': u'amqp', u'port': 5672}] |
| management_version | 3.1.0 |
| message_stats | [] |
${version} = 3.1.0
"""
return json.loads(self._get ('/overview'))
def connections (self):
"""
Список открытых соединений.
"""
return json.loads(self._get ('/connections'))
def get_name_of_all_connections (self):
"""
Список имен всех открытых соединений.
"""
names=[]
data=self.connections ()
for item in data :
names.append(item['name'])
return names
def channels (self):
"""
Список открытых каналов.
"""
return json.loads(self._get ('/channels'))
def exchanges (self):
"""
Список exchange.
*Example:*\n
| ${exchanges}= | Exchanges |
| Log List | ${exchanges} |
| ${item}= | Get From list | ${exchanges} | 1 |
| ${name}= | Get From Dictionary | ${q} | name |
=>\n
List length is 8 and it contains following items:
| 0 | {u'name': u'', u'durable': True, u'vhost': u'/', u'internal': False, u'message_stats': [], u'arguments': {}, u'type': u'direct', u'auto_delete': False} |
| 1 | {u'name': u'amq.direct', u'durable': True, u'vhost': u'/', u'internal': False, u'message_stats': [], u'arguments': {}, u'type': u'direct', u'auto_delete': False} |
...\n
${name} = amq.direct
"""
return json.loads(self._get ('/exchanges'))
def get_names_of_all_exchanges (self):
"""
Список имён всех exchanges.
*Example:*\n
| ${names}= | Get Names Of All Exchanges |
| Log List | ${names} |
=>\n
| List has one item:
| amq.direct
"""
names=[]
data=self.exchanges ()
for item in data :
names.append(item['name'])
return names
def queues (self):
"""
Список очередей.
"""
return json.loads(self._get ('/queues'))
def get_queues_on_vhost (self, vhost = '%2F'):
"""
Список очередей для виртуального хоста.
*Args:*\n
_vhost_ -имя виртуального хоста (перекодируется при помощи urllib.quote);
"""
return json.loads(self._get ('/queues/'+self._quote_vhost(vhost)))
def get_names_of_queues_on_vhost (self, vhost = '%2F'):
"""
Список имен очередей виртуального хоста.
*Args:*\n
- vhost: имя виртуального хоста (перекодируется при помощи urllib.quote)
*Example:*\n
| ${names}= | Get Names Of Queues On Vhost |
| Log List | ${names} |
=>\n
| List has one item:
| federation: ex2 -> rabbit@server.net.ru
"""
names=[]
data=self.get_queues_on_vhost (vhost)
for item in data :
names.append(item['name'])
return names
def queue_exists(self, queue, vhost='%2F'):
"""
Verifies that the one or more queues exists
"""
names = self.get_names_of_queues_on_vhost()
if queue in names:
return True
else:
return False
def delete_queues_by_name (self, name, vhost = '%2F'):
"""
Удаление очереди с виртуального хоста.
*Args:*\n
_name_ - имя очереди (перекодируется urllib.quote);\n
_vhost_ - имя виртуального хоста (перекодируется urllib.quote);\n
"""
return self._delete('/queues/'+self._quote_vhost(vhost)+'/'+urllib.quote(name))
def vhosts (self):
"""
Список виртуальных хостов.
"""
return json.loads(self._get ('/vhosts'))
def nodes(self):
"""
List of nodes in the RabbitMQ cluster
"""
return json.loads(self._get('/nodes'))
@property
def _cluster_name(self):
"""
Name identifying this RabbitMQ cluster.
"""
return json.loads(self._get('/cluster-name'))
def create_queues_by_name(self, name, auto_delete=False, durable=True, arguments={}, vhost='%2F'):
"""
Create an individual queue.
"""
node = self._cluster_name['name']
body = json.dumps({
"auto_delete": auto_delete,
"durable": durable,
"arguments": arguments,
"node": node
})
return self._put('/queues/' + self._quote_vhost(vhost) + '/' + urllib.quote(name), body=body)
def publish_message_by_name(self, queue, msg, properties, vhost='%2F'):
"""
Publish a message to a given exchange
"""
name = "amq.default"
body = json.dumps({
"properties": properties,
"routing_key": queue,
"payload": msg,
"payload_encoding": "string"
})
routed = self._post('/exchanges/' + self._quote_vhost(vhost) +
'/' + urllib.quote(name) + '/publish', body=body)
return json.loads(routed)
def get_messages_by_queue(self, queue, count=5, requeue=False, encoding="auto", truncate=50000, vhost='%2F'):
"""
Get messages from a queue.
"""
body = json.dumps({
"count": count,
"requeue": requeue,
"encoding": encoding,
"truncate": truncate
})
messages = self._post('/queues/' + self._quote_vhost(vhost) +
'/' + urllib.quote(queue) + '/get', body=body)
return json.loads(messages)
def purge_messages_by_queue(self, name, vhost='%2F'):
"""
Purge contents of a queue.
"""
return self._delete('/queues/' + self._quote_vhost(vhost) + '/' + urllib.quote(name) + '/contents')