Add option for real time Terraform output
This commit is contained in:
parent
376c71d1ea
commit
7a553c569f
2 changed files with 30 additions and 4 deletions
10
README.md
10
README.md
|
@ -98,6 +98,16 @@ In python-terraform:
|
|||
tf = terraform(working_dir='/home/test')
|
||||
tf.fmt(diff=True)
|
||||
|
||||
# Terraform Output
|
||||
|
||||
By default, stdout and stderr are captured and returned. This causes the application to appear to hang. To print terraform output in real time, provide the `capture_output` option with any value other than `None`. This will cause the output of terraform to be printed to the terminal in real time. The value of `stdout` and `stderr` below will be `None`.
|
||||
|
||||
|
||||
from python_terraform import Terraform
|
||||
t = Terraform()
|
||||
return_code, stdout, stderr = t.<cmd_name>(capture_output=False)
|
||||
|
||||
|
||||
## default values
|
||||
for apply/plan/destroy command, assign with following default value to make
|
||||
caller easier in python
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
|
||||
import subprocess
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import logging
|
||||
import tempfile
|
||||
|
@ -202,15 +203,27 @@ class Terraform(object):
|
|||
if it's a flag could be used multiple times, assign list to it's value
|
||||
if it's a "var" variable flag, assign dictionary to it
|
||||
if a value is None, will skip this option
|
||||
if the option 'capture_output' is passed (with any value other than
|
||||
True), terraform output will be printed to stdout/stderr and
|
||||
"None" will be returned as out and err.
|
||||
:return: ret_code, out, err
|
||||
"""
|
||||
|
||||
capture_output = kwargs.pop('capture_output', True)
|
||||
if capture_output is True:
|
||||
stderr = subprocess.PIPE
|
||||
stdout = subprocess.PIPE
|
||||
else:
|
||||
stderr = sys.stderr
|
||||
stdout = sys.stdout
|
||||
|
||||
cmd_string = self.generate_cmd_string(cmd, *args, **kwargs)
|
||||
log.debug('command: {c}'.format(c=cmd_string))
|
||||
|
||||
working_folder = self.working_dir if self.working_dir else None
|
||||
|
||||
p = subprocess.Popen(cmd_string, stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE, shell=True,
|
||||
p = subprocess.Popen(cmd_string, stdout=stdout,
|
||||
stderr=stderr, shell=True,
|
||||
cwd=working_folder)
|
||||
out, err = p.communicate()
|
||||
ret_code = p.returncode
|
||||
|
@ -222,7 +235,10 @@ class Terraform(object):
|
|||
log.warn('error: {e}'.format(e=err))
|
||||
|
||||
self.temp_var_files.clean_up()
|
||||
if capture_output is True:
|
||||
return ret_code, out.decode('utf-8'), err.decode('utf-8')
|
||||
else:
|
||||
return ret_code, None, None
|
||||
|
||||
def output(self, name):
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue