From 2a79e8c0bf88dfceed7ebf9cdf610441686860be Mon Sep 17 00:00:00 2001 From: tomasfratrik Date: Wed, 17 Jul 2024 12:12:50 +0200 Subject: [PATCH] Fix ModelViolationError while parsing repo files This error occurs when repo file has invalid definition, specifically the 'name' entry of the config file. Jira: RHEL-19249 --- .../actors/systemfacts/libraries/systemfacts.py | 17 ++++++++++++++++- .../common/libraries/repofileutils.py | 12 +++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/repos/system_upgrade/common/actors/systemfacts/libraries/systemfacts.py b/repos/system_upgrade/common/actors/systemfacts/libraries/systemfacts.py index d1eeb28cdc..10a198da11 100644 --- a/repos/system_upgrade/common/actors/systemfacts/libraries/systemfacts.py +++ b/repos/system_upgrade/common/actors/systemfacts/libraries/systemfacts.py @@ -217,7 +217,22 @@ def get_sysctls_status(): def get_repositories_status(): """ Get a basic information about YUM repositories installed in the system """ - return RepositoriesFacts(repositories=repofileutils.get_parsed_repofiles()) + try: + return RepositoriesFacts(repositories=repofileutils.get_parsed_repofiles()) + except repofileutils.InvalidRepoDefinition as e: + raise StopActorExecutionError( + message='Invalid repository definition', + details={ + 'details': str(e), + 'file': e.repofile, + 'repoid': e.repoid, + 'hint': 'To resolve this issue, please visit: {url}' + .format( + url='https://access.redhat.com/solutions/3185891' + ) + } + ) + def get_selinux_status(): diff --git a/repos/system_upgrade/common/libraries/repofileutils.py b/repos/system_upgrade/common/libraries/repofileutils.py index a563be5204..ed996cada7 100644 --- a/repos/system_upgrade/common/libraries/repofileutils.py +++ b/repos/system_upgrade/common/libraries/repofileutils.py @@ -11,6 +11,13 @@ api.current_logger().warning('repofileutils.py: failed to import dnf') +class InvalidRepoDefinition(Exception): + def __init__(self, message, repofile=None, repoid=None): + super().__init__(message) + self.repofile = repofile + self.repoid = repoid + + def _parse_repository(repoid, repo_data): def asbool(x): return x == '1' @@ -38,7 +45,10 @@ def parse_repofile(repofile): with open(repofile, mode='r') as fp: cp = utils.parse_config(fp, strict=False) for repoid in cp.sections(): - data.append(_parse_repository(repoid, dict(cp.items(repoid)))) + try: + data.append(_parse_repository(repoid, dict(cp.items(repoid)))) + except Exception as e: + raise InvalidRepoDefinition(e, repofile=repofile, repoid=repoid) return RepositoryFile(file=repofile, data=data)