Python oslo_messaging.get_notification_transport() Examples

The following are 21 code examples of oslo_messaging.get_notification_transport(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module oslo_messaging , or try the search function .
Example #1
Source File: listener.py    From searchlight with Apache License 2.0 6 votes vote down vote up
def main():
    if len(sys.argv) < 2:
        print("Supply an exchange")
        sys.exit(0)

    exchange = sys.argv[1]
    pool = sys.argv[2] if len(sys.argv) > 2 else None

    transport = oslo_messaging.get_notification_transport(
        cfg.CONF,
        url='rabbit://%s:%s@%s' % (username, password, host))
    targets = [oslo_messaging.Target(topic=topic, exchange=exchange)]
    endpoints = [EP()]
    oslo_listener = oslo_messaging.get_notification_listener(
        transport, targets, endpoints, pool=pool, executor='threading')
    try:
        print("Started")
        oslo_listener.start()
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        print("Stopping")
        oslo_listener.stop()
        oslo_listener.wait() 
Example #2
Source File: rpc.py    From neutron-lib with Apache License 2.0 6 votes vote down vote up
def init(conf, rpc_ext_mods=None):
    """Initialize the global RPC objects.

    :param conf: The oslo conf to use for initialization.
    :param rpc_ext_mods: Exception modules to expose via RPC.
    :returns: None.
    """
    global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER

    if rpc_ext_mods is None:
        rpc_ext_mods = _DFT_EXMODS
    else:
        rpc_ext_mods = list(set(rpc_ext_mods + _DFT_EXMODS))

    TRANSPORT = oslo_messaging.get_rpc_transport(
        conf, allowed_remote_exmods=rpc_ext_mods)
    NOTIFICATION_TRANSPORT = oslo_messaging.get_notification_transport(
        conf, allowed_remote_exmods=rpc_ext_mods)
    serializer = RequestContextSerializer()
    NOTIFIER = oslo_messaging.Notifier(NOTIFICATION_TRANSPORT,
                                       serializer=serializer) 
Example #3
Source File: rpc.py    From karbor with Apache License 2.0 6 votes vote down vote up
def init(conf):

    global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
    exmods = get_allowed_exmods()
    TRANSPORT = messaging.get_rpc_transport(conf,
                                            allowed_remote_exmods=exmods)
    NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
        conf,
        allowed_remote_exmods=exmods)

    # get_notification_transport has loaded oslo_messaging_notifications config
    # group, so we can now check if notifications are actually enabled.
    if utils.notifications_enabled(conf):
        json_serializer = messaging.JsonPayloadSerializer()
        serializer = RequestContextSerializer(json_serializer)
        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                      serializer=serializer)
    else:
        NOTIFIER = utils.DO_NOTHING 
Example #4
Source File: messaging.py    From aodh with Apache License 2.0 6 votes vote down vote up
def get_transport(conf, url=None, optional=False, cache=True):
    """Initialise the oslo_messaging layer."""
    global TRANSPORTS, DEFAULT_URL
    cache_key = url or DEFAULT_URL
    transport = TRANSPORTS.get(cache_key)
    if not transport or not cache:
        try:
            transport = oslo_messaging.get_notification_transport(conf, url)
        except (oslo_messaging.InvalidTransportURL,
                oslo_messaging.DriverLoadFailure):
            if not optional or url:
                # NOTE(sileht): oslo_messaging is configured but unloadable
                # so reraise the exception
                raise
            return None
        else:
            if cache:
                TRANSPORTS[cache_key] = transport
    return transport 
Example #5
Source File: messaging.py    From vitrage with Apache License 2.0 6 votes vote down vote up
def get_transport(url=None, optional=False, cache=True, rpc=False):
    """Initialise the oslo_messaging layer."""
    global TRANSPORTS, DEFAULT_URL
    cache_key = url or DEFAULT_URL + '_rpc' if rpc else ''
    transport = TRANSPORTS.get(cache_key)
    if not transport or not cache:
        try:
            if rpc:
                transport = oslo_msg.get_rpc_transport(CONF, url)
            else:
                transport = oslo_msg.get_notification_transport(CONF, url)
        except oslo_msg.InvalidTransportURL as e:
            if not optional or e.url:
                # NOTE(sileht): oslo_messaging is configured but unloadable
                # so reraise the exception
                raise
            return None
        else:
            if cache:
                TRANSPORTS[cache_key] = transport
    return transport 
Example #6
Source File: rpc.py    From manila with Apache License 2.0 6 votes vote down vote up
def init(conf):
    global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
    exmods = get_allowed_exmods()
    TRANSPORT = messaging.get_rpc_transport(conf,
                                            allowed_remote_exmods=exmods)
    NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
        conf,
        allowed_remote_exmods=exmods)

    if utils.notifications_enabled(conf):
        json_serializer = messaging.JsonPayloadSerializer()
        serializer = RequestContextSerializer(json_serializer)
        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                      serializer=serializer)
    else:
        NOTIFIER = utils.DO_NOTHING 
Example #7
Source File: listener.py    From searchlight with Apache License 2.0 6 votes vote down vote up
def start(self):
        super(ListenerService, self).start()
        transport = oslo_messaging.get_notification_transport(CONF)
        targets = [
            oslo_messaging.Target(topic=pl_topic, exchange=pl_exchange)
            for pl_topic, pl_exchange in self.topics_exchanges_set
        ]
        endpoints = [
            NotificationEndpoint(self.plugins, PipelineManager(self.plugins))
        ]
        listener = oslo_messaging.get_notification_listener(
            transport,
            targets,
            endpoints,
            executor='threading',
            pool=CONF.listener.notifications_pool)

        listener.start()
        self.listeners.append(listener) 
Example #8
Source File: rpc.py    From masakari with Apache License 2.0 5 votes vote down vote up
def init(conf):
    global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
    exmods = get_allowed_exmods()

    TRANSPORT = create_transport(get_transport_url())
    NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
        conf, allowed_remote_exmods=exmods)
    serializer = RequestContextSerializer(JsonPayloadSerializer())
    NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                  serializer=serializer,
                                  topics=['versioned_notifications']) 
Example #9
Source File: kapacitor_vitrage.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('sendto', help='url')
    args = parser.parse_args()
    data = sys.stdin.readlines()[0]
    transport_url = args.sendto
    transport = messaging.get_notification_transport(cfg.CONF, transport_url)

    driver = 'messagingv2'
    publisher = 'kapacitor_%s' % socket.gethostname()
    notifier = messaging.Notifier(transport,
                                  driver=driver,
                                  publisher_id=publisher,
                                  topics=['vitrage_notifications'])
    alarm = json.loads(data)
    host = alarm['data']['series'][0]['tags']['host']
    priority = alarm['level'].lower()
    alarm.update({'host': host,
                  'priority': priority})
    alarm.pop('data', None)
    alarm_status = alarm['level'].lower()
    event_type = '%s.%s' % (KAPACITOR_EVENT_TYPE, alarm_status)
    logging.info('Send to: %s', transport_url)
    logging.info('BODY:\n----\n%s\n', data)
    logging.info('PUBLISHER: %s', publisher)
    logging.info('EVENT_TYPE: %s', event_type)
    logging.info('\nALARM:\n%s', alarm)
    notifier.info(ctxt={'message_id': uuidutils.generate_uuid(),
                        'publisher_id': publisher},
                  event_type=event_type,
                  payload=alarm)
    logging.info('MESSAGE SENT..') 
Example #10
Source File: vitrageplugin.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def initialize(self):
        """Set up the Vitrage API client and add the notification callback. """

        url = self.config['transport_url']
        transport = messaging.get_notification_transport(cfg.CONF, url)
        self.notifier = messaging.Notifier(transport,
                                           driver='messagingv2',
                                           publisher_id=COLLECTD_DATASOURCE,
                                           topics=['vitrage_notifications'])
        self.add_notification_callback(self.notify) 
Example #11
Source File: zabbix_vitrage.py    From vitrage with Apache License 2.0 5 votes vote down vote up
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument('sendto', help='Vitrage message bus path')
    parser.add_argument('topic', help='zabbix topic')
    parser.add_argument('body', help='zabbix body')
    args = parser.parse_args()

    logging.info('SENDTO: %s', args.sendto)
    logging.info('TOPIC: %s', args.topic)
    logging.info('BODY:\n----\n%s\n', args.body)

    transport_url = args.sendto
    transport = messaging.get_notification_transport(cfg.CONF, transport_url)
    driver = 'messagingv2'
    publisher = 'zabbix_%s' % socket.gethostname()
    notifier = messaging.Notifier(transport,
                                  driver=driver,
                                  publisher_id=publisher,
                                  topics=['vitrage_notifications'])

    alarm_status = args.topic.lower()
    event_type = '%s.%s' % (ZABBIX_EVENT_TYPE, alarm_status)
    payload = create_payload(args.body)

    logging.info('PUBLISHER: %s', publisher)
    logging.info('EVENT_TYPE: %s', event_type)
    logging.info('\nPAYLOAD:\n%s', payload)
    notifier.info(ctxt={'message_id': uuidutils.generate_uuid(),
                        'publisher_id': publisher,
                        'timestamp': datetime.utcnow()},
                  event_type=event_type,
                  payload=payload)
    logging.info('MESSAGE SENT..') 
Example #12
Source File: rpc.py    From tacker with Apache License 2.0 5 votes vote down vote up
def init(conf):
    global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
    exmods = get_allowed_exmods()
    TRANSPORT = oslo_messaging.get_rpc_transport(conf,
                                                 allowed_remote_exmods=exmods)
    NOTIFICATION_TRANSPORT = oslo_messaging.get_notification_transport(
        conf, allowed_remote_exmods=exmods)
    json_serializer = oslo_messaging.JsonPayloadSerializer()
    serializer = RequestContextSerializer(json_serializer)
    NOTIFIER = oslo_messaging.Notifier(NOTIFICATION_TRANSPORT,
                                       serializer=serializer) 
Example #13
Source File: rpc.py    From watcher with Apache License 2.0 5 votes vote down vote up
def init(conf):
    global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
    exmods = get_allowed_exmods()
    TRANSPORT = messaging.get_rpc_transport(
        conf, allowed_remote_exmods=exmods)
    NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
        conf, allowed_remote_exmods=exmods)

    serializer = RequestContextSerializer(JsonPayloadSerializer())
    if not conf.notification_level:
        NOTIFIER = messaging.Notifier(
            NOTIFICATION_TRANSPORT, serializer=serializer, driver='noop')
    else:
        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                      serializer=serializer) 
Example #14
Source File: service.py    From watcher with Apache License 2.0 5 votes vote down vote up
def notification_transport(self):
        if self._notification_transport is None:
            self._notification_transport = om.get_notification_transport(CONF)
        return self._notification_transport 
Example #15
Source File: health_manager.py    From senlin with Apache License 2.0 5 votes vote down vote up
def ListenerProc(exchange, project_id, cluster_id, recover_action):
    """Thread procedure for running an event listener.

    :param exchange: The control exchange for a target service.
    :param project_id: The ID of the project to filter.
    :param cluster_id: The ID of the cluster to filter.
    :param recover_action: The health policy action name.
    """
    transport = messaging.get_notification_transport(cfg.CONF)

    if exchange == cfg.CONF.health_manager.nova_control_exchange:
        endpoint = nova_endpoint.NovaNotificationEndpoint(
            project_id, cluster_id, recover_action
        )

    else:
        endpoint = heat_endpoint.HeatNotificationEndpoint(
            project_id, cluster_id, recover_action
        )

    listener = messaging.get_notification_listener(
        transport, [endpoint.target], [endpoint], executor='threading',
        pool='senlin-listeners'
    )

    listener.start() 
Example #16
Source File: messaging.py    From senlin with Apache License 2.0 5 votes vote down vote up
def setup(url=None, optional=False):
    """Initialise the oslo_messaging layer."""
    global TRANSPORT, GLOBAL_TRANSPORT, NOTIFIER

    if url and url.startswith("fake://"):
        # NOTE: oslo_messaging fake driver uses time.sleep
        # for task switch, so we need to monkey_patch it
        eventlet.monkey_patch(time=True)

    messaging.set_transport_defaults('senlin')
    if not TRANSPORT:
        exmods = ['senlin.common.exception']
        try:
            TRANSPORT = messaging.get_rpc_transport(
                cfg.CONF, url, allowed_remote_exmods=exmods)
        except messaging.InvalidTransportURL as e:
            TRANSPORT = None
            if not optional or e.url:
                # NOTE: oslo_messaging is configured but unloadable
                # so reraise the exception
                raise

    if not NOTIFIER:
        exmods = ['senlin.common.exception']
        try:
            NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
                cfg.CONF, allowed_remote_exmods=exmods)
        except Exception:
            raise

        serializer = RequestContextSerializer(JsonPayloadSerializer())
        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                      serializer=serializer,
                                      topics=cfg.CONF.notification_topics) 
Example #17
Source File: rpc.py    From cyborg with Apache License 2.0 5 votes vote down vote up
def init(conf):
    global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
    exmods = get_allowed_exmods()
    TRANSPORT = messaging.get_rpc_transport(conf,
                                            allowed_remote_exmods=exmods)
    NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
        conf,
        allowed_remote_exmods=exmods)
    serializer = RequestContextSerializer(messaging.JsonPayloadSerializer())
    NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                  serializer=serializer,
                                  topics=['notifications']) 
Example #18
Source File: notifier.py    From searchlight with Apache License 2.0 5 votes vote down vote up
def get_transport():
    return oslo_messaging.get_notification_transport(CONF) 
Example #19
Source File: rpc.py    From designate with Apache License 2.0 5 votes vote down vote up
def init(conf):
    global TRANSPORT, NOTIFIER, NOTIFICATION_TRANSPORT
    exmods = get_allowed_exmods()
    TRANSPORT = create_transport(get_transport_url())
    NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
        conf, allowed_remote_exmods=exmods)
    serializer = RequestContextSerializer(JsonPayloadSerializer())
    NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                  serializer=serializer) 
Example #20
Source File: test.py    From manila with Apache License 2.0 4 votes vote down vote up
def setUp(self):
        """Run before each test method to initialize test environment."""
        super(TestCase, self).setUp()

        conf_fixture.set_defaults(CONF)
        CONF([], default_config_files=[])

        global _DB_CACHE
        if not _DB_CACHE:
            _DB_CACHE = Database(
                db_api,
                migration,
                sql_connection=CONF.database.connection,
                sqlite_db=CONF.sqlite_db,
                sqlite_clean_db=CONF.sqlite_clean_db,
            )
        self.useFixture(_DB_CACHE)

        self.injected = []
        self._services = []
        self.flags(fatal_exception_format_errors=True)
        # This will be cleaned up by the NestedTempfile fixture
        lock_path = self.useFixture(fixtures.TempDir()).path
        self.fixture = self.useFixture(config_fixture.Config(lockutils.CONF))
        self.fixture.config(lock_path=lock_path, group='oslo_concurrency')
        self.fixture.config(
            disable_process_locking=True, group='oslo_concurrency')

        rpc.add_extra_exmods('manila.tests')
        self.addCleanup(rpc.clear_extra_exmods)
        self.addCleanup(rpc.cleanup)

        self.messaging_conf = messaging_conffixture.ConfFixture(CONF)
        self.messaging_conf.transport_url = 'fake:/'
        self.messaging_conf.response_timeout = 15
        self.useFixture(self.messaging_conf)

        oslo_messaging.get_notification_transport(CONF)
        self.override_config('driver', ['test'],
                             group='oslo_messaging_notifications')

        rpc.init(CONF)

        mock.patch('keystoneauth1.loading.load_auth_from_conf_options').start()

        fake_notifier.stub_notifier(self)

        # Locks must be cleaned up after tests
        CONF.set_override('backend_url', 'file://' + lock_path,
                          group='coordination')
        coordination.LOCK_COORDINATOR.start()
        self.addCleanup(coordination.LOCK_COORDINATOR.stop) 
Example #21
Source File: swift.py    From ceilometermiddleware with Apache License 2.0 4 votes vote down vote up
def __init__(self, app, conf):
        self._app = app

        self.ignore_projects = self._get_ignore_projects(conf)

        oslo_messaging.set_transport_defaults(conf.get('control_exchange',
                                                       'swift'))
        self._notifier = oslo_messaging.Notifier(
            oslo_messaging.get_notification_transport(cfg.CONF,
                                                      url=conf.get('url')),
            publisher_id='ceilometermiddleware',
            driver=conf.get('driver', 'messagingv2'),
            topics=[conf.get('topic', 'notifications')])

        self.metadata_headers = [h.strip().replace('-', '_').lower()
                                 for h in conf.get(
                                     "metadata_headers",
                                     "").split(",") if h.strip()]

        self.reseller_prefix = conf.get('reseller_prefix', 'AUTH_')
        if self.reseller_prefix and self.reseller_prefix[-1] != '_':
            self.reseller_prefix += '_'

        LOG.setLevel(getattr(logging, conf.get('log_level', 'WARNING')))

        # NOTE: If the background thread's send queue fills up, the event will
        #  be discarded
        #
        # For backward compatibility we default to False and therefore wait for
        #  sending to complete. This causes swift proxy to hang if the
        #  destination is unavailable.
        self.nonblocking_notify = strutils.bool_from_string(
            conf.get('nonblocking_notify', False))

        # Initialize the sending queue and thread, but only once
        if self.nonblocking_notify and Swift.event_queue is None:
            Swift.threadLock.acquire()
            if Swift.event_queue is None:
                send_queue_size = int(conf.get('send_queue_size', 1000))
                Swift.event_queue = queue.Queue(send_queue_size)
                self.start_sender_thread()
            Swift.threadLock.release()