common.py: catch stderr in exec_cmd 61/14561/3
authorJosep Puigdemont <josep.puigdemont@enea.com>
Wed, 4 May 2016 12:27:23 +0000 (14:27 +0200)
committerJonas Bjurel <jonas.bjurel@ericsson.com>
Tue, 31 May 2016 19:01:59 +0000 (19:01 +0000)
When running commands with exec_cmd(), the stderr of the command is
sent to /dev/null and ignored, and only stdout is retrieved. Thus, when
a command fails and check is enabled, only the output of stdout is
presented to the user, which normally holds no information about the
error.
In this patch we retrieve stderr, and when an error occurs, an exception
is raised with that message.

Fixes https://jira.opnfv.org/browse/FUEL-142

Change-Id: I3940e1a43963a6abec362481b1d4ce7bd7cb816d
Signed-off-by: Josep Puigdemont <josep.puigdemont@enea.com>
deploy/common.py

index 787a21a..41b4e27 100644 (file)
@@ -38,20 +38,20 @@ LOG.addHandler(out_handler)
 os.chmod(LOGFILE, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
 
 def exec_cmd(cmd, check=True):
-    nul_f = open(os.devnull, 'w')
     process = subprocess.Popen(cmd,
                                stdout=subprocess.PIPE,
-                               stderr=nul_f,
+                               stderr=subprocess.PIPE,
                                shell=True)
-    nul_f.close()
-    response = process.communicate()[0].strip()
+    (response, stderr) = process.communicate()
     return_code = process.returncode
+    response = response.strip()
     if check:
         if return_code > 0:
+            stderr = stderr.strip()
             print "Failed command: " + str(cmd)
-            print "Command returned response: " + str(response)
+            print "Command returned response: " + str(stderr)
             print "Command return code: " + str(return_code)
-            raise Exception(response)
+            raise Exception(stderr)
         else:
             print "Command: " + str(cmd)
             print str(response)