infrablocks / ruby_terraform

A simple Ruby wrapper for invoking terraform commands.
MIT License
109 stars 33 forks source link

Change STDOUT and STDERR to $stdout and $stderr respectively #12

Closed cbaines closed 6 years ago

cbaines commented 6 years ago

Use the variables, rather than the constants to enable more easily changing these for the duration of running the Terraform command. I'm hoping this will more easily enable capturing the output.

cbaines commented 6 years ago

Hey,

I've found ruby_terraform pretty useful, thanks for writing it and sharing it :)

I'm interested in capturing the output, in near real time if possible, as I'd like to make it available in a web interface. I think this change should enable changing $stdout and $stderr for the duration of the Terraform command, in a similar manor to how the Output command works.

I've just had the thought that maybe creating the Command instances manually (rather than using the helpers in the RubyTerraform module), might allow setting the stdout and stderr instance variables, so I'll try that out as well.

I think the real time aspect of this might be a bit difficult, as it looks like that would require modifying the lino gem to use something like the background functionality of open4, so I'd be interested if you have any thoughts on that?

Thanks again,

Chris

tobyclemson commented 6 years ago

Hi Christopher,

Thanks for the PR. I'll get this merged in and released now.

I'll have a think about streaming output. I wrote lino too so can always update that if necessary.

Toby

tobyclemson commented 6 years ago

Available in version 0.12.1

tobyclemson commented 6 years ago

So I think what you really want here is a way to inject the streams used for stdout/stderr into a command with the command defaulting to $stdout / $stderr if none is passed. That way you don't need to change globals to get the intended effect.

With respect to real time streaming, my understanding is that the output is already being streamed but it is buffered and flushed periodically such that it's not quite real time. This may be good enough.

Potentially this could be made more real time using the approach outlined in https://stackoverflow.com/questions/10409140/streaming-data-from-stdout. This would need to be a change to lino.

I'll create an issue for this so we can track it and capture ideas.

cbaines commented 6 years ago

Thanks for the PR. I'll get this merged in and released now.

I'll have a think about streaming output. I wrote lino too so can always update that if necessary.

Awesome, thanks Toby :+1:

So I think what you really want here is a way to inject the streams used for stdout/stderr into a command with the command defaulting to $stdout / $stderr if none is passed. That way you don't need to change globals to get the intended effect.

Yep, that would be nice. It would also make it easier to raise an error, say for the output Terraform command, where ruby_terraform needs access to the stdout.

With respect to real time streaming, my understanding is that the output is already being streamed but it is buffered and flushed periodically such that it's not quite real time. This may be good enough.

That's good enough for me at least. I just don't want to get the whole output when the command finishes.