]> git-server-git.apps.pok.os.sepia.ceph.com Git - teuthology.git/commitdiff
misc.py: sh helper to log command and output 530/head
authorLoic Dachary <ldachary@redhat.com>
Sun, 14 Jun 2015 07:35:35 +0000 (09:35 +0200)
committerLoic Dachary <ldachary@redhat.com>
Wed, 1 Jul 2015 16:50:52 +0000 (18:50 +0200)
It is the same as subprocess.check_output, only it log.debug the command
being run as well as its output.

Signed-off-by: Loic Dachary <loic@dachary.org>
teuthology/misc.py

index b27041e70137b53256bbc1eb9a7d951849cc618c..bf6f709b3b35e53bd29cc4f10cf4e549e90a7dc0 100644 (file)
@@ -10,6 +10,7 @@ import logging
 import configobj
 import getpass
 import socket
+import subprocess
 import sys
 import tarfile
 import time
@@ -1257,3 +1258,21 @@ def is_in_dict(searchkey, searchval, d):
         return True
     else:
         return searchval == val
+
+
+def sh(command):
+    """
+    Run the shell command and return the output in ascii (stderr and
+    stdout).  If the command fails, raise an exception. The command
+    and its output are logged, on success and on error.
+    """
+    log.debug(command)
+    output = ''
+    try:
+        output = subprocess.check_output(command, stderr=subprocess.STDOUT,
+                                         shell=True)
+    except subprocess.CalledProcessError as e:
+        log.exception(command + " error " + str(e.output))
+        raise e
+    log.debug(command + " output " + str(output))
+    return output.decode('utf-8')