Skip to content

Commit

Permalink
Allow piped stdin in run_subprocess_with_logging
Browse files Browse the repository at this point in the history
Sometimes we need to used as stdin the output from another command
(e.g. pass secure settings to elasticsearch-keystore tool is
one example).

This commit optionally allows passing stdin from another subprocess
to run_subprocess_with_logging.
  • Loading branch information
dliappis committed Jan 10, 2020
1 parent 8c59b39 commit 43d5362
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions esrally/utils/process.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,15 @@ def exit_status_as_bool(runnable, quiet=False):
return False


def run_subprocess_with_logging(command_line, header=None, level=logging.INFO, env=None):
def run_subprocess_with_logging(command_line, header=None, level=logging.INFO, stdin=None, env=None):
"""
Runs the provided command line in a subprocess. All output will be captured by a logger.
:param command_line: The command line of the subprocess to launch.
:param header: An optional header line that should be logged (this will be logged on info level, regardless of the defined log level).
:param level: The log level to use for output (default: logging.INFO).
:param stdin: The stdout object returned by subprocess.Popen(stdout=PIPE) allowing chaining of shell operations with pipes
(default: None).
:param env: Use specific environment variables (default: None).
:return: The process exit code as an int.
"""
Expand All @@ -75,14 +77,21 @@ def run_subprocess_with_logging(command_line, header=None, level=logging.INFO, e
command_line_args = shlex.split(command_line)
if header is not None:
logger.info(header)
with subprocess.Popen(command_line_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env) as command_line_process:
has_output = True
while has_output:
line = command_line_process.stdout.readline()
if line:
logger.log(level=level, msg=line)
else:
has_output = False

with subprocess.Popen(command_line_args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, env=env,
stdin=stdin if stdin else None) as command_line_process:
if stdin:
output = command_line_process.communicate()
logger.log(level=level, msg=output[0])
else:
has_output = True
while has_output:
line = command_line_process.stdout.readline()
if line:
logger.log(level=level, msg=line)
else:
has_output = False

logger.debug("Subprocess [%s] finished with return code [%s].", command_line, str(command_line_process.returncode))
return command_line_process.returncode

Expand Down

0 comments on commit 43d5362

Please sign in to comment.