1
0
mirror of https://github.com/CumulusNetworks/ifupdown2.git synced 2024-05-06 15:54:50 +00:00

ifupdown: utils: user command output on stdout

Ticket: None
Reviewed By: Roopa, Nikhil G
Testing Done:

$ cat /etc/network/interfaces
auto eth0
iface eth0 inet dhcp
      up ls -l
$

I realized that user command's output wasn't on stdout but in a pipe
This commit fixes this case, the user cmd output is now on stdout.

Signed-off-by: Julien Fortin <julien@cumulusnetworks.com>
This commit is contained in:
Julien Fortin
2016-08-10 17:48:18 +02:00
parent 8d7584a093
commit d9bb082084

View File

@@ -171,7 +171,8 @@ class utils():
close_fds=False,
stdout=True,
stdin=None,
stderr=subprocess.STDOUT):
stderr=subprocess.STDOUT,
user_cmd=False):
"""
exec's commands using subprocess Popen
Args:
@@ -184,20 +185,30 @@ class utils():
return ''
cmd_output = None
if user_cmd:
stdout = True
elif stdout:
stdout = subprocess.PIPE
else:
stdout = cls.DEVNULL
try:
ch = subprocess.Popen(cmd,
env=env,
shell=shell,
close_fds=close_fds,
stdin=subprocess.PIPE if stdin else None,
stdout=subprocess.PIPE if stdout else cls.DEVNULL,
stdout=stdout,
stderr=stderr)
utils.enable_subprocess_signal_forwarding(ch, signal.SIGINT)
if stdout or stdin:
cmd_output = ch.communicate(input=stdin)[0]
cmd_returncode = ch.wait()
except Exception as e:
raise Exception('cmd \'%s\' failed (%s)' % (' '.join(cmd), str(e)))
if user_cmd:
raise Exception('cmd \'%s\' failed (%s)' % (cmd, str(e)))
else:
raise Exception('cmd \'%s\' failed (%s)' %
(' '.join(cmd), str(e)))
finally:
utils.disable_subprocess_signal_forwarding(signal.SIGINT)
if cmd_returncode != 0:
@@ -216,7 +227,8 @@ class utils():
close_fds=close_fds,
stdout=stdout,
stdin=stdin,
stderr=stderr)
stderr=stderr,
user_cmd=True)
@classmethod
def exec_command(cls, cmd, env=None, close_fds=False, stdout=True,