From 6d1f82283965cda027a58aa2f1635371852c851e Mon Sep 17 00:00:00 2001 From: BenjiReis Date: Tue, 19 Sep 2023 10:58:36 +0200 Subject: [PATCH] Display stderr and stdout when run_command fails Throwing a `CalledProcessError` would hide the messages when converted into a `Xenapi.Failure` use a generic `Exception` with a properly formed detail instead. Signed-off-by: BenjiReis --- SOURCES/etc/xapi.d/plugins/xcpngutils/__init__.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/SOURCES/etc/xapi.d/plugins/xcpngutils/__init__.py b/SOURCES/etc/xapi.d/plugins/xcpngutils/__init__.py index 2bed512..6da69b9 100644 --- a/SOURCES/etc/xapi.d/plugins/xcpngutils/__init__.py +++ b/SOURCES/etc/xapi.d/plugins/xcpngutils/__init__.py @@ -63,12 +63,20 @@ def log_unhandled_exception(origin, exception_type, exception_value, return logger +class ProcessException(subprocess.CalledProcessError): + def __init__(self, code, command, stdout, stderr): + super(ProcessException, self).__init__(code, command, output=stdout) + self.stderr = stderr + + def __str__(self): + return "Command '%s' failed with code: %d" % (self.cmd, self.returncode) + def run_command(command, check=True): process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) stdout, stderr = process.communicate() code = process.returncode if check and code != 0: - raise subprocess.CalledProcessError(code, command, None) + raise ProcessException(code, command, stdout, stderr) result = {'stdout': stdout, 'stderr': stderr, 'command': command, 'returncode': code} return result @@ -123,6 +131,11 @@ def wrapper(*args, **kwds): except EnvironmentError as e: message = e.strerror if e.strerror is not None else str(e.args) raise XenAPIPlugin.Failure(str(e.errno), [message, str(e.filename), traceback.format_exc()]) + except ProcessException as e: + raise_plugin_error( + e.returncode, str(e), + details="stdout: '%s', stderr: '%s'" % (e.output, e.stderr), backtrace=traceback.format_exc() + ) except Exception as e: raise_plugin_error('-1', str(e), backtrace=traceback.format_exc())