Skip to content

Commit

Permalink
Refactoring - utilize get_target_major_version where appropriate.
Browse files Browse the repository at this point in the history
Introduces changes required in the code review for the PR668. Majority
of changes related to the actual code lies in refactoring the code to
use the `get_target_major_version` function introduced in this PR where
appropriate, instead of relying on direct version string manipulation.

A new helper function `get_version_major` was introduced to encapsulate
the logic required to parse out the major version from a version string
and to provide a single spot where such manipulation is performed should
some additional logic be performed.

Additional changes include fixes to typos found in docstrings.
  • Loading branch information
MichalHe authored and pirat89 committed Aug 11, 2021
1 parent a10dd7b commit 74dd759
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from leapp.libraries.stdlib import api
from leapp import reporting
from leapp.libraries.common import config, rhsm
from leapp.libraries.common.config.version import get_target_major_version


# TODO: we need to provide this path in a shared library
Expand All @@ -26,12 +27,8 @@ def _the_enablerepo_option_used():
return config.get_env('LEAPP_ENABLE_REPOS', None) is not None


def _get_target_major_version():
return api.current_actor().configuration.version.target.split('.')[0]


def process():
target_major_version = _get_target_major_version()
target_major_version = get_target_major_version()

if target_major_version == '8':
ipu_doc_url = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def test_checktargetrepos_rhsm(monkeypatch):
monkeypatch.setattr(reporting, 'create_report', create_report_mocked())
monkeypatch.setattr(rhsm, 'skip_rhsm', lambda: False)
monkeypatch.setattr(api, 'consume', MockedConsume())
monkeypatch.setattr(checktargetrepos, '_get_target_major_version', lambda: '8')
monkeypatch.setattr(checktargetrepos, 'get_target_major_version', lambda: '8')
checktargetrepos.process()
assert reporting.create_report.called == 0

Expand All @@ -72,7 +72,7 @@ def test_checktargetrepos_no_rhsm(monkeypatch, enable_repos, custom_target_repos
monkeypatch.setattr(reporting, 'create_report', create_report_mocked())
monkeypatch.setattr(rhsm, 'skip_rhsm', lambda: True)
monkeypatch.setattr(api, 'consume', mocked_consume)
monkeypatch.setattr(checktargetrepos, '_get_target_major_version', lambda: '8')
monkeypatch.setattr(checktargetrepos, 'get_target_major_version', lambda: '8')

checktargetrepos.process()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class EnableRHSMTargetRepos(Actor):
We are enabling those RHEL target repos that are equivalent to the enabled source RHEL ones available.
The BaseOS and AppStream repos are enabled on the target RHEL by default. Any other repository needs to
be enabled specifically using the subscription-manager (RHSM) utility. In case some custom repo was used
during the upgrade transaction, this won't be enabled by this actors as it is unknown to the subscription-manager.
during the upgrade transaction, it won't be enabled by this actor as it is unknown to the subscription-manager.
We need to overwrite any RHSM release that may have been set before the upgrade, e.g. 7.6. Reasons:
- If we leave the old source RHEL release set, dnf calls on the upgraded target RHEL would fail.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from leapp.libraries.common import config, mounting, rhsm
from leapp.libraries.common.config.version import get_target_major_version
from leapp.libraries.stdlib import CalledProcessError, api, run
from leapp.models import UsedTargetRepositories

Expand All @@ -18,7 +19,7 @@ def set_rhsm_release():
except CalledProcessError as err:
api.current_logger().warning('Unable to set the {0} release through subscription-manager. When using dnf,'
' content of the latest RHEL {1} minor version will be downloaded.\n{2}'
.format(target_version, target_version.split('.')[0], str(err)))
.format(target_version, get_target_major_version(), str(err)))


def enable_rhsm_repos():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

from leapp.exceptions import StopActorExecutionError
from leapp.libraries.common.config.version import get_major_version
from leapp.libraries.stdlib import run, CalledProcessError
from leapp.models import EnvVar, OSRelease

Expand Down Expand Up @@ -38,10 +39,6 @@
}


def _get_major_version(version):
return version.split('.')[0]


def get_env_vars():
"""
Gather LEAPP_DEVEL environment variables and respective mappings to provide them as messages to be
Expand Down Expand Up @@ -105,7 +102,7 @@ def get_target_version(flavour=LEAPP_UPGRADE_FLAVOUR_DEFAULT):
# If we cannot find a particular major.minor version in the map,
# we fallback to pick a target version just based on a major version.
# This can happen for example when testing not yet released versions
major_version = _get_major_version(current_version_id)
major_version = get_major_version(current_version_id)
target_version = upgrade_paths_map.get((major_version, flavour), None)

return os.getenv('LEAPP_DEVEL_TARGET_RELEASE', None) or target_version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ def test_get_target_version(monkeypatch):
valid_release = _get_os_release(version='8.6', codename='Ootpa').version_id

monkeypatch.delenv('LEAPP_DEVEL_TARGET_RELEASE', raising=False)

monkeypatch.setattr(ipuworkflowconfig, 'get_os_release', lambda x: _get_os_release('8.6', 'Ootpa'))
assert ipuworkflowconfig.get_target_version() == ipuworkflowconfig.upgrade_paths_map[(valid_release, 'default')]

monkeypatch.setenv('LEAPP_DEVEL_TARGET_RELEASE', '')
monkeypatch.setattr(ipuworkflowconfig, 'get_os_release', lambda x: _get_os_release('8.6', 'Ootpa'))
assert ipuworkflowconfig.get_target_version() == ipuworkflowconfig.upgrade_paths_map[(valid_release, 'default')]

monkeypatch.setenv('LEAPP_DEVEL_TARGET_RELEASE', '1.2.3')
assert ipuworkflowconfig.get_target_version() == '1.2.3'

monkeypatch.delenv('LEAPP_DEVEL_TARGET_RELEASE', raising=True)
# unsupported path
monkeypatch.setattr(ipuworkflowconfig, 'get_os_release', lambda x: _get_os_release('8.5', 'Ootpa'))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ def get_installed_event_pkgs(event_pkgs, installed_pkgs):
"""
Get those event's "in" or "out" packages which are already installed.
Even though we don't want to install the already installed pkgs, to be able to upgrade them to
their target RHEL major version. We need to know in which repos they are and enable such repos.
Even though we don't want to install the already installed pkgs, in order to be able to upgrade
them to their target RHEL major version we need to know in which repos they are and enable such repos.
"""
return {k: v for k, v in event_pkgs.items() if k in installed_pkgs}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ class ReportSetTargetRelease(Actor):
Reports information related to the release set in the subscription-manager after the upgrade.
When using Red Hat subscription-manager (RHSM), the release is set by default
to the target version release. In case of skip of the RHSM (--no-rhsm), the
release stay as it was on the source RHEL major version and user has to handle
it manually aftervthe upgrade.
to the target version release. In case of skipping the RHSM (--no-rhsm), the
release stays as it was on the source RHEL and user has to handle it manually
after the upgrade.
"""

name = 'report_set_target_release'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from leapp.libraries.common.config.version import is_rhel_realtime
from leapp.libraries.common.config.version import get_target_major_version, is_rhel_realtime
from leapp.libraries.stdlib import api, CalledProcessError, run
from leapp.models import InstalledTargetKernelVersion

Expand All @@ -9,12 +9,10 @@ def _get_kernel_version(kernel_name):
except CalledProcessError:
return ''

target_major_version = api.current_actor().configuration.version.target.split('.')[0]

for kernel in kernels:
# name-version-release - we want the last two fields only
version = '-'.join(kernel.split('-')[-2:])
if 'el{}'.format(target_major_version) in version:
if 'el{}'.format(get_target_major_version()) in version:
return version
return ''

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
utils,
)
from leapp.libraries.common.config import get_product_type, get_env
from leapp.libraries.common.config.version import get_target_major_version
from leapp.libraries.stdlib import CalledProcessError, api, config, run
from leapp.models import (
CustomTargetRepositoryFile,
Expand Down Expand Up @@ -125,7 +126,7 @@ def prepare_target_userspace(context, userspace_dir, enabled_repos, packages):
"""
Implement the creation of the target userspace.
"""
target_major_version = api.current_actor().configuration.version.target.split('.')[0]
target_major_version = get_target_major_version()
run(['rm', '-rf', userspace_dir])
_create_target_userspace_directories(userspace_dir)
with mounting.BindMount(
Expand Down Expand Up @@ -298,7 +299,7 @@ def _get_all_available_repoids(context):


def _get_rhsm_available_repoids(context):
target_major_version = api.current_actor().configuration.version.target.split('.')[0]
target_major_version = get_target_major_version()
# FIXME: check that required repo IDs (baseos, appstream)
# + or check that all required RHEL repo IDs are available.
if rhsm.skip_rhsm():
Expand Down Expand Up @@ -494,8 +495,7 @@ def _copy_files(context, files):


def _get_target_userspace():
target_major_version = api.current_actor().configuration.version.target.split('.')[0]
return constants.TARGET_USERSPACE.format(target_major_version)
return constants.TARGET_USERSPACE.format(get_target_major_version())


def _create_target_userspace(context, packages, files, target_repoids):
Expand Down
37 changes: 35 additions & 2 deletions repos/system_upgrade/common/libraries/config/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,45 @@
}


def get_major_version(version):
"""
Return the major version from the given version string.
Versioning schema: MAJOR.MINOR.PATCH
It doesn't matter how many dots are present. Everything until the first dot is returned. E.g.:
8.1.0 => 8
7.9 => 7
7 => 7
:param str version: The version string according to the versioning schema described.
:rtype: str
:returns: The major version from the given version string.
"""
return version.split('.')[0]


def get_source_major_version():
return api.current_actor().configuration.version.source.split('.')[0]
"""
Return the major version of the source (original) system.
For more details about about the versioning schema see :func:`get_major_version`.
:rtype: str
:returns: The major version of the source system.
"""
return get_major_version(api.current_actor().configuration.version.source)


def get_target_major_version():
return api.current_actor().configuration.version.target.split('.')[0]
"""
Return the major version of the target system.
For more details about about the versioning schema see :func:`get_major_version`.
:rtype: str
:returns: The major version of the target system.
"""
return get_major_version(api.current_actor().configuration.version.target)


class _SupportedVersionsDict(dict):
Expand Down
6 changes: 3 additions & 3 deletions repos/system_upgrade/common/models/repositoriesmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class RepositoryMap(Model):
topic = TransactionTopic

from_repoid = fields.String()
"""source RHEL repoid as present in the Red Hat CDN"""
"""Source RHEL repoid as present in the Red Hat CDN"""
to_repoid = fields.String()
"""target RHEL repoid as present in the Red Hat CDN"""
"""Target RHEL repoid as present in the Red Hat CDN"""
to_pes_repo = fields.String()
"""target RHEL repo name as used in the Package Evolution Service database"""
"""Target RHEL repo name as used in the Package Evolution Service database"""
from_minor_version = fields.String()
"""To which source RHEL minor versions the mapping relates to"""
to_minor_version = fields.String()
Expand Down
2 changes: 1 addition & 1 deletion repos/system_upgrade/common/models/targetuserspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class RequiredTargetUserspacePackages(Model):
@deprecated(since='2021-04-01', message='Replaced by TargetUserSpaceInitrdEnvTasks')
class RequiredUpgradeInitramPackages(Model):
"""
Requests packages to be installed that the leapp upgrade dracut image generation will succeed
Requests packages to be installed so that the leapp upgrade dracut image generation will succeed
"""
topic = BootPrepTopic

Expand Down

0 comments on commit 74dd759

Please sign in to comment.