diff options
author | Dmitry Tantsur <dtantsur@protonmail.com> | 2023-03-14 16:18:08 +0100 |
---|---|---|
committer | Dmitry Tantsur <dtantsur@protonmail.com> | 2023-03-14 18:26:06 +0100 |
commit | e30ba65f94133f5829b477e1fe255aae1dc519e7 (patch) | |
tree | 765b4cedbf70a3c61fae5f9eac46a80d7afc2b3e /ironic/drivers/modules | |
parent | 3dd54a11018f5f766cbbdaef4aac5e20cb76f7d6 (diff) | |
download | ironic-e30ba65f94133f5829b477e1fe255aae1dc519e7.tar.gz |
Refactoring: clean up inspection data handlers
* Avoid using the term "introspection". We need to settle on either
"inspection" or "introspection", and the Ironic API already uses
the former.
* Accept (and return) inventory and plugin data separately to reflect
the Ironic API (single JSON blobs are an Inspector legacy).
* Make sure to mention the container name in error logging.
* Use more readable formatting syntax for building Swift names.
* Do not mock objects with dicts (in unit tests).
* Simplify inventory API tests.
Change-Id: Id8c4bc6d35b9634f5a5ac2b345a8fd7f1dba13c0
Diffstat (limited to 'ironic/drivers/modules')
-rw-r--r-- | ironic/drivers/modules/inspect_utils.py | 116 | ||||
-rw-r--r-- | ironic/drivers/modules/inspector/interface.py | 14 |
2 files changed, 59 insertions, 71 deletions
diff --git a/ironic/drivers/modules/inspect_utils.py b/ironic/drivers/modules/inspect_utils.py index 432345cf1..f87fcc82b 100644 --- a/ironic/drivers/modules/inspect_utils.py +++ b/ironic/drivers/modules/inspect_utils.py @@ -68,101 +68,88 @@ def create_ports_if_not_exist(task, macs=None): def clean_up_swift_entries(task): - """Delete swift entries containing introspection data. + """Delete swift entries containing inspection data. Delete swift entries related to the node in task.node containing - introspection data. The entries are + inspection data. The entries are ``inspector_data-<task.node.uuid>-inventory`` for hardware inventory and - similar for ``-plugin`` containing the rest of the introspection data. + similar for ``-plugin`` containing the rest of the inspection data. :param task: A TaskManager instance. """ if CONF.inventory.data_backend != 'swift': return swift_api = swift.SwiftAPI() - swift_object_name = '%s-%s' % (_OBJECT_NAME_PREFIX, task.node.uuid) container = CONF.inventory.swift_data_container - inventory_obj_name = swift_object_name + '-inventory' - plugin_obj_name = swift_object_name + '-plugin' + inventory_obj_name = f'{_OBJECT_NAME_PREFIX}-{task.node.uuid}-inventory' + plugin_obj_name = f'{_OBJECT_NAME_PREFIX}-{task.node.uuid}-plugin' try: swift_api.delete_object(inventory_obj_name, container) except swiftclient.exceptions.ClientException as e: - if e.http_status == 404: - # 404 -> entry did not exist - acceptable. - pass - else: - LOG.error("Object %(obj)s related to node %(node)s " - "failed to be deleted with expection: %(e)s", + if e.http_status != 404: + LOG.error("Object %(obj)s in container %(cont)s with inventory " + "for node %(node)s failed to be deleted: %(e)s", {'obj': inventory_obj_name, 'node': task.node.uuid, - 'e': e}) + 'e': e, 'cont': container}) raise exception.SwiftObjectStillExists(obj=inventory_obj_name, node=task.node.uuid) try: swift_api.delete_object(plugin_obj_name, container) except swiftclient.exceptions.ClientException as e: - if e.http_status == 404: - # 404 -> entry did not exist - acceptable. - pass - else: - LOG.error("Object %(obj)s related to node %(node)s " - "failed to be deleted with exception: %(e)s", + if e.http_status != 404: + LOG.error("Object %(obj)s in container %(cont)s with plugin data " + "for node %(node)s failed to be deleted: %(e)s", {'obj': plugin_obj_name, 'node': task.node.uuid, - 'e': e}) + 'e': e, 'cont': container}) raise exception.SwiftObjectStillExists(obj=plugin_obj_name, node=task.node.uuid) -def store_introspection_data(node, introspection_data, context): - """Store introspection data. +def store_inspection_data(node, inventory, plugin_data, context): + """Store inspection data. - Store the introspection data for a node. Either to database - or swift as configured. + Store the inspection data for a node. The storage is either the database + or the Object Storage API (swift/radosgw) as configured. - :param node: the Ironic node that the introspection data is about - :param introspection_data: the data to store + :param node: the Ironic node that the inspection data is about + :param inventory: the inventory to store + :param plugin_data: the plugin data (if any) to store :param context: an admin context """ # If store_data == 'none', do not store the data store_data = CONF.inventory.data_backend if store_data == 'none': - LOG.debug('Introspection data storage is disabled, the data will ' - 'not be saved for node %(node)s', {'node': node.uuid}) + LOG.debug('Inspection data storage is disabled, the data will ' + 'not be saved for node %s', node.uuid) return - inventory_data = introspection_data.pop("inventory") - plugin_data = introspection_data if store_data == 'database': node_inventory.NodeInventory( context, node_id=node.id, - inventory_data=inventory_data, + inventory_data=inventory, plugin_data=plugin_data).create() - LOG.info('Introspection data was stored in database for node ' - '%(node)s', {'node': node.uuid}) + LOG.info('Inspection data was stored in database for node %s', + node.uuid) if store_data == 'swift': - swift_object_name = _store_introspection_data_in_swift( + swift_object_name = _store_inspection_data_in_swift( node_uuid=node.uuid, - inventory_data=inventory_data, + inventory_data=inventory, plugin_data=plugin_data) - LOG.info('Introspection data was stored for node %(node)s in Swift' - ' object %(obj_name)s-inventory and %(obj_name)s-plugin', + LOG.info('Inspection data was stored in Swift for node %(node)s: ' + 'objects %(obj_name)s-inventory and %(obj_name)s-plugin', {'node': node.uuid, 'obj_name': swift_object_name}) -def _node_inventory_convert(node_inventory): - inventory_data = node_inventory['inventory_data'] - plugin_data = node_inventory['plugin_data'] - return {"inventory": inventory_data, "plugin_data": plugin_data} - - -def get_introspection_data(node, context): - """Get introspection data. +def get_inspection_data(node, context): + """Get inspection data. - Retrieve the introspection data for a node. Either from database - or swift as configured. + Retrieve the inspection data for a node either from database + or the Object Storage API (swift/radosgw) as configured. - :param node_id: the Ironic node that the required data is about + :param node: the Ironic node that the required data is about :param context: an admin context :returns: dictionary with ``inventory`` and ``plugin_data`` fields + :raises: NodeInventoryNotFound if no inventory has been saved """ store_data = CONF.inventory.data_backend if store_data == 'none': @@ -170,58 +157,57 @@ def get_introspection_data(node, context): if store_data == 'database': node_inventory = objects.NodeInventory.get_by_node_id( context, node.id) - return _node_inventory_convert(node_inventory) + return {"inventory": node_inventory.inventory_data, + "plugin_data": node_inventory.plugin_data} if store_data == 'swift': try: - node_inventory = _get_introspection_data_from_swift(node.uuid) + return _get_inspection_data_from_swift(node.uuid) except exception.SwiftObjectNotFoundError: raise exception.NodeInventoryNotFound(node=node.uuid) - return node_inventory -def _store_introspection_data_in_swift(node_uuid, inventory_data, plugin_data): - """Uploads introspection data to Swift. +def _store_inspection_data_in_swift(node_uuid, inventory_data, plugin_data): + """Uploads inspection data to Swift. :param data: data to store in Swift :param node_id: ID of the Ironic node that the data came from :returns: name of the Swift object that the data is stored in """ swift_api = swift.SwiftAPI() - swift_object_name = '%s-%s' % (_OBJECT_NAME_PREFIX, node_uuid) + swift_object_name = f'{_OBJECT_NAME_PREFIX}-{node_uuid}' container = CONF.inventory.swift_data_container - swift_api.create_object_from_data(swift_object_name + '-inventory', + swift_api.create_object_from_data(f'{swift_object_name}-inventory', inventory_data, container) - swift_api.create_object_from_data(swift_object_name + '-plugin', + swift_api.create_object_from_data(f'{swift_object_name}-plugin', plugin_data, container) return swift_object_name -def _get_introspection_data_from_swift(node_uuid): - """Get introspection data from Swift. +def _get_inspection_data_from_swift(node_uuid): + """Get inspection data from Swift. :param node_uuid: UUID of the Ironic node that the data came from :returns: dictionary with ``inventory`` and ``plugin_data`` fields """ swift_api = swift.SwiftAPI() - swift_object_name = '%s-%s' % (_OBJECT_NAME_PREFIX, node_uuid) container = CONF.inventory.swift_data_container - inv_obj = swift_object_name + '-inventory' - plug_obj = swift_object_name + '-plugin' + inv_obj = f'{_OBJECT_NAME_PREFIX}-{node_uuid}-inventory' + plug_obj = f'{_OBJECT_NAME_PREFIX}-{node_uuid}-plugin' try: inventory_data = swift_api.get_object(inv_obj, container) except exception.SwiftOperationError: - LOG.error("Failed to retrieve object %(obj)s from swift", - {'obj': inv_obj}) + LOG.error("Failed to retrieve object %(obj)s from container %(cont)s", + {'obj': inv_obj, 'cont': container}) raise exception.SwiftObjectNotFoundError(obj=inv_obj, container=container, operation='get') try: plugin_data = swift_api.get_object(plug_obj, container) except exception.SwiftOperationError: - LOG.error("Failed to retrieve object %(obj)s from swift", - {'obj': plug_obj}) + LOG.error("Failed to retrieve object %(obj)s from container %(cont)s", + {'obj': plug_obj, 'cont': container}) raise exception.SwiftObjectNotFoundError(obj=plug_obj, container=container, operation='get') diff --git a/ironic/drivers/modules/inspector/interface.py b/ironic/drivers/modules/inspector/interface.py index 8792b7b88..e72077003 100644 --- a/ironic/drivers/modules/inspector/interface.py +++ b/ironic/drivers/modules/inspector/interface.py @@ -299,15 +299,17 @@ def _check_status(task): _inspection_error_handler(task, error) elif status.is_finished: _clean_up(task) - store_data = CONF.inventory.data_backend - if store_data == 'none': - LOG.debug('Introspection data storage is disabled, the data will ' - 'not be saved for node %(node)s', {'node': node.uuid}) + if CONF.inventory.data_backend == 'none': + LOG.debug('Inspection data storage is disabled, the data will ' + 'not be saved for node %s', node.uuid) return introspection_data = inspector_client.get_introspection_data( node.uuid, processed=True) - inspect_utils.store_introspection_data(node, introspection_data, - task.context) + # TODO(dtantsur): having no inventory is an abnormal state, handle it. + inventory = introspection_data.pop('inventory', {}) + inspect_utils.store_inspection_data(node, inventory, + introspection_data, + task.context) def _clean_up(task): |