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 25, 2024
1 parent 6bd5090 commit 4fbc573
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 21 deletions.
9 changes: 4 additions & 5 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,8 @@ class ConfigureHostMetering(actions.Action):
"""

id = "CONFIGURE_HOST_METERING_IF_NEEDED"
env_var = None # type: str|None
# This will variable will be available by the time we execute this action.
env_var = tool_opts.configure_host_metering # type: str

def run(self):
"""
Expand All @@ -55,7 +54,7 @@ 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

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 @@ -30,6 +30,11 @@
from six.moves import mock


@pytest.fixture(autouse=True)
def apply_global_tool_opts(monkeypatch, global_tool_opts):
monkeypatch.setattr(hostmetering, "tool_opts", global_tool_opts)


@pytest.fixture
def hostmetering_instance():
return hostmetering.ConfigureHostMetering()
Expand Down
18 changes: 17 additions & 1 deletion convert2rhel/unit_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,26 @@ def run(self):
pass


class FileConfigMock:
SOURCE = "configuration file"

def __init__(self):
self.configure_host_metering = None
self.incomplete_rollback = None
self.tainted_kernel_module_check_skip = None
self.outdated_package_check_skip = None
self.allow_older_version = None
self.allow_unavailable_kmods = None
self.skip_kernel_currency_check = None

def run(self):
pass


@pytest.fixture
def global_tool_opts(monkeypatch):
local_tool_opts = toolopts.ToolOpts()
local_tool_opts.initialize(config_sources=[CliConfigMock()])
local_tool_opts.initialize(config_sources=[CliConfigMock(), FileConfigMock()])
monkeypatch.setattr(toolopts, "tool_opts", local_tool_opts)
return local_tool_opts

Expand Down
19 changes: 18 additions & 1 deletion convert2rhel/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,16 @@ def warn_deprecated_env(env_name):
:param env_name: The name of the environment variable that is deprecated.
:type env_name: str
"""
env_var_to_toolopts_map = {
"CONVERT2RHEL_OUTDATED_PACKAGE_CHECK_SKIP": "outdated_package_check_skip",
"CONVERT2RHEL_SKIP_KERNEL_CURRENCY_CHECK": "skip_kernel_currency_check",
"CONVERT2RHEL_INCOMPLETE_ROLLBACK": "incomplete_rollback",
"CONVERT2RHEL_ALLOW_UNAVAILABLE_KMODS": "allow_unavailable_kmods",
"CONVERT2RHEL_ALLOW_OLDER_VERSION": "allow_older_version",
"CONVERT2RHEL_CONFIGURE_HOST_METERING": "configure_host_metering",
"CONVERT2RHEL_TAINTED_KERNEL_MODULE_CHECK_SKIP": "tainted_kernel_module_check_skip",
}

if env_name not in os.environ:
# Nothing to do here.
return
Expand All @@ -1132,4 +1142,11 @@ def warn_deprecated_env(env_name):
"The environment variable {} is deprecated and is set to be removed on Convert2RHEL 2.4.0.\n"
"Please, use the configuration file instead."
)
return True
key = env_var_to_toolopts_map[env_name]
value = True

# Special override for configure_host_metering. By default, we want to set everything to True.
if key == "configure_host_metering":
value = "auto"

tool_opts.update_opts(key, value)

0 comments on commit 4fbc573

Please sign in to comment.