Skip to content

Commit

Permalink
Display stderr and stdout when run_command fails
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
benjamreis committed Aug 8, 2024
1 parent 9d9d1bf commit 337f331
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion SOURCES/etc/xapi.d/plugins/xcpngutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,21 @@ def log_unhandled_exception(origin, exception_type, exception_value,
return logger


class ProcessException(subprocess.CalledProcessError):
def __init__(self, code, command, stderr, stdout):
super(ProcessException, self).__init__(code, command, None)
self.stderr = stderr
self.stdout = stdout

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, stderr, stdout)
result = {'stdout': stdout, 'stderr': stderr, 'command': command, 'returncode': code}
return result

Expand Down Expand Up @@ -123,6 +132,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.stdout, e.stderr), backtrace=traceback.format_exc()
)
except Exception as e:
raise_plugin_error('-1', str(e), backtrace=traceback.format_exc())

Expand Down

0 comments on commit 337f331

Please sign in to comment.