def cleanup(pre_factory: bool, config_path: str): """Remove/Delete all the data that was created after post install.""" # delete message_types from cortx.utils.message_bus.error import MessageBusError try: # use gconf to clean and delete all message type in messagebus from cortx.utils.message_bus import MessageBus, MessageBusAdmin Conf.load(GCONF_INDEX, config_path, skip_reload=True) message_bus_backend = Conf.get(GCONF_INDEX, MSG_BUS_BACKEND_KEY) message_server_endpoints = Conf.get(GCONF_INDEX,\ f'{EXTERNAL_KEY}>{message_bus_backend}>endpoints') MessageBus.init(message_server_endpoints) mb = MessageBusAdmin(admin_id='cleanup') message_types_list = mb.list_message_types() if message_types_list: mb.deregister_message_type(message_types_list) except MessageBusError as e: raise SetupError(e.rc, "Can not cleanup Message Bus. %s", e) except Exception as e: raise SetupError( errors.ERR_OP_FAILED, "Can not cleanup Message \ Bus. %s", e) return 0
def reset(config_path: str): """Remove/Delete all the data/logs that was created by user/testing.""" from cortx.utils.message_bus.error import MessageBusError try: # use gconf to deregister IEM from cortx.utils.message_bus import MessageBusAdmin, MessageBus from cortx.utils.message_bus import MessageProducer Conf.load(GCONF_INDEX, config_path, skip_reload=True) message_bus_backend = Conf.get('config', MSG_BUS_BACKEND_KEY) message_server_endpoints = Conf.get('config',\ f'{EXTERNAL_KEY}>{message_bus_backend}>endpoints') MessageBus.init(message_server_endpoints) mb = MessageBusAdmin(admin_id='reset') message_types_list = mb.list_message_types() if message_types_list: for message_type in message_types_list: producer = MessageProducer(producer_id=message_type,\ message_type=message_type, method='sync') producer.delete() except MessageBusError as e: raise SetupError(e.rc, "Can not reset Message Bus. %s", e) except Exception as e: raise SetupError( errors.ERR_OP_FAILED, "Internal error, can not \ reset Message Bus. %s", e) return 0
def init(cls, component: str, source: str, cluster_id: str, \ message_server_endpoints: str, **message_server_kwargs): """ Set the Event Message context Parameters: component Component that generates the IEM. For e.g. 'S3', 'SSPL' source Single character that indicates the type of component. For e.g. H-Hardware, S-Software, F-Firmware, O-OS """ cls._component = component cls._source = source cls._site_id = 1 cls._rack_id = 1 cls._node_id = Conf.machine_id cls._cluster_id = cluster_id if cls._component is None: Log.error("Invalid component type: %s" % cls._component ) raise EventMessageError(errno.EINVAL, "Invalid component type: %s", \ cls._component) if cls._source not in cls._SOURCE.keys(): Log.error("Invalid source type: %s" % cls._source) raise EventMessageError(errno.EINVAL, "Invalid source type: %s", \ cls._source) MessageBus.init(message_server_endpoints, **message_server_kwargs) cls._producer = MessageProducer(producer_id='event_producer', \ message_type='IEM', method='sync') Log.info("IEM Producer initialized for component %s and source %s" % \ (cls._component, cls._source))
def init(): """ Initialize utils MessageBus Library with kafka endpoints once per service. In future utils will throw error if init done multiple times. If any new service will come which uses MessageBus then init should be done there. """ message_server_endpoints = Conf.get( const.HA_GLOBAL_INDEX, f"kafka_config{const._DELIM}endpoints") utils_message_bus.init(message_server_endpoints)
def setUpClass(cls,\ cluster_conf_path: str = 'yaml:///etc/cortx/cluster.conf'): """Register the test message_type.""" if TestMessage._cluster_conf_path: cls.cluster_conf_path = TestMessage._cluster_conf_path else: cls.cluster_conf_path = cluster_conf_path Conf.load('config', cls.cluster_conf_path, skip_reload=True) message_server_endpoints = Conf.get('config',\ 'cortx>external>kafka>endpoints') Log.init('message_bus', '/var/log', level='INFO', \ backup_count=5, file_size_in_mb=5) MessageBus.init(message_server_endpoints=message_server_endpoints) cls._admin = MessageBusAdmin(admin_id='register') cls._admin.register_message_type(message_types= \ [TestMessage._message_type], partitions=1)
def subscribe(cls, component: str, message_server_endpoints: str, \ **message_server_kwargs): """ Subscribe to IEM alerts Parameters: component Component that generates the IEM. For e.g. 'S3', 'SSPL' """ if component is None: Log.error("Invalid component type: %s" % component) raise EventMessageError(errno.EINVAL, "Invalid component type: %s", \ component) MessageBus.init(message_server_endpoints, **message_server_kwargs) cls._consumer = MessageConsumer(consumer_id='event_consumer', \ consumer_group=component, message_types=['IEM'], \ auto_ack=True, offset='earliest') Log.info("IEM Consumer initialized for component: %s" % component)
def setUpClass(cls, \ cluster_conf_path: str = 'yaml:///etc/cortx/cluster.conf'): """Register the test message_type.""" if TestKVPayloadMessage._cluster_conf_path: cls.cluster_conf_path = TestKVPayloadMessage._cluster_conf_path else: cls.cluster_conf_path = cluster_conf_path Conf.load('config', cls.cluster_conf_path, skip_reload=True) message_server_endpoints = Conf.get('config',\ 'cortx>external>kafka>endpoints') Log.init('message_bus', '/var/log', level='INFO', \ backup_count=5, file_size_in_mb=5) MessageBus.init(message_server_endpoints=message_server_endpoints) cls._admin = MessageBusAdmin(admin_id='register') cls._admin.register_message_type(message_types= \ [TestKVPayloadMessage._message_type], partitions=1) cls._consumer = MessageConsumer(consumer_id='kv_consumer', \ consumer_group='kv', message_types=[TestKVPayloadMessage.\ _message_type], auto_ack=True, offset='earliest') cls._producer = MessageProducer(producer_id='kv_producer', \ message_type=TestKVPayloadMessage._message_type, method='sync')
def __init__(self, message_server_endpoints, message_server_port=28300,\ cluster_id=None): super().__init__() MessageBus.init(message_server_endpoints=message_server_endpoints) from cortx.utils.iem_framework import IemRequestHandler from cortx.utils.message_bus import MessageBusRequestHandler from cortx.utils.audit_log import AuditLogRequestHandler IemRequestHandler.cluster_id = cluster_id IemRequestHandler.message_server_endpoints = message_server_endpoints self.app.add_routes([web.post('/EventMessage/event', IemRequestHandler.send), \ web.get('/EventMessage/event', IemRequestHandler.receive), \ web.post('/MessageBus/message/{message_type}', \ MessageBusRequestHandler.send), \ web.get('/MessageBus/message/{message_type}', \ MessageBusRequestHandler.receive), web.post('/AuditLog/message/', \ AuditLogRequestHandler.send), web.post('/AuditLog/webhook/', \ AuditLogRequestHandler.send_webhook_info) ]) super().run_app(web, message_server_port)
def init(config_path: str): """Perform initialization.""" # Create message_type for Event Message from cortx.utils.message_bus import MessageBus, MessageBusAdmin from cortx.utils.message_bus.error import MessageBusError try: # Read the config values # use gconf to create IEM topic Conf.load(GCONF_INDEX, config_path, skip_reload=True) message_bus_backend = Conf.get(GCONF_INDEX, MSG_BUS_BACKEND_KEY) message_server_endpoints = Conf.get(GCONF_INDEX,\ f'{EXTERNAL_KEY}>{message_bus_backend}>endpoints') MessageBus.init(message_server_endpoints) admin = MessageBusAdmin(admin_id='register') admin.register_message_type(message_types=['IEM',\ 'audit_messages'], partitions=1) except MessageBusError as e: if 'TOPIC_ALREADY_EXISTS' not in e.desc: raise SetupError(e.rc, "Unable to create message_type. %s", e) return 0