From: Alfredo Deza Date: Tue, 25 Jul 2017 18:09:52 +0000 (-0400) Subject: ceph-volume: process: allow stdin to be used X-Git-Tag: ses5-milestone10~3^2~5^2~22 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=80d3e6785ca0ee45fcd6b0d19d9965c1c55dfc08;p=ceph.git ceph-volume: process: allow stdin to be used Signed-off-by: Alfredo Deza --- diff --git a/src/ceph-volume/ceph_volume/process.py b/src/ceph-volume/ceph_volume/process.py index 5008bc490c2d..bc5047a179c3 100644 --- a/src/ceph-volume/ceph_volume/process.py +++ b/src/ceph-volume/ceph_volume/process.py @@ -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