From 0f1fa4c041f3b0cd9cb2cfa52d4adb021831ff89 Mon Sep 17 00:00:00 2001 From: Darko Poljak Date: Mon, 18 Sep 2017 23:04:01 +0200 Subject: [PATCH] Handle init manifest error nicely. --- cdist/__init__.py | 36 ++++++++++++++++++++++++++++++++++++ cdist/core/manifest.py | 14 +++++++++----- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cdist/__init__.py b/cdist/__init__.py index 0022b40c..cc9c8875 100644 --- a/cdist/__init__.py +++ b/cdist/__init__.py @@ -125,6 +125,42 @@ type: {o.cdist_type.absolute_path}'''.format( return '\n'.join(output) +class InitialManifestError(Error): + """Something went wrong while executing initial manifest""" + def __init__(self, initial_manifest, stdout_path, stderr_path, subject=''): + self.initial_manifest = initial_manifest + self.stdout_path = stdout_path + self.stderr_path = stderr_path + if isinstance(subject, Error): + self.original_error = subject + else: + self.original_error = None + self.message = str(subject) + self.line_length = 74 + + @property + def stderr(self): + output = [] + label = "init" + ':stderr ' + if os.path.getsize(self.stderr_path) > 0: + output.append('{0:-<{1}}'.format(label, self.line_length)) + with open(self.stderr_path, 'r') as fd: + output.append(fd.read()) + return '\n'.join(output) + + def __str__(self): + output = [] + output.append(self.message) + output.append('''{label:-<{length}} +initial manifest: {im}'''.format( + label='---- initial manifest ', + length=self.line_length, + im=self.initial_manifest) + ) + output.append(self.stderr) + return '\n'.join(output) + + def file_to_list(filename): """Return list from \n seperated file""" if os.path.isfile(filename): diff --git a/cdist/core/manifest.py b/cdist/core/manifest.py index 12b5b005..a4277aa3 100644 --- a/cdist/core/manifest.py +++ b/cdist/core/manifest.py @@ -158,11 +158,15 @@ class Manifest(object): stdout_path = os.path.join(self.local.stdout_base_path, which) with open(stderr_path, 'ba+') as stderr, \ open(stdout_path, 'ba+') as stdout: - self.local.run_script( - initial_manifest, - env=self.env_initial_manifest(initial_manifest), - message_prefix=message_prefix, - stdout=stdout, stderr=stderr) + try: + self.local.run_script( + initial_manifest, + env=self.env_initial_manifest(initial_manifest), + message_prefix=message_prefix, + stdout=stdout, stderr=stderr) + except cdist.Error as e: + raise cdist.InitialManifestError(initial_manifest, stdout_path, + stderr_path, e) def env_type_manifest(self, cdist_object): type_manifest = os.path.join(self.local.type_path,