Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow piped stdin in run_subprocess_with_logging #863

Merged
merged 3 commits into from
Jan 13, 2020

Conversation

dliappis
Copy link
Contributor

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.

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.
@dliappis dliappis added enhancement Improves the status quo :misc Changes that don't affect users directly: linter fixes, test improvements, etc. labels Jan 10, 2020
@dliappis dliappis added this to the 1.4.0 milestone Jan 10, 2020
@@ -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):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find any existing tests for this, so deferred -- for now -- writing new ones.

And also only log piped output if it exists
# Conflicts:
#	esrally/utils/process.py
@dliappis dliappis force-pushed the allow-pipes-with-logging-subprocess branch from d6b778c to 2f263cb Compare January 10, 2020 15:03
@dliappis
Copy link
Contributor Author

Rebased #859 into this one.

Copy link
Member

@danielmitterdorfer danielmitterdorfer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left one suggestion to simplify the code.

env=env,
stdin=stdin if stdin else None,
preexec_fn=pre_exec) as command_line_process:
if stdin:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about we unify the block's body to:

stdout, _ = command_line_process.communicate()
if stdout:
    logger.log(level=level, msg=stdout)

Note: In any case we should also pass universal_newlines=True to subprocess.Popen to ensure line-endings are properly treated. This did not work before as well (meaning we see \n at the end of each line in the log file) but wasn't so much of a problem because we wrote a log message for each line.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#communicate() will wait until the process completes. As a result the existing code (including looping through command_line_process.stdout.readline() and "streaming" the results) won't work.

Or are you suggesting replacing the everything we currently have within the with: block with the above 3 lines?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am suggesting that we replace the whole block with the above three lines. Given the we use it only for short-lived subprocesses anyway, I think it is ok to "lose" the streaming aspect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! With this approach in mind I pushed 0631661

Copy link
Member

@danielmitterdorfer danielmitterdorfer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@dliappis dliappis merged commit acdae6d into elastic:master Jan 13, 2020
@dliappis dliappis deleted the allow-pipes-with-logging-subprocess branch January 13, 2020 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improves the status quo :misc Changes that don't affect users directly: linter fixes, test improvements, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants