Ejemplo n.º 1
0
def rhmsg_backend():
    """Launch consumer backend based on rhmsg to consume message from UMB"""
    from rhmsg.activemq.consumer import AMQConsumer

    def _consumer_wrapper(msg, data=None):
        """Wrap UMB message in a message object

        fedora-messaging passes a message object rather than a raw dict into
        consumer function, but rhmsg does pass a dict instead. This wrapper
        makes it easier to handle message in a unified way in function
        ``consume``.

        :param msg: a proton.Message object represeting received message.
        :param data: any data passed from caller calling ``consumer.consume``.
        """
        logger.debug('Received message: %r', msg)
        try:
            consume(UMBMessage(msg))
        except json.JSONDecodeError as e:
            logger.error(f'Cannot decode message body: {msg.body}')
            logger.error(f'Reason: {str(e)}')

    consumer = AMQConsumer(
        urls=conf.rhmsg_brokers,
        certificate=conf.rhmsg_certificate,
        private_key=conf.rhmsg_private_key,
        trusted_certificates=conf.rhmsg_ca_cert,
    )

    consumer.consume(conf.rhmsg_queue,
                     callback=_consumer_wrapper,
                     subscription_name=conf.rhmsg_subscription_name)
Ejemplo n.º 2
0
    def test_run_container_to_consume(self, ReceiverHandler, Container,
                                      SSLDomain):
        consumer = AMQConsumer(**CONSUMER_CONFIG)
        message_handler = Mock()
        consumer.consume('myqueue', message_handler)

        Container.assert_called_once_with(ReceiverHandler.return_value)
        Container.return_value.run.assert_called_once()
Ejemplo n.º 3
0
 def test_consume_sub_name(self, ReceiverHandler, Container, SSLDomain):
     """Test creation of a ReceiverHandler when there is a subscription_name
     provided."""
     handler = ReceiverHandler.return_value
     handler.result = 'foo'
     consumer = AMQConsumer(**CONSUMER_CONFIG)
     consumer.consume('myqueue', Mock(), subscription_name='test-sub')
     self.assertEqual(ReceiverHandler.call_count, 1)
     kwargs = ReceiverHandler.call_args[1]
     self.assertIn('subscription_name', kwargs)
     self.assertEqual(kwargs['subscription_name'], 'test-sub')
Ejemplo n.º 4
0
def get_bus_consumer(env, certificate, private_key, trusted_certificates):
    """This is just a wrapper around creating a consumer. We're going to
do need this in multiple places though, so we want to ensure we do it
the same way each time.
    """
    return AMQConsumer(urls=URLS[env or 'stage'], certificate=certificate,
                       private_key=private_key, trusted_certificates=trusted_certificates)
Ejemplo n.º 5
0
def get_consumer(env,
                 client_cert_path,
                 client_key_path,
                 ca_chain_path=DEFAULT_CA_CHAIN):
    return AMQConsumer(urls=RH_UMB_URLS[env],
                       certificate=client_cert_path,
                       private_key=client_key_path,
                       trusted_certificates=ca_chain_path)
def main():
    """
    Main function to start listening
    """
    try:

        # Create our consumer
        log.info("Connecting to ACTIVEMQ as a consumer...")
        c = AMQConsumer(urls=(ACTIVEMQ_URL_1, ACTIVEMQ_URL_2),
                        certificate=CERT,
                        private_key=KEY,
                        trusted_certificates=CA_CERTS)
        # Start listening
        log.info('Starting up CD service...')
        c.consume(ACTIVEMQ_QUERY, lambda msg, data: handle_message(msg, data))

    except Exception as e:
        log.error(f"Error!\nException {e}\nSending email..")
        report_email('failure', 'Continuous-Deployment-Main',
                     traceback.format_exc())
Ejemplo n.º 7
0
 def test_ssl_domain(self, SSLDomain):
     consumer = AMQConsumer(**CONSUMER_CONFIG)
     SSLDomain.assert_called_once_with(SSLDomain.MODE_CLIENT)
     ssl_domain = consumer.ssl_domain
     ssl_domain.set_credentials.assert_called_once_with(
         CONSUMER_CONFIG['certificate'], CONSUMER_CONFIG['private_key'],
         None)
     ssl_domain.set_trusted_ca_db.assert_called_once_with(
         CONSUMER_CONFIG['trusted_certificates'])
     ssl_domain.set_peer_authentication.assert_called_once_with(
         SSLDomain.VERIFY_PEER)
Ejemplo n.º 8
0
def consume_messages(options):
    consumer_config = {
        'urls': broker_envs[options.env],
        'certificate': options.cert_file,
        'private_key': options.private_key_file,
        'trusted_certificates': options.ca_certs,
    }
    if options.durable and not options.subscription_name:
        options.subscription_name = options.address
    consumer = AMQConsumer(**consumer_config)
    consumer.consume(options.address,
                     selector=options.selector,
                     callback=message_handler,
                     auto_accept=False,
                     subscription_name=options.subscription_name,
                     data={
                         'dump': options.dump_message,
                         'pp': options.pretty_print,
                         'one_message_only': options.one_message_only,
                         'manual_ack': options.manual_ack,
                     })