acertmgr: properly format action output

This commit is contained in:
Kishi85 2019-04-02 10:57:57 +02:00
parent 79b625619a
commit 4510aaf393
2 changed files with 22 additions and 5 deletions

View File

@ -185,10 +185,22 @@ def main():
try:
# Run actions in a shell environment (to allow shell syntax) as stated in the configuration
output = subprocess.check_output(action, shell=True, stderr=subprocess.STDOUT)
log("Executed '{}' successfully: {}".format(action, output))
logmsg = "Action succeeded: {}".format(action)
if len(output) > 0:
if getattr(output, 'decode', None):
# Decode function available? Use it to get a proper str
output = output.decode('utf-8')
logmsg += os.linesep + tools.indent(output, 18) # 18 = len("Action succeeded: ")
log(logmsg)
except subprocess.CalledProcessError as e:
log("Execution of '{}' failed with error '{}': {}".format(e.cmd, e.returncode, e.output), e,
error=True)
output = e.output
logmsg = "Action failed: ({}) {}".format(e.returncode, e.cmd)
if len(output) > 0:
if getattr(output, 'decode', None):
# Decode function available? Use it to get a proper str
output = output.decode('utf-8')
logmsg += os.linesep + tools.indent(output, 15) # 15 = len("Action failed: ")
log(logmsg, error=True)
exceptions.append(e)
deployment_success = False

View File

@ -31,6 +31,12 @@ class InvalidCertificateError(Exception):
pass
# @brief a simple, portable indent function
def indent(text, spaces=0):
ind = ' ' * spaces
return os.linesep.join(ind + line for line in text.splitlines())
# @brief wrapper for log output
def log(msg, exc=None, error=False, warning=False):
if error:
@ -49,8 +55,7 @@ def log(msg, exc=None, error=False, warning=False):
else:
formatted_exc = traceback.format_exception(type(exc), exc, getattr(exc, '__traceback__', None))
exc_string = ''.join(formatted_exc) if isinstance(formatted_exc, list) else str(formatted_exc)
indent = ' ' * len(prefix)
output += os.linesep + os.linesep.join(indent + line for line in exc_string.splitlines())
output += os.linesep + indent(exc_string, len(prefix))
if error or warning:
sys.stderr.write(output + os.linesep)