Skip to content

Commit

Permalink
UpgradeInitramfsGenerator: drop distutils dependency
Browse files Browse the repository at this point in the history
Originally the actor used distutils.version.LooseVersion to detect
newest version of installed kernel package inside target userspace
container. But the distutils python module has become deprecated
and we should not use it anymore in Python 3.12+.

However, we do not expect to see multiple versions of kernel present
inside the target userspace container as the container is created
during the IPU process from scratch (always). Considering the presence
of multiple kernels to be sign of error, which could negatively affect
also additional actions later.

Updated the solution, raising an error if multiple kernels are detected
inside the container. As we expect one kernel only, no need to compare
versions of particular possible kernel packages.

Note there are now just these cases when this could happen:
  * a third party package is required to be installed inside the
    container
  * an explicit requirement to install particular version of container
    is made by a custom actor
  • Loading branch information
pirat89 authored and Rezney committed Jul 18, 2024
1 parent 3f7377b commit 236483d
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import shutil
from distutils.version import LooseVersion

from leapp.exceptions import StopActorExecutionError
from leapp.libraries.common import dnfplugin, mounting
Expand Down Expand Up @@ -30,23 +29,35 @@ def _get_target_kernel_version(context):

kernel_version = None
try:
results = context.call(['rpm', '-qa', 'kernel-core'], split=True)

versions = [ver.replace('kernel-core-', '') for ver in results['stdout']]
api.current_logger().debug(
'Versions detected {versions}.'
.format(versions=versions))
sorted_versions = sorted(versions, key=LooseVersion, reverse=True)
kernel_version = next(iter(sorted_versions), None)
# NOTE: Currently we install/use always kernel-core in the upgrade
# initramfs. We do not use currently any different kernel package
# in the container. Note this could change in future e.g. on aarch64
# for IPU 9 -> 10.
# TODO(pstodulk): Investigate situation on ARM systems. OAMG-11433
results = context.call(['rpm', '-qa', 'kernel-core'], split=True)['stdout']
except CalledProcessError:
raise StopActorExecutionError(
'Cannot get version of the installed kernel.',
details={'Problem': 'Could not query the currently installed kernel through rmp.'})
'Cannot get version of the installed kernel inside container.',
details={'Problem': 'Could not query the currently installed kernel inside container using rpm.'})

if len(results) > 1:
# this is should not happen. It's hypothetic situation, which alone it's
# already error. So skipping more sophisticated implementation.
# The container is always created during the upgrade and as that we expect
# always one-and-only kernel installed.
raise StopActorExecutionError(
'Cannot get version of the installed kernel inside container.',
details={'Problem': 'Detected unexpectedly multiple kernels inside target userspace container.'}
)

# kernel version == version-release from package
kernel_version = '-'.join(results[0].rsplit("-", 2)[-2:])
api.current_logger().debug('Detected kernel version inside container: {}.'.format(kernel_version))

if not kernel_version:
raise StopActorExecutionError(
'Cannot get version of the installed kernel.',
details={'Problem': 'A rpm query for the available kernels did not produce any results.'})
'Cannot get version of the installed kernel inside container.',
details={'Problem': 'An rpm query for the available kernels did not produce any results.'})

return kernel_version

Expand Down

0 comments on commit 236483d

Please sign in to comment.