summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitry Tantsur <dtantsur@protonmail.com>2023-03-01 14:21:39 +0100
committerDmitry Tantsur <dtantsur@protonmail.com>2023-03-01 14:50:59 +0100
commitd43f7fbc64a5eb59b3eb409ccc8afcfd7f2859d9 (patch)
tree0d15a2a88aee4abc8a827e6c39a72316faa68c3e
parent9acfd513638f5014b2e125d2777ce18e8af03361 (diff)
downloadironic-d43f7fbc64a5eb59b3eb409ccc8afcfd7f2859d9.tar.gz
Refactoring: extract some common functions from the inspector code
Change-Id: I0acc5303c1a38645318fb9be4cb068d069b7fe6a
-rw-r--r--ironic/common/utils.py28
-rw-r--r--ironic/drivers/modules/inspect_utils.py13
-rw-r--r--ironic/drivers/modules/inspector/interface.py41
3 files changed, 45 insertions, 37 deletions
diff --git a/ironic/common/utils.py b/ironic/common/utils.py
index 9ae88d4d6..793b4b501 100644
--- a/ironic/common/utils.py
+++ b/ironic/common/utils.py
@@ -26,6 +26,7 @@ import hashlib
import ipaddress
import os
import re
+import shlex
import shutil
import tempfile
import time
@@ -696,3 +697,30 @@ def stop_after_retries(option, group=None):
return retry_state.attempt_number >= num_retries + 1
return should_stop
+
+
+def is_loopback(hostname_or_ip):
+ """Check if the provided hostname or IP address is a loopback."""
+ try:
+ return ipaddress.ip_address(hostname_or_ip).is_loopback
+ except ValueError: # host name
+ return hostname_or_ip in ('localhost', 'localhost.localdomain')
+
+
+def parse_kernel_params(params):
+ """Parse kernel parameters into a dictionary.
+
+ ``None`` is used as a value for parameters that are not in
+ the ``key=value`` format.
+
+ :param params: kernel parameters as a space-delimited string.
+ """
+ result = {}
+ for s in shlex.split(params):
+ try:
+ key, value = s.split('=', 1)
+ except ValueError:
+ result[s] = None
+ else:
+ result[key] = value
+ return result
diff --git a/ironic/drivers/modules/inspect_utils.py b/ironic/drivers/modules/inspect_utils.py
index 0089302c1..432345cf1 100644
--- a/ironic/drivers/modules/inspect_utils.py
+++ b/ironic/drivers/modules/inspect_utils.py
@@ -27,7 +27,7 @@ LOG = logging.getLogger(__name__)
_OBJECT_NAME_PREFIX = 'inspector_data'
-def create_ports_if_not_exist(task, macs):
+def create_ports_if_not_exist(task, macs=None):
"""Create ironic ports from MAC addresses data dict.
Creates ironic ports from MAC addresses data returned with inspection or
@@ -36,8 +36,17 @@ def create_ports_if_not_exist(task, macs):
pair.
:param task: A TaskManager instance.
- :param macs: A sequence of MAC addresses.
+ :param macs: A sequence of MAC addresses. If ``None``, fetched from
+ the task's management interface.
"""
+ if macs is None:
+ macs = task.driver.management.get_mac_addresses(task)
+ if not macs:
+ LOG.warning("Not attempting to create any port as no NICs "
+ "were discovered in 'enabled' state for node %s",
+ task.node.uuid)
+ return
+
node = task.node
for mac in macs:
if not netutils.is_valid_mac(mac):
diff --git a/ironic/drivers/modules/inspector/interface.py b/ironic/drivers/modules/inspector/interface.py
index 731029dbe..8792b7b88 100644
--- a/ironic/drivers/modules/inspector/interface.py
+++ b/ironic/drivers/modules/inspector/interface.py
@@ -15,8 +15,6 @@ Modules required to work with ironic_inspector:
https://pypi.org/project/ironic-inspector
"""
-import ipaddress
-import shlex
from urllib import parse as urlparse
import eventlet
@@ -47,14 +45,8 @@ def _get_callback_endpoint(client):
return root
parts = urlparse.urlsplit(root)
- is_loopback = False
- try:
- # ip_address requires a unicode string on Python 2
- is_loopback = ipaddress.ip_address(parts.hostname).is_loopback
- except ValueError: # host name
- is_loopback = (parts.hostname == 'localhost')
- if is_loopback:
+ if utils.is_loopback(parts.hostname):
raise exception.InvalidParameterValue(
_('Loopback address %s cannot be used as an introspection '
'callback URL') % parts.hostname)
@@ -145,26 +137,14 @@ def _ironic_manages_boot(task, raise_exc=False):
return True
-def _parse_kernel_params():
- """Parse kernel params from the configuration."""
- result = {}
- for s in shlex.split(CONF.inspector.extra_kernel_params):
- try:
- key, value = s.split('=', 1)
- except ValueError:
- result[s] = None
- else:
- result[key] = value
- return result
-
-
def _start_managed_inspection(task):
"""Start inspection managed by ironic."""
try:
cli = client.get_client(task.context)
endpoint = _get_callback_endpoint(cli)
- params = dict(_parse_kernel_params(),
- **{'ipa-inspection-callback-url': endpoint})
+ params = dict(
+ utils.parse_kernel_params(CONF.inspector.extra_kernel_params),
+ **{'ipa-inspection-callback-url': endpoint})
if utils.fast_track_enabled(task.node):
params['ipa-api-url'] = deploy_utils.get_ironic_api_url()
@@ -199,7 +179,7 @@ class Inspector(base.InspectInterface):
:param task: a task from TaskManager.
:raises: UnsupportedDriverExtension
"""
- _parse_kernel_params()
+ utils.parse_kernel_params(CONF.inspector.extra_kernel_params)
if CONF.inspector.require_managed_boot:
_ironic_manages_boot(task, raise_exc=True)
@@ -214,16 +194,7 @@ class Inspector(base.InspectInterface):
:raises: HardwareInspectionFailure on failure
"""
try:
- enabled_macs = task.driver.management.get_mac_addresses(task)
- if enabled_macs:
- inspect_utils.create_ports_if_not_exist(task, enabled_macs)
- else:
- LOG.warning("Not attempting to create any port as no NICs "
- "were discovered in 'enabled' state for node "
- "%(node)s: %(mac_data)s",
- {'mac_data': enabled_macs,
- 'node': task.node.uuid})
-
+ inspect_utils.create_ports_if_not_exist(task)
except exception.UnsupportedDriverExtension:
LOG.debug('Pre-creating ports prior to inspection not supported'
' on node %s.', task.node.uuid)