]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-disk: allow to exit nicely when using command_check_call
authorAlfredo Deza <adeza@redhat.com>
Fri, 3 Jun 2016 15:37:00 +0000 (11:37 -0400)
committerAlfredo Deza <adeza@redhat.com>
Fri, 9 Dec 2016 12:52:25 +0000 (07:52 -0500)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
src/ceph-disk/ceph_disk/main.py

index ccdc26bc87878049e45c075aa2d83e1a60cd84dd..d2d8d230853607f1779f27504bed57865a5c4779 100755 (executable)
@@ -461,19 +461,34 @@ def _bytes2str(string):
     return string.decode('utf-8') if isinstance(string, bytes) else string
 
 
-def command_check_call(arguments):
+def command_check_call(arguments, exit=False):
     """
     Safely execute a ``subprocess.check_call`` call making sure that the
     executable exists and raising a helpful error message if it does not.
 
+    When ``exit`` is set to ``True`` this helper will do a clean (sans
+    traceback) system exit.
     .. note:: This should be the preferred way of calling
     ``subprocess.check_call`` since it provides the caller with the safety net
     of making sure that executables *will* be found and will error nicely
     otherwise.
     """
     arguments = _get_command_executable(arguments)
-    LOG.info('Running command: %s', ' '.join(arguments))
-    return subprocess.check_call(arguments)
+    command = ' '.join(arguments)
+    LOG.info('Running command: %s', command)
+    try:
+        return subprocess.check_call(arguments)
+    except subprocess.CalledProcessError as error:
+        if exit:
+            if error.output:
+                LOG.error(error.output)
+            raise SystemExit(
+                "'{cmd}' failed with status code {returncode}".format(
+                    cmd=command,
+                    returncode=error.returncode,
+                )
+            )
+        raise
 
 
 def platform_distro():