Skip to content

Commit

Permalink
refactor: Improved logger usage
Browse files Browse the repository at this point in the history
This refactor of the usage of our logging is to get away from custom
functions that causes attribute errors in editors and IDEs. This will
also standarize the usage of logger and the loglevels.

In future PRs we can include changes to include prepare, rollback, and
final specific message styles.

Co-authored-by: Freya Gustavsson <freya@venefilyn.se>
  • Loading branch information
Venefilyn committed Aug 19, 2024
1 parent 6f654f4 commit d97b77e
Show file tree
Hide file tree
Showing 59 changed files with 502 additions and 454 deletions.
3 changes: 2 additions & 1 deletion convert2rhel/actions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,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.
Expand Down
6 changes: 2 additions & 4 deletions convert2rhel/actions/conversion/list_non_red_hat_pkgs_left.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 2 additions & 4 deletions convert2rhel/actions/conversion/lock_releasever.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions convert2rhel/actions/conversion/pkg_manager_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
7 changes: 3 additions & 4 deletions convert2rhel/actions/conversion/preserve_only_rhel_kernel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
__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


loggerinst = logging.getLogger(__name__)
loggerinst = logger.root_logger.getChild(__name__)


class InstallRhelKernel(actions.Action):
Expand Down Expand Up @@ -186,7 +185,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")
Expand Down
4 changes: 2 additions & 2 deletions convert2rhel/actions/conversion/set_efi_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion convert2rhel/actions/conversion/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
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):
Expand Down
4 changes: 2 additions & 2 deletions convert2rhel/actions/post_conversion/hostmetering.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions convert2rhel/actions/post_conversion/kernel_boot_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions convert2rhel/actions/post_conversion/remove_tmp_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions convert2rhel/actions/post_conversion/update_grub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
34 changes: 17 additions & 17 deletions convert2rhel/actions/pre_ponr_changes/backup_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

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
Expand All @@ -42,15 +42,15 @@
)


loggerinst = logging.getLogger(__name__)
logger = root_logger.getChild(__name__)


class BackupRedhatRelease(actions.Action):
id = "BACKUP_REDHAT_RELEASE"

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()

Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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()

Expand All @@ -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"])
)

Expand All @@ -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:
Expand All @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion convert2rhel/actions/pre_ponr_changes/handle_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
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):
Expand Down
3 changes: 2 additions & 1 deletion convert2rhel/actions/pre_ponr_changes/kernel_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
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"

Expand Down
3 changes: 2 additions & 1 deletion convert2rhel/actions/pre_ponr_changes/special_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
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):
Expand Down
3 changes: 2 additions & 1 deletion convert2rhel/actions/pre_ponr_changes/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion convert2rhel/actions/pre_ponr_changes/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
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):
Expand Down
Loading

0 comments on commit d97b77e

Please sign in to comment.