From 087066687cf96690e08863a748a0dd24eed0e7e6 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Sat, 20 Apr 2019 14:26:09 +0200 Subject: [PATCH] Report explorer error better Fixes #766 --- cdist/__init__.py | 41 +++++++++++++++++++++++++++++++++++++++-- cdist/config.py | 9 ++++----- cdist/core/explorer.py | 30 +++++++++++++++++++++++------- 3 files changed, 66 insertions(+), 14 deletions(-) diff --git a/cdist/__init__.py b/cdist/__init__.py index 000a571c..c673b3ba 100644 --- a/cdist/__init__.py +++ b/cdist/__init__.py @@ -181,17 +181,40 @@ class CdistObjectError(CdistEntityError): params, stdout_paths, stderr_paths, subject) +class CdistObjectExplorerError(CdistEntityError): + """ + Something went wrong while working on a specific + cdist object explorer + """ + def __init__(self, cdist_object, explorer_name, explorer_path, + stderr_path, subject=''): + params = [ + ('object name', cdist_object.name, ), + ('object path', cdist_object.absolute_path, ), + ('object source', " ".join(cdist_object.source), ), + ('object type', os.path.realpath( + cdist_object.cdist_type.absolute_path), ), + ('explorer name', explorer_name, ), + ('explorer path', explorer_path, ), + ] + stdout_paths = [] + stderr_paths = [ + ('remote', stderr_path, ), + ] + super().__init__("explorer '{}' of object '{}'".format( + explorer_name, cdist_object.name), params, stdout_paths, + stderr_paths, subject) + + class InitialManifestError(CdistEntityError): """Something went wrong while executing initial manifest""" def __init__(self, initial_manifest, stdout_path, stderr_path, subject=''): params = [ ('path', initial_manifest, ), ] - stdout_paths = [] stdout_paths = [ ('init', stdout_path, ), ] - stderr_paths = [] stderr_paths = [ ('init', stderr_path, ), ] @@ -199,6 +222,20 @@ class InitialManifestError(CdistEntityError): stderr_paths, subject) +class GlobalExplorerError(CdistEntityError): + """Something went wrong while executing global explorer""" + def __init__(self, name, path, stderr_path, subject=''): + params = [ + ('name', name, ), + ('path', path, ), + ] + stderr_paths = [ + ('remote', stderr_path, ), + ] + super().__init__("global explorer '{}'".format(name), + params, [], stderr_paths, subject) + + def file_to_list(filename): """Return list from \n seperated file""" if os.path.isfile(filename): diff --git a/cdist/config.py b/cdist/config.py index 11c433db..fae2ff46 100644 --- a/cdist/config.py +++ b/cdist/config.py @@ -702,12 +702,11 @@ class Config(object): def object_prepare(self, cdist_object, transfer_type_explorers=True): """Prepare object: Run type explorer + manifest""" + self.log.verbose("Preparing object {}".format(cdist_object.name)) + self.log.verbose( + "Running manifest and explorers for " + cdist_object.name) + self.explorer.run_type_explorers(cdist_object, transfer_type_explorers) try: - self.log.verbose("Preparing object {}".format(cdist_object.name)) - self.log.verbose( - "Running manifest and explorers for " + cdist_object.name) - self.explorer.run_type_explorers(cdist_object, - transfer_type_explorers) self.manifest.run_type_manifest(cdist_object) cdist_object.state = core.CdistObject.STATE_PREPARED except cdist.Error as e: diff --git a/cdist/core/explorer.py b/cdist/core/explorer.py index 0b3d7b2e..26b7021a 100644 --- a/cdist/core/explorer.py +++ b/cdist/core/explorer.py @@ -109,10 +109,17 @@ class Explorer(object): self._run_global_explorers_parallel(out_path) def _run_global_explorer(self, explorer, out_path): - output = self.run_global_explorer(explorer) - path = os.path.join(out_path, explorer) - with open(path, 'w') as fd: - fd.write(output) + try: + path = os.path.join(out_path, explorer) + output = self.run_global_explorer(explorer) + with open(path, 'w') as fd: + fd.write(output) + except cdist.Error as e: + local_path = os.path.join(self.local.global_explorer_path, + explorer) + stderr_path = os.path.join(self.local.stderr_base_path, "remote") + raise cdist.GlobalExplorerError(explorer, local_path, stderr_path, + e) def _run_global_explorers_seq(self, out_path): self.log.debug("Running global explorers sequentially") @@ -186,11 +193,20 @@ class Explorer(object): self.log.trace("Transferring object parameters for object: %s", cdist_object.name) self.transfer_object_parameters(cdist_object) - for explorer in self.list_type_explorer_names(cdist_object.cdist_type): - output = self.run_type_explorer(explorer, cdist_object) + cdist_type = cdist_object.cdist_type + for explorer in self.list_type_explorer_names(cdist_type): self.log.trace("Running type explorer '%s' for object '%s'", explorer, cdist_object.name) - cdist_object.explorers[explorer] = output + try: + output = self.run_type_explorer(explorer, cdist_object) + cdist_object.explorers[explorer] = output + except cdist.Error as e: + path = os.path.join(self.local.type_path, + cdist_type.explorer_path, + explorer) + stderr_path = os.path.join(self.local.stderr_base_path, "remote") + raise cdist.CdistObjectExplorerError( + cdist_object, explorer, path, stderr_path, e) def run_type_explorer(self, explorer, cdist_object): """Run the given type explorer for the given object and return