Skip to content

Commit

Permalink
refactor: logging stages
Browse files Browse the repository at this point in the history
This refactors the logging stages that we output when we log in specific
files depending on what we are doing. Meaning that any changes that
has to do with preparing or rollback will now output the stage within
the logger formatter rather than leaving it up to each class

E.g. any logs with "Prepare: text" or "Rollback: text" will now be
handled at the log-level
  • Loading branch information
Venefilyn committed Sep 12, 2024
1 parent 17a4ad2 commit bc0067d
Show file tree
Hide file tree
Showing 20 changed files with 51 additions and 41 deletions.
2 changes: 1 addition & 1 deletion convert2rhel/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ def run(self, successes=None, failures=None, skips=None):
running is WARNING or better (WARNING or SUCCESS) and
failure as worse than WARNING (OVERRIDABLE, ERROR)
"""
logger.task("Prepare: %s" % self.task_header)
logger.task("%s" % self.task_header)

if self._has_run:
raise ActionError("Stage %s has already run." % self.stage_name)
Expand Down
8 changes: 4 additions & 4 deletions convert2rhel/actions/pre_ponr_changes/backup_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BackupRedhatRelease(actions.Action):

def run(self):
"""Backup redhat release file before starting conversion process"""
logger.task("Prepare: Backup Redhat Release Files")
logger.task("Backup Redhat Release Files")

super(BackupRedhatRelease, self).run()

Expand All @@ -77,7 +77,7 @@ class BackupRepository(actions.Action):

def run(self):
"""Backup .repo files in /etc/yum.repos.d/ so the repositories can be restored on rollback."""
logger.task("Prepare: Backup Repository Files")
logger.task("Backup Repository Files")

super(BackupRepository, self).run()

Expand All @@ -103,7 +103,7 @@ class BackupYumVariables(actions.Action):

def run(self):
"""Backup varsdir folder in /etc/{yum,dnf}/vars so the variables can be restored on rollback."""
logger.task("Prepare: Backup variables")
logger.task("Backup variables")

super(BackupYumVariables, self).run()

Expand Down Expand Up @@ -144,7 +144,7 @@ def run(self):
"""Backup changed package files"""
super(BackupPackageFiles, self).run()

logger.task("Prepare: Backup package files")
logger.task("Backup package files")

package_files_changes = self._get_changed_package_files()

Expand Down
8 changes: 3 additions & 5 deletions convert2rhel/actions/pre_ponr_changes/handle_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def run(self):
"""
super(ListThirdPartyPackages, self).run()

logger.task("Prepare: List third-party packages")
logger.task("List third-party packages")
third_party_pkgs = pkghandler.get_third_party_pkgs()
if third_party_pkgs:
# RHELC-884 disable the RHEL repos to avoid reaching them when checking original system.
Expand Down Expand Up @@ -98,12 +98,10 @@ def run(self):
all_pkgs = []
pkgs_removed = []
try:
logger.task("Prepare: Searching for the following excluded packages")
logger.task("Searching for the following excluded packages")
excluded_pkgs = sorted(pkghandler.get_packages_to_remove(system_info.excluded_pkgs))

logger.task(
"Prepare: Searching for packages containing .repo files or affecting variables in the .repo files"
)
logger.task("Searching for packages containing .repo files or affecting variables in the .repo files")
repofile_pkgs = sorted(pkghandler.get_packages_to_remove(system_info.repofile_pkgs))

logger.info("\n")
Expand Down
2 changes: 1 addition & 1 deletion convert2rhel/actions/pre_ponr_changes/kernel_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def run(self):
"""Ensure that the host kernel modules are compatible with RHEL."""
super(EnsureKernelModulesCompatibility, self).run()

logger.task("Prepare: Ensure kernel modules compatibility with RHEL")
logger.task("Ensure kernel modules compatibility with RHEL")

try:
host_kmods = self._get_loaded_kmods()
Expand Down
2 changes: 1 addition & 1 deletion convert2rhel/actions/pre_ponr_changes/special_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def run(self):
"""
super(RemoveIwlax2xxFirmware, self).run()

logger.task("Prepare: Resolve possible edge case")
logger.task("Resolve possible edge case")
iwl7260_firmware = system_info.is_rpm_installed(name="iwl7260-firmware")
iwlax2xx_firmware = system_info.is_rpm_installed(name="iwlax2xx-firmware")

Expand Down
22 changes: 11 additions & 11 deletions convert2rhel/actions/pre_ponr_changes/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def run(self):
# The subscription-manager-rhsm-certificates package contains this cert but for
# example on CentOS Linux 7 this package is missing the cert due to intentional
# debranding. Thus we need to ensure the cert is in place even when the pkg is installed.
logger.task("Prepare: Install cdn.redhat.com SSL CA certificate")
logger.task("Install cdn.redhat.com SSL CA certificate")
repo_cert = RestorablePEMCert(_REDHAT_CDN_CACERT_SOURCE_DIR, _REDHAT_CDN_CACERT_TARGET_DIR)
backup.backup_control.push(repo_cert)

Expand All @@ -65,7 +65,7 @@ def run(self):

# Import the Red Hat GPG Keys for installing Subscription-manager
# and for later.
logger.task("Prepare: Import Red Hat GPG keys")
logger.task("Import Red Hat GPG keys")
pkghandler.install_gpg_keys()


Expand Down Expand Up @@ -97,18 +97,18 @@ def run(self):
return

try:
logger.task("Prepare: Subscription Manager - Check for installed packages")
logger.task("Subscription Manager - Check for installed packages")
subscription_manager_pkgs = subscription.needed_subscription_manager_pkgs()
if not subscription_manager_pkgs:
logger.info("Subscription Manager is already present")
else:
logger.task("Prepare: Subscription Manager - Install packages")
logger.task("Subscription Manager - Install packages")
subscription.install_rhel_subscription_manager(subscription_manager_pkgs)

logger.task("Prepare: Subscription Manager - Verify installation")
logger.task("Subscription Manager - Verify installation")
subscription.verify_rhsm_installed()

logger.task("Prepare: Install a RHEL product certificate for RHSM")
logger.task("Install a RHEL product certificate for RHSM")
product_cert = RestorablePEMCert(_RHSM_PRODUCT_CERT_SOURCE_DIR, _RHSM_PRODUCT_CERT_TARGET_DIR)
backup.backup_control.push(product_cert)
except SystemExit as e:
Expand Down Expand Up @@ -165,7 +165,7 @@ def run(self):
)
return

logger.task("Prepare: Subscription Manager - Reload configuration")
logger.task("Subscription Manager - Reload configuration")
# We will use subscription-manager later to enable the RHEL repos so we need to make
# sure subscription-manager knows about the product certificate. Refreshing
# subscription info will do that.
Expand Down Expand Up @@ -205,19 +205,19 @@ def run(self):
# condition or a separate Action. Not doing it now because we
# have to disentangle the exception handling when we do that.
if subscription.should_subscribe():
logger.task("Prepare: Subscription Manager - Subscribe system")
logger.task("Subscription Manager - Subscribe system")
restorable_subscription = RestorableSystemSubscription()
backup.backup_control.push(restorable_subscription)

logger.task("Prepare: Subscription Manager - Disable all repositories")
logger.task("Subscription Manager - Disable all repositories")
backup.backup_control.push(RestorableDisableRepositories())

logger.task("Prepare: Get RHEL repository IDs")
logger.task("Get RHEL repository IDs")
rhel_repoids = repo.get_rhel_repoids()

# we need to enable repos after removing repofile pkgs, otherwise
# we don't get backups to restore from on a rollback
logger.task("Prepare: Subscription Manager - Enable RHEL repositories")
logger.task("Subscription Manager - Enable RHEL repositories")
subscription.enable_repos(rhel_repoids)
except OSError as e:
# This should not occur anymore as all the relevant OSError has been changed to a CriticalError
Expand Down
2 changes: 1 addition & 1 deletion convert2rhel/actions/pre_ponr_changes/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def run(self):
super(ValidatePackageManagerTransaction, self).run()

try:
logger.task("Prepare: Validate the %s transaction", pkgmanager.TYPE)
logger.task("Validate the %s transaction", pkgmanager.TYPE)
transaction_handler = pkgmanager.create_transaction_handler()
transaction_handler.run_transaction(
validate_transaction=True,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class CheckFirewalldAvailability(actions.Action):
def run(self):
"""Error out if the firewalld service is running on the system."""
super(CheckFirewalldAvailability, self).run()
logger.task("Prepare: Check that firewalld is running")
logger.task("Check that firewalld is running")

if system_info.id == "oracle" and system_info.version.major == 8 and system_info.version.minor >= 8:
# If firewalld is not present on the system, we can just skip skip.
Expand Down
2 changes: 1 addition & 1 deletion convert2rhel/actions/system_checks/convert2rhel_latest.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Convert2rhelLatest(actions.Action):

def run(self):
"""Make sure that we are running the latest downstream version of convert2rhel"""
logger.task("Prepare: Check if this is the latest version of Convert2RHEL")
logger.task("Check if this is the latest version of Convert2RHEL")

super(Convert2rhelLatest, self).run()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def run(self):
- the repository "baseurl" is accessible and contains repository metadata
"""
super(CustomReposAreValid, self).run()
logger.task("Prepare: Check if --enablerepo repositories are accessible")
logger.task("Check if --enablerepo repositories are accessible")

if not tool_opts.no_rhsm:
logger.info("Did not perform the check of repositories due to the use of RHSM for the conversion.")
Expand Down
2 changes: 1 addition & 1 deletion convert2rhel/actions/system_checks/dbus.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class DbusIsRunning(actions.Action):
def run(self):
"""Error out if we need to register with rhsm and the dbus daemon is not running."""
super(DbusIsRunning, self).run()
logger.task("Prepare: Check that DBus Daemon is running")
logger.task("Check that DBus Daemon is running")

if not subscription.should_subscribe():
logger.info("Did not perform the check because we have been asked not to subscribe this system to RHSM.")
Expand Down
2 changes: 1 addition & 1 deletion convert2rhel/actions/system_checks/duplicate_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def run(self):
"""Ensure that there are no duplicate system packages installed."""
super(DuplicatePackages, self).run()

logger.task("Prepare: Check if there are any duplicate installed packages on the system")
logger.task("Check if there are any duplicate installed packages on the system")
output, ret_code = utils.run_subprocess(["/usr/bin/package-cleanup", "--dupes", "--quiet"], print_output=False)
if not output:
return
Expand Down
2 changes: 1 addition & 1 deletion convert2rhel/actions/system_checks/efi.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def run(self):
"""Inhibit the conversion when we are not able to handle UEFI."""
super(Efi, self).run()

logger.task("Prepare: Check the firmware interface type (BIOS/UEFI)")
logger.task("Check the firmware interface type (BIOS/UEFI)")
if not grub.is_efi():
logger.info("BIOS detected.")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class IsLoadedKernelLatest(actions.Action):
def run(self): # pylint: disable= too-many-return-statements
"""Check if the loaded kernel is behind or of the same version as in yum repos."""
super(IsLoadedKernelLatest, self).run()
logger.task("Prepare: Check if the loaded kernel version is the most recent")
logger.task("Check if the loaded kernel version is the most recent")

if system_info.id == "oracle" and system_info.eus_system:
logger.info(
Expand Down
2 changes: 1 addition & 1 deletion convert2rhel/actions/system_checks/package_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class PackageUpdates(actions.Action):
def run(self):
"""Ensure that the system packages installed are up-to-date."""
super(PackageUpdates, self).run()
logger.task("Prepare: Check if the installed packages are up-to-date")
logger.task("Check if the installed packages are up-to-date")

if system_info.id == "oracle" and system_info.eus_system:
logger.info(
Expand Down
4 changes: 2 additions & 2 deletions convert2rhel/actions/system_checks/readonly_mounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ReadonlyMountMnt(actions.Action):

def run(self):
super(ReadonlyMountMnt, self).run()
logger.task("Prepare: Check if /mnt is read-write")
logger.task("Check if /mnt is read-write")

if readonly_mount_detection("/mnt"):
self.set_result(
Expand All @@ -67,7 +67,7 @@ class ReadonlyMountSys(actions.Action):

def run(self):
super(ReadonlyMountSys, self).run()
logger.task("Prepare: Check if /sys is read-write")
logger.task("Check if /sys is read-write")

if readonly_mount_detection("/sys"):
self.set_result(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def run(self):
original system.
"""
super(RhelCompatibleKernel, self).run()
logger.task("Prepare: Check kernel compatibility with RHEL")
logger.task("Check kernel compatibility with RHEL")
for check_function in (_bad_kernel_version, _bad_kernel_package_signature, _bad_kernel_substring):
try:
check_function(system_info.booted_kernel)
Expand Down
2 changes: 1 addition & 1 deletion convert2rhel/actions/system_checks/tainted_kmods.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def run(self):
"""
super(TaintedKmods, self).run()

logger.task("Prepare: Check if loaded kernel modules are not tainted")
logger.task("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 = os.environ.get("CONVERT2RHEL_TAINTED_KERNEL_MODULE_CHECK_SKIP", None)
Expand Down
11 changes: 11 additions & 0 deletions convert2rhel/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,17 @@ def colorize(message, color="OKGREEN"):
return "".join((getattr(bcolors, color), message, bcolors.ENDC))


class ConversionStage:
stages = {"prepare": "Prepare"}
current = None # type: str|None

@classmethod
def set_stage(cls, stage): # type: (str) -> None
if stage == None or stage in cls.stages:
cls.current = cls.stages[stage]
raise NotImplementedError("The stage {} is not implemented in the ConversionStage class".format(stage))


class CustomFormatter(logging.Formatter):
"""
Custom formatter to handle different logging formats based on logging level.
Expand Down
11 changes: 6 additions & 5 deletions convert2rhel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def main_locked():
initialize_file_logging("convert2rhel.log", logger_module.LOG_DIR)

try:
logger_module.ConversionStage.set_stage("prepare")
perform_boilerplate()

gather_system_info()
Expand Down Expand Up @@ -320,10 +321,10 @@ def _handle_inhibitors_found_exception():
def perform_boilerplate():
"""Standard interactions with the user prior to doing any conversion work."""
# license agreement
loggerinst.task("Prepare: Show Red Hat software EULA")
loggerinst.task("Show Red Hat software EULA")
show_eula()

loggerinst.task("Prepare: Inform about data collection")
loggerinst.task("Inform about data collection")
breadcrumbs.breadcrumbs.print_data_collection()


Expand All @@ -347,18 +348,18 @@ def show_eula():
def gather_system_info():
"""Retrieve information about the system to be converted"""
# gather system information
loggerinst.task("Prepare: Gather system information")
loggerinst.task("Gather system information")
systeminfo.system_info.resolve_system_info()
systeminfo.system_info.print_system_information()
breadcrumbs.breadcrumbs.collect_early_data()


def prepare_system():
"""Setup the environment to do the conversion within"""
loggerinst.task("Prepare: Clear YUM/DNF version locks")
loggerinst.task("Clear YUM/DNF version locks")
pkghandler.clear_versionlock()

loggerinst.task("Prepare: Clean yum cache metadata")
loggerinst.task("Clean yum cache metadata")
pkgmanager.clean_yum_metadata()


Expand Down

0 comments on commit bc0067d

Please sign in to comment.