ignore_errors parameter supported in shell plugin

This commit is contained in:
Fabio Manganiello 2018-10-20 17:56:36 +02:00
parent 4fce1dd2e1
commit fc7fa37010

View file

@ -9,13 +9,14 @@ class ShellPlugin(Plugin):
""" """
@action @action
def exec(self, cmd): def exec(self, cmd, ignore_errors=False):
""" """
Execute a command. Execute a command.
:param cmd: Command to execute :param cmd: Command to execute
:type cmd: str :type cmd: str
:param ignore_errors: If set, then any errors in the command execution will be ignored. Otherwise a RuntimeError will be thrown (default value: False)
:returns: A response object where the ``output`` field will contain the command output as a string, and the ``errors`` field will contain whatever was sent to stderr. :returns: A response object where the ``output`` field will contain the command output as a string, and the ``errors`` field will contain whatever was sent to stderr.
""" """
@ -23,6 +24,10 @@ class ShellPlugin(Plugin):
return subprocess.check_output( return subprocess.check_output(
cmd, stderr=subprocess.STDOUT, shell=True).decode('utf-8') cmd, stderr=subprocess.STDOUT, shell=True).decode('utf-8')
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
if ignore_errors:
self.logger.warning('Command {} failed with error: {}'.format(
cmd, e.output.decode('utf-8')))
else:
raise RuntimeError(e.output.decode('utf-8')) raise RuntimeError(e.output.decode('utf-8'))