Handle init manifest error nicely.

This commit is contained in:
Darko Poljak 2017-09-18 23:04:01 +02:00 committed by Darko Poljak
parent 5813a6bcf3
commit 0f1fa4c041
2 changed files with 45 additions and 5 deletions

View file

@ -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):

View file

@ -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,