|
| 1 | +# Copyright(C) 2025 Red Hat, Inc. |
| 2 | +# |
| 3 | +# This program is free software: you can redistribute it and/or modify |
| 4 | +# it under the terms of the GNU General Public License as published by |
| 5 | +# the Free Software Foundation, either version 3 of the License, or |
| 6 | +# (at your option) any later version. |
| 7 | +# |
| 8 | +# This program is distributed in the hope that it will be useful, |
| 9 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | +# GNU General Public License for more details. |
| 12 | +# |
| 13 | +# You should have received a copy of the GNU General Public License |
| 14 | +# along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 15 | + |
| 16 | +__metaclass__ = type |
| 17 | + |
| 18 | +import os |
| 19 | +import shutil |
| 20 | + |
| 21 | +from convert2rhel import actions |
| 22 | +from convert2rhel import backup |
| 23 | +from convert2rhel.backup.files import InstalledFile, RestorableFile |
| 24 | +from convert2rhel.logger import root_logger |
| 25 | +from convert2rhel.pkghandler import get_files_owned_by_package, get_packages_to_remove |
| 26 | +from convert2rhel.repo import DEFAULT_DNF_VARS_DIR, DEFAULT_YUM_VARS_DIR |
| 27 | +from convert2rhel.systeminfo import system_info |
| 28 | +from convert2rhel.toolopts.config import loggerinst |
| 29 | + |
| 30 | +logger = root_logger.getChild(__name__) |
| 31 | + |
| 32 | + |
| 33 | +class BackUpYumVariables(actions.Action): |
| 34 | + id = "BACKUP_YUM_VARIABLES" |
| 35 | + # We don't make a distinction between /etc/yum/vars/ and /etc/yum/vars/ in this Action. Wherever the files are we |
| 36 | + # back them up. |
| 37 | + yum_var_dirs = [DEFAULT_DNF_VARS_DIR, DEFAULT_YUM_VARS_DIR] |
| 38 | + |
| 39 | + def run(self): |
| 40 | + """Back up yum variable files in /etc/{yum,dnf}/vars/ owned by packages that are known to install these yum |
| 41 | + variable files (such as system-release). We back them up to be able to restore them right after we remove these |
| 42 | + packages. We need to restore the variable files because we use repofiles also installed by these packages and |
| 43 | + yum does not allow specifying a custom directory with yum variable files. This functionality came later with dnf |
| 44 | + however we apply the same approach to both yum and dnf for the sake of code simplicity. |
| 45 | + """ |
| 46 | + logger.task("Back up yum variables") |
| 47 | + |
| 48 | + super(BackUpYumVariables, self).run() |
| 49 | + |
| 50 | + logger.debug("Getting a list of files owned by packages affecting variables in .repo files.") |
| 51 | + yum_var_affecting_pkgs = get_packages_to_remove(system_info.repofile_pkgs) |
| 52 | + yum_var_filepaths = self._get_yum_var_files_owned_by_pkgs( |
| 53 | + [pkg_obj.nevra.name for pkg_obj in yum_var_affecting_pkgs] |
| 54 | + ) |
| 55 | + |
| 56 | + self._back_up_var_files(yum_var_filepaths) |
| 57 | + |
| 58 | + def _get_yum_var_files_owned_by_pkgs(self, pkg_names): |
| 59 | + """Get paths of yum var files owned by the packages passed to the method.""" |
| 60 | + pkg_owned_files = set() |
| 61 | + for pkg in pkg_names: |
| 62 | + pkg_owned_files.union(get_files_owned_by_package(pkg)) # using set() and union() to get unique paths |
| 63 | + |
| 64 | + # Out of all the files owned by the packages get just those in yum/dnf var dirs |
| 65 | + yum_var_filepaths = [path for path in pkg_owned_files if os.path.dirname(path) in self.yum_var_dirs] |
| 66 | + |
| 67 | + return yum_var_filepaths |
| 68 | + |
| 69 | + def _back_up_var_files(self, paths): |
| 70 | + """Back up yum variable files. |
| 71 | +
|
| 72 | + :param paths: Paths to the variable files to back up |
| 73 | + :type paths: list[str] |
| 74 | + """ |
| 75 | + logger.info("Backing up variables files from {}.".format(" and ".join(self.yum_var_dirs))) |
| 76 | + if not paths: |
| 77 | + logger.info("No variables files backed up.") |
| 78 | + |
| 79 | + for filepath in paths: |
| 80 | + restorable_file = RestorableFile(filepath) |
| 81 | + backup.backup_control.push(restorable_file) |
| 82 | + |
| 83 | + |
| 84 | +class RestoreYumVarFiles(actions.Action): |
| 85 | + id = "RESTORE_YUM_VAR_FILES" |
| 86 | + dependencies = ("REMOVE_SPECIAL_PACKAGES",) |
| 87 | + |
| 88 | + def run(self): |
| 89 | + """Right after removing packages that own yum variable files in the REMOVE_SPECIAL_PACKAGES Action, in this |
| 90 | + Action we restore these files to /etc/{yum,dnf}/vars/ so that yum can use them when accessing the original |
| 91 | + vendor repositories (which are backed up in a temporary folder and passed to yum through the --setopt=reposdir= |
| 92 | + option). |
| 93 | + The ideal solution would be to use the --setopt=varsdir= option also for the temporary folder where yum variable |
| 94 | + files are backed up however the option was only introduced in dnf so it's not available in RHEL 7 and its |
| 95 | + derivatives. For the sake of using just one approach to simplify the codebase, we are restoring the yum variable |
| 96 | + files no matter the package manager. |
| 97 | + We use the backup controller to record that we've restored the variable files meaning that upon rollback the |
| 98 | + files get removed. As part of the rollback we also install beck the packages that include these files so they'll |
| 99 | + be present. |
| 100 | + TODO: These restored variable files should not be present after a successful conversion. One option is to |
| 101 | + enhance the backup controller to indicate that a certain activity should be rolled back not only during a rollback |
| 102 | + but also after a successful conversion. With such a flag we would add a new post-conversion Action to run the |
| 103 | + backup controller restoration but only for the activities recorded with this flag. |
| 104 | + """ |
| 105 | + super(RestoreYumVarFiles, self).run() |
| 106 | + |
| 107 | + backed_up_yum_var_dirs = backup.get_backed_up_yum_var_dirs() |
| 108 | + loggerinst.task("Restoring yum variable files") |
| 109 | + loggerinst.info( |
| 110 | + "In a previous step we removed a package that might have come with yum variables and in case we" |
| 111 | + " need to access {} repositories (e.g. when installing dependencies of subscription-manager) we" |
| 112 | + " need these yum variables available.".format(system_info.name) |
| 113 | + ) |
| 114 | + for orig_yum_var_dir in backed_up_yum_var_dirs: |
| 115 | + for backed_up_yum_var_filepath in os.listdir(backed_up_yum_var_dirs[orig_yum_var_dir]): |
| 116 | + try: |
| 117 | + shutil.copy2(backed_up_yum_var_filepath, orig_yum_var_dir) |
| 118 | + logger.debug("Copied {} from backup to {}.".format(backed_up_yum_var_filepath, orig_yum_var_dir)) |
| 119 | + except (OSError, IOError) as err: |
| 120 | + # IOError for py2 and OSError for py3 |
| 121 | + # Not being able to restore the yum variables might or might not cause problems down the road. No |
| 122 | + # need to stop the conversion because of that. The warning message below should be enough of a clue |
| 123 | + # for resolving subsequent yum errors. |
| 124 | + logger.warning( |
| 125 | + "Couldn't copy {} to {}. Error: {}".format( |
| 126 | + backed_up_yum_var_filepath, orig_yum_var_dir, err.strerror |
| 127 | + ) |
| 128 | + ) |
| 129 | + return |
| 130 | + restored_file = InstalledFile( |
| 131 | + os.path.join(orig_yum_var_dir, os.path.basename(backed_up_yum_var_filepath)) |
| 132 | + ) |
| 133 | + backup.backup_control.push(restored_file) |
0 commit comments