From 691d1d999a90f77034378059614cc2542e74238d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20Bia=C5=82obrzecki?= Date: Wed, 9 Mar 2022 09:11:04 +0100 Subject: [PATCH] * Add failed command to error message (#3001) --- .../src/command/apt_cache.py | 2 +- .../src/command/command.py | 20 +++++++++++-------- .../src/command/dnf_repoquery.py | 6 +++--- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/ansible/playbooks/roles/repository/files/download-requirements/src/command/apt_cache.py b/ansible/playbooks/roles/repository/files/download-requirements/src/command/apt_cache.py index 3db702fb23..02146000bf 100644 --- a/ansible/playbooks/roles/repository/files/download-requirements/src/command/apt_cache.py +++ b/ansible/playbooks/roles/repository/files/download-requirements/src/command/apt_cache.py @@ -34,7 +34,7 @@ def __get_package_candidate_version(self, package: str, version: str = '') -> st if 'Candidate' in line: return line.split(': ')[-1] - raise CriticalError(f'Candidate for {package} not found.') + raise CriticalError(f'Candidate for {package} not found, command: `{self.command()}`') def get_package_info(self, package: str, version: str = '') -> Dict[str, str]: """ diff --git a/ansible/playbooks/roles/repository/files/download-requirements/src/command/command.py b/ansible/playbooks/roles/repository/files/download-requirements/src/command/command.py index bfb7eb6b6b..1203bdcaf5 100644 --- a/ansible/playbooks/roles/repository/files/download-requirements/src/command/command.py +++ b/ansible/playbooks/roles/repository/files/download-requirements/src/command/command.py @@ -14,6 +14,7 @@ def __init__(self, process_name: str, retries: int, pipe_args: List[str] = None) self.__proc_name: str = process_name self.__retries: int = retries self.__pipe_args: List[str] = pipe_args # used for __or__ + self.__command: str = '' def name(self) -> str: return self.__proc_name @@ -21,6 +22,9 @@ def name(self) -> str: def pipe_args(self) -> List[str]: return self.__pipe_args or [] + def command(self) -> str: + return self.__command + def run(self, args: List[str], capture_output: bool = True, accept_nonzero_returncode: bool = False) -> subprocess.CompletedProcess: @@ -35,13 +39,15 @@ def run(self, args: List[str], process_args = [self.__proc_name] process_args.extend(args) + self.__command = f'{self.__proc_name} {" ".join(args)}' + additional_args = {'encoding': 'utf-8'} if capture_output: additional_args['stdout'] = subprocess.PIPE additional_args['stderr'] = subprocess.PIPE for count in range(self.__retries): - logging.debug(f'[{count + 1}/{self.__retries}] Running: {self.__proc_name} {" ".join(args)} ') + logging.debug(f'[{count + 1}/{self.__retries}] Running: `{self.__command}` ') process = subprocess.run(process_args, **additional_args) @@ -53,7 +59,7 @@ def run(self, args: List[str], logging.warn(process.stderr) - raise CriticalError('Retries count reached maximum!') + raise CriticalError(f'Retries count reached maximum, command: `{self.__command}`') def __or__(self, command) -> str: """ @@ -65,10 +71,10 @@ def __or__(self, command) -> str: """ lproc_name = f'{self.__proc_name} {" ".join(self.__pipe_args)}' rproc_name = f'{command.name()} {" ".join(command.pipe_args())}' - whole_process_name = f'{lproc_name} | {rproc_name}' + self.__command = f'{lproc_name} | {rproc_name}' for count in range(self.__retries): - logging.debug(f'[{count + 1}/{self.__retries}] Running: {whole_process_name}') + logging.debug(f'[{count + 1}/{self.__retries}] Running: `{self.__command}`') lproc = subprocess.Popen([self.__proc_name] + self.__pipe_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -82,7 +88,7 @@ def __or__(self, command) -> str: logging.warn(lproc.stderr if not lproc.returncode == 0 else rproc.stderr) - raise CriticalError('Retries count reached maximum!') + raise CriticalError(f'Retries count reached maximum, command: `{self.__command}`') def _run_and_filter(self, args: List[str]) -> List[str]: """ @@ -92,6 +98,4 @@ def _run_and_filter(self, args: List[str]) -> List[str]: :returns: filtered output lines from the subprocess stdout """ raw_output = self.run(args).stdout - - elems: List[str] = [elem for elem in raw_output.split('\n')] - return list(filter(lambda elem: elem != '', elems)) # filter empty lines + return list(filter(lambda elem: elem != '', raw_output.split('\n'))) # filter empty lines diff --git a/ansible/playbooks/roles/repository/files/download-requirements/src/command/dnf_repoquery.py b/ansible/playbooks/roles/repository/files/download-requirements/src/command/dnf_repoquery.py index 6e562b219e..22ac19280b 100644 --- a/ansible/playbooks/roles/repository/files/download-requirements/src/command/dnf_repoquery.py +++ b/ansible/playbooks/roles/repository/files/download-requirements/src/command/dnf_repoquery.py @@ -76,9 +76,9 @@ def query(self, package: str, queryformat: str, archlist: List[str]) -> List[str def output_handler(output: str): """ In addition to errors, handle missing packages """ if not output: - raise PackageNotfound(f'repoquery failed for package `{package}`, reason: package not found') + raise PackageNotfound(f'repoquery failed for package `{package}`, reason: package not found, command: `{self.command()}`') elif 'error' in output: - raise CriticalError(f'repoquery failed for package `{package}`, reason: `{output}`') + raise CriticalError(f'repoquery failed for package `{package}`, reason: `{output}`, command: `{self.command()}`') return self.__query(package, queryformat, archlist, False, False, output_handler) @@ -97,6 +97,6 @@ def get_dependencies(self, package: str, queryformat: str, archlist: List[str]) def output_handler(output: str): """ Handle errors """ if 'error' in output: - raise CriticalError(f'repoquery failed for package `{package}`, reason: `{output}`') + raise CriticalError(f'repoquery failed for package `{package}`, reason: `{output}`, command: `{self.command()}`') return self.__query(package, queryformat, archlist, True, True, output_handler)