From 2c611167b9de8056d0a95df3547a74a494e646ee Mon Sep 17 00:00:00 2001 From: Freya Gustavsson Date: Tue, 20 Aug 2024 15:43:36 +0000 Subject: [PATCH] refactor: move codebase to new logging syntax --- convert2rhel/actions/__init__.py | 4 +- .../conversion/list_non_red_hat_pkgs_left.py | 6 +- .../actions/conversion/lock_releasever.py | 6 +- .../actions/conversion/pkg_manager_config.py | 5 +- .../conversion/preserve_only_rhel_kernel.py | 7 +- .../actions/conversion/set_efi_config.py | 4 +- .../actions/conversion/transaction.py | 4 +- .../actions/post_conversion/hostmetering.py | 4 +- .../post_conversion/kernel_boot_files.py | 4 +- .../modified_rpm_files_diff.py | 5 +- .../actions/post_conversion/remove_tmp_dir.py | 4 +- .../actions/post_conversion/update_grub.py | 5 +- .../actions/pre_ponr_changes/backup_system.py | 36 ++--- .../pre_ponr_changes/handle_packages.py | 4 +- .../pre_ponr_changes/kernel_modules.py | 4 +- .../actions/pre_ponr_changes/special_cases.py | 4 +- .../actions/pre_ponr_changes/subscription.py | 5 +- .../actions/pre_ponr_changes/transaction.py | 4 +- convert2rhel/actions/report.py | 5 +- .../check_firewalld_availability.py | 5 +- .../system_checks/convert2rhel_latest.py | 5 +- .../system_checks/custom_repos_are_valid.py | 4 +- convert2rhel/actions/system_checks/dbus.py | 4 +- .../system_checks/duplicate_packages.py | 4 +- convert2rhel/actions/system_checks/efi.py | 5 +- convert2rhel/actions/system_checks/els.py | 4 +- convert2rhel/actions/system_checks/eus.py | 4 +- .../system_checks/is_loaded_kernel_latest.py | 5 +- .../actions/system_checks/package_updates.py | 5 +- .../actions/system_checks/readonly_mounts.py | 4 +- .../system_checks/rhel_compatible_kernel.py | 4 +- .../actions/system_checks/tainted_kmods.py | 5 +- convert2rhel/applock.py | 11 +- convert2rhel/backup/__init__.py | 8 +- convert2rhel/backup/certs.py | 28 ++-- convert2rhel/backup/files.py | 36 ++--- convert2rhel/backup/packages.py | 41 ++--- convert2rhel/backup/subscription.py | 19 +-- convert2rhel/breadcrumbs.py | 12 +- convert2rhel/checks.py | 5 +- convert2rhel/grub.py | 5 +- convert2rhel/initialize.py | 4 +- convert2rhel/main.py | 4 +- convert2rhel/pkghandler.py | 43 ++--- convert2rhel/pkgmanager/__init__.py | 12 +- .../pkgmanager/handlers/dnf/__init__.py | 42 ++--- .../pkgmanager/handlers/dnf/callback.py | 22 +-- .../pkgmanager/handlers/yum/__init__.py | 50 +++--- .../pkgmanager/handlers/yum/callback.py | 14 +- convert2rhel/redhatrelease.py | 13 +- convert2rhel/repo.py | 11 +- convert2rhel/subscription.py | 148 +++++++++--------- convert2rhel/systeminfo.py | 5 +- convert2rhel/toolopts.py | 44 +++--- .../stage_tests/action_exceptions/test.py | 6 +- convert2rhel/unit_tests/logger_test.py | 52 +++--- convert2rhel/utils/__init__.py | 68 ++++---- tests/integration/conftest.py | 4 +- 58 files changed, 448 insertions(+), 437 deletions(-) diff --git a/convert2rhel/actions/__init__.py b/convert2rhel/actions/__init__.py index 0a9e72b81f..6e66116267 100644 --- a/convert2rhel/actions/__init__.py +++ b/convert2rhel/actions/__init__.py @@ -20,7 +20,6 @@ import collections import importlib import itertools -import logging import pkgutil import traceback @@ -29,9 +28,10 @@ import six from convert2rhel import utils +from convert2rhel.logger import root_logger -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) #: Status code of an Action. diff --git a/convert2rhel/actions/conversion/list_non_red_hat_pkgs_left.py b/convert2rhel/actions/conversion/list_non_red_hat_pkgs_left.py index c15a446458..a03d026140 100644 --- a/convert2rhel/actions/conversion/list_non_red_hat_pkgs_left.py +++ b/convert2rhel/actions/conversion/list_non_red_hat_pkgs_left.py @@ -15,14 +15,12 @@ __metaclass__ = type -import logging - -from convert2rhel import actions +from convert2rhel import actions, logger from convert2rhel.pkghandler import get_installed_pkgs_w_different_fingerprint, print_pkg_info from convert2rhel.systeminfo import system_info -loggerinst = logging.getLogger(__name__) +loggerinst = logger.root_logger.getChild(__name__) class ListNonRedHatPkgsLeft(actions.Action): diff --git a/convert2rhel/actions/conversion/lock_releasever.py b/convert2rhel/actions/conversion/lock_releasever.py index 760d377a9f..51e6dfe6d7 100644 --- a/convert2rhel/actions/conversion/lock_releasever.py +++ b/convert2rhel/actions/conversion/lock_releasever.py @@ -15,14 +15,12 @@ __metaclass__ = type -import logging - -from convert2rhel import actions, utils +from convert2rhel import actions, logger, utils from convert2rhel.systeminfo import system_info from convert2rhel.toolopts import tool_opts -loggerinst = logging.getLogger(__name__) +loggerinst = logger.root_logger.getChild(__name__) class LockReleaseverInRHELRepositories(actions.Action): diff --git a/convert2rhel/actions/conversion/pkg_manager_config.py b/convert2rhel/actions/conversion/pkg_manager_config.py index cca27f9926..3f2bf0666a 100644 --- a/convert2rhel/actions/conversion/pkg_manager_config.py +++ b/convert2rhel/actions/conversion/pkg_manager_config.py @@ -15,12 +15,11 @@ __metaclass__ = type -import logging - from convert2rhel import actions, redhatrelease +from convert2rhel.logger import root_logger -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class ConfigurePkgManager(actions.Action): diff --git a/convert2rhel/actions/conversion/preserve_only_rhel_kernel.py b/convert2rhel/actions/conversion/preserve_only_rhel_kernel.py index 6a6fb91673..74ba125b4e 100644 --- a/convert2rhel/actions/conversion/preserve_only_rhel_kernel.py +++ b/convert2rhel/actions/conversion/preserve_only_rhel_kernel.py @@ -16,17 +16,16 @@ __metaclass__ = type import glob -import logging import os import re -from convert2rhel import actions, pkghandler, pkgmanager, utils +from convert2rhel import actions, logger, pkghandler, pkgmanager, utils from convert2rhel.systeminfo import system_info _kernel_update_needed = None -loggerinst = logging.getLogger(__name__) +loggerinst = logger.root_logger.getChild(__name__) class InstallRhelKernel(actions.Action): @@ -229,7 +228,7 @@ def run(self): """ super(FixDefaultKernel, self).run() - loggerinst = logging.getLogger(__name__) + loggerinst = logger.root_logger.getChild(__name__) loggerinst.info("Checking for incorrect boot kernel") kernel_sys_cfg = utils.get_file_content("/etc/sysconfig/kernel") diff --git a/convert2rhel/actions/conversion/set_efi_config.py b/convert2rhel/actions/conversion/set_efi_config.py index 30c62348d8..99a024b336 100644 --- a/convert2rhel/actions/conversion/set_efi_config.py +++ b/convert2rhel/actions/conversion/set_efi_config.py @@ -15,15 +15,15 @@ __metaclass__ = type -import logging import os import shutil from convert2rhel import actions, grub, systeminfo from convert2rhel.grub import CENTOS_EFIDIR_CANONICAL_PATH, RHEL_EFIDIR_CANONICAL_PATH +from convert2rhel.logger import root_logger -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class NewDefaultEfiBin(actions.Action): diff --git a/convert2rhel/actions/conversion/transaction.py b/convert2rhel/actions/conversion/transaction.py index f44a43bf16..104c30df81 100644 --- a/convert2rhel/actions/conversion/transaction.py +++ b/convert2rhel/actions/conversion/transaction.py @@ -15,12 +15,12 @@ __metaclass__ = type -import logging from convert2rhel import actions, exceptions, pkgmanager +from convert2rhel.logger import root_logger -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class ConvertSystemPackages(actions.Action): diff --git a/convert2rhel/actions/post_conversion/hostmetering.py b/convert2rhel/actions/post_conversion/hostmetering.py index d0332f4192..03f7d8914f 100644 --- a/convert2rhel/actions/post_conversion/hostmetering.py +++ b/convert2rhel/actions/post_conversion/hostmetering.py @@ -16,17 +16,17 @@ __metaclass__ = type -import logging 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.utils import run_subprocess -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class ConfigureHostMetering(actions.Action): diff --git a/convert2rhel/actions/post_conversion/kernel_boot_files.py b/convert2rhel/actions/post_conversion/kernel_boot_files.py index 5352c7f69a..7382b6157c 100644 --- a/convert2rhel/actions/post_conversion/kernel_boot_files.py +++ b/convert2rhel/actions/post_conversion/kernel_boot_files.py @@ -15,15 +15,15 @@ __metaclass__ = type -import logging import os from convert2rhel import actions, checks, grub +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import system_info from convert2rhel.utils import run_subprocess -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) VMLINUZ_FILEPATH = "/boot/vmlinuz-%s" """The path to the vmlinuz file in a system.""" diff --git a/convert2rhel/actions/post_conversion/modified_rpm_files_diff.py b/convert2rhel/actions/post_conversion/modified_rpm_files_diff.py index afa771197d..7ad1ebbff2 100644 --- a/convert2rhel/actions/post_conversion/modified_rpm_files_diff.py +++ b/convert2rhel/actions/post_conversion/modified_rpm_files_diff.py @@ -16,16 +16,15 @@ __metaclass__ = type import difflib -import logging import os from convert2rhel import actions, utils -from convert2rhel.logger import LOG_DIR +from convert2rhel.logger import LOG_DIR, root_logger from convert2rhel.systeminfo import system_info from convert2rhel.toolopts import POST_RPM_VA_LOG_FILENAME, PRE_RPM_VA_LOG_FILENAME -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class ModifiedRPMFilesDiff(actions.Action): diff --git a/convert2rhel/actions/post_conversion/remove_tmp_dir.py b/convert2rhel/actions/post_conversion/remove_tmp_dir.py index ad1fda08aa..557f617db0 100644 --- a/convert2rhel/actions/post_conversion/remove_tmp_dir.py +++ b/convert2rhel/actions/post_conversion/remove_tmp_dir.py @@ -16,14 +16,14 @@ __metaclass__ = type import errno -import logging import shutil from convert2rhel import actions +from convert2rhel.logger import root_logger from convert2rhel.utils import TMP_DIR -loggerinst = logging.getLogger(__name__) +loggerinst = root_logger.getChild(__name__) class RemoveTmpDir(actions.Action): diff --git a/convert2rhel/actions/post_conversion/update_grub.py b/convert2rhel/actions/post_conversion/update_grub.py index 03d5092b3d..676e7d52ad 100644 --- a/convert2rhel/actions/post_conversion/update_grub.py +++ b/convert2rhel/actions/post_conversion/update_grub.py @@ -15,13 +15,12 @@ __metaclass__ = type -import logging - from convert2rhel import actions, backup, grub, utils from convert2rhel.backup.files import RestorableFile +from convert2rhel.logger import root_logger -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class UpdateGrub(actions.Action): diff --git a/convert2rhel/actions/pre_ponr_changes/backup_system.py b/convert2rhel/actions/pre_ponr_changes/backup_system.py index 5e1a1dfb05..2635936d1e 100644 --- a/convert2rhel/actions/pre_ponr_changes/backup_system.py +++ b/convert2rhel/actions/pre_ponr_changes/backup_system.py @@ -15,13 +15,13 @@ __metaclass__ = type -import logging + import os import re from convert2rhel import actions, backup, exceptions from convert2rhel.backup.files import MissingFile, RestorableFile -from convert2rhel.logger import LOG_DIR +from convert2rhel.logger import LOG_DIR, root_logger from convert2rhel.pkghandler import VERSIONLOCK_FILE_PATH 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 @@ -42,7 +42,7 @@ ) -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class BackupRedhatRelease(actions.Action): @@ -50,7 +50,7 @@ class BackupRedhatRelease(actions.Action): def run(self): """Backup redhat release file before starting conversion process""" - loggerinst.task("Prepare: Backup Redhat Release Files") + logger.task("Prepare: Backup Redhat Release Files") super(BackupRedhatRelease, self).run() @@ -77,20 +77,20 @@ class BackupRepository(actions.Action): def run(self): """Backup .repo files in /etc/yum.repos.d/ so the repositories can be restored on rollback.""" - loggerinst.task("Prepare: Backup Repository Files") + logger.task("Prepare: Backup Repository Files") super(BackupRepository, self).run() - loggerinst.info("Backing up .repo files from %s." % DEFAULT_YUM_REPOFILE_DIR) + logger.info("Backing up .repo files from %s." % DEFAULT_YUM_REPOFILE_DIR) if not os.listdir(DEFAULT_YUM_REPOFILE_DIR): - loggerinst.info("Repository folder %s seems to be empty.", DEFAULT_YUM_REPOFILE_DIR) + logger.info("Repository folder %s seems to be empty.", DEFAULT_YUM_REPOFILE_DIR) for repo in os.listdir(DEFAULT_YUM_REPOFILE_DIR): # backing up redhat.repo so repo files are properly backed up when doing satellite conversions if not repo.endswith(".repo"): - loggerinst.info("Skipping backup as %s is not a repository file." % repo) + logger.info("Skipping backup as %s is not a repository file." % repo) continue repo_path = os.path.join(DEFAULT_YUM_REPOFILE_DIR, repo) @@ -103,15 +103,15 @@ class BackupYumVariables(actions.Action): def run(self): """Backup varsdir folder in /etc/{yum,dnf}/vars so the variables can be restored on rollback.""" - loggerinst.task("Prepare: Backup variables") + logger.task("Prepare: Backup variables") super(BackupYumVariables, self).run() - loggerinst.info("Backing up variables files from %s." % DEFAULT_YUM_VARS_DIR) + logger.info("Backing up variables files from %s." % DEFAULT_YUM_VARS_DIR) self._backup_variables(path=DEFAULT_YUM_VARS_DIR) if system_info.version.major >= 8: - loggerinst.info("Backing up variables files from %s." % DEFAULT_DNF_VARS_DIR) + logger.info("Backing up variables files from %s." % DEFAULT_DNF_VARS_DIR) self._backup_variables(path=DEFAULT_DNF_VARS_DIR) def _backup_variables(self, path): @@ -129,7 +129,7 @@ def _backup_variables(self, path): variable_files_backed_up = True if not variable_files_backed_up: - loggerinst.info("No variables files backed up.") + logger.info("No variables files backed up.") class BackupPackageFiles(actions.Action): @@ -144,7 +144,7 @@ def run(self): """Backup changed package files""" super(BackupPackageFiles, self).run() - loggerinst.task("Prepare: Backup package files") + logger.task("Prepare: Backup package files") package_files_changes = self._get_changed_package_files() @@ -168,7 +168,7 @@ def run(self): restorable_file = RestorableFile(file["path"]) backup.backup_control.push(restorable_file) else: - loggerinst.debug( + logger.debug( "File {filepath} already backed up - not backing up again".format(filepath=file["path"]) ) @@ -189,14 +189,14 @@ def _get_changed_package_files(self): # Catch the IOError due Python 2 compatibility except IOError as err: if os.environ.get("CONVERT2RHEL_INCOMPLETE_ROLLBACK", None): - loggerinst.debug("Skipping backup of the package files. CONVERT2RHEL_INCOMPLETE_ROLLBACK detected.") + logger.debug("Skipping backup of the package files. CONVERT2RHEL_INCOMPLETE_ROLLBACK detected.") # Return empty list results in no backup of the files return data else: # The file should be there # If missing conversion is in unknown state - loggerinst.warning("Error(%s): %s" % (err.errno, err.strerror)) - loggerinst.critical("Missing file {rpm_va_output} in it's location".format(rpm_va_output=path)) + logger.warning("Error(%s): %s" % (err.errno, err.strerror)) + logger.critical("Missing file {rpm_va_output} in it's location".format(rpm_va_output=path)) lines = output.strip().split("\n") for line in lines: @@ -214,7 +214,7 @@ def _parse_line(self, line): if not match: # line not matching the regex if line.strip() != "": # Line is not empty string - loggerinst.debug("Skipping invalid output %s" % line) + logger.debug("Skipping invalid output %s" % line) return {"status": None, "file_type": None, "path": None} line = line.split() diff --git a/convert2rhel/actions/pre_ponr_changes/handle_packages.py b/convert2rhel/actions/pre_ponr_changes/handle_packages.py index d0606a5ced..dd4a9bd3dd 100644 --- a/convert2rhel/actions/pre_ponr_changes/handle_packages.py +++ b/convert2rhel/actions/pre_ponr_changes/handle_packages.py @@ -15,15 +15,15 @@ __metaclass__ = type -import logging 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.systeminfo import system_info -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class ListThirdPartyPackages(actions.Action): diff --git a/convert2rhel/actions/pre_ponr_changes/kernel_modules.py b/convert2rhel/actions/pre_ponr_changes/kernel_modules.py index 0dcd49bce1..da75c44b09 100644 --- a/convert2rhel/actions/pre_ponr_changes/kernel_modules.py +++ b/convert2rhel/actions/pre_ponr_changes/kernel_modules.py @@ -16,18 +16,18 @@ __metaclass__ = type import itertools -import logging 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.utils import run_subprocess -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) LINK_PREVENT_KMODS_FROM_LOADING = "https://access.redhat.com/solutions/41278" diff --git a/convert2rhel/actions/pre_ponr_changes/special_cases.py b/convert2rhel/actions/pre_ponr_changes/special_cases.py index 3893839429..425a396bd9 100644 --- a/convert2rhel/actions/pre_ponr_changes/special_cases.py +++ b/convert2rhel/actions/pre_ponr_changes/special_cases.py @@ -15,14 +15,14 @@ __metaclass__ = type -import logging from convert2rhel import actions +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import system_info from convert2rhel.utils import run_subprocess -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class RemoveIwlax2xxFirmware(actions.Action): diff --git a/convert2rhel/actions/pre_ponr_changes/subscription.py b/convert2rhel/actions/pre_ponr_changes/subscription.py index 03741292a0..3f9fcb97e3 100644 --- a/convert2rhel/actions/pre_ponr_changes/subscription.py +++ b/convert2rhel/actions/pre_ponr_changes/subscription.py @@ -15,7 +15,7 @@ __metaclass__ = type -import logging + import os.path from convert2rhel import actions, backup, exceptions, pkghandler, repo, subscription, toolopts, utils @@ -25,9 +25,10 @@ RestorableDisableRepositories, RestorableSystemSubscription, ) +from convert2rhel.logger import root_logger -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) # Source and target directories for the cdn.redhat.com domain ssl ca cert that: # - we tell customers to use when installing convert2rhel from that domain diff --git a/convert2rhel/actions/pre_ponr_changes/transaction.py b/convert2rhel/actions/pre_ponr_changes/transaction.py index d5dce2eedd..6c6d4b569a 100644 --- a/convert2rhel/actions/pre_ponr_changes/transaction.py +++ b/convert2rhel/actions/pre_ponr_changes/transaction.py @@ -15,12 +15,12 @@ __metaclass__ = type -import logging from convert2rhel import actions, exceptions, pkgmanager +from convert2rhel.logger import root_logger -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class ValidatePackageManagerTransaction(actions.Action): diff --git a/convert2rhel/actions/report.py b/convert2rhel/actions/report.py index e63aa1b60e..68da500565 100644 --- a/convert2rhel/actions/report.py +++ b/convert2rhel/actions/report.py @@ -17,7 +17,6 @@ import copy import json -import logging import textwrap from convert2rhel import utils @@ -29,10 +28,10 @@ format_action_status_message, level_for_combined_action_data, ) -from convert2rhel.logger import colorize +from convert2rhel.logger import colorize, root_logger -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) #: The filename to store the results of running preassessment CONVERT2RHEL_PRE_CONVERSION_JSON_RESULTS = "/var/log/convert2rhel/convert2rhel-pre-conversion.json" diff --git a/convert2rhel/actions/system_checks/check_firewalld_availability.py b/convert2rhel/actions/system_checks/check_firewalld_availability.py index 657a8d1488..a80a419748 100644 --- a/convert2rhel/actions/system_checks/check_firewalld_availability.py +++ b/convert2rhel/actions/system_checks/check_firewalld_availability.py @@ -15,15 +15,16 @@ __metaclass__ = type -import logging + import os import re from convert2rhel import actions, systeminfo +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import system_info -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) # Path to the firewalld config file. FIREWALLD_CONFIG_FILE = "/etc/firewalld/firewalld.conf" diff --git a/convert2rhel/actions/system_checks/convert2rhel_latest.py b/convert2rhel/actions/system_checks/convert2rhel_latest.py index f44df64ae7..5a552b61e4 100644 --- a/convert2rhel/actions/system_checks/convert2rhel_latest.py +++ b/convert2rhel/actions/system_checks/convert2rhel_latest.py @@ -15,7 +15,7 @@ __metaclass__ = type -import logging + import os.path import rpm @@ -23,11 +23,12 @@ from convert2rhel import __file__ as convert2rhel_file from convert2rhel import __version__ as running_convert2rhel_version from convert2rhel import actions, exceptions, repo, utils +from convert2rhel.logger import root_logger from convert2rhel.pkghandler import parse_pkg_string from convert2rhel.systeminfo import system_info -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) C2R_REPOFILE_URLS = { 7: "https://cdn-public.redhat.com/content/public/addon/dist/convert2rhel/server/7/7Server/x86_64/files/repofile.repo", diff --git a/convert2rhel/actions/system_checks/custom_repos_are_valid.py b/convert2rhel/actions/system_checks/custom_repos_are_valid.py index 4598ffbc54..3f4ae4657b 100644 --- a/convert2rhel/actions/system_checks/custom_repos_are_valid.py +++ b/convert2rhel/actions/system_checks/custom_repos_are_valid.py @@ -15,14 +15,14 @@ __metaclass__ = type -import logging from convert2rhel import actions +from convert2rhel.logger import root_logger from convert2rhel.pkgmanager import call_yum_cmd from convert2rhel.toolopts import tool_opts -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class CustomReposAreValid(actions.Action): diff --git a/convert2rhel/actions/system_checks/dbus.py b/convert2rhel/actions/system_checks/dbus.py index 934b29e502..bd8abeee47 100644 --- a/convert2rhel/actions/system_checks/dbus.py +++ b/convert2rhel/actions/system_checks/dbus.py @@ -15,13 +15,13 @@ __metaclass__ = type -import logging from convert2rhel import actions, subscription +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import system_info -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class DbusIsRunning(actions.Action): diff --git a/convert2rhel/actions/system_checks/duplicate_packages.py b/convert2rhel/actions/system_checks/duplicate_packages.py index 961093f5f9..f7cf8bec86 100644 --- a/convert2rhel/actions/system_checks/duplicate_packages.py +++ b/convert2rhel/actions/system_checks/duplicate_packages.py @@ -15,13 +15,13 @@ __metaclass__ = type -import logging from convert2rhel import actions, utils +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import system_info -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class DuplicatePackages(actions.Action): diff --git a/convert2rhel/actions/system_checks/efi.py b/convert2rhel/actions/system_checks/efi.py index cd9f268b51..20cef8f44d 100644 --- a/convert2rhel/actions/system_checks/efi.py +++ b/convert2rhel/actions/system_checks/efi.py @@ -15,14 +15,15 @@ __metaclass__ = type -import logging + import os.path from convert2rhel import actions, grub +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import system_info -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class Efi(actions.Action): diff --git a/convert2rhel/actions/system_checks/els.py b/convert2rhel/actions/system_checks/els.py index 52e1889b90..fcd45d2bdb 100644 --- a/convert2rhel/actions/system_checks/els.py +++ b/convert2rhel/actions/system_checks/els.py @@ -16,14 +16,14 @@ __metaclass__ = type import datetime -import logging from convert2rhel import actions +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import ELS_RELEASE_DATE, system_info from convert2rhel.toolopts import tool_opts -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class ElsSystemCheck(actions.Action): diff --git a/convert2rhel/actions/system_checks/eus.py b/convert2rhel/actions/system_checks/eus.py index 38aeb86f7a..a047603942 100644 --- a/convert2rhel/actions/system_checks/eus.py +++ b/convert2rhel/actions/system_checks/eus.py @@ -16,14 +16,14 @@ __metaclass__ = type import datetime -import logging from convert2rhel import actions +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import EUS_MINOR_VERSIONS, system_info from convert2rhel.toolopts import tool_opts -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class EusSystemCheck(actions.Action): diff --git a/convert2rhel/actions/system_checks/is_loaded_kernel_latest.py b/convert2rhel/actions/system_checks/is_loaded_kernel_latest.py index dde78285ea..0484abbf76 100644 --- a/convert2rhel/actions/system_checks/is_loaded_kernel_latest.py +++ b/convert2rhel/actions/system_checks/is_loaded_kernel_latest.py @@ -15,16 +15,17 @@ __metaclass__ = type -import logging + 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.utils import run_subprocess -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class IsLoadedKernelLatest(actions.Action): diff --git a/convert2rhel/actions/system_checks/package_updates.py b/convert2rhel/actions/system_checks/package_updates.py index 945ab044b5..8fb9542c45 100644 --- a/convert2rhel/actions/system_checks/package_updates.py +++ b/convert2rhel/actions/system_checks/package_updates.py @@ -15,15 +15,16 @@ __metaclass__ = type -import logging + import os from convert2rhel import actions, pkgmanager, utils +from convert2rhel.logger import root_logger from convert2rhel.pkghandler import get_total_packages_to_update from convert2rhel.systeminfo import system_info -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class PackageUpdates(actions.Action): diff --git a/convert2rhel/actions/system_checks/readonly_mounts.py b/convert2rhel/actions/system_checks/readonly_mounts.py index 425a130e4e..3eb59e3886 100644 --- a/convert2rhel/actions/system_checks/readonly_mounts.py +++ b/convert2rhel/actions/system_checks/readonly_mounts.py @@ -15,13 +15,13 @@ __metaclass__ = type -import logging from convert2rhel import actions +from convert2rhel.logger import root_logger from convert2rhel.utils import get_file_content -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) def readonly_mount_detection(mount_point): diff --git a/convert2rhel/actions/system_checks/rhel_compatible_kernel.py b/convert2rhel/actions/system_checks/rhel_compatible_kernel.py index feb7e6ea38..4c37f09954 100644 --- a/convert2rhel/actions/system_checks/rhel_compatible_kernel.py +++ b/convert2rhel/actions/system_checks/rhel_compatible_kernel.py @@ -15,15 +15,15 @@ __metaclass__ = type -import logging from convert2rhel import actions +from convert2rhel.logger import root_logger from convert2rhel.pkghandler import get_installed_pkg_information from convert2rhel.systeminfo import system_info from convert2rhel.utils import run_subprocess -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) # The kernel version stays the same throughout a RHEL major version COMPATIBLE_KERNELS_VERS = { diff --git a/convert2rhel/actions/system_checks/tainted_kmods.py b/convert2rhel/actions/system_checks/tainted_kmods.py index 34e6d35c1b..e33de2826b 100644 --- a/convert2rhel/actions/system_checks/tainted_kmods.py +++ b/convert2rhel/actions/system_checks/tainted_kmods.py @@ -15,14 +15,15 @@ __metaclass__ = type -import logging + import os from convert2rhel import actions +from convert2rhel.logger import root_logger from convert2rhel.utils import run_subprocess -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) LINK_KMODS_RH_POLICY = "https://access.redhat.com/third-party-software-support" LINK_PREVENT_KMODS_FROM_LOADING = "https://access.redhat.com/solutions/41278" diff --git a/convert2rhel/applock.py b/convert2rhel/applock.py index a337c54263..515c730550 100644 --- a/convert2rhel/applock.py +++ b/convert2rhel/applock.py @@ -18,13 +18,14 @@ __metaclass__ = type import errno -import logging import os import tempfile +from convert2rhel.logger import root_logger + _DEFAULT_LOCK_DIR = "/var/run/lock" -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class ApplicationLockedError(Exception): @@ -98,7 +99,7 @@ def _try_create(self): if exc.errno == errno.EEXIST: return False raise exc - loggerinst.debug("%s." % self) + logger.debug("%s." % self) return True @property @@ -151,7 +152,7 @@ def try_to_lock(self, _recursive=False): raise ApplicationLockedError("%s locked by process %d" % (self._pidfile, pid)) # The lock file was created by a process that has exited; # remove it and try again. - loggerinst.info("Cleaning up lock held by exited process %d." % pid) + logger.info("Cleaning up lock held by exited process %d." % pid) os.unlink(self._pidfile) self.try_to_lock(_recursive=True) @@ -166,7 +167,7 @@ def unlock(self): return os.unlink(self._pidfile) self._locked = False - loggerinst.debug("%s." % self) + logger.debug("%s." % self) def __enter__(self): self.try_to_lock() diff --git a/convert2rhel/backup/__init__.py b/convert2rhel/backup/__init__.py index 73ea984cf9..e1b687148c 100644 --- a/convert2rhel/backup/__init__.py +++ b/convert2rhel/backup/__init__.py @@ -19,11 +19,11 @@ import abc import hashlib -import logging import os import six +from convert2rhel.logger import root_logger from convert2rhel.repo import DEFAULT_YUM_REPOFILE_DIR from convert2rhel.utils import TMP_DIR @@ -31,7 +31,7 @@ # Directory for temporary backing up files, packages and other relevant stuff. BACKUP_DIR = os.path.join(TMP_DIR, "backup") -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) def get_backedup_system_repos(): @@ -74,7 +74,7 @@ def push(self, restorable): # if it is, we skip it for r in self._restorables: if r == restorable: - loggerinst.debug("Skipping: {} has already been backed up".format(restorable.__class__.__name__)) + logger.debug("Skipping: {} has already been backed up".format(restorable.__class__.__name__)) return restorable.enable() @@ -130,7 +130,7 @@ def pop_all(self): except (Exception, SystemExit) as e: # Don't let a failure in one restore influence the others message = "Error while rolling back a %s: %s" % (restorable.__class__.__name__, str(e)) - loggerinst.warning(message) + logger.warning(message) # Add the rollback failures to the list self._rollback_failures.append(message) diff --git a/convert2rhel/backup/certs.py b/convert2rhel/backup/certs.py index b8f56bbe4a..0dbb894148 100644 --- a/convert2rhel/backup/certs.py +++ b/convert2rhel/backup/certs.py @@ -18,16 +18,16 @@ __metaclass__ = type import errno -import logging import os import shutil from convert2rhel import exceptions, utils from convert2rhel.backup import RestorableChange +from convert2rhel.logger import root_logger from convert2rhel.utils import files -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class RestorableRpmKey(RestorableChange): @@ -112,14 +112,14 @@ def enable(self): return if os.path.exists(self._target_cert_path): - loggerinst.info("Certificate already present at %s. Skipping copy." % self._target_cert_path) + logger.info("Certificate already present at %s. Skipping copy." % self._target_cert_path) self.previously_installed = True else: try: files.mkdir_p(self._target_cert_dir) shutil.copy2(self._source_cert_path, self._target_cert_dir) except OSError as err: - loggerinst.critical_no_exit("OSError({0}): {1}".format(err.errno, err.strerror)) + logger.critical_no_exit("OSError({0}): {1}".format(err.errno, err.strerror)) raise exceptions.CriticalError( id_="FAILED_TO_INSTALL_CERTIFICATE", title="Failed to install certificate.", @@ -128,18 +128,18 @@ def enable(self): % (self._get_source_cert_path, self._target_cert_dir, err.errno, err.strerror), ) - loggerinst.info("Certificate %s copied to %s." % (self._cert_filename, self._target_cert_dir)) + logger.info("Certificate %s copied to %s." % (self._cert_filename, self._target_cert_dir)) super(RestorablePEMCert, self).enable() def restore(self): """Remove certificate (.pem), which was copied to system's cert dir.""" - loggerinst.task("Rollback: Remove installed certificate") + logger.task("Rollback: Remove installed certificate") if self.enabled and not self.previously_installed: self._restore() else: - loggerinst.info("Certificate %s was present before conversion. Skipping removal." % self._cert_filename) + logger.info("Certificate %s was present before conversion. Skipping removal." % self._cert_filename) super(RestorablePEMCert, self).restore() @@ -170,13 +170,13 @@ def _restore(self): if "not owned by any package" in output: file_unowned = True elif "No such file or directory" in output: - loggerinst.info("Certificate already removed from %s" % self._target_cert_path) + logger.info("Certificate already removed from %s" % self._target_cert_path) else: - loggerinst.warning( + logger.warning( "Unable to determine if a package owns certificate %s. Skipping removal." % self._target_cert_path ) else: - loggerinst.info( + logger.info( "A package was installed that owns the certificate %s. Skipping removal." % self._target_cert_path ) @@ -187,11 +187,11 @@ def _restore(self): try: os.remove(self._target_cert_path) - loggerinst.info("Certificate %s removed" % self._target_cert_path) + logger.info("Certificate %s removed" % self._target_cert_path) except OSError as err: if err.errno == errno.ENOENT: # Resolves RHSM error when removing certs, as the system might not have installed any certs yet - loggerinst.info("No certificates found to be removed.") + logger.info("No certificates found to be removed.") return # Will be handled in BackupController @@ -201,12 +201,12 @@ def _restore(self): def _get_cert(cert_dir): """Return the .pem certificate filename.""" if not os.access(cert_dir, os.R_OK | os.X_OK): - loggerinst.critical("Error: Could not access %s." % cert_dir) + logger.critical("Error: Could not access %s." % cert_dir) pem_filename = None for filename in os.listdir(cert_dir): if filename.endswith(".pem"): pem_filename = filename break if not pem_filename: - loggerinst.critical("Error: No certificate (.pem) found in %s." % cert_dir) + logger.critical("Error: No certificate (.pem) found in %s." % cert_dir) return pem_filename diff --git a/convert2rhel/backup/files.py b/convert2rhel/backup/files.py index 8ef936cbb4..ca3d6dc262 100644 --- a/convert2rhel/backup/files.py +++ b/convert2rhel/backup/files.py @@ -18,15 +18,15 @@ __metaclass__ = type import hashlib -import logging import os import shutil from convert2rhel import exceptions from convert2rhel.backup import BACKUP_DIR, RestorableChange +from convert2rhel.logger import root_logger -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class RestorableFile(RestorableChange): @@ -53,16 +53,16 @@ def enable(self): if self.enabled: return - loggerinst.info("Backing up %s." % self.filepath) + logger.info("Backing up %s." % self.filepath) if os.path.isfile(self.filepath): try: backup_path = self._hash_backup_path() self.backup_path = backup_path shutil.copy2(self.filepath, backup_path) - loggerinst.debug("Copied %s to %s." % (self.filepath, backup_path)) + logger.debug("Copied %s to %s." % (self.filepath, backup_path)) except (OSError, IOError) as err: # IOError for py2 and OSError for py3 - loggerinst.critical_no_exit("Error(%s): %s" % (err.errno, err.strerror)) + logger.critical_no_exit("Error(%s): %s" % (err.errno, err.strerror)) raise exceptions.CriticalError( id_="FAILED_TO_SAVE_FILE_TO_BACKUP_DIR", title="Failed to copy file to the backup directory.", @@ -75,7 +75,7 @@ def enable(self): diagnosis="Failed to backup %s. Errno: %s, Error: %s" % (self.filepath, err.errno, err.strerror), ) else: - loggerinst.info("Can't find %s.", self.filepath) + logger.info("Can't find %s.", self.filepath) return # Set the enabled value @@ -120,12 +120,12 @@ def restore(self, rollback=True): :raises IOError: When the backed up file is missing. """ if rollback: - loggerinst.task("Rollback: Restore %s from backup" % self.filepath) + logger.task("Rollback: Restore %s from backup" % self.filepath) else: - loggerinst.info("Restoring %s from backup" % self.filepath) + logger.info("Restoring %s from backup" % self.filepath) if not self.enabled: - loggerinst.info("%s hasn't been backed up." % self.filepath) + logger.info("%s hasn't been backed up." % self.filepath) return # Possible exceptions will be handled in the BackupController @@ -135,10 +135,10 @@ def restore(self, rollback=True): os.remove(self.backup_path) if rollback: - loggerinst.info("File %s restored." % self.filepath) + logger.info("File %s restored." % self.filepath) super(RestorableFile, self).restore() else: - loggerinst.debug("File %s restored." % self.filepath) + logger.debug("File %s restored." % self.filepath) # not setting enabled to false since this is not being rollback # restoring the backed up file for conversion purposes @@ -146,9 +146,9 @@ def remove(self): """Remove restored file from original place, backup isn't removed""" try: os.remove(self.filepath) - loggerinst.debug("File %s removed." % self.filepath) + logger.debug("File %s removed." % self.filepath) except (OSError, IOError): - loggerinst.debug("Couldn't remove restored file %s" % self.filepath) + logger.debug("Couldn't remove restored file %s" % self.filepath) def __eq__(self, value): if hash(self) == hash(value): @@ -174,14 +174,14 @@ def enable(self): return if os.path.isfile(self.filepath): - loggerinst.debug( + logger.debug( "The file {filepath} is present on the system before conversion, skipping it.".format( filepath=self.filepath ) ) return - loggerinst.info("Marking file {filepath} as missing on system.".format(filepath=self.filepath)) + logger.info("Marking file {filepath} as missing on system.".format(filepath=self.filepath)) super(MissingFile, self).enable() def restore(self): @@ -196,13 +196,13 @@ def restore(self): if not self.enabled: return - loggerinst.task("Rollback: Remove file created during conversion {filepath}".format(filepath=self.filepath)) + logger.task("Rollback: Remove file created during conversion {filepath}".format(filepath=self.filepath)) if not os.path.isfile(self.filepath): - loggerinst.info("File {filepath} wasn't created during conversion".format(filepath=self.filepath)) + logger.info("File {filepath} wasn't created during conversion".format(filepath=self.filepath)) else: # Possible exceptions will be handled in the BackupController os.remove(self.filepath) - loggerinst.info("File {filepath} removed".format(filepath=self.filepath)) + logger.info("File {filepath} removed".format(filepath=self.filepath)) super(MissingFile, self).restore() diff --git a/convert2rhel/backup/packages.py b/convert2rhel/backup/packages.py index 23d13eed65..f2c96dee10 100644 --- a/convert2rhel/backup/packages.py +++ b/convert2rhel/backup/packages.py @@ -17,7 +17,7 @@ __metaclass__ = type -import logging + import os from convert2rhel import exceptions, repo, utils @@ -25,10 +25,11 @@ # Fine to import call_yum_cmd for now, but we really should figure out a way to # split this out. +from convert2rhel.logger import root_logger from convert2rhel.pkgmanager import call_yum_cmd -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) # NOTE: Over time we want to replace this with pkghandler.RestorablePackageSet. @@ -85,18 +86,18 @@ def enable(self): return if not os.path.isdir(BACKUP_DIR): - loggerinst.warning("Can't access %s" % BACKUP_DIR) + logger.warning("Can't access %s" % BACKUP_DIR) return - loggerinst.info("Backing up the packages: %s." % ",".join(self.pkgs)) - loggerinst.debug("Using repository files stored in %s." % self.reposdir) + logger.info("Backing up the packages: %s." % ",".join(self.pkgs)) + logger.debug("Using repository files stored in %s." % self.reposdir) if self.reposdir: # Check if the reposdir exists and if the directory is empty if (os.path.exists(self.reposdir) and len(os.listdir(self.reposdir)) == 0) or not os.path.exists( self.reposdir ): - loggerinst.info("The repository directory %s seems to be empty or non-existent.") + logger.info("The repository directory %s seems to be empty or non-existent.") self.reposdir = None for pkg in self.pkgs: @@ -124,9 +125,9 @@ def restore(self): utils.remove_orphan_folders() - loggerinst.task("Rollback: Install removed packages") + logger.task("Rollback: Install removed packages") if not self._backedup_pkgs_paths: - loggerinst.warning("Couldn't find a backup for %s package." % ",".join(self.pkgs)) + logger.warning("Couldn't find a backup for %s package." % ",".join(self.pkgs)) raise exceptions.CriticalError( id_="FAILED_TO_INSTALL_PACKAGES", title="Couldn't find package backup", @@ -145,23 +146,23 @@ def _install_local_rpms(self, replace=False, critical=True): """Install packages locally available.""" if not self._backedup_pkgs_paths: - loggerinst.info("No package to install.") + logger.info("No package to install.") return False cmd = ["rpm", "-i"] if replace: cmd.append("--replacepkgs") - loggerinst.info("Installing packages:\t%s" % ", ".join(self.pkgs)) + logger.info("Installing packages:\t%s" % ", ".join(self.pkgs)) for pkg in self._backedup_pkgs_paths: cmd.append(pkg) output, ret_code = utils.run_subprocess(cmd, print_output=False) if ret_code != 0: pkgs_as_str = utils.format_sequence_as_message(self.pkgs) - loggerinst.debug(output.strip()) + logger.debug(output.strip()) if critical: - loggerinst.critical_no_exit("Error: Couldn't install %s packages." % pkgs_as_str) + logger.critical_no_exit("Error: Couldn't install %s packages." % pkgs_as_str) raise exceptions.CriticalError( id_="FAILED_TO_INSTALL_PACKAGES", title="Couldn't install packages.", @@ -175,7 +176,7 @@ def _install_local_rpms(self, replace=False, critical=True): % (pkgs_as_str, cmd, output, ret_code), ) - loggerinst.warning("Couldn't install %s packages." % pkgs_as_str) + logger.warning("Couldn't install %s packages." % pkgs_as_str) return False return True @@ -249,12 +250,12 @@ def _enable(self): exit early. """ if not self.pkgs_to_install: - loggerinst.info("All packages were already installed") + logger.info("All packages were already installed") return formatted_pkgs_sequence = utils.format_sequence_as_message(self.pkgs_to_install) - loggerinst.debug("RPMs scheduled for installation: %s" % formatted_pkgs_sequence) + logger.debug("RPMs scheduled for installation: %s" % formatted_pkgs_sequence) output, ret_code = call_yum_cmd( command="install", @@ -271,7 +272,7 @@ def _enable(self): ) if ret_code: - loggerinst.critical_no_exit( + logger.critical_no_exit( "Failed to install scheduled packages. Check the yum output below for details:\n\n %s" % output ) raise exceptions.CriticalError( @@ -284,8 +285,8 @@ def _enable(self): # Need to do this here instead of in pkghandler.call_yum_cmd() to avoid # double printing the output if an error occurred. - loggerinst.info(output.rstrip("\n")) - loggerinst.info("\nPackages we installed or updated:\n%s" % formatted_pkgs_sequence) + logger.info(output.rstrip("\n")) + logger.info("\nPackages we installed or updated:\n%s" % formatted_pkgs_sequence) # We could rely on these always being installed/updated when # self.enabled is True but putting the values into separate attributes @@ -298,8 +299,8 @@ def restore(self): if not self.enabled: return - loggerinst.task("Rollback: Remove installed packages") - loggerinst.info("Removing set of installed pkgs: %s" % utils.format_sequence_as_message(self.installed_pkgs)) + logger.task("Rollback: Remove installed packages") + logger.info("Removing set of installed pkgs: %s" % utils.format_sequence_as_message(self.installed_pkgs)) utils.remove_pkgs(self.installed_pkgs, critical=False) super(RestorablePackageSet, self).restore() diff --git a/convert2rhel/backup/subscription.py b/convert2rhel/backup/subscription.py index 629825c58d..92685eb463 100644 --- a/convert2rhel/backup/subscription.py +++ b/convert2rhel/backup/subscription.py @@ -17,14 +17,15 @@ __metaclass__ = type -import logging + import re from convert2rhel import subscription, utils from convert2rhel.backup import RestorableChange +from convert2rhel.logger import root_logger -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class RestorableSystemSubscription(RestorableChange): @@ -51,14 +52,14 @@ def restore(self): if not self.enabled: return - loggerinst.task("Rollback: RHSM-related actions") + logger.task("Rollback: RHSM-related actions") try: subscription.unregister_system() except subscription.UnregisterError as e: - loggerinst.warning(str(e)) + logger.warning(str(e)) except OSError: - loggerinst.warning("subscription-manager not installed, skipping") + logger.warning("subscription-manager not installed, skipping") super(RestorableSystemSubscription, self).restore() @@ -81,7 +82,7 @@ def enable(self): def restore(self): if self._is_attached: - loggerinst.task("Rollback: Removing auto-attached subscription") + logger.task("Rollback: Removing auto-attached subscription") subscription.remove_subscription() super(RestorableAutoAttachmentSubscription, self).restore() @@ -123,7 +124,7 @@ def enable(self): if repositories: self._repos_to_enable = repositories - loggerinst.debug("Repositories enabled in the system prior to the conversion: %s" % ",".join(repositories)) + logger.debug("Repositories enabled in the system prior to the conversion: %s" % ",".join(repositories)) subscription.disable_repos() super(RestorableDisableRepositories, self).enable() @@ -132,10 +133,10 @@ def restore(self): if not self.enabled: return - loggerinst.task("Rollback: Restoring state of the repositories") + logger.task("Rollback: Restoring state of the repositories") if self._repos_to_enable: - loggerinst.debug("Repositories to enable: %s" % ",".join(self._repos_to_enable)) + logger.debug("Repositories to enable: %s" % ",".join(self._repos_to_enable)) # This is not the ideal state. We should really have a generic # class for enabling/disabling the repositories we have touched for diff --git a/convert2rhel/breadcrumbs.py b/convert2rhel/breadcrumbs.py index 985acb9ee2..a2852d1fe4 100644 --- a/convert2rhel/breadcrumbs.py +++ b/convert2rhel/breadcrumbs.py @@ -18,7 +18,6 @@ __metaclass__ = type import json -import logging import os import re import sys @@ -26,6 +25,7 @@ from datetime import datetime from convert2rhel import pkghandler, toolopts, utils +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import system_info from convert2rhel.utils import files @@ -40,7 +40,7 @@ # Unique identifier under the RHSM custom facts. RHSM_CUSTOM_FACTS_NAMESPACE = "conversions" -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class Breadcrumbs: @@ -188,13 +188,13 @@ def data(self): def _save_migration_results(self): """Write the results of the breadcrumbs to the migration-results file.""" - loggerinst.info("Writing breadcrumbs to '%s'.", MIGRATION_RESULTS_FILE) + logger.info("Writing breadcrumbs to '%s'.", MIGRATION_RESULTS_FILE) _write_obj_to_array_json(path=MIGRATION_RESULTS_FILE, new_object=self.data, key="activities") def _save_rhsm_facts(self): """Write the results of the breadcrumbs to the rhsm custom facts file.""" if not os.path.exists(RHSM_CUSTOM_FACTS_FOLDER): - loggerinst.debug("No RHSM facts folder found at '%s'. Creating a new one..." % RHSM_CUSTOM_FACTS_FOLDER) + logger.debug("No RHSM facts folder found at '%s'. Creating a new one..." % RHSM_CUSTOM_FACTS_FOLDER) # Using mkdir_p here as the `/etc/rhsm` might not exist at all. # Usually this can happen if we fail in the first run and we want to # save the custom facts gathered so far, or, if the `--no-rhsm` option @@ -204,7 +204,7 @@ def _save_rhsm_facts(self): files.mkdir_p(RHSM_CUSTOM_FACTS_FOLDER) data = utils.flatten(dictionary=self.data, parent_key=RHSM_CUSTOM_FACTS_NAMESPACE) - loggerinst.info("Writing RHSM custom facts to '%s'.", RHSM_CUSTOM_FACTS_FILE) + logger.info("Writing RHSM custom facts to '%s'.", RHSM_CUSTOM_FACTS_FILE) # We don't need to use `_write_obj_to_array_json` function here, because # we only care about dumping the facts without having multiple copies of # it. @@ -212,7 +212,7 @@ def _save_rhsm_facts(self): def print_data_collection(self): """Print information about data collection and ask for acknowledgement.""" - loggerinst.info( + logger.info( "The convert2rhel utility generates a %s file that contains the below data about the system conversion." " The subscription-manager then uploads the data to the server the system is registered to.\n" "- The Convert2RHEL command as executed\n" diff --git a/convert2rhel/checks.py b/convert2rhel/checks.py index b79e7e4338..cebe70cb7a 100644 --- a/convert2rhel/checks.py +++ b/convert2rhel/checks.py @@ -15,13 +15,14 @@ __metaclass__ = type -import logging + import os +from convert2rhel.logger import root_logger from convert2rhel.utils import run_subprocess -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) VMLINUZ_FILEPATH = "/boot/vmlinuz-%s" diff --git a/convert2rhel/grub.py b/convert2rhel/grub.py index d3f18a1113..9d0cef4f31 100644 --- a/convert2rhel/grub.py +++ b/convert2rhel/grub.py @@ -17,14 +17,15 @@ __metaclass__ = type -import logging + import os import re from convert2rhel import systeminfo, utils +from convert2rhel.logger import root_logger -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) GRUB2_BIOS_ENTRYPOINT = "/boot/grub2" """The entrypoint path of the BIOS GRUB2""" diff --git a/convert2rhel/initialize.py b/convert2rhel/initialize.py index 3d5a2659e1..806b80677d 100644 --- a/convert2rhel/initialize.py +++ b/convert2rhel/initialize.py @@ -24,7 +24,7 @@ from convert2rhel import logger as logger_module -loggerinst = logging.getLogger(__name__) +loggerinst = logger_module.root_logger.getChild(__name__) def disable_root_logger(): @@ -37,7 +37,7 @@ def disable_root_logger(): bad user data could end up causing the dbus logging to log rhsm passwords and other credentials. """ - logging.getLogger().addHandler(logging.NullHandler()) + logger_module.root_logger.addHandler(logging.NullHandler()) def initialize_logger(): diff --git a/convert2rhel/main.py b/convert2rhel/main.py index 44c92a5a9b..dbeca425b1 100644 --- a/convert2rhel/main.py +++ b/convert2rhel/main.py @@ -17,7 +17,7 @@ __metaclass__ = type -import logging + import os from convert2rhel import actions, applock, backup, breadcrumbs, exceptions @@ -26,7 +26,7 @@ from convert2rhel.actions import level_for_raw_action_data, report -loggerinst = logging.getLogger(__name__) +loggerinst = logger_module.root_logger.getChild(__name__) class _AnalyzeExit(Exception): diff --git a/convert2rhel/pkghandler.py b/convert2rhel/pkghandler.py index ce7a19cd24..c2ce89787b 100644 --- a/convert2rhel/pkghandler.py +++ b/convert2rhel/pkghandler.py @@ -17,7 +17,7 @@ __metaclass__ = type -import logging + import os import os.path import re @@ -29,11 +29,12 @@ from convert2rhel import backup, pkgmanager, repo, utils from convert2rhel.backup.certs import RestorableRpmKey from convert2rhel.backup.files import RestorableFile +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import system_info from convert2rhel.toolopts import tool_opts -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) # Limit the number of loops over yum command calls for the case there was # an error. @@ -176,7 +177,7 @@ def get_installed_pkg_information(pkg_name="*"): ) ) except ValueError as e: - loggerinst.debug("Failed to parse a package: %s", e) + logger.debug("Failed to parse a package: %s", e) return normalized_list @@ -197,7 +198,7 @@ def get_rpm_header(pkg_obj): return rpm_hdr # Package not found in the rpm db - loggerinst.critical("Unable to find package '%s' in the rpm database." % pkg_obj.name) + logger.critical("Unable to find package '%s' in the rpm database." % pkg_obj.name) def get_installed_pkg_objects(name=None, version=None, release=None, arch=None): @@ -379,7 +380,7 @@ def print_pkg_info(pkgs, disable_repos=None): :type disable_repos: List[str] """ - loggerinst.info(format_pkg_info(pkgs, disable_repos)) + logger.info(format_pkg_info(pkgs, disable_repos)) def _get_package_repositories(pkgs, disable_repos=None): @@ -416,7 +417,7 @@ def _get_package_repositories(pkgs, disable_repos=None): # In case of repoquery returning an retcode different from 0, let's log the # output as a debug and return N/A for the caller. if retcode != 0: - loggerinst.debug("Repoquery exited with return code %s and with output: %s", retcode, " ".join(output)) + logger.debug("Repoquery exited with return code %s and with output: %s", retcode, " ".join(output)) for package in pkgs: repositories_mapping[package] = "N/A" else: @@ -428,7 +429,7 @@ def _get_package_repositories(pkgs, disable_repos=None): repoid = split_output[1] repositories_mapping[nevra] = repoid if repoid else "N/A" else: - loggerinst.debug("Got a line without the C2R identifier: %s", line) + logger.debug("Got a line without the C2R identifier: %s", line) return repositories_mapping @@ -564,7 +565,7 @@ def get_packages_to_remove(pkgs): temp = "." * (50 - len(pkg) - 2) pkg_objects = get_installed_pkgs_w_different_fingerprint(system_info.fingerprints_rhel, pkg) pkgs_to_remove.extend(pkg_objects) - loggerinst.info("%s %s %s" % (pkg, temp, str(len(pkg_objects)))) + logger.info("%s %s %s" % (pkg, temp, str(len(pkg_objects)))) return pkgs_to_remove @@ -597,9 +598,9 @@ def install_gpg_keys(): restorable_key = RestorableRpmKey(gpg_key) backup.backup_control.push(restorable_key) except utils.ImportGPGKeyError as e: - loggerinst.critical("Importing the GPG key into rpm failed:\n %s" % str(e)) + logger.critical("Importing the GPG key into rpm failed:\n %s" % str(e)) - loggerinst.info("GPG key %s imported successfuly.", gpg_key) + logger.info("GPG key %s imported successfuly.", gpg_key) def handle_no_newer_rhel_kernel_available(): @@ -642,7 +643,7 @@ def get_kernel(kernels_raw): def replace_non_rhel_installed_kernel(version): """Replace the installed non-RHEL kernel with RHEL kernel with same version.""" - loggerinst.warning( + logger.warning( "The convert2rhel utlity is going to force-replace the only" " kernel installed, which has the same NEVRA as the" " only available RHEL kernel. If anything goes wrong" @@ -665,9 +666,9 @@ def replace_non_rhel_installed_kernel(version): enable_repos=repos_to_enable, ) if not path: - loggerinst.critical("Unable to download the RHEL kernel package.") + logger.critical("Unable to download the RHEL kernel package.") - loggerinst.info("Replacing %s %s with RHEL kernel with the same NEVRA ... " % (system_info.name, pkg)) + logger.info("Replacing %s %s with RHEL kernel with the same NEVRA ... " % (system_info.name, pkg)) output, ret_code = utils.run_subprocess( # The --nodeps is needed as some kernels depend on system-release (alias for redhat-release) and that package # is not installed at this stage. @@ -682,9 +683,9 @@ def replace_non_rhel_installed_kernel(version): print_output=False, ) if ret_code != 0: - loggerinst.critical("Unable to replace the kernel package: %s" % output) + logger.critical("Unable to replace the kernel package: %s" % output) - loggerinst.info("\nRHEL %s installed.\n" % pkg) + logger.info("\nRHEL %s installed.\n" % pkg) def update_rhel_kernel(): @@ -692,7 +693,7 @@ def update_rhel_kernel(): convert2rhel needs to install older RHEL kernel version first. In this function, RHEL kernel is updated to the latest available version. """ - loggerinst.info("Updating RHEL kernel.") + logger.info("Updating RHEL kernel.") pkgmanager.call_yum_cmd(command="update", args=["kernel"]) @@ -706,16 +707,16 @@ def clear_versionlock(): """ if os.path.isfile(VERSIONLOCK_FILE_PATH) and os.path.getsize(VERSIONLOCK_FILE_PATH) > 0: - loggerinst.warning("YUM/DNF versionlock plugin is in use. It may cause the conversion to fail.") - loggerinst.info("Upon continuing, we will clear all package version locks.") + logger.warning("YUM/DNF versionlock plugin is in use. It may cause the conversion to fail.") + logger.info("Upon continuing, we will clear all package version locks.") utils.ask_to_continue() backup.backup_control.push(RestorableFile(VERSIONLOCK_FILE_PATH)) - loggerinst.info("Clearing package versions locks...") + logger.info("Clearing package versions locks...") pkgmanager.call_yum_cmd("versionlock", args=["clear"], print_output=False) else: - loggerinst.info("Usage of YUM/DNF versionlock plugin not detected.") + logger.info("Usage of YUM/DNF versionlock plugin not detected.") @utils.run_as_child_process @@ -1071,7 +1072,7 @@ def get_highest_package_version(pkgs): name, nevra_list = pkgs if not nevra_list: - loggerinst.debug("The list of %s packages is empty." % name) + logger.debug("The list of %s packages is empty." % name) raise ValueError highest_version = nevra_list[0] diff --git a/convert2rhel/pkgmanager/__init__.py b/convert2rhel/pkgmanager/__init__.py index d7e1ee9d1e..0d9e93011b 100644 --- a/convert2rhel/pkgmanager/__init__.py +++ b/convert2rhel/pkgmanager/__init__.py @@ -17,16 +17,16 @@ __metaclass__ = type -import logging from contextlib import contextmanager from convert2rhel import utils +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import system_info from convert2rhel.toolopts import tool_opts -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) try: # this is used in pkghandler.py to parse version strings in the _parse_pkg_with_yum function @@ -99,13 +99,13 @@ def clean_yum_metadata(): output, ret_code = utils.run_subprocess( ("yum", "clean", "metadata", "--enablerepo=*", "--quiet"), print_output=False ) - loggerinst.debug("Output of yum clean metadata:\n%s" % output) + logger.debug("Output of yum clean metadata:\n%s" % output) if ret_code != 0: - loggerinst.warning("Failed to clean yum metadata:\n%s" % output) + logger.warning("Failed to clean yum metadata:\n%s" % output) return - loggerinst.info("Cached repositories metadata cleaned successfully.") + logger.info("Cached repositories metadata cleaned successfully.") @contextmanager @@ -258,6 +258,6 @@ def call_yum_cmd( # handle when yum returns non-zero code when there is nothing to do nothing_to_do_error_exists = stdout.endswith("Error: Nothing to do\n") if returncode == 1 and nothing_to_do_error_exists: - loggerinst.debug("Yum has nothing to do. Ignoring.") + logger.debug("Yum has nothing to do. Ignoring.") returncode = 0 return stdout, returncode diff --git a/convert2rhel/pkgmanager/handlers/dnf/__init__.py b/convert2rhel/pkgmanager/handlers/dnf/__init__.py index 7e166b23ec..6154a3100a 100644 --- a/convert2rhel/pkgmanager/handlers/dnf/__init__.py +++ b/convert2rhel/pkgmanager/handlers/dnf/__init__.py @@ -17,9 +17,9 @@ __metaclass__ = type -import logging from convert2rhel import exceptions, pkgmanager +from convert2rhel.logger import root_logger from convert2rhel.pkghandler import get_system_packages_for_replacement from convert2rhel.pkgmanager.handlers.base import TransactionHandlerBase from convert2rhel.pkgmanager.handlers.dnf.callback import ( @@ -30,7 +30,7 @@ from convert2rhel.systeminfo import system_info -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) """Instance of the logger used in this module.""" @@ -96,7 +96,7 @@ def _enable_repos(self): self._base.read_all_repos() repos = self._base.repos.all() enabled_repos = system_info.get_enabled_rhel_repos() - loggerinst.info("Enabling RHEL repositories:\n%s" % "\n".join(enabled_repos)) + logger.info("Enabling RHEL repositories:\n%s" % "\n".join(enabled_repos)) try: for repo in repos: # Disable the repositories that we don't want if the `repo.id` @@ -107,8 +107,8 @@ def _enable_repos(self): # Load metadata of the enabled repositories self._base.fill_sack() except pkgmanager.exceptions.RepoError as e: - loggerinst.debug("Loading repository metadata failed: %s" % e) - loggerinst.critical_no_exit("Failed to populate repository metadata.") + logger.debug("Loading repository metadata failed: %s" % e) + logger.critical_no_exit("Failed to populate repository metadata.") raise exceptions.CriticalError( id_="FAILED_TO_ENABLE_REPOS", title="Failed to enable repositories.", @@ -127,10 +127,10 @@ def _swap_base_os_specific_packages(self): # Related issue: https://issues.redhat.com/browse/RHELC-1130, see comments # to get more proper description of solution for old_package, new_package in system_info.swap_pkgs.items(): - loggerinst.debug("Checking if %s installed for later swap." % old_package) + logger.debug("Checking if %s installed for later swap." % old_package) is_installed = system_info.is_rpm_installed(old_package) if is_installed: - loggerinst.debug("Package %s will be swapped to %s during conversion." % (old_package, new_package)) + logger.debug("Package %s will be swapped to %s during conversion." % (old_package, new_package)) # Order of commands based on DNF implementation of swap, different from YUM order: # https://github.com/rpm-software-management/dnf/blob/master/dnf/cli/commands/swap.py#L60 self._base.install(pkg_spec=new_package) @@ -147,7 +147,7 @@ def _perform_operations(self): original_os_pkgs = get_system_packages_for_replacement() upgrades = self._base.sack.query().upgrades().latest() - loggerinst.info("Adding %s packages to the dnf transaction set.", system_info.name) + logger.info("Adding %s packages to the dnf transaction set.", system_info.name) for pkg in original_os_pkgs: # Splitting the name and arch so we can filter it out in the list @@ -169,7 +169,7 @@ def _perform_operations(self): try: self._base.downgrade_to(pkg_spec=pkg, strict=True) except pkgmanager.exceptions.PackagesNotInstalledError: - loggerinst.warning("Package %s not available in RHEL repositories.", pkg) + logger.warning("Package %s not available in RHEL repositories.", pkg) self._swap_base_os_specific_packages() @@ -183,12 +183,12 @@ def _resolve_dependencies(self): :raises SystemExit: If we fail to resolve the dependencies or downloading the packages. """ - loggerinst.info("Resolving the dependencies of the packages in the dnf transaction set.") + logger.info("Resolving the dependencies of the packages in the dnf transaction set.") try: self._base.resolve(allow_erasing=True) except pkgmanager.exceptions.DepsolveError as e: - loggerinst.debug("Got the following exception message: %s" % e) - loggerinst.critical_no_exit("Failed to resolve dependencies in the transaction.") + logger.debug("Got the following exception message: %s" % e) + logger.critical_no_exit("Failed to resolve dependencies in the transaction.") raise exceptions.CriticalError( id_="FAILED_TO_RESOLVE_DEPENDENCIES", title="Failed to resolve dependencies.", @@ -196,12 +196,12 @@ def _resolve_dependencies(self): diagnosis="Resolve dependencies failed with error %s." % (str(e)), ) - loggerinst.info("Downloading the packages that were added to the dnf transaction set.") + logger.info("Downloading the packages that were added to the dnf transaction set.") try: self._base.download_packages(self._base.transaction.install_set, PackageDownloadCallback()) except pkgmanager.exceptions.DownloadError as e: - loggerinst.debug("Got the following exception message: %s" % e) - loggerinst.critical_no_exit("Failed to download the transaction packages.") + logger.debug("Got the following exception message: %s" % e) + logger.critical_no_exit("Failed to download the transaction packages.") raise exceptions.CriticalError( id_="FAILED_TO_DOWNLOAD_TRANSACTION_PACKAGES", title="Failed to download packages in the transaction.", @@ -219,10 +219,10 @@ def _process_transaction(self, validate_transaction): """ if validate_transaction: - loggerinst.info("Validating the dnf transaction set, no modifications to the system will happen this time.") + logger.info("Validating the dnf transaction set, no modifications to the system will happen this time.") self._base.conf.tsflags.append("test") else: - loggerinst.info("Replacing %s packages. This process may take some time to finish." % system_info.name) + logger.info("Replacing %s packages. This process may take some time to finish." % system_info.name) try: self._base.do_transaction(display=TransactionDisplayCallback()) @@ -230,8 +230,8 @@ def _process_transaction(self, validate_transaction): pkgmanager.exceptions.Error, pkgmanager.exceptions.TransactionCheckError, ) as e: - loggerinst.debug("Got the following exception message: %s", e) - loggerinst.critical_no_exit("Failed to validate the dnf transaction.") + logger.debug("Got the following exception message: %s", e) + logger.critical_no_exit("Failed to validate the dnf transaction.") raise exceptions.CriticalError( id_="FAILED_TO_VALIDATE_TRANSACTION", title="Failed to validate dnf transaction.", @@ -240,9 +240,9 @@ def _process_transaction(self, validate_transaction): ) if validate_transaction: - loggerinst.info("Successfully validated the dnf transaction set.") + logger.info("Successfully validated the dnf transaction set.") else: - loggerinst.info("System packages replaced successfully.") + logger.info("System packages replaced successfully.") def run_transaction(self, validate_transaction=False): """Run the dnf transaction. diff --git a/convert2rhel/pkgmanager/handlers/dnf/callback.py b/convert2rhel/pkgmanager/handlers/dnf/callback.py index 4089011ee0..6a648ee533 100644 --- a/convert2rhel/pkgmanager/handlers/dnf/callback.py +++ b/convert2rhel/pkgmanager/handlers/dnf/callback.py @@ -60,12 +60,12 @@ # https://github.com/rpm-software-management/dnf/blob/4.7.0/dnf/cli/output.py __metaclass__ = type -import logging from convert2rhel import pkgmanager +from convert2rhel.logger import root_logger -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) """Instance of the logger used in this module.""" @@ -101,18 +101,18 @@ def pkg_added(self, pkg, mode): message = self._DEPSOLVE_MODES[mode] except KeyError: message = None - loggerinst.debug("Unknown operation (%s) for package '%s'." % (mode, pkg)) + logger.debug("Unknown operation (%s) for package '%s'." % (mode, pkg)) if message: - loggerinst.info(message, pkg) + logger.info(message, pkg) def start(self): """Handle the beginning of the dependency resolution process.""" - loggerinst.info("Starting dependency resolution process.") + logger.info("Starting dependency resolution process.") def end(self): """Handle the end of the dependency resolution process.""" - loggerinst.info("Finished dependency resolution process.") + logger.info("Finished dependency resolution process.") class PackageDownloadCallback(pkgmanager.DownloadProgress): @@ -217,7 +217,7 @@ def end(self, payload, status, err_msg): message = "(%d/%d): %s" % (self.done_files, self.total_files, package) if message: - loggerinst.info(message) + logger.info(message) class TransactionDisplayCallback(pkgmanager.TransactionDisplay): @@ -248,7 +248,7 @@ def progress(self, package, action, ti_done, ti_total, ts_done, ts_total): # We don't have any package or action (actually, it's probably that it # will be all empty), let's just return earlier. if action is None or package is None: - loggerinst.debug("No action or package was provided in the callback.") + logger.debug("No action or package was provided in the callback.") return # Here we convert the package to a str because we just need to do a @@ -265,7 +265,7 @@ def progress(self, package, action, ti_done, ti_total, ts_done, ts_total): # no matter if it is the same update or not, so, the below statement # prevents the same message being sent more than once to the user. if self.last_package_seen != package: - loggerinst.info(message) + logger.info(message) self.last_package_seen = package @@ -283,7 +283,7 @@ def scriptout(self, msgs): # no matter if the messages are empty or not, so, the below statement # prevents the same message being sent to the user with empty strings. if msgs: - loggerinst.warning("Scriptlet output: %s", msgs.decode()) + logger.warning("Scriptlet output: %s", msgs.decode()) def error(self, message): """Report an error that occurred during the transaction. @@ -291,4 +291,4 @@ def error(self, message): :param message: The error message sent by the API. :type message: str """ - loggerinst.error("Transaction error: %s", message) + logger.error("Transaction error: %s", message) diff --git a/convert2rhel/pkgmanager/handlers/yum/__init__.py b/convert2rhel/pkgmanager/handlers/yum/__init__.py index a6f08d381b..6ace2d710b 100644 --- a/convert2rhel/pkgmanager/handlers/yum/__init__.py +++ b/convert2rhel/pkgmanager/handlers/yum/__init__.py @@ -18,12 +18,12 @@ __metaclass__ = type import hashlib -import logging import os import re from convert2rhel import backup, exceptions, pkgmanager, utils from convert2rhel.backup.packages import RestorablePackage +from convert2rhel.logger import root_logger from convert2rhel.pkghandler import get_system_packages_for_replacement from convert2rhel.pkgmanager.handlers.base import TransactionHandlerBase from convert2rhel.pkgmanager.handlers.yum.callback import PackageDownloadCallback, TransactionDisplayCallback @@ -32,7 +32,7 @@ from convert2rhel.utils import remove_pkgs -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) """Instance of the logger used in this module.""" MAX_NUM_OF_ATTEMPTS_TO_RESOLVE_DEPS = 3 @@ -56,9 +56,9 @@ def _resolve_yum_problematic_dependencies(output): """ packages_to_remove = [] if output: - loggerinst.debug("Dependency resolution failed:\n- %s" % "\n- ".join(output)) + logger.debug("Dependency resolution failed:\n- %s" % "\n- ".join(output)) else: - loggerinst.debug("Dependency resolution failed with no detailed message reported by yum.") + logger.debug("Dependency resolution failed with no detailed message reported by yum.") for package in output: resolve_error = re.findall(EXTRACT_PKG_FROM_YUM_DEPSOLVE, str(package)) @@ -69,7 +69,7 @@ def _resolve_yum_problematic_dependencies(output): if packages_to_remove: # Need to make each item in the list unique, one pkg can be present more times in the list. packages_to_remove = list(set(packages_to_remove)) - loggerinst.debug( + logger.debug( "Removing problematic packages to continue with the conversion:\n%s", "\n".join(packages_to_remove), ) @@ -87,9 +87,9 @@ def _resolve_yum_problematic_dependencies(output): ) remove_pkgs(pkgs_to_remove=packages_to_remove, critical=True) - loggerinst.debug("Finished backing up and removing the packages.") + logger.debug("Finished backing up and removing the packages.") else: - loggerinst.warning("Unable to resolve dependency issues.") + logger.warning("Unable to resolve dependency issues.") class YumTransactionHandler(TransactionHandlerBase): @@ -159,13 +159,13 @@ def _enable_repos(self): # Set the download progress display self._base.repos.setProgressBar(PackageDownloadCallback()) enabled_repos = system_info.get_enabled_rhel_repos() - loggerinst.info("Enabling RHEL repositories:\n%s" % "\n".join(enabled_repos)) + logger.info("Enabling RHEL repositories:\n%s" % "\n".join(enabled_repos)) try: for repo in enabled_repos: self._base.repos.enableRepo(repo) except pkgmanager.Errors.RepoError as e: - loggerinst.debug("Loading repository metadata failed: %s" % e) - loggerinst.critical_no_exit("Failed to populate repository metadata.") + logger.debug("Loading repository metadata failed: %s" % e) + logger.critical_no_exit("Failed to populate repository metadata.") raise exceptions.CriticalError( id_="FAILED_TO_ENABLE_REPOS", title="Failed to enable repositories.", @@ -184,10 +184,10 @@ def _swap_base_os_specific_packages(self): # Related issue: https://issues.redhat.com/browse/RHELC-1130, see comments # to get more proper description of solution for old_package, new_package in system_info.swap_pkgs.items(): - loggerinst.debug("Checking if %s installed for later swap." % old_package) + logger.debug("Checking if %s installed for later swap." % old_package) is_installed = system_info.is_rpm_installed(old_package) if is_installed: - loggerinst.debug("Package %s will be swapped to %s during conversion." % (old_package, new_package)) + logger.debug("Package %s will be swapped to %s during conversion." % (old_package, new_package)) # Order of operations based on YUM implementation of swap: # https://github.com/rpm-software-management/yum/blob/master/yumcommands.py#L3488 self._base.remove(pattern=old_package) @@ -204,7 +204,7 @@ def _perform_operations(self): original_os_pkgs = get_system_packages_for_replacement() self._enable_repos() - loggerinst.info("Adding %s packages to the yum transaction set.", system_info.name) + logger.info("Adding %s packages to the yum transaction set.", system_info.name) try: for pkg in original_os_pkgs: @@ -226,14 +226,14 @@ def _perform_operations(self): pkgmanager.Errors.ReinstallRemoveError, pkgmanager.Errors.DowngradeError, ): - loggerinst.warning("Package %s not available in RHEL repositories.", pkg) + logger.warning("Package %s not available in RHEL repositories.", pkg) # Swapping the packages needs to be after the operations # If not, swapped packages are removed from transaction as obsolete self._swap_base_os_specific_packages() except pkgmanager.Errors.NoMoreMirrorsRepoError as e: - loggerinst.debug("Got the following exception message: %s", e) - loggerinst.critical_no_exit("There are no suitable mirrors available for the loaded repositories.") + logger.debug("Got the following exception message: %s", e) + logger.critical_no_exit("There are no suitable mirrors available for the loaded repositories.") raise exceptions.CriticalError( id_="FAILED_TO_LOAD_REPOSITORIES", title="Failed to find suitable mirrors for the load repositories.", @@ -270,7 +270,7 @@ def _resolve_dependencies(self): return that message, otherwise, return None. :rtype: str | None """ - loggerinst.info("Resolving the dependencies of the packages in the yum transaction set.") + logger.info("Resolving the dependencies of the packages in the yum transaction set.") ret_code, msg = self._base.resolveDeps() if ret_code == 1: @@ -289,12 +289,12 @@ def _process_transaction(self, validate_transaction): if validate_transaction: self._base.conf.tsflags.append("test") - loggerinst.info( + logger.info( "Downloading and validating the yum transaction set, no modifications to the system will happen " "this time." ) else: - loggerinst.info( + logger.info( "Replacing %s packages. This process may take some time to finish.", system_info.name, ) @@ -317,8 +317,8 @@ def _process_transaction(self, validate_transaction): # - pkgmanager.Errors.YumDownloadError # - pkgmanager.Errors.YumBaseError # - pkgmanager.Errors.YumGPGCheckError - loggerinst.debug("Got the following exception message: %s", e) - loggerinst.critical_no_exit("Failed to validate the yum transaction.") + logger.debug("Got the following exception message: %s", e) + logger.critical_no_exit("Failed to validate the yum transaction.") raise exceptions.CriticalError( id_="FAILED_TO_VALIDATE_TRANSACTION", title="Failed to validate yum transaction.", @@ -327,9 +327,9 @@ def _process_transaction(self, validate_transaction): ) if validate_transaction: - loggerinst.info("Successfully validated the yum transaction set.") + logger.info("Successfully validated the yum transaction set.") else: - loggerinst.info("System packages replaced successfully.") + logger.info("System packages replaced successfully.") def run_transaction(self, validate_transaction=False): """Run the yum transaction. @@ -354,14 +354,14 @@ def run_transaction(self, validate_transaction=False): if "Depsolving loop limit reached" not in messages and validate_transaction: _resolve_yum_problematic_dependencies(messages) - loggerinst.info("Retrying to resolve dependencies %s", attempts) + logger.info("Retrying to resolve dependencies %s", attempts) attempts += 1 else: resolve_deps_finished = True break if not resolve_deps_finished: - loggerinst.critical_no_exit("Failed to resolve dependencies in the transaction.") + logger.critical_no_exit("Failed to resolve dependencies in the transaction.") raise exceptions.CriticalError( id_="FAILED_TO_RESOLVE_DEPENDENCIES", title="Failed to resolve dependencies.", diff --git a/convert2rhel/pkgmanager/handlers/yum/callback.py b/convert2rhel/pkgmanager/handlers/yum/callback.py index 587def17d6..2197f5d951 100644 --- a/convert2rhel/pkgmanager/handlers/yum/callback.py +++ b/convert2rhel/pkgmanager/handlers/yum/callback.py @@ -58,12 +58,12 @@ __metaclass__ = type -import logging from convert2rhel import pkgmanager +from convert2rhel.logger import root_logger -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) """Instance of the logger used in this module.""" # We need to double inherit here, both from the callback class and the base @@ -106,9 +106,9 @@ def updateProgress(self, name, frac, fread, ftime): if self.last_package_seen != name: # Metadata download abut repositories will be sent to this class too. if name.endswith(".rpm"): - loggerinst.info("Downloading package: %s", name) + logger.info("Downloading package: %s", name) else: - loggerinst.debug("Downloading repository metadata: %s", name) + logger.debug("Downloading repository metadata: %s", name) self.last_package_seen = name @@ -162,7 +162,7 @@ def event(self, package, action, te_current, te_total, ts_current, ts_total): # not matter if it is the same update or not, so, the below statement # prevents the same message being sent more than once to the user. if self.last_package_seen != package: - loggerinst.info(message) + logger.info(message) self.last_package_seen = package @@ -182,7 +182,7 @@ def scriptout(self, package, msgs): # no matter if the messages are empty or not, so, the below statement # prevents the same message being sent to the user with empty strings. if msgs: - loggerinst.info("Scriptlet output %s: %s", package, msgs) + logger.info("Scriptlet output %s: %s", package, msgs) def errorlog(self, msg): """Report an error that occurred during the transaction. @@ -190,4 +190,4 @@ def errorlog(self, msg): :param msg: The error message sent by the API. :type message: str """ - loggerinst.error("Transaction error: %s", msg) + logger.error("Transaction error: %s", msg) diff --git a/convert2rhel/redhatrelease.py b/convert2rhel/redhatrelease.py index 6dda1d9d79..32fbf9f657 100644 --- a/convert2rhel/redhatrelease.py +++ b/convert2rhel/redhatrelease.py @@ -17,15 +17,16 @@ __metaclass__ = type -import logging + import os import re from convert2rhel import pkgmanager, utils from convert2rhel.backup.files import RestorableFile +from convert2rhel.logger import root_logger -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) OS_RELEASE_FILEPATH = "/etc/os-release" @@ -35,7 +36,7 @@ def get_system_release_filepath(): release_filepath = "/etc/system-release" # RHEL 7/8 based OSes if os.path.isfile(release_filepath): return release_filepath - loggerinst.critical("Error: Unable to find the /etc/system-release file containing the OS name and version") + logger.critical("Error: Unable to find the /etc/system-release file containing the OS name and version") def get_system_release_content(): @@ -46,7 +47,7 @@ def get_system_release_content(): try: return utils.get_file_content(filepath) except EnvironmentError as err: - loggerinst.critical("%s\n%s file is essential for running this tool." % (err, filepath)) + logger.critical("%s\n%s file is essential for running this tool." % (err, filepath)) class PkgManagerConf: @@ -78,9 +79,9 @@ def patch(self): # package is replaced but this config file is left unchanged and it keeps the original distroverpkg setting. self._comment_out_distroverpkg_tag() self._write_altered_pkg_manager_conf() - loggerinst.info("%s patched." % self._pkg_manager_conf_path) + logger.info("%s patched." % self._pkg_manager_conf_path) else: - loggerinst.info("Skipping patching, package manager configuration file has not been modified.") + logger.info("Skipping patching, package manager configuration file has not been modified.") return diff --git a/convert2rhel/repo.py b/convert2rhel/repo.py index 92cecb0395..8e6c68ebd0 100644 --- a/convert2rhel/repo.py +++ b/convert2rhel/repo.py @@ -17,7 +17,7 @@ __metaclass__ = type -import logging + import tempfile from contextlib import closing @@ -25,6 +25,7 @@ from six.moves import urllib from convert2rhel import exceptions +from convert2rhel.logger import root_logger from convert2rhel.systeminfo import system_info from convert2rhel.toolopts import tool_opts from convert2rhel.utils import TMP_DIR, store_content_to_file @@ -34,7 +35,7 @@ DEFAULT_YUM_VARS_DIR = "/etc/yum/vars" DEFAULT_DNF_VARS_DIR = "/etc/dnf/vars" -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) def get_rhel_repoids(): @@ -52,7 +53,7 @@ def get_rhel_repoids(): else: repos_needed = system_info.default_rhsm_repoids - loggerinst.info("RHEL repository IDs to enable: %s" % ", ".join(repos_needed)) + logger.info("RHEL repository IDs to enable: %s" % ", ".join(repos_needed)) return repos_needed @@ -116,14 +117,14 @@ def download_repofile(repofile_url): "The requested repository file seems to be empty. No content received when checking for url: %s" % repofile_url ) - loggerinst.critical_no_exit(description) + logger.critical_no_exit(description) raise exceptions.CriticalError( id_="REPOSITORY_FILE_EMPTY_CONTENT", title="No content available in a repository file", description=description, ) - loggerinst.info("Successfully downloaded a repository file from %s." % repofile_url) + logger.info("Successfully downloaded a repository file from %s." % repofile_url) return contents.decode() except urllib.error.URLError as err: raise exceptions.CriticalError( diff --git a/convert2rhel/subscription.py b/convert2rhel/subscription.py index b41de02273..80c0dec1e4 100644 --- a/convert2rhel/subscription.py +++ b/convert2rhel/subscription.py @@ -19,7 +19,6 @@ import hashlib import json -import logging import os import re @@ -32,13 +31,14 @@ from convert2rhel import backup, exceptions, i18n, pkghandler, repo, utils from convert2rhel.backup.packages import RestorablePackageSet +from convert2rhel.logger import root_logger from convert2rhel.redhatrelease import os_release_file from convert2rhel.repo import DEFAULT_DNF_VARS_DIR, DEFAULT_YUM_VARS_DIR from convert2rhel.systeminfo import system_info from convert2rhel.toolopts import _should_subscribe, tool_opts -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) # We need to translate config settings between names used for the subscription-manager DBus API and # names used for the RHSM config file. This is the mapping for the settings we care about. @@ -98,13 +98,13 @@ class SubscriptionAutoAttachmentError(Exception): def remove_subscription(): """Remove all subscriptions added from auto attachment""" - loggerinst.info("Removing auto attached subscriptions.") + logger.info("Removing auto attached subscriptions.") subscription_removal_cmd = ["subscription-manager", "remove", "--all"] output, ret_code = utils.run_subprocess(subscription_removal_cmd, print_output=False) if ret_code != 0: raise SubscriptionRemovalError("Subscription removal result\n%s" % output) else: - loggerinst.info("Subscription removal successful.") + logger.info("Subscription removal successful.") def auto_attach_subscription(): @@ -116,14 +116,14 @@ def auto_attach_subscription(): if ret_code != 0: raise SubscriptionAutoAttachmentError("Unsuccessful auto attachment of a subscription.") else: - loggerinst.info("Subscription attachment successful.") + logger.info("Subscription attachment successful.") return True def unregister_system(): """Unregister the system from RHSM.""" - loggerinst.info("Unregistering the system.") + logger.info("Unregistering the system.") # We are calling run_subprocess with rpm here because of a bug in # Oracle/CentOS Linux 7 in which the process always exits with 1 in case of # a rollback when KeyboardInterrupt is raised. To avoid many changes and @@ -134,7 +134,7 @@ def unregister_system(): # https://github.com/rpm-software-management/rpm/blob/rpm-4.11.x/lib/rpmdb.c#L640 _, ret_code = utils.run_subprocess(["rpm", "--quiet", "-q", "subscription-manager"]) if ret_code != 0: - loggerinst.info("The subscription-manager package is not installed.") + logger.info("The subscription-manager package is not installed.") return unregistration_cmd = ["subscription-manager", "unregister"] @@ -142,7 +142,7 @@ def unregister_system(): if ret_code != 0: raise UnregisterError("System unregistration result:\n%s" % output) else: - loggerinst.info("System unregistered successfully.") + logger.info("System unregistered successfully.") def register_system(): @@ -159,7 +159,7 @@ def register_system(): MAX_NUM_OF_ATTEMPTS_TO_SUBSCRIBE, ) - loggerinst.info( + logger.info( "%sRegistering the system using subscription-manager ...", attempt_msg, ) @@ -172,11 +172,11 @@ def register_system(): # - in RHEL 9 before 9.2: https://bugzilla.redhat.com/show_bug.cgi?id=2121350 # Explicitly unregister here to workaround that in any version, # to not have to do version checks, keeping things simpler. - loggerinst.info("Unregistering the system to clear the server's state for our registration.") + logger.info("Unregistering the system to clear the server's state for our registration.") try: unregister_system() except UnregisterError as e: - loggerinst.warning(str(e)) + logger.warning(str(e)) # Hack: currently, on RHEL7, subscription-manager unregister is # reporting that the system is not registered but then calling the @@ -188,7 +188,7 @@ def register_system(): # # For the short term, we are going to stop the rhsm service to work # around this issue. It should restart on its own when needed: - loggerinst.info( + logger.info( "Stopping the RHSM service so that registration does not think that the host is already registered." ) try: @@ -196,7 +196,7 @@ def register_system(): except StopRhsmError as e: # The system really might not be registered yet and also not running rhsm # so ignore the error and try to register the system. - loggerinst.info(str(e)) + logger.info(str(e)) # Register the system registration_cmd = RegistrationCommand.from_tool_opts(tool_opts) @@ -209,7 +209,7 @@ def register_system(): # RHELC-16 os_release_file.restore(rollback=False) except (OSError, IOError) as e: - loggerinst.critical_no_exit( + logger.critical_no_exit( "Failed to restore the /etc/os-release file needed for subscribing the system with message: %s" % str(e) ) raise exceptions.CriticalError( @@ -226,12 +226,12 @@ def register_system(): # RHELC-16 # Will be removed only if the registration succeed os_release_file.remove() - loggerinst.info("System registration succeeded.") + logger.info("System registration succeeded.") except KeyboardInterrupt: # When the user hits Control-C to exit, we shouldn't retry raise except Exception as e: - loggerinst.info("System registration failed with error: %s" % str(e)) + logger.info("System registration failed with error: %s" % str(e)) troublesome_exception = e sleep(REGISTRATION_ATTEMPT_DELAYS[attempt]) attempt += 1 @@ -241,7 +241,7 @@ def register_system(): else: # While-else # We made the maximum number of subscription-manager retries and still failed - loggerinst.critical_no_exit("Unable to register the system through subscription-manager.") + logger.critical_no_exit("Unable to register the system through subscription-manager.") raise exceptions.CriticalError( id_="FAILED_TO_SUBSCRIBE_SYSTEM", title="Failed to subscribe system.", @@ -267,7 +267,7 @@ def refresh_subscription_info(): "Asking subscription-manager to reexamine its configuration failed: %s; output: %s" % (ret_code, output) ) - loggerinst.info("subscription-manager has reloaded its configuration.") + logger.info("subscription-manager has reloaded its configuration.") def _stop_rhsm(): @@ -276,7 +276,7 @@ def _stop_rhsm(): output, ret_code = utils.run_subprocess(cmd, print_output=False) if ret_code != 0: raise StopRhsmError("Stopping RHSM failed with code: %s; output: %s" % (ret_code, output)) - loggerinst.info("RHSM service stopped.") + logger.info("RHSM service stopped.") class RegistrationCommand: @@ -344,29 +344,29 @@ def from_tool_opts(cls, tool_opts): :arg tool_opts: The :class:`convert2rhel.toolopts.ToolOpts` structure to retrieve the subscription-manager information from. """ - loggerinst.info("Gathering subscription-manager registration info ... ") + logger.info("Gathering subscription-manager registration info ... ") registration_attributes = {} if tool_opts.org: - loggerinst.info(" ... organization detected") + logger.info(" ... organization detected") registration_attributes["org"] = tool_opts.org if tool_opts.activation_key: # Activation key has been passed # -> username/password not required # -> organization required - loggerinst.info(" ... activation key detected") + logger.info(" ... activation key detected") registration_attributes["activation_key"] = tool_opts.activation_key else: # No activation key -> username/password required if tool_opts.username and tool_opts.password: - loggerinst.info(" ... activation key not found, using given username and password") + logger.info(" ... activation key not found, using given username and password") else: - loggerinst.info(" ... activation key not found, username and password required") + logger.info(" ... activation key not found, username and password required") if tool_opts.username: - loggerinst.info(" ... username detected") + logger.info(" ... username detected") username = tool_opts.username else: username = "" @@ -376,7 +376,7 @@ def from_tool_opts(cls, tool_opts): registration_attributes["username"] = username if tool_opts.password: - loggerinst.info(" ... password detected") + logger.info(" ... password detected") password = tool_opts.password else: password = "" @@ -386,13 +386,13 @@ def from_tool_opts(cls, tool_opts): registration_attributes["password"] = password if tool_opts.rhsm_hostname: - loggerinst.debug(" ... using custom RHSM hostname") + logger.debug(" ... using custom RHSM hostname") registration_attributes["rhsm_hostname"] = tool_opts.rhsm_hostname if tool_opts.rhsm_port: - loggerinst.debug(" ... using custom RHSM port") + logger.debug(" ... using custom RHSM port") registration_attributes["rhsm_port"] = tool_opts.rhsm_port if tool_opts.rhsm_prefix: - loggerinst.debug(" ... using custom RHSM prefix") + logger.debug(" ... using custom RHSM prefix") registration_attributes["rhsm_prefix"] = tool_opts.rhsm_prefix return cls(**registration_attributes) @@ -439,14 +439,14 @@ def __call__(self): # if we need one in the future. REGISTER_OPTS_DICT = dbus.Dictionary({}, signature="sv", variant_level=1) - loggerinst.debug("Getting a handle to the system dbus") + logger.debug("Getting a handle to the system dbus") system_bus = dbus.SystemBus() # Create a new bus so we can talk to rhsm privately (For security: # talking on the system bus might be eavesdropped in certain scenarios) - loggerinst.debug("Getting a subscription-manager RegisterServer object from dbus") + logger.debug("Getting a subscription-manager RegisterServer object from dbus") register_server = system_bus.get_object("com.redhat.RHSM1", "/com/redhat/RHSM1/RegisterServer") - loggerinst.debug("Starting a private DBus to talk to subscription-manager") + logger.debug("Starting a private DBus to talk to subscription-manager") address = register_server.Start( i18n.SUBSCRIPTION_MANAGER_LOCALE, dbus_interface="com.redhat.RHSM1.RegisterServer", @@ -454,17 +454,17 @@ def __call__(self): try: # Use the private bus to register the machine - loggerinst.debug("Connecting to the private DBus") + logger.debug("Connecting to the private DBus") private_bus = dbus.connection.Connection(address) try: if self.password: if self.org: - loggerinst.info("Organization: %s", utils.OBFUSCATION_STRING) - loggerinst.info("Username: %s", utils.OBFUSCATION_STRING) - loggerinst.info("Password: %s", utils.OBFUSCATION_STRING) - loggerinst.info("Connection Options: %s", self.connection_opts) - loggerinst.info("Locale settings: %s", i18n.SUBSCRIPTION_MANAGER_LOCALE) + logger.info("Organization: %s", utils.OBFUSCATION_STRING) + logger.info("Username: %s", utils.OBFUSCATION_STRING) + logger.info("Password: %s", utils.OBFUSCATION_STRING) + logger.info("Connection Options: %s", self.connection_opts) + logger.info("Locale settings: %s", i18n.SUBSCRIPTION_MANAGER_LOCALE) args = ( self.org or "", self.username, @@ -484,10 +484,10 @@ def __call__(self): ) else: - loggerinst.info("Organization: %s", utils.OBFUSCATION_STRING) - loggerinst.info("Activation Key: %s", utils.OBFUSCATION_STRING) - loggerinst.info("Connection Options: %s", self.connection_opts) - loggerinst.info("Locale settings: %s", i18n.SUBSCRIPTION_MANAGER_LOCALE) + logger.info("Organization: %s", utils.OBFUSCATION_STRING) + logger.info("Activation Key: %s", utils.OBFUSCATION_STRING) + logger.info("Connection Options: %s", self.connection_opts) + logger.info("Locale settings: %s", i18n.SUBSCRIPTION_MANAGER_LOCALE) args = ( self.org, [self.activation_key], @@ -526,7 +526,7 @@ def __call__(self): finally: # Always shut down the private bus - loggerinst.debug("Shutting down private DBus instance") + logger.debug("Shutting down private DBus instance") register_server.Stop( i18n.SUBSCRIPTION_MANAGER_LOCALE, dbus_interface="com.redhat.RHSM1.RegisterServer", @@ -554,7 +554,7 @@ def _set_connection_opts_in_config(self): # dbus_interface="com.redhat.RHSM1.ConfigServer", # ) if self.connection_opts: - loggerinst.info("Setting RHSM connection configuration.") + logger.info("Setting RHSM connection configuration.") sub_man_config_command = ["subscription-manager", "config"] for option, value in self.connection_opts.items(): sub_man_config_command.append("--%s=%s" % (CONNECT_OPT_NAME_TO_CONFIG_KEY[option], value)) @@ -563,12 +563,12 @@ def _set_connection_opts_in_config(self): if ret_code != 0: raise ValueError("Error setting the subscription-manager connection configuration: %s" % output) - loggerinst.info("Successfully set RHSM connection configuration.") + logger.info("Successfully set RHSM connection configuration.") def is_registered(): """Check if the machine we're running on is registered with subscription-manager.""" - loggerinst.debug("Checking whether the host was registered.") + logger.debug("Checking whether the host was registered.") output, ret_code = utils.run_subprocess(["subscription-manager", "identity"]) # Registered: ret_code 0 and output like: @@ -577,12 +577,12 @@ def is_registered(): # org name: 13460994 # org ID: 13460994 if ret_code == 0: - loggerinst.debug("Host was registered.") + logger.debug("Host was registered.") return True # Unregistered: ret_code 1 and output like: # This system is not yet registered. Try 'subscription-manager register --help' for more information. - loggerinst.debug("Host was not registered.") + logger.debug("Host was not registered.") return False @@ -672,36 +672,36 @@ def attach_subscription(): """ # check if SCA is enabled if is_sca_enabled(): - loggerinst.info("Simple Content Access is enabled, skipping subscription attachment") + logger.info("Simple Content Access is enabled, skipping subscription attachment") if tool_opts.pool: - loggerinst.warning( + logger.warning( "Because Simple Content Access is enabled the subscription specified by the pool ID will not be attached." ) return True if tool_opts.activation_key: - loggerinst.info("Using the activation key provided through the command line...") + logger.info("Using the activation key provided through the command line...") return True pool = ["subscription-manager", "attach"] if tool_opts.auto_attach: pool.append("--auto") - loggerinst.info("Auto-attaching compatible subscriptions to the system ...") + logger.info("Auto-attaching compatible subscriptions to the system ...") elif tool_opts.pool: # The subscription pool ID has been passed through a command line # option pool.extend(["--pool", tool_opts.pool]) - loggerinst.info("Attaching provided subscription pool ID to the system ...") + logger.info("Attaching provided subscription pool ID to the system ...") elif not tool_opts.auto_attach and not tool_opts.pool: # defaulting to --auto similiar to the functioning of subscription-manager pool.append("--auto") - loggerinst.info("Auto-attaching compatible subscriptions to the system ...") + logger.info("Auto-attaching compatible subscriptions to the system ...") _, ret_code = utils.run_subprocess(pool) if ret_code != 0: # Unsuccessful attachment, e.g. the pool ID is incorrect or the # number of purchased attachments has been depleted. - loggerinst.critical_no_exit( + logger.critical_no_exit( "Unsuccessful attachment of a subscription. Please refer to https://access.redhat.com/management/" " where you can either enable the SCA, create an activation key, or find a Pool ID of the subscription" " you wish to use and pass it to convert2rhel through the `--pool` CLI option." @@ -721,13 +721,13 @@ def get_pool_id(sub_raw_attrs): if pool_id: return pool_id.group(1) - loggerinst.critical("Cannot parse the subscription pool ID from string:\n%s" % sub_raw_attrs) + logger.critical("Cannot parse the subscription pool ID from string:\n%s" % sub_raw_attrs) def verify_rhsm_installed(): """Make sure that subscription-manager has been installed.""" if not pkghandler.get_installed_pkg_information("subscription-manager"): - loggerinst.critical_no_exit( + logger.critical_no_exit( "The subscription-manager package is not installed correctly. You could try manually installing it before running convert2rhel" ) raise exceptions.CriticalError( @@ -737,7 +737,7 @@ def verify_rhsm_installed(): remediations="Manually installing subscription-manager before running convert2rhel.", ) else: - loggerinst.info("subscription-manager installed correctly.") + logger.info("subscription-manager installed correctly.") def disable_repos(): @@ -750,7 +750,7 @@ def disable_repos(): cmd.extend(disable_cmd) output, ret_code = utils.run_subprocess(cmd, print_output=False) if ret_code != 0: - loggerinst.critical_no_exit("Could not disable subscription-manager repositories:\n%s" % output) + logger.critical_no_exit("Could not disable subscription-manager repositories:\n%s" % output) raise exceptions.CriticalError( id_="FAILED_TO_DISABLE_SUBSCRIPTION_MANAGER_REPOSITORIES", title="Could not disable repositories through subscription-manager.", @@ -758,7 +758,7 @@ def disable_repos(): diagnosis="Failed to disable repositories: %s." % (output), ) - loggerinst.info("Repositories disabled.") + logger.info("Repositories disabled.") def enable_repos(rhel_repoids): @@ -781,7 +781,7 @@ def enable_repos(rhel_repoids): """ repos_to_enable = tool_opts.enablerepo if tool_opts.enablerepo else rhel_repoids - loggerinst.info("Trying to enable the following RHEL repositories: %s" % ", ".join(repos_to_enable)) + logger.info("Trying to enable the following RHEL repositories: %s" % ", ".join(repos_to_enable)) submgr_enable_repos(repos_to_enable) system_info.submgr_enabled_repos = repos_to_enable @@ -796,14 +796,14 @@ def submgr_enable_repos(repos_to_enable): output, ret_code = utils.run_subprocess(enable_cmd, print_output=False) if ret_code != 0: description = "Repositories were not possible to enable through subscription-manager:\n%s" % output - loggerinst.critical_no_exit(description) + logger.critical_no_exit(description) raise exceptions.CriticalError( id_="FAILED_TO_ENABLE_RHSM_REPOSITORIES", title="Failed to enable RHSM repositories", description=description, ) - loggerinst.info("Repositories enabled through subscription-manager") + logger.info("Repositories enabled through subscription-manager") def needed_subscription_manager_pkgs(): @@ -836,10 +836,10 @@ def needed_subscription_manager_pkgs(): # `get_installed_pkg_information()` again. installed_submgr_pkgs = [pkg.nevra.name for pkg in installed_submgr_pkgs] - loggerinst.debug("Need the following packages: %s" % utils.format_sequence_as_message(subscription_manager_pkgs)) - loggerinst.debug("Detected the following packages: %s" % utils.format_sequence_as_message(installed_submgr_pkgs)) + logger.debug("Need the following packages: %s" % utils.format_sequence_as_message(subscription_manager_pkgs)) + logger.debug("Detected the following packages: %s" % utils.format_sequence_as_message(installed_submgr_pkgs)) - loggerinst.debug("Packages we will install: %s" % utils.format_sequence_as_message(to_install_pkgs)) + logger.debug("Packages we will install: %s" % utils.format_sequence_as_message(to_install_pkgs)) return to_install_pkgs @@ -892,21 +892,21 @@ def update_rhsm_custom_facts(): "breadcrumbs" from convert2rhel as RHSM facts. """ if not tool_opts.no_rhsm: - loggerinst.info("Updating RHSM custom facts collected during the conversion.") + logger.info("Updating RHSM custom facts collected during the conversion.") cmd = ["subscription-manager", "facts", "--update"] output, ret_code = utils.run_subprocess(cmd, print_output=False) if ret_code != 0: - loggerinst.warning( + logger.warning( "Failed to update the RHSM custom facts with return code '%s' and output '%s'.", ret_code, output, ) return ret_code, output else: - loggerinst.info("RHSM custom facts uploaded successfully.") + logger.info("RHSM custom facts uploaded successfully.") else: - loggerinst.info("Skipping updating RHSM custom facts.") + logger.info("Skipping updating RHSM custom facts.") return None, None @@ -918,16 +918,16 @@ def get_rhsm_facts(): """ rhsm_facts = {} if tool_opts.no_rhsm: - loggerinst.info("Ignoring RHSM facts collection. --no-rhsm is used.") + logger.info("Ignoring RHSM facts collection. --no-rhsm is used.") return rhsm_facts - loggerinst.info("Reading RHSM facts file.") + logger.info("Reading RHSM facts file.") try: with open(RHSM_FACTS_FILE, mode="r") as handler: rhsm_facts = json.load(handler) - loggerinst.info("RHSM facts loaded.") + logger.info("RHSM facts loaded.") except (IOError, ValueError) as e: - loggerinst.critical_no_exit( + logger.critical_no_exit( "Failed to get the RHSM facts : %s." % e, ) return rhsm_facts diff --git a/convert2rhel/systeminfo.py b/convert2rhel/systeminfo.py index 0bc9ef12c0..dd496d14fc 100644 --- a/convert2rhel/systeminfo.py +++ b/convert2rhel/systeminfo.py @@ -17,7 +17,7 @@ __metaclass__ = type -import logging + import os import re import time @@ -135,7 +135,7 @@ def __init__(self): self.cfg_filename = None self.cfg_content = None self.system_release_file_content = None - self.logger = None + self.logger = logger.root_logger.getChild(__name__) # IDs of the default Red Hat CDN repositories that correspond to the current system self.default_rhsm_repoids = None # IDs of the Extended Update Support (EUS) Red Hat CDN repositories that correspond to the current system @@ -152,7 +152,6 @@ def __init__(self): self.booted_kernel = "" def resolve_system_info(self): - self.logger = logging.getLogger(__name__) self.system_release_file_content = self.get_system_release_file_content() system_release_data = self.parse_system_release_content() diff --git a/convert2rhel/toolopts.py b/convert2rhel/toolopts.py index 4a3f0fd7b9..e440cafa29 100644 --- a/convert2rhel/toolopts.py +++ b/convert2rhel/toolopts.py @@ -18,7 +18,6 @@ import argparse import copy -import logging import os import re import sys @@ -26,9 +25,10 @@ from six.moves import configparser, urllib from convert2rhel import __version__, utils +from convert2rhel.logger import root_logger -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) # Paths for configuration files CONFIG_PATHS = ["~/.convert2rhel.ini", "/etc/convert2rhel.ini"] @@ -352,7 +352,7 @@ def _process_cli_options(self): if parsed_opts.no_rpm_va: if tool_opts.activity == "analysis": - loggerinst.warning( + logger.warning( "We will proceed with ignoring the --no-rpm-va option as running rpm -Va" " in the analysis mode is essential for a complete rollback to the original" " system state at the end of the analysis." @@ -366,7 +366,7 @@ def _process_cli_options(self): " incomplete rollback, set the CONVERT2RHEL_INCOMPLETE_ROLLBACK=1 environment" " variable. Otherwise, remove the --no-rpm-va option." ) - loggerinst.critical(message) + logger.critical(message) if parsed_opts.username: tool_opts.username = parsed_opts.username @@ -387,12 +387,12 @@ def _process_cli_options(self): for repo in duplicate_repos: message += "\n%s" % repo message += "\nThis ambiguity may have unintended consequences." - loggerinst.warning(message) + logger.warning(message) if parsed_opts.no_rhsm: tool_opts.no_rhsm = True if not tool_opts.enablerepo: - loggerinst.critical("The --enablerepo option is required when --no-rhsm is used.") + logger.critical("The --enablerepo option is required when --no-rhsm is used.") if parsed_opts.eus: tool_opts.eus = True @@ -417,11 +417,11 @@ def _process_cli_options(self): if parsed_opts.serverurl: if tool_opts.no_rhsm: - loggerinst.warning("Ignoring the --serverurl option. It has no effect when --no-rhsm is used.") + logger.warning("Ignoring the --serverurl option. It has no effect when --no-rhsm is used.") # WARNING: We cannot use the following helper until after no_rhsm, # username, password, activation_key, and organization have been set. elif not _should_subscribe(tool_opts): - loggerinst.warning( + logger.warning( "Ignoring the --serverurl option. It has no effect when no credentials to subscribe the system were given." ) else: @@ -440,7 +440,7 @@ def _process_cli_options(self): # to their internal subscription-manager server but it # would really be passed externally. That's not a good # security practice. - loggerinst.critical( + logger.critical( "Failed to parse a valid subscription-manager server from the --serverurl option.\n" "Please check for typos and run convert2rhel again with a corrected --serverurl.\n" "Supplied serverurl: %s\nError: %s" % (parsed_opts.serverurl, e) @@ -465,7 +465,7 @@ def _process_cli_options(self): # Security notice if tool_opts.password or tool_opts.activation_key: - loggerinst.warning( + logger.warning( "Passing the RHSM password or activation key through the --activationkey or --password options is" " insecure as it leaks the values through the list of running processes. We recommend using the safer" " --config-file option instead." @@ -473,43 +473,43 @@ def _process_cli_options(self): # Checks of multiple authentication sources if tool_opts.password and tool_opts.activation_key: - loggerinst.warning( + logger.warning( "Either a password or an activation key can be used for system registration." " We're going to use the activation key." ) # Config files matches if config_opts.username and parsed_opts.username: - loggerinst.warning( + logger.warning( "You have passed the RHSM username through both the command line and the" " configuration file. We're going to use the command line values." ) if config_opts.org and parsed_opts.org: - loggerinst.warning( + logger.warning( "You have passed the RHSM org through both the command line and the" " configuration file. We're going to use the command line values." ) if (config_opts.activation_key or config_opts.password) and (parsed_opts.activationkey or parsed_opts.password): - loggerinst.warning( + logger.warning( "You have passed either the RHSM password or activation key through both the command line and the" " configuration file. We're going to use the command line values." ) if (tool_opts.org and not tool_opts.activation_key) or (not tool_opts.org and tool_opts.activation_key): - loggerinst.critical( + logger.critical( "Either the --org or the --activationkey option is missing. You can't use one without the other." ) if (parsed_opts.password or config_opts.password) and not (parsed_opts.username or config_opts.username): - loggerinst.warning( + logger.warning( "You have passed the RHSM password without an associated username. Please provide a username together" " with the password." ) if (parsed_opts.username or config_opts.username) and not (parsed_opts.password or config_opts.password): - loggerinst.warning( + logger.warning( "You have passed the RHSM username without an associated password. Please provide a password together" " with the username." ) @@ -521,7 +521,7 @@ def _log_command_used(): and the logfile """ command = " ".join(utils.hide_secrets(sys.argv)) - loggerinst.info("convert2rhel command used:\n{0}".format(command)) + logger.info("convert2rhel command used:\n{0}".format(command)) def options_from_config_files(cfg_path=None): @@ -562,7 +562,7 @@ def options_from_config_files(cfg_path=None): for path in paths: if os.path.exists(path): if not oct(os.stat(path).st_mode)[-4:].endswith("00"): - loggerinst.critical("The %s file must only be accessible by the owner (0600)" % path) + logger.critical("The %s file must only be accessible by the owner (0600)" % path) config_file.read(path) for header in config_file.sections(): @@ -572,11 +572,11 @@ def options_from_config_files(cfg_path=None): # Solving priority if supported_opts[option.lower()] is None: supported_opts[option] = config_file.get(header, option) - loggerinst.debug("Found %s in %s" % (option, path)) + logger.debug("Found %s in %s" % (option, path)) else: - loggerinst.warning("Unsupported option %s in %s" % (option, path)) + logger.warning("Unsupported option %s in %s" % (option, path)) elif header not in headers and header != "DEFAULT": - loggerinst.warning("Unsupported header %s in %s." % (header, path)) + logger.warning("Unsupported header %s in %s." % (header, path)) return supported_opts diff --git a/convert2rhel/unit_tests/actions/data/stage_tests/action_exceptions/test.py b/convert2rhel/unit_tests/actions/data/stage_tests/action_exceptions/test.py index f526e3b465..f8e5bc7d9f 100644 --- a/convert2rhel/unit_tests/actions/data/stage_tests/action_exceptions/test.py +++ b/convert2rhel/unit_tests/actions/data/stage_tests/action_exceptions/test.py @@ -1,11 +1,11 @@ __metaclass__ = type -import logging from convert2rhel import actions +from convert2rhel.logger import root_logger -loginst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) class DivideByZeroTest(actions.Action): @@ -23,7 +23,7 @@ class LogCriticalTest(actions.Action): def run(self): super(LogCriticalTest, self).run() - loginst.critical("Critical log will cause a SystemExit.") + logger.critical("Critical log will cause a SystemExit.") class SuccessTest(actions.Action): diff --git a/convert2rhel/unit_tests/logger_test.py b/convert2rhel/unit_tests/logger_test.py index 453b6b80c2..c6ff31862c 100644 --- a/convert2rhel/unit_tests/logger_test.py +++ b/convert2rhel/unit_tests/logger_test.py @@ -36,7 +36,7 @@ def test_logger_handlers(monkeypatch, tmpdir, read_std, global_tool_opts): global_tool_opts.debug = True # debug entries > stdout if True logger_module.setup_logger_handler() logger_module.add_file_handler(log_name=log_fname, log_dir=str(tmpdir)) - logger = logging.getLogger(__name__) + logger = logger_module.root_logger.getChild(__name__) # emitting some log entries logger.info("Test info: %s", "data") @@ -56,7 +56,7 @@ def test_logger_handlers(monkeypatch, tmpdir, read_std, global_tool_opts): def test_tools_opts_debug(monkeypatch, read_std, is_py2, global_tool_opts): monkeypatch.setattr("convert2rhel.toolopts.tool_opts", global_tool_opts) logger_module.setup_logger_handler() - logger = logging.getLogger(__name__) + logger = logger_module.root_logger.getChild(__name__) global_tool_opts.debug = True logger.debug("debug entry 1: %s", "data") stdouterr_out, stdouterr_err = read_std() @@ -77,20 +77,20 @@ def test_tools_opts_debug(monkeypatch, read_std, is_py2, global_tool_opts): assert "debug entry 2: data" not in stdouterr_out -class TestCustomLogger: +class Testroot_logger: @pytest.mark.parametrize( ("log_method_name", "level_name"), ( - ("task", "TASK"), + ("task", "INFO"), ("file", "DEBUG"), ("warning", "WARNING"), ("critical_no_exit", "CRITICAL"), ), ) def test_logger_custom_logger(self, log_method_name, level_name, caplog): - """Test CustomLogger.""" + """Test root_logger.""" logger_module.setup_logger_handler() - logger = logging.getLogger(__name__) + logger = logger_module.root_logger.getChild(__name__) log_method = getattr(logger, log_method_name) log_method("Some task: %s", "data") @@ -100,9 +100,9 @@ def test_logger_custom_logger(self, log_method_name, level_name, caplog): assert caplog.records[-1].levelname == level_name def test_logger_critical(self, caplog): - """Test CustomLogger.""" + """Test root_logger.""" logger_module.setup_logger_handler() - logger = logging.getLogger(__name__) + logger = logger_module.root_logger.getChild(__name__) with pytest.raises(SystemExit): logger.critical("Critical error: %s", "data") @@ -117,14 +117,13 @@ def test_logger_critical(self, caplog): "file", "debug", "warning", - "critical_no_exit", ], ) def test_logger_custom_logger_insufficient_level(self, log_method_name, caplog): - """Test CustomLogger.""" + """Test root_logger.""" logger_module.setup_logger_handler() - logger = logging.getLogger(__name__) - logger.setLevel(logging.CRITICAL + 40) + logger = logger_module.root_logger + logger.setLevel(logging.CRITICAL) log_method = getattr(logger, log_method_name) log_method("Some task: %s", "data") @@ -132,17 +131,6 @@ def test_logger_custom_logger_insufficient_level(self, log_method_name, caplog): assert "Some task: data" not in caplog.text assert not caplog.records - def test_logger_critical_insufficient_level(self, caplog): - """Test CustomLogger.""" - logger_module.setup_logger_handler() - logger = logging.getLogger(__name__) - logger.setLevel(logging.CRITICAL + 40) - - logger.critical("Critical error: %s", "data") - - assert not caplog.records - assert "Critical error: data\n" not in caplog.text - @pytest.mark.parametrize( ("log_name", "path_exists"), @@ -204,3 +192,21 @@ def test_logfile_buffer_handler(read_std): stdouterr_out, _ = read_std() assert "message 1" not in stdouterr_out assert "message 2" in stdouterr_out + + +class TestCustomFormatter: + """For testing the Custom Formatter to work as expected.""" + + def test_task_logger(self, read_std): + logger = logging.getLogger("convert2rhel") + stdout_handler = logging.StreamHandler(sys.stdout) + formatter = logger_module.CustomFormatter("%(message)s") + formatter.disable_colors(True) + stdout_handler.setFormatter(formatter) + stdout_handler.setLevel(logging.DEBUG) + logger.addHandler(stdout_handler) + + logger.info("Testing", extra={"isTask": True}) + + stdouterr_out, stdouterr_err = read_std() + assert "TASK - [Testing]" in stdouterr_out diff --git a/convert2rhel/utils/__init__.py b/convert2rhel/utils/__init__.py index ce3ee5816d..aa1c22656e 100644 --- a/convert2rhel/utils/__init__.py +++ b/convert2rhel/utils/__init__.py @@ -20,7 +20,6 @@ import fcntl import getpass import json -import logging import multiprocessing import os import re @@ -40,9 +39,10 @@ from six import moves from convert2rhel import exceptions, i18n +from convert2rhel.logger import root_logger -loggerinst = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) # A string we're using to replace sensitive information (like an RHSM password) in logs, terminal output, etc. OBFUSCATION_STRING = "*" * 5 @@ -260,12 +260,12 @@ def inner_wrapper(*args, **kwargs): # API) will keep executing until they finish their execution and # ignore the call for termination issued by the parent. To avoid # having "zombie" processes, we need to wait for them to finish. - loggerinst.warning("Terminating child process...") + logger.warning("Terminating child process...") if process.is_alive(): - loggerinst.debug("Process with pid %s is alive", process.pid) + logger.debug("Process with pid %s is alive", process.pid) process.terminate() - loggerinst.debug("Process with pid %s exited", process.pid) + logger.debug("Process with pid %s exited", process.pid) # If there is a KeyboardInterrupt raised while the child process is # being executed, let's just re-raise it to the stack and move on. @@ -280,7 +280,7 @@ def inner_wrapper(*args, **kwargs): def require_root(): if os.geteuid() != 0: - loggerinst.critical("The tool needs to be run under the root user.\nNo changes were made to the system.") + logger.critical("The tool needs to be run under the root user.\nNo changes were made to the system.") def get_file_content(filename, as_list=False): @@ -325,7 +325,7 @@ def restart_system(): if tool_opts.restart: run_subprocess(["reboot"]) else: - loggerinst.warning("In order to boot the RHEL kernel, restart of the system is needed.") + logger.warning("In order to boot the RHEL kernel, restart of the system is needed.") def run_subprocess(cmd, print_cmd=True, print_output=True): @@ -343,7 +343,7 @@ def run_subprocess(cmd, print_cmd=True, print_output=True): raise TypeError("cmd should be a list, not a str") if print_cmd: - loggerinst.debug("Calling command '%s'" % " ".join(cmd)) + logger.debug("Calling command '%s'" % " ".join(cmd)) process = subprocess.Popen( # pylint: disable=consider-using-with # Popen is only a context manager in Python-3.2+ @@ -357,7 +357,7 @@ def run_subprocess(cmd, print_cmd=True, print_output=True): line = line.decode("utf8") output += line if print_output: - loggerinst.info(line.rstrip("\n")) + logger.info(line.rstrip("\n")) # Call communicate() to wait for the process to terminate so that we can # get the return code. @@ -402,7 +402,7 @@ def run_cmd_in_pty(cmd, expect_script=(), print_cmd=True, print_output=True, col raise TypeError("cmd should be a list, not a str") if print_cmd: - loggerinst.debug("Calling command '%s'" % " ".join(cmd)) + logger.debug("Calling command '%s'" % " ".join(cmd)) process = PexpectSpawnWithDimensions( cmd[0], @@ -435,7 +435,7 @@ def run_cmd_in_pty(cmd, expect_script=(), print_cmd=True, print_output=True, col output = process.before.decode() if print_output: - loggerinst.info(output.rstrip("\n")) + logger.info(output.rstrip("\n")) return output, return_code @@ -514,7 +514,7 @@ def ask_to_continue(): if cont == "y": break if cont == "n": - loggerinst.critical("User canceled the conversion\n") + logger.critical("User canceled the conversion\n") def prompt_user(question, password=False): @@ -531,7 +531,7 @@ def prompt_user(question, password=False): response = getpass.getpass(color_question) else: response = moves.input(color_question) - loggerinst.info("\n") + logger.info("\n") return response @@ -543,10 +543,10 @@ def log_traceback(debug): traceback_str = get_traceback_str() if debug: # Print the traceback to the user when debug option used - loggerinst.debug(traceback_str) + logger.debug(traceback_str) else: # Print the traceback to the log file in any way - loggerinst.file(traceback_str) + logger.file(traceback_str) def get_traceback_str(): @@ -559,11 +559,11 @@ def remove_tmp_dir(): """Remove temporary folder (TMP_DIR), not needed post-conversion.""" try: shutil.rmtree(TMP_DIR) - loggerinst.info("Temporary folder %s removed" % TMP_DIR) + logger.info("Temporary folder %s removed" % TMP_DIR) except OSError as err: - loggerinst.warning("Failed removing temporary folder %s\nError (%s): %s" % (TMP_DIR, err.errno, err.strerror)) + logger.warning("Failed removing temporary folder %s\nError (%s): %s" % (TMP_DIR, err.errno, err.strerror)) except TypeError: - loggerinst.warning("TypeError error while removing temporary folder %s" % TMP_DIR) + logger.warning("TypeError error while removing temporary folder %s" % TMP_DIR) class DictWListValues(dict): @@ -639,7 +639,7 @@ def download_pkg( """ from convert2rhel.systeminfo import system_info - loggerinst.debug("Downloading the %s package." % pkg) + logger.debug("Downloading the %s package." % pkg) # On RHEL 7, it's necessary to invoke yumdownloader with -v, otherwise there's no output to stdout. cmd = ["yumdownloader", "-v", "--setopt=exclude=", "--destdir=%s" % dest] @@ -681,8 +681,8 @@ def download_pkg( report_on_a_download_error(output, pkg) return None - loggerinst.info("Successfully downloaded the %s package." % pkg) - loggerinst.debug("Path of the downloaded package: %s" % path) + logger.info("Successfully downloaded the %s package." % pkg) + logger.debug("Path of the downloaded package: %s" % path) return path @@ -703,7 +703,7 @@ def remove_pkgs(pkgs_to_remove, critical=True): pkgs_removed = [] if not pkgs_to_remove: - loggerinst.info("No package to remove") + logger.info("No package to remove") return pkgs_removed pkgs_failed_to_remove = [] @@ -712,7 +712,7 @@ def remove_pkgs(pkgs_to_remove, critical=True): # handle the epoch well and considers the package we want to remove as not installed. On the other hand, the # epoch in NEVRA returned by dnf is handled by rpm just fine. nvra = _remove_epoch_from_yum_nevra_notation(nevra) - loggerinst.info("Removing package: %s" % nvra) + logger.info("Removing package: %s" % nvra) _, ret_code = run_subprocess(["rpm", "-e", "--nodeps", nvra]) if ret_code != 0: pkgs_failed_to_remove.append(nevra) @@ -722,7 +722,7 @@ def remove_pkgs(pkgs_to_remove, critical=True): if pkgs_failed_to_remove: pkgs_as_str = format_sequence_as_message(pkgs_failed_to_remove) if critical: - loggerinst.critical_no_exit("Error: Couldn't remove %s." % pkgs_as_str) + logger.critical_no_exit("Error: Couldn't remove %s." % pkgs_as_str) raise exceptions.CriticalError( id_="FAILED_TO_REMOVE_PACKAGES", title="Couldn't remove packages.", @@ -730,7 +730,7 @@ def remove_pkgs(pkgs_to_remove, critical=True): diagnosis="Couldn't remove %s." % pkgs_as_str, ) else: - loggerinst.warning("Couldn't remove %s." % pkgs_as_str) + logger.warning("Couldn't remove %s." % pkgs_as_str) return pkgs_removed @@ -760,7 +760,7 @@ def report_on_a_download_error(output, pkg): :param output: Output of the yumdownloader call :param pkg: Name of a package to be downloaded """ - loggerinst.warning("Output from the yumdownloader call:\n%s" % output) + logger.warning("Output from the yumdownloader call:\n%s" % output) # Note: Using toolopts here is a temporary solution. We need to # restructure this to raise an exception on error and have the caller @@ -791,7 +791,7 @@ def report_on_a_download_error(output, pkg): if toolopts.tool_opts.activity == "conversion": if "CONVERT2RHEL_INCOMPLETE_ROLLBACK" not in os.environ: - loggerinst.critical( + logger.critical( "Couldn't download the %s package. This means we will not be able to do a" " complete rollback and may put the system in a broken state.\n" "Check to make sure that the %s repositories are enabled" @@ -800,14 +800,14 @@ def report_on_a_download_error(output, pkg): " 'CONVERT2RHEL_INCOMPLETE_ROLLBACK=1'." % (pkg, system_info.name) ) else: - loggerinst.warning( + logger.warning( "Couldn't download the %s package. This means we will not be able to do a" " complete rollback and may put the system in a broken state.\n" "'CONVERT2RHEL_INCOMPLETE_ROLLBACK' environment variable detected, continuing" " conversion." % pkg ) else: - loggerinst.critical( + logger.critical( "Couldn't download the %s package which is needed to do a rollback of this action." " Check to make sure that the %s repositories are enabled and the package is" " updated to its latest version.\n" @@ -826,7 +826,7 @@ def get_rpm_path_from_yumdownloader_output(cmd, output, dest): RHEL 8: "[SKIPPED] oraclelinux-release-8.2-1.0.8.el8.x86_64.rpm: Already downloaded" """ if not output: - loggerinst.warning("The output of running yumdownloader is unexpectedly empty. Command:\n%s" % cmd) + logger.warning("The output of running yumdownloader is unexpectedly empty. Command:\n%s" % cmd) return None rpm_name_match = re.search(r"\S+\.rpm", output) @@ -837,7 +837,7 @@ def get_rpm_path_from_yumdownloader_output(cmd, output, dest): elif pkg_nevra_match: path = os.path.join(dest, pkg_nevra_match.group(1) + ".rpm") else: - loggerinst.warning( + logger.warning( "Couldn't find the name of the downloaded rpm in the output of yumdownloader.\n" "Command:\n%s\nOutput:\n%s" % (cmd, output) ) @@ -942,9 +942,7 @@ def find_keyid(keyfile): # If we get here, we tried and failed to rmtree five times # Don't make this fatal but do let the user know so they can clean # it up themselves. - loggerinst.info( - "Failed to remove temporary directory %s that held Red Hat gpg public keys." % temporary_dir - ) + logger.info("Failed to remove temporary directory %s that held Red Hat gpg public keys." % temporary_dir) keyid = None for line in output.splitlines(): @@ -1046,7 +1044,7 @@ def hide_secrets( sanitized_list.append(arg) if hide_next: - loggerinst.debug( + logger.debug( "Passed arguments had an option, '{0}', without an expected secret parameter".format(sanitized_list[-1]) ) diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index 657ff1e627..6119d241f2 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -21,6 +21,8 @@ from _pytest.warning_types import PytestUnknownMarkWarning from dotenv import dotenv_values +from convert2rhel.logger import root_logger + try: from pathlib import Path @@ -29,7 +31,7 @@ logging.basicConfig(level=os.environ.get("DEBUG", "INFO"), stream=sys.stderr) -logger = logging.getLogger(__name__) +logger = root_logger.getChild(__name__) def pytest_collection_modifyitems(items):