Ejemplo n.º 1
0
 def _override_vhost(self):
     old_vhost = self.rabbitmq_vhost
     auth = CloudbrainAuth(self.config['authUrl'])
     self.rabbitmq_vhost = auth.get_vhost_by_username(self.rabbitmq_user)
     if old_vhost not in ["/", ""]:
         _LOGGER.warn("Configured RabbitMQ Vhost is being overridden.")
         _LOGGER.warn("New Vhost: %s" % self.rabbitmq_vhost)
Ejemplo n.º 2
0
    def __init__(self, base_routing_key, rabbitmq_user, rabbitmq_pwd):

        super(PikaSubscriber, self).__init__(base_routing_key)
        _LOGGER.debug("Base routing key: %s" % self.base_routing_key)
        _LOGGER.debug("Routing keys: %s" % self.routing_keys)
        _LOGGER.debug("Metric buffers: %s" % self.metric_buffers)

        self.rabbitmq_user = rabbitmq_user
        self.rabbitmq_pwd = rabbitmq_pwd

        self.connection = None
        self.channels = {}
        self.config = get_config()
        self.rabbitmq_address = self.config['rabbitHost']
        self.auth = CloudbrainAuth(self.config['authUrl'])
        self.rabbitmq_vhost = self.auth.get_vhost(rabbitmq_user, rabbitmq_pwd)
 def connect(self):
     auth = CloudbrainAuth(self.rabbit_auth_url)
     if self.token:
         credentials = pika.PlainCredentials(self.token, '')
         vhost = auth.get_vhost_by_token(self.token)
         connection_params = pika.ConnectionParameters(
             host=self.rabbitmq_address, virtual_host=vhost,
             credentials=credentials)
     else:
         credentials = pika.PlainCredentials(self.rabbitmq_user,
                                             self.rabbitmq_pwd)
         vhost = getattr(self, 'rabbitmq_vhost',
                         auth.get_vhost_by_username(self.rabbitmq_user))
         connection_params = pika.ConnectionParameters(
             host=self.rabbitmq_address, virtual_host=vhost,
             credentials=credentials)
     self.connection = pika.adapters.tornado_connection.TornadoConnection(
         connection_params,
         self.on_connected,
         stop_ioloop_on_close=False,
         custom_ioloop=IOLoop.instance())
Ejemplo n.º 4
0
 def connect(self):
     auth = CloudbrainAuth(self.rabbit_auth_url)
     if self.token:
         credentials = pika.PlainCredentials(self.token, '')
         vhost = auth.get_vhost_by_token(self.token)
         connection_params = pika.ConnectionParameters(
             host=self.rabbitmq_address,
             virtual_host=vhost,
             credentials=credentials)
     else:
         credentials = pika.PlainCredentials(self.rabbitmq_user,
                                             self.rabbitmq_pwd)
         vhost = getattr(self, 'rabbitmq_vhost',
                         auth.get_vhost_by_username(self.rabbitmq_user))
         connection_params = pika.ConnectionParameters(
             host=self.rabbitmq_address,
             virtual_host=vhost,
             credentials=credentials)
     self.connection = pika.adapters.tornado_connection.TornadoConnection(
         connection_params,
         self.on_connected,
         stop_ioloop_on_close=False,
         custom_ioloop=IOLoop.instance())
Ejemplo n.º 5
0
    def __init__(self,
                 base_routing_key,
                 rabbitmq_user,
                 rabbitmq_pwd):

        super(PikaPublisher, self).__init__(base_routing_key)
        _LOGGER.debug("Base routing key: %s" % self.base_routing_key)
        _LOGGER.debug("Routing keys: %s" % self.routing_keys)
        _LOGGER.debug("Metric buffers: %s" % self.metric_buffers)

        self.rabbitmq_user = rabbitmq_user
        self.rabbitmq_pwd = rabbitmq_pwd

        self.connection = None
        self.channels = {}
        self.config = get_config()
        self.rabbitmq_address = self.config['rabbitHost']
        self.auth = CloudbrainAuth(self.config['authUrl'])
        self.rabbitmq_vhost = self.auth.get_vhost(rabbitmq_user, rabbitmq_pwd)
Ejemplo n.º 6
0
class PikaPublisher(PublisherInterface):
    """
    Publish data to RabbitMQ exchanges. The name of the exchange is the
    routing key.
    """

    def __init__(self,
                 base_routing_key,
                 rabbitmq_user,
                 rabbitmq_pwd):

        super(PikaPublisher, self).__init__(base_routing_key)
        _LOGGER.debug("Base routing key: %s" % self.base_routing_key)
        _LOGGER.debug("Routing keys: %s" % self.routing_keys)
        _LOGGER.debug("Metric buffers: %s" % self.metric_buffers)

        self.rabbitmq_user = rabbitmq_user
        self.rabbitmq_pwd = rabbitmq_pwd

        self.connection = None
        self.channels = {}
        self.config = get_config()
        self.rabbitmq_address = self.config['rabbitHost']
        self.auth = CloudbrainAuth(self.config['authUrl'])
        self.rabbitmq_vhost = self.auth.get_vhost(rabbitmq_user, rabbitmq_pwd)

    def connect(self):
        credentials = pika.PlainCredentials(self.rabbitmq_user,
                                            self.rabbitmq_pwd)

        self.connection = pika.BlockingConnection(pika.ConnectionParameters(
            host=self.rabbitmq_address,
            virtual_host=self.rabbitmq_vhost,
            credentials=credentials))

    def disconnect(self):
        for channel in self.channels.values():
            if channel:
                channel.close()
        self.connection.close()

    def register(self, metric_name, num_channels, buffer_size=1):

        routing_key = "%s:%s" % (self.base_routing_key, metric_name)
        self.register_metric(routing_key,
                             metric_name,
                             num_channels,
                             buffer_size)
        self._rabbitmq_register(routing_key)

    def _rabbitmq_register(self, routing_key):
        channel = self.connection.channel()
        channel.exchange_declare(exchange=routing_key, exchange_type='direct')
        self.channels[routing_key] = channel

    def publish(self, metric_name, data):

        routing_key = "%s:%s" % (self.base_routing_key, metric_name)

        # add the data to the metric buffer
        data_to_send = self.metric_buffers[routing_key].add(data)

        # publish only if you reached the max buffer size
        if data_to_send:
            self._rabbitmq_publish(routing_key, data_to_send)

    def _rabbitmq_publish(self, routing_key, data):

        # delivery_mode=2 make the message persistent
        self.channels[routing_key].basic_publish(
            exchange=routing_key,
            routing_key=routing_key,
            body=json.dumps(data),
            properties=pika.BasicProperties(delivery_mode=2))
Ejemplo n.º 7
0
def _get_vhost(username, password):
    config = get_config()
    auth = CloudbrainAuth(config['authUrl'])
    return auth.get_vhost(username, password)
Ejemplo n.º 8
0
class PikaSubscriber(SubscriberInterface):
    def __init__(self, base_routing_key, rabbitmq_user, rabbitmq_pwd):

        super(PikaSubscriber, self).__init__(base_routing_key)
        _LOGGER.debug("Base routing key: %s" % self.base_routing_key)
        _LOGGER.debug("Routing keys: %s" % self.routing_keys)
        _LOGGER.debug("Metric buffers: %s" % self.metric_buffers)

        self.rabbitmq_user = rabbitmq_user
        self.rabbitmq_pwd = rabbitmq_pwd

        self.connection = None
        self.channels = {}
        self.config = get_config()
        self.rabbitmq_address = self.config['rabbitHost']
        self.auth = CloudbrainAuth(self.config['authUrl'])
        self.rabbitmq_vhost = self.auth.get_vhost(rabbitmq_user, rabbitmq_pwd)

    def connect(self):
        credentials = pika.PlainCredentials(self.rabbitmq_user,
                                            self.rabbitmq_pwd)

        self.connection = pika.BlockingConnection(
            pika.ConnectionParameters(host=self.rabbitmq_address,
                                      virtual_host=self.rabbitmq_vhost,
                                      credentials=credentials))

    def register(self, metric_name, num_channels, buffer_size=1):

        routing_key = "%s:%s" % (self.base_routing_key, metric_name)
        self.register_metric(routing_key, metric_name, num_channels,
                             buffer_size)
        self._rabbitmq_register(routing_key)

    def _rabbitmq_register(self, routing_key):
        channel = self.connection.channel()
        channel.exchange_declare(exchange=routing_key, type='direct')

        queue_name = channel.queue_declare(exclusive=True).method.queue
        channel.queue_bind(exchange=routing_key,
                           queue=queue_name,
                           routing_key=routing_key)

        self.channels[routing_key] = {
            'channel': channel,
            'queue_name': queue_name
        }

    def disconnect(self):
        for channel in self.channels.values():
            if channel:
                channel['channel'].stop_consuming()
                channel['channel'].close()
        self.connection.close()

    def subscribe(self, metric_name, callback):

        routing_key = '%s:%s' % (self.base_routing_key, metric_name)
        self._rabbitmq_subscribe(routing_key, callback)

    def _rabbitmq_subscribe(self, routing_key, callback):
        channel = self.channels[routing_key]['channel']
        queue_name = self.channels[routing_key]['queue_name']

        channel.basic_consume(callback,
                              queue=queue_name,
                              exclusive=True,
                              no_ack=True)

        channel.start_consuming()

    def get_one_message(self, metric_name):
        routing_key = '%s:%s' % (self.base_routing_key, metric_name)
        return self._rabbitmq_get_one_message(routing_key)

    def _rabbitmq_get_one_message(self, routing_key):
        channel = self.channels[routing_key]['channel']
        queue_name = self.channels[routing_key]['queue_name']
        meth_frame, header_frame, body = channel.basic_get(queue_name)

        return body
Ejemplo n.º 9
0
class PikaPublisher(PublisherInterface):
    """
    Publish data to RabbitMQ exchanges. The name of the exchange is the
    routing key.
    """
    def __init__(self, base_routing_key, rabbitmq_user, rabbitmq_pwd):

        super(PikaPublisher, self).__init__(base_routing_key)
        _LOGGER.debug("Base routing key: %s" % self.base_routing_key)
        _LOGGER.debug("Routing keys: %s" % self.routing_keys)
        _LOGGER.debug("Metric buffers: %s" % self.metric_buffers)

        self.rabbitmq_user = rabbitmq_user
        self.rabbitmq_pwd = rabbitmq_pwd

        self.connection = None
        self.channels = {}
        self.config = get_config()
        self.rabbitmq_address = self.config['rabbitHost']
        self.auth = CloudbrainAuth(self.config['authUrl'])
        self.rabbitmq_vhost = self.auth.get_vhost(rabbitmq_user, rabbitmq_pwd)

    def connect(self):
        credentials = pika.PlainCredentials(self.rabbitmq_user,
                                            self.rabbitmq_pwd)

        self.connection = pika.BlockingConnection(
            pika.ConnectionParameters(host=self.rabbitmq_address,
                                      virtual_host=self.rabbitmq_vhost,
                                      credentials=credentials))

    def disconnect(self):
        for channel in self.channels.values():
            if channel:
                channel.close()
        self.connection.close()

    def register(self, metric_name, num_channels, buffer_size=1):

        routing_key = "%s:%s" % (self.base_routing_key, metric_name)
        self.register_metric(routing_key, metric_name, num_channels,
                             buffer_size)
        self._rabbitmq_register(routing_key)

    def _rabbitmq_register(self, routing_key):
        channel = self.connection.channel()
        channel.exchange_declare(exchange=routing_key, type='direct')
        self.channels[routing_key] = channel

    def publish(self, metric_name, data):

        routing_key = "%s:%s" % (self.base_routing_key, metric_name)

        # add the data to the metric buffer
        data_to_send = self.metric_buffers[routing_key].add(data)

        # publish only if you reached the max buffer size
        if data_to_send:
            self._rabbitmq_publish(routing_key, data_to_send)

    def _rabbitmq_publish(self, routing_key, data):

        # delivery_mode=2 make the message persistent
        self.channels[routing_key].basic_publish(
            exchange=routing_key,
            routing_key=routing_key,
            body=json.dumps(data),
            properties=pika.BasicProperties(delivery_mode=2))
Ejemplo n.º 10
0
def _get_vhost(username, password):
    config = get_config()
    auth = CloudbrainAuth(config['authUrl'])
    return auth.get_vhost(username, password)
Ejemplo n.º 11
0
class PikaSubscriber(SubscriberInterface):
    def __init__(self,
                 base_routing_key,
                 rabbitmq_user,
                 rabbitmq_pwd):

        super(PikaSubscriber, self).__init__(base_routing_key)
        _LOGGER.debug("Base routing key: %s" % self.base_routing_key)
        _LOGGER.debug("Routing keys: %s" % self.routing_keys)
        _LOGGER.debug("Metric buffers: %s" % self.metric_buffers)

        self.rabbitmq_user = rabbitmq_user
        self.rabbitmq_pwd = rabbitmq_pwd

        self.connection = None
        self.channels = {}
        self.config = get_config()
        self.rabbitmq_address = self.config['rabbitHost']
        self.auth = CloudbrainAuth(self.config['authUrl'])
        self.rabbitmq_vhost = self.auth.get_vhost(rabbitmq_user, rabbitmq_pwd)

    def connect(self):
        credentials = pika.PlainCredentials(self.rabbitmq_user,
                                            self.rabbitmq_pwd)

        self.connection = pika.BlockingConnection(pika.ConnectionParameters(
            host=self.rabbitmq_address,
            virtual_host=self.rabbitmq_vhost,
            credentials=credentials))

    def register(self, metric_name, num_channels, buffer_size=1):

        routing_key = "%s:%s" % (self.base_routing_key, metric_name)
        self.register_metric(routing_key,
                             metric_name,
                             num_channels,
                             buffer_size)
        self._rabbitmq_register(routing_key)

    def _rabbitmq_register(self, routing_key):
        channel = self.connection.channel()
        channel.exchange_declare(exchange=routing_key, exchange_type='direct')

        queue_name = channel.queue_declare(exclusive=True).method.queue
        channel.queue_bind(exchange=routing_key,
                           queue=queue_name,
                           routing_key=routing_key)

        self.channels[routing_key] = {'channel': channel,
                                      'queue_name': queue_name}

    def disconnect(self):
        for channel in self.channels.values():
            if channel:
                channel['channel'].stop_consuming()
                channel['channel'].close()
        self.connection.close()

    def subscribe(self, metric_name, callback):

        routing_key = '%s:%s' % (self.base_routing_key, metric_name)
        self._rabbitmq_subscribe(routing_key, callback)

    def _rabbitmq_subscribe(self, routing_key, callback):
        channel = self.channels[routing_key]['channel']
        queue_name = self.channels[routing_key]['queue_name']

        channel.basic_consume(callback,
                              queue=queue_name,
                              exclusive=True,
                              no_ack=True)

        channel.start_consuming()

    def get_one_message(self, metric_name):
        routing_key = '%s:%s' % (self.base_routing_key, metric_name)
        return self._rabbitmq_get_one_message(routing_key)

    def _rabbitmq_get_one_message(self, routing_key):
        channel = self.channels[routing_key]['channel']
        queue_name = self.channels[routing_key]['queue_name']
        meth_frame, header_frame, body = channel.basic_get(queue_name)

        return body