From 507d0bba80242318cd9a797d7cce7a13e0fe4b5b Mon Sep 17 00:00:00 2001 From: to-bar <46519524+to-bar@users.noreply.github.com> Date: Thu, 2 Jun 2022 10:57:59 +0000 Subject: [PATCH] Ensure tar is not uninstalled too early --- .../src/mode/base_mode.py | 17 +++++-- .../src/mode/debian_family_mode.py | 12 +++-- .../src/mode/red_hat_family_mode.py | 45 ++++++++++++------- 3 files changed, 52 insertions(+), 22 deletions(-) diff --git a/ansible/playbooks/roles/repository/files/download-requirements/src/mode/base_mode.py b/ansible/playbooks/roles/repository/files/download-requirements/src/mode/base_mode.py index 3ae4ed2fba..980841243b 100644 --- a/ansible/playbooks/roles/repository/files/download-requirements/src/mode/base_mode.py +++ b/ansible/playbooks/roles/repository/files/download-requirements/src/mode/base_mode.py @@ -209,9 +209,15 @@ def _cleanup(self): """ pass - def _clean_up_repository_files(self): + def _cleanup_packages(self): """ - Additional routines before unpacking backup to remove repository files under the /etc directory. + Remove installed packages. + """ + pass + + def _remove_repository_files(self): + """ + Additional routines before unpacking backup to remove all repository files under the /etc directory. """ pass @@ -221,7 +227,7 @@ def __restore_repositories(self): """ if self._cfg.repos_backup_file.exists() and self._cfg.repos_backup_file.stat().st_size: logging.info('Restoring repository files...') - self._clean_up_repository_files() + self._remove_repository_files() self._tools.tar.unpack(filename=self._cfg.repos_backup_file, directory=Path('/'), absolute_names=True, @@ -291,4 +297,9 @@ def run(self): self._cleanup() logging.info('Done running cleanup.') + # requires tar but has to be run after cleanup self.__restore_repositories() + + logging.info('Cleaning up installed packages...') + self._cleanup_packages() + logging.info('Done cleaning up installed packages.') diff --git a/ansible/playbooks/roles/repository/files/download-requirements/src/mode/debian_family_mode.py b/ansible/playbooks/roles/repository/files/download-requirements/src/mode/debian_family_mode.py index 405be76ddd..7ee8af4290 100644 --- a/ansible/playbooks/roles/repository/files/download-requirements/src/mode/debian_family_mode.py +++ b/ansible/playbooks/roles/repository/files/download-requirements/src/mode/debian_family_mode.py @@ -16,6 +16,7 @@ def __init__(self, config: Config): super().__init__(config) self.__create_repo_paths() self.__installed_packages: List[str] = [] + self.__repos_config_dir: Path = Path('/etc/apt/sources.list.d') def __create_repo_paths(self): for repo in self._repositories.keys(): @@ -126,9 +127,12 @@ def _download_grafana_dashboard(self, dashboard: str, output_file: Path): def _download_crane_binary(self, url: str, dest: Path): self._tools.wget.download(url, dest) - def _clean_up_repository_files(self): - for repofile in Path('/etc/apt/sources.list.d').iterdir(): - repofile.unlink() + def _remove_repository_files(self): + logging.debug(f'Removing files from {self.__repos_config_dir}...') + for repo_file in self.__repos_config_dir.iterdir(): + logging.debug(f'- {repo_file.name}') + repo_file.unlink() + logging.debug('Done removing files.') def _cleanup(self): # cleaning up 3rd party repositories @@ -136,6 +140,6 @@ def _cleanup(self): if data['path'].exists(): data['path'].unlink() - # remove installed packages + def _cleanup_packages(self): for package in self.__installed_packages: self._tools.apt.remove(package) diff --git a/ansible/playbooks/roles/repository/files/download-requirements/src/mode/red_hat_family_mode.py b/ansible/playbooks/roles/repository/files/download-requirements/src/mode/red_hat_family_mode.py index bf6490f120..7cf52e96c2 100644 --- a/ansible/playbooks/roles/repository/files/download-requirements/src/mode/red_hat_family_mode.py +++ b/ansible/playbooks/roles/repository/files/download-requirements/src/mode/red_hat_family_mode.py @@ -19,8 +19,9 @@ def __init__(self, config: Config): self.__all_queried_packages: Set[str] = set() self.__archs: List[str] = [config.os_arch.value, 'noarch'] self.__base_packages: List[str] = ['curl', 'python3-dnf-plugins-core', 'wget', 'tar'] - self.__installed_packages: List[str] = [] self.__dnf_cache_path: Path = Path('/var/cache/dnf') + self.__installed_packages: List[str] = [] + self.__repos_config_dir: Path = Path('/etc/yum.repos.d') try: dnf_config = configparser.ConfigParser() @@ -111,22 +112,33 @@ def _add_third_party_repositories(self): def __remove_dnf_cache_for_custom_repos(self): # clean metadata for upgrades (when the same package can be downloaded from changed repo) - repocaches: List[str] = list(self.__dnf_cache_path.iterdir()) + cache_paths: List[str] = list(self.__dnf_cache_path.iterdir()) id_names = [ '2ndquadrant', 'docker-ce', 'epel', - ] + [self._repositories[key]['id'] for key in self._repositories.keys()] + ] + [repo['id'] for _, repo in self._repositories.items()] + + matched_cache_paths: List[str] = [] + + for path in cache_paths: + for repo_name in id_names: + if path.name.startswith(repo_name): + matched_cache_paths.append(path) + break + + if matched_cache_paths: + matched_cache_paths.sort() + logging.debug(f'Removing DNF cache files from {self.__dnf_cache_path}...') - for repocache in repocaches: - matched_ids = [repocache.name.startswith(repo_name) for repo_name in id_names] - if any(matched_ids): + for path in matched_cache_paths: + logging.debug(f'- {path.name}') try: - if repocache.is_dir(): - shutil.rmtree(str(repocache)) + if path.is_dir(): + shutil.rmtree(str(path)) else: - repocache.unlink() + path.unlink() except FileNotFoundError: logging.debug('__remove_dnf_cache_for_custom_repos: cache directory already removed') @@ -212,14 +224,17 @@ def _download_grafana_dashboard(self, dashboard: str, output_file: Path): def _download_crane_binary(self, url: str, dest: Path): self._tools.wget.download(url, dest, additional_params=False) - def _clean_up_repository_files(self): - for repofile in Path('/etc/yum.repos.d').iterdir(): - repofile.unlink() + def _remove_repository_files(self): + logging.debug(f'Removing files from {self.__repos_config_dir}...') + for repo_file in self.__repos_config_dir.iterdir(): + logging.debug(f'- {repo_file.name}') + repo_file.unlink() + logging.debug('Done removing files.') def _cleanup(self): - # remove installed packages + self.__remove_dnf_cache_for_custom_repos() + + def _cleanup_packages(self): for package in self.__installed_packages: if self._tools.rpm.is_package_installed(package): self._tools.dnf.remove(package) - - self.__remove_dnf_cache_for_custom_repos()