1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
17 from vstf.common import message
18 from vstf.common import excepts
19 from vstf.rpc_frame_work import constant
21 LOG = logging.getLogger(__name__)
24 class RpcProxy(object):
26 def __init__(self, host,
30 """create a connection to rabbitmq,direct call and fan call supported.
33 # try to create connection of rabbitmq
35 self._connection = None
36 self._queue = str(uuid.uuid4())
37 self._consume_tag = None
43 self.url = 'amqp://' + self.user + ':' + self.passwd + \
44 '@' + self.srv + ':' + self.port + '/%2F'
46 self.connect(host, self.setup_vstf_producer)
47 except Exception as e:
48 LOG.error("create connection failed. e:%(e)s", {'e': e})
54 def connect(self, host, ok_callback):
55 """Create a Blocking connection to the rabbitmq-server
57 :param str host: the rabbitmq-server's host
58 :param obj ok_callback: if connect success than do this function
61 LOG.info("Connect to the server %s", host)
62 self._connection = pika.BlockingConnection(
63 pika.URLParameters(self.url))
67 def setup_vstf_producer(self):
69 self.create_exchange(constant.exchange_d, constant.DIRECT)
73 def open_channel(self):
74 self._channel = self._connection.channel()
76 self._channel.confirm_delivery()
78 def create_exchange(self, name, type):
79 LOG.info("Create %s exchange: %s", type, name)
80 self._channel.exchange_declare(exchange=name, type=type)
82 def bind_queues(self):
83 LOG.info("Declare queue %s and bind it to exchange %s",
84 self._queue, constant.exchange_d)
85 self._channel.queue_declare(queue=self._queue, exclusive=True)
86 self._channel.queue_bind(
87 exchange=constant.exchange_d,
90 def start_consumer(self):
91 LOG.info("Start response consumer")
92 self._consume_tag = self._channel.basic_consume(self.on_response,
96 def stop_consuming(self):
97 """Tell RabbitMQ that you would like to stop consuming by sending the
98 Basic.Cancel RPC command.
102 LOG.info('Sending a Basic.Cancel RPC command to RabbitMQ')
103 self._channel.basic_cancel(self._consume_tag)
107 def close_channel(self):
108 """Call to close the channel with RabbitMQ cleanly by issuing the
109 Channel.Close RPC command.
112 LOG.info('Closing the channel')
113 self._channel.close()
115 def close_connection(self):
116 """This method closes the connection to RabbitMQ."""
117 LOG.info('Closing connection')
118 self._connection.close()
121 self.stop_consuming()
122 self.close_connection()
124 def on_response(self, ch, method, props, body):
125 """this func reciver the msg"""
127 if self.corr_id == props.correlation_id:
128 self.response = json.loads(body)
129 LOG.debug("Proxy producer reciver the msg: head:%(h)s, body:%(b)s", {
130 'h': self.response.get('head'), 'b': self.response.get('body')})
132 LOG.warn("Proxy producer Drop the msg "
133 "because of the wrong correlation id, %s\n" % body)
135 def publish(self, target, corrid, body):
136 properties = pika.BasicProperties(reply_to=self._queue,
137 correlation_id=corrid)
139 "start to publish message to the exchange=%s, target=%s, msg=%s",
143 return self._channel.basic_publish(exchange=constant.exchange_d,
146 properties=properties,
147 body=message.encode(body))
149 def call(self, msg, target='agent', timeout=constant.TIMEOUT):
150 """send msg to agent by id, this func will wait ack until timeout
151 :msg the msg to be sent
153 :timeout timeout of waiting response
157 queue = constant.queue_common + target
158 # the msg request and respone must be match by corr_id
159 self.corr_id = str(uuid.uuid4())
161 msg = message.add_context(msg, corrid=self.corr_id)
163 # send msg to the queue
165 ret = self.publish(queue, self.corr_id, msg)
166 except Exception as e:
167 LOG.error(message.dumpstrace())
168 raise excepts.ChannelDie
170 # if delivery msg failed. return error
171 # clean the msg in the queue
173 LOG.error("productor message delivery failed.")
174 return "Message can not be deliveryed, please check the connection of agent."
177 t_begin = time.time()
178 while self.response is None:
179 self._connection.process_data_events()
180 count = time.time() - t_begin
182 LOG.error("Command timeout!")
183 # flush the msg of the queue
184 self._channel.queue_purge(queue=queue)
185 # self.channel.basic_cancel()
188 msg_body = message.get_body(message.decode(self.response))
190 # deal with exceptions
192 and isinstance(msg_body, dict) \
193 and 'exception' in msg_body:
194 ename = str(msg_body['exception'].get('name'))
195 if hasattr(exceptions, ename):
196 e = getattr(exceptions, ename)()
198 class CallError(Exception):
202 e.message = str(msg_body['exception'].get('message'))
203 e.args = msg_body['exception'].get('args')
209 class Server(object):
211 def __init__(self, host=None,
215 super(Server, self).__init__()
216 # Default use salt's master ip as rabbit rpc server ip
219 "Can not create rpc proxy because of the None rabbitmq server address.")
226 self.proxy = RpcProxy(host=host,
230 except Exception as e:
233 def call(self, msg, msg_id, timeout=constant.TIMEOUT):
234 """when you add a server listen to the rabbit
235 you must appoint which queue to be listen.
236 :@queue the queue name.
241 ret = self.proxy.call(msg, target=msg_id, timeout=timeout)
242 except excepts.ChannelDie:
243 # this may be the proxy die, try to reconnect to the rabbit
245 self.proxy = RpcProxy(host=self.host,
249 if self.proxy is None:
250 raise excepts.UnsolvableExit
254 # if retry happened except, throw to uplay
255 ret = self.proxy.call(msg, target=msg_id, timeout=timeout)
260 """when you want to send msg to all agent and no reply, use this func"""
261 LOG.warn("cast not support now.")
263 def make_msg(self, method, **kargs):
264 return {'method': method,