@@ -1479,6 +1479,25 @@ def write_to_file(file_base_name: str, artifacts_section: dict):
14791479 except UploadError as e :
14801480 raise PublishArtifactsError from e
14811481
1482+ def _stop_env (self ):
1483+ if not os .path .exists (self ._work_dir ):
1484+ return 0 , '' , f'Working directory { self ._work_dir } does not exist'
1485+ self ._logger .info (
1486+ 'Destroying the environment %s...' ,
1487+ self .env_name ,
1488+ )
1489+ self ._logger .debug (
1490+ 'Running "terraform destroy --auto-approve" command'
1491+ )
1492+ cmd_args = ['destroy' , '--auto-approve' , '-no-color' ]
1493+ if self .TF_VARIABLES_FILE :
1494+ cmd_args .extend (['--var-file' , self .TF_VARIABLES_FILE ])
1495+ return local ['terraform' ].with_cwd (self ._work_dir ).run (
1496+ args = cmd_args ,
1497+ retcode = None ,
1498+ timeout = CONFIG .provision_timeout ,
1499+ )
1500+
14821501 # After: install_package and run_tests
14831502 @command_decorator (
14841503 'stop_environment' ,
@@ -1487,35 +1506,23 @@ def write_to_file(file_base_name: str, artifacts_section: dict):
14871506 is_abortable = False ,
14881507 )
14891508 def stop_env (self ):
1490- if os .path .exists (self ._work_dir ):
1491- self ._logger .info (
1492- 'Destroying the environment %s...' ,
1493- self .env_name ,
1494- )
1495- self ._logger .debug (
1496- 'Running "terraform destroy --auto-approve" command'
1497- )
1498- cmd_args = ['destroy' , '--auto-approve' , '-no-color' ]
1499- if self .TF_VARIABLES_FILE :
1500- cmd_args .extend (['--var-file' , self .TF_VARIABLES_FILE ])
1501- return local ['terraform' ].with_cwd (self ._work_dir ).run (
1502- args = cmd_args ,
1503- retcode = None ,
1504- timeout = CONFIG .provision_timeout ,
1505- )
1509+ return self ._stop_env ()
15061510
15071511 def erase_work_dir (self ):
1508- if self ._work_dir and os .path .exists (self ._work_dir ):
1509- self ._logger .info ('Erasing working directory...' )
1510- try :
1511- shutil .rmtree (self ._work_dir )
1512- except Exception as e :
1513- self ._logger .error (
1514- 'Error while erasing working directory: %s' ,
1515- e ,
1516- )
1517- else :
1518- self ._logger .info ('Working directory was successfully removed' )
1512+ if not self ._work_dir :
1513+ return
1514+ if self ._work_dir and not os .path .exists (self ._work_dir ):
1515+ return
1516+ self ._logger .info ('Erasing working directory...' )
1517+ try :
1518+ shutil .rmtree (self ._work_dir )
1519+ except Exception as e :
1520+ self ._logger .error (
1521+ 'Error while erasing working directory: %s' ,
1522+ e ,
1523+ )
1524+ else :
1525+ self ._logger .info ('Working directory was successfully removed' )
15191526
15201527 def setup (self , skip_provision : bool = False ):
15211528 self ._stats ['started_at' ] = datetime .datetime .utcnow ().isoformat ()
0 commit comments