summaryrefslogtreecommitdiff
path: root/ironic_python_agent/utils.py
diff options
context:
space:
mode:
authorDmitry Tantsur <dtantsur@protonmail.com>2020-12-16 18:17:24 +0100
committerDmitry Tantsur <dtantsur@protonmail.com>2020-12-16 18:17:24 +0100
commitd69f12e0fd379d57443c73c169149593a6a87240 (patch)
tree67aaef5a13af519f93e1d96fd398e6c84de3b788 /ironic_python_agent/utils.py
parent9ae99506c07f25eda1f85b68fc8538b88615d9ae (diff)
downloadironic-python-agent-d69f12e0fd379d57443c73c169149593a6a87240.tar.gz
Handle situation when a configdrive is already mounted
Glean mounts the configdrive and does not unmount it afterwards. If a mount point already exists, just use it. Change-Id: Ia62279afbb9fd9770864942dc40629b69ae8f4ae
Diffstat (limited to 'ironic_python_agent/utils.py')
-rw-r--r--ironic_python_agent/utils.py62
1 files changed, 39 insertions, 23 deletions
diff --git a/ironic_python_agent/utils.py b/ironic_python_agent/utils.py
index b2310d00..a897210e 100644
--- a/ironic_python_agent/utils.py
+++ b/ironic_python_agent/utils.py
@@ -212,6 +212,39 @@ def _early_log(msg, *args):
print('ironic-python-agent:', msg % args, file=sys.stderr)
+def _copy_config_from(path):
+ for ext in ('', '.d'):
+ src = os.path.join(path, 'etc', 'ironic-python-agent%s' % ext)
+ if not os.path.isdir(src):
+ _early_log('%s not found', src)
+ continue
+
+ dest = '/etc/ironic-python-agent%s' % ext
+ _early_log('Copying configuration from %s to %s', src, dest)
+ try:
+ os.makedirs(dest, exist_ok=True)
+
+ # TODO(dtantsur): use shutil.copytree(.., dirs_exist_ok=True)
+ # when the minimum supported Python is 3.8.
+ for name in os.listdir(src):
+ src_file = os.path.join(src, name)
+ dst_file = os.path.join(dest, name)
+ shutil.copy(src_file, dst_file)
+ except Exception as exc:
+ msg = ("Unable to copy vmedia configuration %s to %s: %s"
+ % (src, dest, exc))
+ raise errors.VirtualMediaBootError(msg)
+
+
+def _find_mount_point(device):
+ try:
+ path, _e = execute('findmnt', '-n', '-oTARGET', device)
+ except processutils.ProcessExecutionError:
+ return
+ else:
+ return path.strip()
+
+
def copy_config_from_vmedia():
"""Copies any configuration from a virtual media device.
@@ -223,29 +256,12 @@ def copy_config_from_vmedia():
_early_log('No virtual media device detected')
return
- with _mounted(vmedia_device_file) as vmedia_mount_point:
- for ext in ('', '.d'):
- src = os.path.join(vmedia_mount_point, 'etc',
- 'ironic-python-agent%s' % ext)
- if not os.path.isdir(src):
- _early_log('%s not found', src)
- continue
-
- dest = '/etc/ironic-python-agent%s' % ext
- _early_log('Copying configuration from %s to %s', src, dest)
- try:
- os.makedirs(dest, exist_ok=True)
-
- # TODO(dtantsur): use shutil.copytree(.., dirs_exist_ok=True)
- # when the minimum supported Python is 3.8.
- for name in os.listdir(src):
- src_file = os.path.join(src, name)
- dst_file = os.path.join(dest, name)
- shutil.copy(src_file, dst_file)
- except Exception as exc:
- msg = ("Unable to copy vmedia configuration %s to %s: %s"
- % (src, dest, exc))
- raise errors.VirtualMediaBootError(msg)
+ mounted = _find_mount_point(vmedia_device_file)
+ if mounted:
+ _copy_config_from(mounted)
+ else:
+ with _mounted(vmedia_device_file) as vmedia_mount_point:
+ _copy_config_from(vmedia_mount_point)
def _get_cached_params():