Skip to content

Commit

Permalink
Ensure tar is not uninstalled too early
Browse files Browse the repository at this point in the history
  • Loading branch information
to-bar committed Jun 2, 2022
1 parent 63e48b9 commit 507d0bb
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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.')
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -126,16 +127,19 @@ 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
for data in self._repositories.values():
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)
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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')

Expand Down Expand Up @@ -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()

0 comments on commit 507d0bb

Please sign in to comment.