コード例 #1
0
def onDeviceDeleted(object, event):
    # Clean up any AMQP queues we may have created for this device.

    if not IObjectWillBeAddedEvent.providedBy(event):
        connectionInfo = getUtility(IAMQPConnectionInfo)
        queueSchema = getUtility(IQueueSchema)

        # For some reason, if an error gets thrown by queue_delete, it seems
        # to break the connection, so we'll just use a separate connection
        # for each call to it.
        for queue in ('$OpenStackInboundEvent', '$OpenStackInboundPerf'):
            queueName = substitute_replacements(queueSchema._queue_nodes[queue].name,
                                                {'device': object.id})

            amqpClient = BlockingPublisher(connectionInfo, queueSchema)
            channel = amqpClient.getChannel()
            try:
                LOG.debug("Removing AMQP queue %s" % queueName)
                channel.queue_delete(queueName)
                LOG.info("Removed AMQP queue %s successfully." % queueName)
            except AMQPChannelException, e:
                # if the queue doesn't exist, don't worry about it.
                if e.amqp_reply_code == 404:
                    LOG.debug('Queue %s did not exist', queueName)
                else:
                    LOG.exception(e)

            amqpClient.close()
コード例 #2
0
class BlockingQueuePublisher(object):
    """
    Class that is responsible for sending messages to the amqp exchange.
    """
    implements(IQueuePublisher)

    def __init__(self):
        self.reconnect()

    def reconnect(self):
        connectionInfo = getUtility(IAMQPConnectionInfo)
        queueSchema = getUtility(IQueueSchema)
        self._client = BlockingPublisher(connectionInfo, queueSchema)

    def publish(self, exchange, routing_key, message, createQueues=None,
                mandatory=False, immediate=False, headers=None,
                declareExchange=True):
        if createQueues:
            for queue in createQueues:
                self._client.createQueue(queue)
        self._client.publish(exchange, routing_key, message,
                             mandatory=mandatory, immediate=immediate,
                             headers=headers, declareExchange=declareExchange)

    @property
    def channel(self):
        return self._client.getChannel()

    def close(self):
        """
        Closes the channel and connection
        """
        self._client.close()
コード例 #3
0
ファイル: publisher.py プロジェクト: zenoss/zenoss-prodbin
class BlockingQueuePublisher(object):
    """
    Class that is responsible for sending messages to the amqp exchange.
    """
    implements(IQueuePublisher)

    def __init__(self):
        self.reconnect()

    def reconnect(self):
        connectionInfo = getUtility(IAMQPConnectionInfo)
        queueSchema = getUtility(IQueueSchema)
        self._client = BlockingPublisher(connectionInfo, queueSchema)

    def publish(self, exchange, routing_key, message, createQueues=None,
                mandatory=False, headers=None,
                declareExchange=True):
        if createQueues:
            for queue in createQueues:
                if not self._client.queueExists(queue):
                    self.reconnect()
                    self._client.createQueue(queue)
        self._client.publish(exchange, routing_key, message,
                             mandatory=mandatory, headers=headers,
                             declareExchange=declareExchange)

    @property
    def channel(self):
        return self._client.getChannel()

    def close(self):
        """
        Closes the channel and connection
        """
        self._client.close()
コード例 #4
0
ファイル: schema.py プロジェクト: zenoss/zenoss-prodbin
def removeZenPackQueuesExchanges(path):
    """
    Attempts to remove all the queues that the zenpack registered

    @type  Path: string
    @param Path: Absolute path to the zenpack (from zenpack.path())
    """
    schema = _loadQjs(path)
    if not schema:
        # no queues to remove
        return

    connectionInfo = getUtility(IAMQPConnectionInfo)
    queueSchema = getUtility(IQueueSchema)
    amqpClient = BlockingPublisher(connectionInfo, queueSchema)
    channel = amqpClient.getChannel()
    queues = schema[0].get('queues', [])
    exchanges = schema[0].get('exchanges', [])
    log = logging.getLogger('zen.ZenMessaging')

    # queues
    for identifier, queue in queues.iteritems():
        name = queue['name']
        try:
            substitute_replacements(name, None)
        except MissingReplacementException:
            # Ignore these - they can't be automatically deleted
            continue
        try:
            log.info("Removing queue %s", name)
            channel.queue_delete(name)
        except Exception as e:
            # the queue might already be deleted etc, do not fail if we can't
            # remove it
            log.debug(e)
            log.info("Unable to remove queue %s", name)

    # exchanges
    for identifier, exchange in exchanges.iteritems():
        name = exchange['name']
        try:
            log.info("Removing exchange %s", name)
            channel.exchange_delete(name)
        except Exception as e:
            # the queue might already be deleted etc, do not fail if we can't
            # remove it
            log.debug(e)
            log.info("Unable to remove exchange %s", name)
    amqpClient.close()
コード例 #5
0
def create_exchanges():
    print "Verifying exchanges"

    # Create all the AMQP exchanges we require.   This should be run before we try
    # to have ceilometer send zenoss any events.
    zcml.load_config('meta.zcml', zope.component)
    zcml.load_config('configure.zcml', zope.component)
    zcml.load_config('configure.zcml', Products.ZenMessaging.queuemessaging)

    connectionInfo = getUtility(IAMQPConnectionInfo)
    queueSchema = getUtility(IQueueSchema)
    amqpClient = BlockingPublisher(connectionInfo, queueSchema)
    channel = amqpClient.getChannel()

    for exchange in ('$OpenStackInbound', ):
        exchangeConfig = queueSchema.getExchange(exchange)
        print "Verifying configuration of exchange '%s' (%s)" % (exchange, exchangeConfig.name)

        # Declare the exchange to which the message is being sent
        getAdapter(channel, IAMQPChannelAdapter).declareExchange(exchangeConfig)

    amqpClient.close()
import zope.component
from zope.component import getUtility, getAdapter
from Products.Five import zcml

import Products.ZenMessaging.queuemessaging
from zenoss.protocols.interfaces import IAMQPConnectionInfo, IQueueSchema, IAMQPChannelAdapter
from zenoss.protocols.amqp import Publisher as BlockingPublisher

# Create all the AMQP exchanges we require.   This should be run before we try
# to have ceilometer send zenoss any events.

if __name__ == '__main__':
    zcml.load_config('meta.zcml', zope.component)
    zcml.load_config('configure.zcml', zope.component)
    zcml.load_config('configure.zcml', Products.ZenMessaging.queuemessaging)

    connectionInfo = getUtility(IAMQPConnectionInfo)
    queueSchema = getUtility(IQueueSchema)
    amqpClient = BlockingPublisher(connectionInfo, queueSchema)
    channel = amqpClient.getChannel()

    for exchange in ('$OpenStackInbound', '$OpenStackInboundHeartbeats',):
        exchangeConfig = queueSchema.getExchange(exchange)
        print "Verifying configuration of exchange '%s' (%s)" % (exchange, exchangeConfig.name) 

        # Declare the exchange to which the message is being sent
        getAdapter(channel, IAMQPChannelAdapter).declareExchange(exchangeConfig)

    amqpClient.close()