From 80d9a2d512c58478f5ef6781dd79746ab531168b Mon Sep 17 00:00:00 2001 From: Adam Hosek Date: Wed, 25 Sep 2024 16:47:10 +0200 Subject: [PATCH] [RHELC-1677] Fix missing repository directory Some packages (e.g. rocky-release or alma-release on EL9) can remove repository directory as their part. Without the directory, sub-man is unable to generate redhat.repo file. This results in inaccessibility of all rhel repositories. --- .../actions/pre_ponr_changes/handle_packages.py | 17 +++++++++++++++++ .../pre_ponr_changes/handle_packages_test.py | 14 ++++++++++++++ .../test_system_release_backup.py | 8 -------- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/convert2rhel/actions/pre_ponr_changes/handle_packages.py b/convert2rhel/actions/pre_ponr_changes/handle_packages.py index 6d667ccaf..b811da8b4 100644 --- a/convert2rhel/actions/pre_ponr_changes/handle_packages.py +++ b/convert2rhel/actions/pre_ponr_changes/handle_packages.py @@ -15,11 +15,13 @@ __metaclass__ = type +import os from convert2rhel import actions, pkghandler, repo, utils from convert2rhel.backup import backup_control, get_backedup_system_repos from convert2rhel.backup.packages import RestorablePackage from convert2rhel.logger import root_logger +from convert2rhel.repo import DEFAULT_YUM_REPOFILE_DIR from convert2rhel.systeminfo import system_info @@ -132,6 +134,13 @@ def run(self): # There is needed for avoid reaching out RHEL repositories while requesting info about pkgs. disable_repos = repo.get_rhel_repos_to_disable() pkgs_removed = _remove_packages_unless_from_redhat(pkgs_list=all_pkgs, disable_repos=disable_repos) + + # https://issues.redhat.com/browse/RHELC-1677 + # In some cases the {system}-release package takes ownership of the /etc/yum.repos.d/ directory, + # when the package gets forcefully removed, the directory gets removed as well. Subscription-manager + # doesn't expect this and without the directory the redhat.repo isn't re-created. This results in an inability + # to access any repositories as the repository directory doesn't exist. + _fix_repos_directory() except SystemExit as e: # TODO(r0x0d): Places where we raise SystemExit and need to be # changed to something more specific. @@ -193,3 +202,11 @@ def _remove_packages_unless_from_redhat(pkgs_list, disable_repos=None): logger.debug("Successfully removed {} packages".format(len(pkgs_list))) return pkgs_removed + + +def _fix_repos_directory(): + """Check if repository directory is present. If the directory is missing, create it.""" + repo_dir = DEFAULT_YUM_REPOFILE_DIR + if not os.path.exists(repo_dir): + os.mkdir(repo_dir) + logger.debug("Recreated repository directory {} as it was removed with some special package.".format(repo_dir)) diff --git a/convert2rhel/unit_tests/actions/pre_ponr_changes/handle_packages_test.py b/convert2rhel/unit_tests/actions/pre_ponr_changes/handle_packages_test.py index ef6e1b72a..1333bd8be 100644 --- a/convert2rhel/unit_tests/actions/pre_ponr_changes/handle_packages_test.py +++ b/convert2rhel/unit_tests/actions/pre_ponr_changes/handle_packages_test.py @@ -15,6 +15,8 @@ __metaclass__ = type +import os + import pytest import six @@ -272,3 +274,15 @@ def test_remove_packages_unless_from_redhat(pkgs_to_remove, monkeypatch, caplog) assert "Removing the following {} packages".format(len(pkgs_to_remove)) in caplog.records[-3].message assert "Successfully removed {} packages".format(len(pkgs_to_remove)) in caplog.records[-1].message + + +@pytest.mark.parametrize("dir_exists", (True, False)) +def test_fix_repos_directory(monkeypatch, tmpdir, dir_exists): + repo_dir = tmpdir.join("repo") + if dir_exists: + repo_dir.mkdir() + monkeypatch.setattr(handle_packages, "DEFAULT_YUM_REPOFILE_DIR", str(repo_dir)) + + handle_packages._fix_repos_directory() + + assert os.path.exists(str(repo_dir)) diff --git a/tests/integration/tier0/non-destructive/system-release-backup/test_system_release_backup.py b/tests/integration/tier0/non-destructive/system-release-backup/test_system_release_backup.py index de55dd751..245007e65 100644 --- a/tests/integration/tier0/non-destructive/system-release-backup/test_system_release_backup.py +++ b/tests/integration/tier0/non-destructive/system-release-backup/test_system_release_backup.py @@ -92,14 +92,6 @@ def test_override_inhibitor_os_release_restored( c2r.expect("'CONVERT2RHEL_INCOMPLETE_ROLLBACK' environment variable detected, continuing conversion.") - # Apparently the whole "/etc/yum.repos.d" dir is missing when the *-release package gets erased - # re-create it, so sub-man is able to refresh the redhat.repo file - # TODO remove this part when/if https://issues.redhat.com/browse/RHELC-1677 gets fixed - c2r.expect("Ensure kernel modules compatibility with RHEL") - missing_dir = "/etc/yum.repos.d" - if not os.path.exists(missing_dir): - os.mkdir(missing_dir) - c2r.expect("Continue with the system conversion") c2r.sendline("n") assert c2r.exitstatus == 1