]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
ceph-volume: process: allow stdin to be used
authorAlfredo Deza <adeza@redhat.com>
Tue, 25 Jul 2017 18:09:52 +0000 (14:09 -0400)
committerAlfredo Deza <adeza@redhat.com>
Fri, 4 Aug 2017 14:25:58 +0000 (10:25 -0400)
Signed-off-by: Alfredo Deza <adeza@redhat.com>
src/ceph-volume/ceph_volume/process.py

index 5008bc490c2d91de9feca6271ea0a783f1c301f2..bc5047a179c3033f2c06c5fa6edd0e1f9c4f7a9b 100644 (file)
@@ -58,18 +58,22 @@ def run(command, **kw):
     """
     stop_on_error = kw.pop('stop_on_error', True)
     command_msg = "Running command: %s" % ' '.join(command)
+    stdin = kw.pop('stdin', None)
     logger.info(command_msg)
     terminal.write(command_msg)
-    terminal_logging = kw.get('terminal_logging', True)
+    terminal_logging = kw.pop('terminal_logging', True)
 
     process = subprocess.Popen(
         command,
+        stdin=subprocess.PIPE,
         stdout=subprocess.PIPE,
         stderr=subprocess.PIPE,
         close_fds=True,
         **kw
     )
 
+    if stdin:
+        process.communicate(stdin)
     while True:
         reads, _, _ = select(
             [process.stdout.fileno(), process.stderr.fileno()],
@@ -107,25 +111,29 @@ def call(command, **kw):
     Useful when system calls are needed to act on output, and that same output
     shouldn't get displayed on the terminal.
 
-    :param terminal_logging: Log command to terminal, defaults to False
+    :param terminal_verbose: Log command output to terminal, defaults to False, and
+                             it is forcefully set to True if a return code is non-zero
     """
-    terminal_logging = kw.pop('terminal_logging', False)
+    terminal_verbose = kw.pop('terminal_verbose', False)
     command_msg = "Running command: %s" % ' '.join(command)
+    stdin = kw.pop('stdin', None)
     logger.info(command_msg)
-    if terminal_logging:
-        terminal.write(command_msg)
+    terminal.write(command_msg)
 
     process = subprocess.Popen(
         command,
         stdout=subprocess.PIPE,
         stderr=subprocess.PIPE,
+        stdin=subprocess.PIPE,
         close_fds=True,
         **kw
     )
-
+    if stdin:
+        stdout_stream, stderr_stream = process.communicate(stdin)
+    else:
+        stdout_stream = process.stdout.read()
+        stderr_stream = process.stderr.read()
     returncode = process.wait()
-    stdout_stream = process.stdout.read()
-    stderr_stream = process.stderr.read()
     if not isinstance(stdout_stream, str):
         stdout_stream = stdout_stream.decode('utf-8')
     if not isinstance(stderr_stream, str):
@@ -133,11 +141,16 @@ def call(command, **kw):
     stdout = stdout_stream.splitlines()
     stderr = stderr_stream.splitlines()
 
+    if returncode != 0:
+        # set to true so that we can log the stderr/stdout that callers would
+        # do anyway
+        terminal_verbose = True
+
     # the following can get a messed up order in the log if the system call
     # returns output with both stderr and stdout intermingled. This separates
     # that.
     for line in stdout:
-        log_output('stdout', line, False)
+        log_output('stdout', line, terminal_verbose)
     for line in stderr:
-        log_output('stderr', line, False)
+        log_output('stderr', line, terminal_verbose)
     return stdout, stderr, returncode