@@ -102,7 +102,8 @@ def parse_args(init_vars=None, tf_vars=None, targets=None, **kw):
102
102
for arg in _TG_BOOL_ARGS if kw .get (f"tg_{ arg } " )]
103
103
for arg in _TG_KV_ARGS :
104
104
if kw .get (f"tg_{ arg } " ):
105
- cmd_args += [f'--terragrunt-{ arg .replace ("_" , "-" )} ' , kw [f"tg_{ arg } " ]]
105
+ cmd_args += [f'--terragrunt-{ arg .replace ("_" , "-" )} ' ,
106
+ kw [f"tg_{ arg } " ]]
106
107
if kw .get ('tg_parallelism' ):
107
108
cmd_args .append (f'--terragrunt-parallelism { kw ["tg_parallelism" ]} ' )
108
109
if isinstance (kw .get ('tg_override_attr' ), dict ):
@@ -296,7 +297,8 @@ def __init__(self, tfdir, basedir=None, binary='terraform', env=None):
296
297
self .env = os .environ .copy ()
297
298
self .tg_run_all = False
298
299
self ._plan_formatter = lambda out : TerraformPlanOutput (json .loads (out ))
299
- self ._output_formatter = lambda out : TerraformValueDict (json .loads (out ))
300
+ self ._output_formatter = lambda out : TerraformValueDict (
301
+ json .loads (out ))
300
302
if env is not None :
301
303
self .env .update (env )
302
304
@@ -324,11 +326,13 @@ def remove_readonly(func, path, excinfo):
324
326
for tg_dir in glob .glob (path , recursive = True ):
325
327
if os .path .isdir (tg_dir ):
326
328
shutil .rmtree (tg_dir , onerror = remove_readonly )
327
- _LOGGER .debug ('Restoring original TF files after prevent destroy changes' )
329
+ _LOGGER .debug (
330
+ 'Restoring original TF files after prevent destroy changes' )
328
331
if restore_files :
329
332
for bkp_file in Path (tfdir ).rglob ('*.bkp' ):
330
333
try :
331
- shutil .copy (str (bkp_file ), f'{ str (bkp_file ).strip (".bkp" )} ' )
334
+ shutil .copy (str (bkp_file ),
335
+ f'{ str (bkp_file ).strip (".bkp" )} ' )
332
336
except (IOError , OSError ):
333
337
_LOGGER .exception (
334
338
f'Unable to restore terraform file { bkp_file .resolve ()} ' )
@@ -378,8 +382,9 @@ def setup(self, extra_files=None, plugin_dir=None, init_vars=None,
378
382
with open (tf_file , 'r' ) as src :
379
383
terraform = src .read ()
380
384
with open (tf_file , 'w' ) as src :
381
- terraform = re .sub (r'prevent_destroy\s+=\s+true' ,
382
- 'prevent_destroy = false' , terraform )
385
+
386
+ terraform = re .sub (
387
+ r'prevent_destroy\s+=\s+true' , 'prevent_destroy = false' , terraform )
383
388
src .write (terraform )
384
389
except (OSError , IOError ):
385
390
_LOGGER .exception (
@@ -450,7 +455,8 @@ def plan(self, input=False, color=False, refresh=True, tf_vars=None,
450
455
try :
451
456
return self ._plan_formatter (result .out )
452
457
except json .JSONDecodeError as e :
453
- raise TerraformTestError ('Error decoding plan output: {}' .format (e ))
458
+ raise TerraformTestError (
459
+ 'Error decoding plan output: {}' .format (e ))
454
460
455
461
def apply (self , input = False , color = False , auto_approve = True ,
456
462
tf_vars = None , targets = None , tf_var_file = None , ** kw ):
@@ -514,21 +520,37 @@ def execute_command(self, cmd, *cmd_args):
514
520
cmdline = [self .binary , * self ._tg_ra (), cmd ]
515
521
cmdline += cmd_args
516
522
_LOGGER .info (cmdline )
523
+ retcode = None
524
+ full_output_lines = []
517
525
try :
518
- p = subprocess .Popen (cmdline , stdout = subprocess .PIPE ,
519
- stderr = subprocess .PIPE , cwd = self .tfdir , env = self .env )
526
+ p = subprocess .Popen (cmdline ,
527
+ stdout = subprocess .PIPE ,
528
+ stderr = subprocess .PIPE ,
529
+ cwd = self .tfdir ,
530
+ env = self .env ,
531
+ universal_newlines = True ,
532
+ encoding = 'utf-8' ,
533
+ errors = 'ignore' )
534
+ while True :
535
+ output = p .stdout .readline ()
536
+ if output == '' and p .poll () is not None :
537
+ break
538
+ if output :
539
+ _LOGGER .info (output .strip ())
540
+ full_output_lines .append (output )
541
+ retcode = p .poll ()
542
+ p .stdout .close ()
543
+ p .wait ()
520
544
except FileNotFoundError as e :
521
545
raise TerraformTestError ('Terraform executable not found: %s' % e )
522
546
out , err = p .communicate ()
523
- out = out .decode ('utf-8' , errors = 'ignore' )
524
- err = err .decode ('utf-8' , errors = 'ignore' )
525
- retcode = p .returncode
547
+ full_output = "" .join (full_output_lines )
526
548
if retcode == 1 :
527
549
message = 'Error running command {command}: {retcode} {out} {err}' .format (
528
- command = cmd , retcode = retcode , out = out , err = err )
550
+ command = cmd , retcode = retcode , out = full_output , err = err )
529
551
_LOGGER .critical (message )
530
552
raise TerraformTestError (message )
531
- return TerraformCommandOutput (retcode , out , err )
553
+ return TerraformCommandOutput (retcode , full_output , err )
532
554
533
555
def _tg_ra (self ) -> List [str ]:
534
556
"""if run_all return ['run-all'] else [] """
0 commit comments