Skip to content

Commit

Permalink
Use toolopts instead of environment variable
Browse files Browse the repository at this point in the history
Following the strategy of moving all the environment variables to be
inside the configuration file, this patch introduces the ability of
converting the env_var to their toolopts counterpart. This will help in
the deprecation later down the road when all users are comfortable
enough using the config file.
  • Loading branch information
r0x0d committed Sep 26, 2024
1 parent c4f3ccf commit 2792a05
Show file tree
Hide file tree
Showing 16 changed files with 152 additions and 76 deletions.
25 changes: 12 additions & 13 deletions convert2rhel/actions/post_conversion/hostmetering.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@

__metaclass__ = type


import os

from convert2rhel import actions, systeminfo
from convert2rhel.logger import root_logger
from convert2rhel.pkgmanager import call_yum_cmd
from convert2rhel.subscription import get_rhsm_facts
from convert2rhel.systeminfo import system_info
from convert2rhel.toolopts import tool_opts
from convert2rhel.utils import run_subprocess, warn_deprecated_env


Expand All @@ -37,7 +35,6 @@ class ConfigureHostMetering(actions.Action):
"""

id = "CONFIGURE_HOST_METERING_IF_NEEDED"
env_var = None # type: str|None

def run(self):
"""
Expand All @@ -55,11 +52,11 @@ def run(self):

super(ConfigureHostMetering, self).run()

self.env_var = warn_deprecated_env("CONVERT2RHEL_CONFIGURE_HOST_METERING")
warn_deprecated_env("CONVERT2RHEL_CONFIGURE_HOST_METERING")
if not self._check_env_var():
return False

if system_info.version.major != 7 and self.env_var == "auto":
if system_info.version.major != 7 and tool_opts.configure_host_metering == "auto":
logger.info("Did not perform host metering configuration. Only supported for RHEL 7.")
self.add_message(
level="INFO",
Expand All @@ -71,7 +68,7 @@ def run(self):

is_hyperscaler = self.is_running_on_hyperscaler()

if not is_hyperscaler and self.env_var == "auto":
if not is_hyperscaler and tool_opts.configure_host_metering == "auto":
logger.info("Did not perform host-metering configuration.")
self.add_message(
level="INFO",
Expand Down Expand Up @@ -144,7 +141,7 @@ def _check_env_var(self):
:return: Return True if the value is equal to auto|force, otherwise False
:rtype: bool
"""
if self.env_var is None:
if tool_opts.configure_host_metering is None:
logger.debug("CONVERT2RHEL_CONFIGURE_HOST_METERING was not set. Skipping it.")
self.add_message(
level="INFO",
Expand All @@ -154,18 +151,20 @@ def _check_env_var(self):
)
return False

if self.env_var not in ("force", "auto"):
logger.debug("Value for environment variable not recognized: %s" % self.env_var)
if tool_opts.configure_host_metering not in ("force", "auto"):
logger.debug("Value for environment variable not recognized: %s" % tool_opts.configure_host_metering)
self.add_message(
level="WARNING",
id="UNRECOGNIZED_OPTION_CONFIGURE_HOST_METERING",
title="Unrecognized option in CONVERT2RHEL_CONFIGURE_HOST_METERING environment variable.",
description="Environment variable {env_var} not recognized.".format(env_var=self.env_var),
description="Environment variable {env_var} not recognized.".format(
env_var=tool_opts.configure_host_metering
),
remediations="Set the option to `auto` value if you want to configure host metering.",
)
return False

if self.env_var == "force":
if tool_opts.configure_host_metering == "force":
logger.warning(
"The `force' option has been used for the CONVERT2RHEL_CONFIGURE_HOST_METERING environment variable."
" Please note that this option is mainly used for testing and will configure host-metering unconditionally. "
Expand All @@ -179,7 +178,7 @@ def _check_env_var(self):
" will configure host-metering unconditionally."
" For generic usage please use the 'auto' option.",
)
elif self.env_var == "auto":
elif tool_opts.configure_host_metering == "auto":
logger.debug("Automatic detection of host hyperscaler and configuration.")

return True
Expand Down
4 changes: 3 additions & 1 deletion convert2rhel/actions/pre_ponr_changes/backup_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from convert2rhel.redhatrelease import os_release_file, system_release_file
from convert2rhel.repo import DEFAULT_DNF_VARS_DIR, DEFAULT_YUM_REPOFILE_DIR, DEFAULT_YUM_VARS_DIR
from convert2rhel.systeminfo import system_info
from convert2rhel.toolopts import tool_opts
from convert2rhel.utils import warn_deprecated_env
from convert2rhel.utils.rpm import PRE_RPM_VA_LOG_FILENAME

Expand Down Expand Up @@ -189,7 +190,8 @@ def _get_changed_package_files(self):
output = f.read()
# Catch the IOError due Python 2 compatibility
except IOError as err:
if warn_deprecated_env("CONVERT2RHEL_INCOMPLETE_ROLLBACK"):
warn_deprecated_env("CONVERT2RHEL_INCOMPLETE_ROLLBACK")
if tool_opts.incomplete_rollback:
logger.debug("Skipping backup of the package files. CONVERT2RHEL_INCOMPLETE_ROLLBACK detected.")
# Return empty list results in no backup of the files
return data
Expand Down
5 changes: 3 additions & 2 deletions convert2rhel/actions/pre_ponr_changes/kernel_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
__metaclass__ = type

import itertools
import os
import re

from functools import cmp_to_key

from convert2rhel import actions, pkghandler
from convert2rhel.logger import root_logger
from convert2rhel.systeminfo import system_info
from convert2rhel.toolopts import tool_opts
from convert2rhel.utils import run_subprocess, warn_deprecated_env


Expand Down Expand Up @@ -247,7 +247,8 @@ def run(self):

# Check if we have the environment variable set, if we do, send a
# warning and return.
if warn_deprecated_env("CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS"):
warn_deprecated_env("CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS")
if tool_opts.allow_unavailable_kmods:
logger.warning(
"Detected 'CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS' environment variable."
" We will continue the conversion with the following kernel modules unavailable in RHEL:\n"
Expand Down
4 changes: 3 additions & 1 deletion convert2rhel/actions/system_checks/convert2rhel_latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from convert2rhel.logger import root_logger
from convert2rhel.pkghandler import parse_pkg_string
from convert2rhel.systeminfo import system_info
from convert2rhel.toolopts import tool_opts
from convert2rhel.utils import warn_deprecated_env


Expand Down Expand Up @@ -171,7 +172,8 @@ def run(self):
formatted_available_version = _format_EVR(*precise_available_version)

if ver_compare < 0:
if warn_deprecated_env("CONVERT2RHEL_ALLOW_OLDER_VERSION"):
warn_deprecated_env("CONVERT2RHEL_ALLOW_OLDER_VERSION")
if tool_opts.allow_older_version:
diagnosis = (
"You are currently running %s and the latest version of convert2rhel is %s.\n"
"'CONVERT2RHEL_ALLOW_OLDER_VERSION' environment variable detected, continuing conversion"
Expand Down
6 changes: 3 additions & 3 deletions convert2rhel/actions/system_checks/is_loaded_kernel_latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
__metaclass__ = type


import os

from convert2rhel import actions, repo
from convert2rhel.logger import root_logger
from convert2rhel.pkghandler import compare_package_versions
from convert2rhel.systeminfo import system_info
from convert2rhel.toolopts import tool_opts
from convert2rhel.utils import run_subprocess, warn_deprecated_env


Expand Down Expand Up @@ -67,7 +66,8 @@ def run(self): # pylint: disable= too-many-return-statements
# Repoquery failed to detected any kernel or kernel-core packages in it's repositories
# we allow the user to provide a environment variable to override the functionality and proceed
# with the conversion, otherwise, we just throw a critical logging to them.
if warn_deprecated_env("CONVERT2RHEL_SKIP_KERNEL_CURRENCY_CHECK"):
warn_deprecated_env("CONVERT2RHEL_SKIP_KERNEL_CURRENCY_CHECK")
if tool_opts.skip_kernel_currency_check:
logger.warning(
"Detected 'CONVERT2RHEL_SKIP_KERNEL_CURRENCY_CHECK' environment variable, we will skip "
"the %s comparison.\n"
Expand Down
5 changes: 3 additions & 2 deletions convert2rhel/actions/system_checks/package_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from convert2rhel.logger import root_logger
from convert2rhel.pkghandler import get_total_packages_to_update
from convert2rhel.systeminfo import system_info
from convert2rhel.toolopts import tool_opts
from convert2rhel.utils import warn_deprecated_env


Expand Down Expand Up @@ -80,15 +81,15 @@ def run(self):
return

if len(packages_to_update) > 0:
package_not_up_to_date_skip = warn_deprecated_env("CONVERT2RHEL_OUTDATED_PACKAGE_CHECK_SKIP")
warn_deprecated_env("CONVERT2RHEL_OUTDATED_PACKAGE_CHECK_SKIP")
package_not_up_to_date_error_message = (
"The system has %s package(s) not updated based on repositories defined in the system repositories.\n"
"List of packages to update: %s.\n\n"
"Not updating the packages may cause the conversion to fail.\n"
"Consider updating the packages before proceeding with the conversion."
% (len(packages_to_update), " ".join(packages_to_update))
)
if not package_not_up_to_date_skip:
if not tool_opts.outdated_package_check_skip:
logger.warning(package_not_up_to_date_error_message)
self.set_result(
level="OVERRIDABLE",
Expand Down
8 changes: 3 additions & 5 deletions convert2rhel/actions/system_checks/tainted_kmods.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@

__metaclass__ = type


import os

from convert2rhel import actions
from convert2rhel.logger import root_logger
from convert2rhel.toolopts import tool_opts
from convert2rhel.utils import run_subprocess, warn_deprecated_env


Expand All @@ -46,15 +44,15 @@ def run(self):
logger.task("Prepare: Check if loaded kernel modules are not tainted")
unsigned_modules, _ = run_subprocess(["grep", "(", "/proc/modules"])
module_names = "\n ".join([mod.split(" ")[0] for mod in unsigned_modules.splitlines()])
tainted_kmods_skip = warn_deprecated_env("CONVERT2RHEL_TAINTED_KERNEL_MODULE_CHECK_SKIP")
warn_deprecated_env("CONVERT2RHEL_TAINTED_KERNEL_MODULE_CHECK_SKIP")
diagnosis = (
"Tainted kernel modules detected:\n {0}\n"
"Third-party components are not supported per our "
"software support policy:\n{1}\n".format(module_names, LINK_KMODS_RH_POLICY)
)

if unsigned_modules:
if not tainted_kmods_skip:
if not tool_opts.tainted_kernel_module_check_skip:
self.set_result(
level="OVERRIDABLE",
id="TAINTED_KMODS_DETECTED",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import pytest
import six

from convert2rhel import actions, systeminfo
from convert2rhel import actions, systeminfo, toolopts
from convert2rhel.actions.post_conversion import hostmetering
from convert2rhel.systeminfo import Version, system_info
from convert2rhel.unit_tests import RunSubprocessMocked, assert_actions_result, run_subprocess_side_effect
Expand All @@ -41,7 +41,7 @@ def hostmetering_instance():
(
{},
Version(7, 9),
False, # not on hyperscaler
False, # not on hyperscalershould_configure_metering
"auto",
False,
),
Expand Down Expand Up @@ -125,11 +125,17 @@ def hostmetering_instance():
),
)
def test_configure_host_metering(
monkeypatch, rhsm_facts, os_version, should_configure_metering, envvar, hostmetering_instance, managed_service
monkeypatch,
rhsm_facts,
os_version,
should_configure_metering,
envvar,
hostmetering_instance,
managed_service,
global_tool_opts,
):
if envvar:
monkeypatch.setenv("CONVERT2RHEL_CONFIGURE_HOST_METERING", envvar)

monkeypatch.setattr(toolopts, "tool_opts", global_tool_opts)
monkeypatch.setenv("CONVERT2RHEL_CONFIGURE_HOST_METERING", envvar)
monkeypatch.setattr(system_info, "version", os_version)
monkeypatch.setattr(hostmetering, "get_rhsm_facts", mock.Mock(return_value=rhsm_facts))
yum_mock = mock.Mock(return_value=(0, ""))
Expand All @@ -152,7 +158,7 @@ def test_configure_host_metering(
subprocess_mock.assert_any_call(["systemctl", "enable", "host-metering.service"])
subprocess_mock.assert_any_call(["systemctl", "start", "host-metering.service"])
else:
assert ret is False, "Should not configure host-metering."
assert not ret, "Should not configure host-metering."
assert yum_mock.call_count == 0, "Should not install anything."
assert subprocess_mock.call_count == 0, "Should not configure anything."

Expand Down Expand Up @@ -352,11 +358,11 @@ def test_configure_host_metering_messages_and_results(
service_running,
action_message,
action_result,
global_tool_opts,
):
"""Test outputted report/message in each part of the action."""
if env_var:
monkeypatch.setenv("CONVERT2RHEL_CONFIGURE_HOST_METERING", env_var)

monkeypatch.setattr(system_info, "version", os_version)
# The facts aren't used during the test run
monkeypatch.setattr(hostmetering, "get_rhsm_facts", mock.Mock(return_value=None))
Expand All @@ -368,7 +374,7 @@ def test_configure_host_metering_messages_and_results(
hostmetering_instance, "_enable_host_metering_service", mock.Mock(return_value=enable_host_metering_service)
)
monkeypatch.setattr(systeminfo, "is_systemd_managed_service_running", mock.Mock(return_value=service_running))

monkeypatch.setattr(toolopts, "tool_opts", global_tool_opts)
hostmetering_instance.run()

assert action_message.issuperset(hostmetering_instance.messages)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import pytest
import six

from convert2rhel import unit_tests
from convert2rhel import toolopts, unit_tests
from convert2rhel.actions.pre_ponr_changes import backup_system
from convert2rhel.backup import files
from convert2rhel.backup.files import RestorableFile
Expand Down Expand Up @@ -220,31 +220,29 @@ def test_get_changed_package_files(self, rpm_va_output, expected, message, caplo
else:
assert "" == caplog.text

@pytest.mark.parametrize(
("env_var", "message"),
(
(True, "Skipping backup of the package files. CONVERT2RHEL_INCOMPLETE_ROLLBACK detected."),
(False, "Missing file {rpm_va_output} in it's location"),
),
)
def test_get_changed_package_files_missing(self, caplog, message, env_var, tmpdir, monkeypatch):
def test_get_changed_package_files_missing(
self, caplog, tmpdir, monkeypatch, backup_package_files_action, global_tool_opts
):
message = "Skipping backup of the package files. CONVERT2RHEL_INCOMPLETE_ROLLBACK detected."
monkeypatch.setattr(backup_system, "LOG_DIR", str(tmpdir))
monkeypatch.setenv("CONVERT2RHEL_INCOMPLETE_ROLLBACK", "1")
monkeypatch.setattr(toolopts, "tool_opts", global_tool_opts)

if env_var:
os.environ["CONVERT2RHEL_INCOMPLETE_ROLLBACK"] = "1"
else:
# Unset the variable
os.environ.pop("CONVERT2RHEL_INCOMPLETE_ROLLBACK", None)
backup_package_files_action._get_changed_package_files()
assert caplog.records[-1].message == message

backup_package_file = backup_system.BackupPackageFiles()
def test_get_changed_package_file_system_exit(
self, monkeypatch, tmpdir, backup_package_files_action, global_tool_opts
):
tmp_path = str(tmpdir)
monkeypatch.setattr(backup_system, "tool_opts", global_tool_opts)
monkeypatch.setattr(backup_system, "LOG_DIR", str(tmp_path))

try:
backup_package_file._get_changed_package_files()
except SystemExit:
path = os.path.join(str(tmpdir), PRE_RPM_VA_LOG_FILENAME)
assert caplog.records[-1].message == message.format(rpm_va_output=path)
else:
assert caplog.records[-1].message == message
rpm_va_path = os.path.join(tmp_path, PRE_RPM_VA_LOG_FILENAME)
message = "Missing file {} in it's location".format(rpm_va_path)

with pytest.raises(SystemExit, match=message):
backup_package_files_action._get_changed_package_files()

@pytest.mark.parametrize(
("rpm_va_output", "message"),
Expand Down
Loading

0 comments on commit 2792a05

Please sign in to comment.