Example #1
0
    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 #2
0
 def test_warning_when_notifier_transport(self, log):
     transport = oslo_messaging.get_notification_transport(self.conf)
     oslo_messaging.RPCClient(transport, oslo_messaging.Target())
     log.warning.assert_called_once_with(
         "Using notification transport for RPC. Please use "
         "get_rpc_transport to obtain an RPC transport "
         "instance.")
Example #3
0
    def test_batch_size(self):
        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              url='fake:')

        endpoint = mock.Mock()
        endpoint.info.return_value = None
        listener_thread = self._setup_listener(transport, [endpoint],
                                               batch=(5, None))

        notifier = self._setup_notifier(transport)
        for _ in range(10):
            notifier.info({}, 'an_event.start', 'test message')

        self.wait_for_messages(2)
        self.assertFalse(listener_thread.stop())

        messages = [dict(ctxt={},
                         publisher_id='testpublisher',
                         event_type='an_event.start',
                         payload='test message',
                         metadata={'message_id': mock.ANY,
                                   'timestamp': mock.ANY})]

        endpoint.info.assert_has_calls([mock.call(messages * 5),
                                        mock.call(messages * 5)])
Example #4
0
def init(conf):
    global TRANSPORT, NOTIFICATION_TRANSPORT, LEGACY_NOTIFIER, NOTIFIER
    exmods = get_allowed_exmods()
    TRANSPORT = messaging.get_transport(conf,
                                        allowed_remote_exmods=exmods,
                                        aliases=TRANSPORT_ALIASES)
    NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
        conf, allowed_remote_exmods=exmods, aliases=TRANSPORT_ALIASES)
    serializer = RequestContextSerializer(JsonPayloadSerializer())
    if conf.notification_format == 'unversioned':
        LEGACY_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                             serializer=serializer)
        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                      serializer=serializer, driver='noop')
    elif conf.notification_format == 'both':
        LEGACY_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                             serializer=serializer)
        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                      serializer=serializer,
                                      topics=['versioned_notifications'])
    else:
        LEGACY_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                             serializer=serializer,
                                             driver='noop')
        NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                      serializer=serializer,
                                      topics=['versioned_notifications'])
Example #5
0
    def start(self):
        super(RPCService, self).start()
        target = messaging.Target(topic=self.topic, server=self.host)
        endpoints = [self.manager]
        serializer = objects_base.KongmingObjectSerializer()
        self.rpcserver = rpc.get_server(target, endpoints, serializer)
        self.rpcserver.start()
        admin_context = context.get_admin_context()
        self.tg.add_dynamic_timer(
            self.manager.periodic_tasks,
            periodic_interval_max=CONF.periodic_interval,
            context=admin_context)
        LOG.info('Created RPC server for service %(service)s on host '
                 '%(host)s.',
                 {'service': self.topic, 'host': self.host})
        if self.init_notification_listner:
            transport = messaging.get_notification_transport(CONF)
            targets = [
                messaging.Target(topic='versioned_notifications',
                                 exchange='nova')
            ]
            endpoints = [
                notification_handler.NotificationEndpoint()
            ]
            self.notification_listener = messaging.get_notification_listener(
                transport,
                targets,
                endpoints,
                executor='threading',
                pool='kongming-notification-handler')

            self.notification_listener.start()
Example #6
0
    def test_topics_from_kwargs(self):
        self.config(driver=['log'],
                    group='oslo_messaging_notifications')
        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              url='fake:')

        notifier = oslo_messaging.Notifier(transport, 'test.localhost',
                                           topics=['topic1', 'topic2'])
        self.assertEqual(['topic1', 'topic2'], notifier._topics)
Example #7
0
def init(conf):
    global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
    exmods = get_allowed_exmods()
    TRANSPORT = oslo_messaging.get_transport(conf, allowed_remote_exmods=exmods, aliases=TRANSPORT_ALIASES)
    NOTIFICATION_TRANSPORT = oslo_messaging.get_notification_transport(
        conf, allowed_remote_exmods=exmods, aliases=TRANSPORT_ALIASES
    )
    serializer = RequestContextSerializer()
    NOTIFIER = oslo_messaging.Notifier(NOTIFICATION_TRANSPORT, serializer=serializer)
Example #8
0
 def __init__(self, *args, **kwargs):
     # NOTE(dhellmann): Avoid a cyclical import by doing this one
     # at runtime.
     import oslo_messaging
     logging.Handler.__init__(self, *args, **kwargs)
     self._transport = oslo_messaging.get_notification_transport(cfg.CONF)
     self._notifier = oslo_messaging.Notifier(
         self._transport,
         publisher_id='error.publisher')
Example #9
0
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 #10
0
    def test_transport_url(self):
        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              self.url)
        self.addCleanup(transport.cleanup)
        driver = transport._driver

        self.assertEqual(self.expected['hostaddrs'], driver.pconn.hostaddrs)
        self.assertEqual(self.expected['username'], driver.pconn.username)
        self.assertEqual(self.expected['password'], driver.pconn.password)
        self.assertEqual(self.expected['vhost'], driver.virtual_host)
Example #11
0
def init(conf):
    global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
    exmods = get_allowed_exmods()
    TRANSPORT = oslo_messaging.get_transport(conf,
                                             allowed_remote_exmods=exmods)
    NOTIFICATION_TRANSPORT = oslo_messaging.get_notification_transport(
        conf, allowed_remote_exmods=exmods)
    serializer = RequestContextSerializer()
    NOTIFIER = oslo_messaging.Notifier(NOTIFICATION_TRANSPORT,
                                       serializer=serializer)
Example #12
0
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)
Example #13
0
    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 #14
0
    def __init__(self):
        if not CONF.stats.env_audit_enabled:
            return

        if Notification.transport is None:
            Notification.transport = messaging.get_notification_transport(CONF)
        self._notifier = messaging.Notifier(
            Notification.transport,
            publisher_id=('murano.%s' % socket.gethostname()),
            driver='messaging')
Example #15
0
 def __init__(self):
     self._zk = None
     transport = oslo_messaging.get_notification_transport(CONF)
     targets = [oslo_messaging.Target(exchange='ceilometer', topic='event')]
     endpoints = [NotificationHandler(self.zk)]
     server = oslo_messaging.get_notification_listener(transport,
                                                       targets,
                                                       endpoints,
                                                       executor='threading')
     self.server = server
Example #16
0
    def test_topics_from_config(self):
        self.config(driver=['log'],
                    group='oslo_messaging_notifications')
        self.config(topics=['topic1', 'topic2'],
                    group='oslo_messaging_notifications')
        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              url='fake:')

        notifier = oslo_messaging.Notifier(transport, 'test.localhost')
        self.assertEqual(['topic1', 'topic2'], notifier._topics)
Example #17
0
 def __init__(self, app, **conf):
     self.notifier = notify.Notifier(
         oslo_messaging.get_notification_transport(cfg.CONF,
                                                   conf.get('url')),
         publisher_id=conf.get('publisher_id',
                               os.path.basename(sys.argv[0])))
     self.service_name = conf.get('service_name')
     self.ignore_req_list = [x.upper().strip() for x in
                             conf.get('ignore_req_list', '').split(',')]
     super(RequestNotifier, self).__init__(app)
Example #18
0
    def test_transport_url(self):
        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              self.url)
        self.addCleanup(transport.cleanup)
        driver = transport._driver

        self.assertEqual(self.expected['hostaddrs'], driver.pconn.hostaddrs)
        self.assertEqual(self.expected['username'], driver.pconn.username)
        self.assertEqual(self.expected['password'], driver.pconn.password)
        self.assertEqual(self.expected['vhost'], driver.virtual_host)
Example #19
0
    def test_notifier(self):
        self.config(driver=['noop'],
                    group='oslo_messaging_notifications')

        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              url='fake:')

        notifier = oslo_messaging.Notifier(transport, 'test.localhost')

        self.assertFalse(notifier.is_enabled())
Example #20
0
def get_notification_listener():

    endpoints = [report_notification, track_instance, untrack_instance]
    transport = messaging.get_notification_transport(CONF)
    s_target = target.Target(topic='murano', server=str(uuid.uuid4()))
    listener = messaging.get_notification_listener(transport, [s_target],
                                                   endpoints,
                                                   executor='threading')

    return listener
Example #21
0
File: rpc.py Project: opnfv/cran
def init(conf):
    global TRANSPORT, NOTIFICATION_TRANSPORT, NOTIFIER
    exmods = get_allowed_exmods()
    #TRANSPORT = messaging.get_rpc_transport(conf,
    TRANSPORT = messaging.get_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 #22
0
    def setUp(self):
        super(TestRoutingNotifier, self).setUp()
        self.config(driver=['routing'],
                    group='oslo_messaging_notifications')

        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              url='fake:')
        self.notifier = oslo_messaging.Notifier(transport)
        self.router = self.notifier._driver_mgr['routing'].obj

        self.assertTrue(self.notifier.is_enabled())
Example #23
0
    def test_notifier_retry_config(self):
        conf = self.messaging_conf.conf
        self.config(driver=['messaging'],
                    group='oslo_messaging_notifications')

        conf.set_override('retry', 3, group='oslo_messaging_notifications')
        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              url='fake:')
        notifier = oslo_messaging.Notifier(transport, retry=5)

        self.assertEqual(5, notifier.retry)
    def test_instanciate(self):
        temp_dir = self.useFixture(fixtures.TempDir()).path
        self.config(location=temp_dir,
                    group='oslo_messaging_notifications')
        transport = oslo_messaging.get_notification_transport(self.conf)
        oslo_messaging.Notifier(transport, driver='prometheus_exporter',
                                topics=['my_topics'])

        self.assertEqual(self.conf.oslo_messaging_notifications.location,
                         temp_dir)
        self.assertTrue(os.path.isdir(
            self.conf.oslo_messaging_notifications.location))
Example #25
0
    def test_warning_when_notifier_transport(self, log):
        transport = oslo_messaging.get_notification_transport(self.conf)
        target = oslo_messaging.Target(topic='foo', server='bar')
        endpoints = [object()]
        serializer = object()

        oslo_messaging.get_rpc_server(transport, target,
                                      endpoints, serializer=serializer)
        log.warning.assert_called_once_with(
            "Using notification transport for RPC. Please use "
            "get_rpc_transport to obtain an RPC transport "
            "instance.")
Example #26
0
 def __init__(self, app, **conf):
     self.notifier = notify.Notifier(
         oslo_messaging.get_notification_transport(cfg.CONF,
                                                   conf.get('url')),
         publisher_id=conf.get('publisher_id',
                               os.path.basename(sys.argv[0])))
     self.service_name = conf.get('service_name')
     self.ignore_req_list = [
         x.upper().strip()
         for x in conf.get('ignore_req_list', '').split(',')
     ]
     super(RequestNotifier, self).__init__(app)
 def __init__(self, environment):
     if StatusReporter.transport is None:
         StatusReporter.transport = messaging.get_notification_transport(
             CONF)
     self._notifier = messaging.Notifier(
         StatusReporter.transport,
         publisher_id=uuidutils.generate_uuid(),
         topic='murano')
     if isinstance(environment, six.string_types):
         self._environment_id = environment
     else:
         self._environment_id = environment.id
Example #28
0
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 #29
0
    def test_logging_conf(self, mock_utcnow):
        fake_transport = oslo_messaging.get_notification_transport(self.conf)
        with mock.patch('oslo_messaging.transport._get_transport',
                        return_value=fake_transport):
            logging.config.dictConfig({
                'version': 1,
                'handlers': {
                    'notification': {
                        'class': 'oslo_messaging.LoggingNotificationHandler',
                        'level': self.priority.upper(),
                        'url': 'test://',
                    },
                },
                'loggers': {
                    'default': {
                        'handlers': ['notification'],
                        'level': self.priority.upper(),
                    },
                },
            })

        mock_utcnow.return_value = datetime.datetime.utcnow()

        levelno = getattr(logging, self.priority.upper())

        logger = logging.getLogger('default')
        lineno = sys._getframe().f_lineno + 1
        logger.log(levelno, 'foobar')

        n = oslo_messaging.notify._impl_test.NOTIFICATIONS[0][1]
        self.assertEqual(getattr(self, 'queue', self.priority.upper()),
                         n['priority'])
        self.assertEqual('logrecord', n['event_type'])
        self.assertEqual(str(timeutils.utcnow()), n['timestamp'])
        self.assertIsNone(n['publisher_id'])
        pathname = __file__
        if pathname.endswith(('.pyc', '.pyo')):
            pathname = pathname[:-1]
        self.assertDictEqual(
            n['payload'], {
                'process': os.getpid(),
                'funcName': 'test_logging_conf',
                'name': 'default',
                'thread': None,
                'levelno': levelno,
                'processName': 'MainProcess',
                'pathname': pathname,
                'lineno': lineno,
                'msg': 'foobar',
                'exc_info': None,
                'levelname': logging.getLevelName(levelno),
                'extra': None
            })
Example #30
0
    def test_warning_when_notifier_transport(self, log):
        transport = oslo_messaging.get_notification_transport(self.conf)
        target = oslo_messaging.Target(topic='foo', server='bar')
        endpoints = [object()]
        serializer = object()

        oslo_messaging.get_rpc_server(transport, target,
                                      endpoints, serializer=serializer)
        log.warning.assert_called_once_with(
            "Using notification transport for RPC. Please use "
            "get_rpc_transport to obtain an RPC transport "
            "instance.")
Example #31
0
    def start(self):
        endpoints = [report_notification, track_instance, untrack_instance]

        transport = messaging.get_notification_transport(CONF)
        s_target = target.Target(topic='murano', server=str(uuid.uuid4()))

        self.server = messaging.get_notification_listener(transport,
                                                          [s_target],
                                                          endpoints,
                                                          executor='eventlet')

        self.server.start()
        super(NotificationService, self).start()
Example #32
0
def setup_notifications():
    global NOTIFICATION_TRANSPORT, NOTIFIER, MESSAGING_TRANSPORT
    try:
        NOTIFICATION_TRANSPORT = messaging.get_notification_transport(cfg.CONF)
    except Exception:
        LOG.error("Unable to setup notification transport. Reusing "
                  "service transport for that.")
        setup_service_messaging()
        NOTIFICATION_TRANSPORT = MESSAGING_TRANSPORT

    serializer = ContextSerializer(JsonPayloadSerializer())
    NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                  serializer=serializer)
Example #33
0
    def start(self):
        endpoints = [
            TaskProcessingEndpoint(),
            StaticActionEndpoint(),
            SchemaEndpoint()
        ]

        transport = messaging.get_notification_transport(CONF)
        s_target = target.Target('murano', 'tasks', server=str(uuid.uuid4()))
        self.server = messaging.get_rpc_server(transport, s_target, endpoints,
                                               'eventlet')
        self.server.start()
        super(EngineService, self).start()
Example #34
0
def ListenerProc(exchange, project_id, cluster_id):
    transport = messaging.get_notification_transport(cfg.CONF)
    targets = [
        messaging.Target(topic='notifications', exchange=exchange),
    ]
    endpoints = [
        NotificationEndpoint(project_id, cluster_id),
    ]
    listener = messaging.get_notification_listener(
        transport, targets, endpoints, pool="senlin-listeners")

    listener.start()
    listener.wait()
Example #35
0
def ListenerProc(exchange, project_id, cluster_id):
    transport = messaging.get_notification_transport(cfg.CONF)
    targets = [
        messaging.Target(topic='versioned_notifications', exchange=exchange),
    ]
    endpoints = [
        NotificationEndpoint(project_id, cluster_id),
    ]
    listener = messaging.get_notification_listener(
        transport, targets, endpoints, executor='threading',
        pool="senlin-listeners")

    listener.start()
Example #36
0
def get_server_for_sp(sp):
    """Get notification listener for a particular service provider.

    The server can be run in the background under eventlet using .start()
    """
    cfg = config.get_conf_for_sp(sp)
    transport = oslo_messaging.get_notification_transport(CONF, cfg.messagebus)
    targets = [oslo_messaging.Target(topic='notifications')]
    return oslo_messaging.get_notification_listener(transport,
                                                    targets,
                                                    get_endpoints_for_sp(
                                                        cfg.sp_name),
                                                    executor='eventlet')
Example #37
0
def get_server_for_sp(sp):
    """Get notification listener for a particular service provider.

    The server can be run in the background under eventlet using .start()
    """
    cfg = config.get_conf_for_sp(sp)
    transport = oslo_messaging.get_notification_transport(CONF, cfg.messagebus)
    targets = [oslo_messaging.Target(topic='notifications')]
    return oslo_messaging.get_notification_listener(
            transport,
            targets,
            get_endpoints_for_sp(cfg.sp_name),
            executor='eventlet')
Example #38
0
 def __init__(self, monitor_plugins):
     """Initialize a notification monitor."""
     LOG.debug('Initializing a notification monitor...')
     try:
         self.handlers = defaultdict(list)
         self.listener = oslo_messaging.get_notification_listener(
             oslo_messaging.get_notification_transport(cfg.CONF),
             self._get_targets(monitor_plugins),
             self._get_endpoints(monitor_plugins),
             executor='eventlet')
         LOG.debug('Notification listener is successfully created.')
     except Exception as e:
         LOG.exception('Failed to create a notification listener. (%s)',
                       str(e))
Example #39
0
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
    def __init__(self, app, conf):
        self.logger = get_logger(conf, log_route='ceilometer')
        self._app = app

        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')]
        )
Example #41
0
 def __init__(self):
     """
     The method for notification client to send message called info(ctxt, msg, priority, retry).
     :param ctxt: (dict) A request context dict
     :param msg: (dict) Message to be sent
     :param priority: (str) Priority of the message(info, warn, error). It is the same as the function name in
         server's EndPoint.
     :param retry: (int) The num for client to re-send the message when it is failed. It is optional, and the
         default is None or -1(Means do not retry)
     """
     super(NotificationClient, self).__init__()
     self.transport = oslo_messaging.get_notification_transport(cfg.CONF)
     self.publisher_id = 'oslo_test'
     self.client = oslo_messaging.Notifier(self.transport, self.publisher_id, driver='messaging', topic=self.topic)
Example #42
0
def create_notifier(conf, log):
    if oslo_messaging:
        transport = oslo_messaging.get_notification_transport(
            conf.oslo_conf_obj, url=conf.get('transport_url'))

        notifier = oslo_messaging.Notifier(transport,
                                           os.path.basename(sys.argv[0]),
                                           driver=conf.get('driver'),
                                           topics=conf.get('topics'))

        return _MessagingNotifier(notifier)

    else:
        return _LogNotifier(log)
Example #43
0
def get_specific_transport(url, optional, exmods, is_for_notifications=False):
    try:
        if is_for_notifications:
            return oslo_messaging.get_notification_transport(
                cfg.CONF, url, allowed_remote_exmods=exmods, aliases=_ALIASES)
        else:
            return oslo_messaging.get_transport(
                cfg.CONF, url, allowed_remote_exmods=exmods, aliases=_ALIASES)
    except oslo_messaging.InvalidTransportURL as e:
        if not optional or e.url:
            # NOTE(sileht): oslo_messaging is configured but unloadable
            # so reraise the exception
            raise
        else:
            return None
Example #44
0
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)
def start_server(endpoints=[], topic='test_topic', url=''):
    transport = oslo_messaging.get_notification_transport(cfg.CONF, url=url)
    targets = [oslo_messaging.Target(topic=topic)]
    server = oslo_messaging.get_notification_listener(transport, targets,
                                                      endpoints)

    try:
        server.start()
        server.wait()
    except KeyboardInterrupt:
        print("stop server")
        server.stop()
    except Exception as e:
        print("[ERROR] faield to start server '%s'" % (e.message))
        server.stop()
Example #46
0
File: rpc.py Project: yachongli/nca
def init(conf, rpc_ext_mods=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 #47
0
def get_specific_transport(url, optional, exmods, is_for_notifications=False):
    try:
        if is_for_notifications:
            return oslo_messaging.get_notification_transport(
                cfg.CONF, url, allowed_remote_exmods=exmods)
        else:
            return oslo_messaging.get_rpc_transport(
                cfg.CONF, url, allowed_remote_exmods=exmods)
    except oslo_messaging.InvalidTransportURL as e:
        if not optional or e.url:
            # NOTE(sileht): oslo_messaging is configured but unloadable
            # so reraise the exception
            raise
        else:
            return None
Example #48
0
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 #49
0
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 #50
0
    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 #51
0
    def test_serializer(self, mock_utcnow):
        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              url='fake:')

        serializer = msg_serializer.NoOpSerializer()

        notifier = oslo_messaging.Notifier(transport,
                                           'test.localhost',
                                           driver='test',
                                           topics=['test'],
                                           serializer=serializer)

        message_id = uuid.uuid4()
        uuid.uuid4 = mock.Mock(return_value=message_id)

        mock_utcnow.return_value = datetime.datetime.utcnow()

        serializer.serialize_context = mock.Mock()
        serializer.serialize_context.return_value = dict(user='******')

        serializer.serialize_entity = mock.Mock()
        serializer.serialize_entity.return_value = 'sbar'

        notifier.info(dict(user='******'), 'test.notify', 'bar')

        message = {
            'message_id': str(message_id),
            'publisher_id': 'test.localhost',
            'event_type': 'test.notify',
            'priority': 'INFO',
            'payload': 'sbar',
            'timestamp': str(timeutils.utcnow()),
        }

        self.assertEqual([(dict(user='******'), message, 'INFO', -1)],
                         _impl_test.NOTIFICATIONS)

        uuid.uuid4.assert_called_once_with()
        serializer.serialize_context.assert_called_once_with(dict(user='******'))
        serializer.serialize_entity.assert_called_once_with(dict(user='******'),
                                                            'bar')
Example #52
0
def _get_notifier():
    """Return a notifier object.

    If _notifier is None it means that a notifier object has not been set.
    If _notifier is False it means that a notifier has previously failed to
    construct.
    Otherwise it is a constructed Notifier object.
    """
    global _notifier

    if _notifier is None:
        host = CONF.default_publisher_id or socket.gethostname()
        try:
            transport = oslo_messaging.get_notification_transport(CONF)
            _notifier = oslo_messaging.Notifier(transport,
                                                "identity.%s" % host)
        except Exception:
            LOG.exception(_LE("Failed to construct notifier"))
            _notifier = False

    return _notifier
Example #53
0
def setup_notifications():
    global NOTIFICATION_TRANSPORT, NOTIFIER, MESSAGING_TRANSPORT
    if not cfg.CONF.oslo_messaging_notifications.enable:
        LOG.info(_LI("Notifications disabled"))
        return

    try:
        NOTIFICATION_TRANSPORT = messaging.get_notification_transport(
            cfg.CONF, aliases=_ALIASES)
    except Exception:
        LOG.error(_LE("Unable to setup notification transport. Reusing "
                      "service transport for that."))

        setup_service_messaging()

        NOTIFICATION_TRANSPORT = MESSAGING_TRANSPORT

    serializer = ContextSerializer(JsonPayloadSerializer())
    NOTIFIER = messaging.Notifier(
        NOTIFICATION_TRANSPORT, serializer=serializer)
    LOG.info(_LI("Notifications enabled"))
Example #54
0
def init(conf):
    global TRANSPORT, NOTIFICATION_TRANSPORT
    global SENSORS_NOTIFIER, VERSIONED_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())
    SENSORS_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                          serializer=serializer)
    if conf.notification_level is None:
        VERSIONED_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                                serializer=serializer,
                                                driver='noop')
    else:
        VERSIONED_NOTIFIER = messaging.Notifier(NOTIFICATION_TRANSPORT,
                                                serializer=serializer,
                                                topics=['ironic_versioned_'
                                                        'notifications'])
Example #55
0
def setup(conf, binary, host):
    if conf.profiler.enabled:

        # Note(wangxiyuan): OSprofiler now support some kind of backends, such
        # as Ceilometer, ElasticSearch, Messaging and MongoDB.
        # 1. Ceilometer is only used for data collection, and Messaging is only
        # used for data transfer. So Ceilometer only works when Messaging is
        # enabled.
        # 2. ElasticSearch and MongoDB support both data collection and
        # transfer. So they can be used standalone.
        # 3. Choose which backend depends on the config option
        # "connection_string" , and the default value is "messaging://".
        backend_uri = conf.profiler.connection_string
        if "://" not in backend_uri:
            backend_uri += "://"
        parsed_connection = urlparse.urlparse(backend_uri)
        backend_type = parsed_connection.scheme
        if backend_type == "messaging":
            import oslo_messaging
            _notifier = notifier.create(
                backend_uri, oslo_messaging, {},
                oslo_messaging.get_notification_transport(conf),
                "Zaqar", binary, host)
        else:
            _notifier = notifier.create(backend_uri, project="Zaqar",
                                        service=binary, host=host)
        notifier.set(_notifier)
        LOG.warning("OSProfiler is enabled.\nIt means that person who "
                    "knows any of hmac_keys that are specified in "
                    "/etc/zaqar/zaqar.conf can trace his requests. \n In "
                    "real life only operator can read this file so there "
                    "is no security issue. Note that even if person can "
                    "trigger profiler, only admin user can retrieve trace "
                    "information.\n"
                    "To disable OSprofiler set in zaqar.conf:\n"
                    "[profiler]\nenabled=false")
        web.enable(conf.profiler.hmac_keys)
    else:
        web.disable()
Example #56
0
    def test_notifier(self, mock_utcnow):
        self.config(driver=['log'],
                    group='oslo_messaging_notifications')

        transport = oslo_messaging.get_notification_transport(self.conf,
                                                              url='fake:')

        notifier = oslo_messaging.Notifier(transport, 'test.localhost')

        message_id = uuid.uuid4()
        uuid.uuid4 = mock.Mock()
        uuid.uuid4.return_value = message_id

        mock_utcnow.return_value = datetime.datetime.utcnow()

        logger = mock.Mock()

        message = {
            'message_id': str(message_id),
            'publisher_id': 'test.localhost',
            'event_type': 'test.notify',
            'priority': 'INFO',
            'payload': 'bar',
            'timestamp': str(timeutils.utcnow()),
        }

        with mock.patch.object(logging, 'getLogger') as gl:
            gl.return_value = logger

            notifier.info({}, 'test.notify', 'bar')

            uuid.uuid4.assert_called_once_with()
            logging.getLogger.assert_called_once_with(
                'oslo.messaging.notification.test.notify')

        logger.info.assert_called_once_with(JsonMessageMatcher(message))

        self.assertTrue(notifier.is_enabled())
Example #57
0
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 #58
0
def get_transport():
    return oslo_messaging.get_notification_transport(CONF)