automate: move working_dir to be set in constructor
This commit is contained in:
parent
efdaad977a
commit
109851745b
7 changed files with 59 additions and 43 deletions
|
@ -27,7 +27,7 @@ class TerraformAutomation(BaseAutomation):
|
|||
Short name for the provider used by this module.
|
||||
"""
|
||||
|
||||
def automate(self, working_dir: str, full: bool = False) -> Tuple[bool, str]:
|
||||
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
||||
"""
|
||||
Runs the Terraform automation module. The run will follow these steps:
|
||||
|
||||
|
@ -46,10 +46,12 @@ class TerraformAutomation(BaseAutomation):
|
|||
:return: success status and Terraform apply logs
|
||||
"""
|
||||
|
||||
if not self.working_dir:
|
||||
raise RuntimeError("No working directory specified.")
|
||||
prehook_result = self.tf_prehook() # pylint: disable=assignment-from-no-return
|
||||
self.tf_generate(working_dir)
|
||||
self.tf_init(working_dir)
|
||||
returncode, logs = self.tf_apply(working_dir, refresh=self.always_refresh or full)
|
||||
self.tf_generate()
|
||||
self.tf_init()
|
||||
returncode, logs = self.tf_apply(self.working_dir, refresh=self.always_refresh or full)
|
||||
self.tf_posthook(prehook_result=prehook_result)
|
||||
return returncode == 0, logs
|
||||
|
||||
|
@ -59,6 +61,8 @@ class TerraformAutomation(BaseAutomation):
|
|||
lock_timeout: int = 15) -> Tuple[int, str]:
|
||||
if not parallelism:
|
||||
parallelism = self.parallelism
|
||||
if not self.working_dir:
|
||||
raise RuntimeError("No working directory specified.")
|
||||
# The following subprocess call takes external input, but is providing
|
||||
# the argument list as an array such that argument injection would be
|
||||
# ineffective.
|
||||
|
@ -76,11 +80,12 @@ class TerraformAutomation(BaseAutomation):
|
|||
return tfcmd.returncode, tfcmd.stdout.decode('utf-8')
|
||||
|
||||
@abstractmethod
|
||||
def tf_generate(self, working_dir) -> None:
|
||||
def tf_generate(self) -> None:
|
||||
raise NotImplementedError()
|
||||
|
||||
def tf_init(self, working_dir: str, *,
|
||||
lock_timeout: int = 15) -> None:
|
||||
def tf_init(self, *, lock_timeout: int = 15) -> None:
|
||||
if not self.working_dir:
|
||||
raise RuntimeError("No working directory specified.")
|
||||
# The init command does not support JSON output.
|
||||
# The following subprocess call takes external input, but is providing
|
||||
# the argument list as an array such that argument injection would be
|
||||
|
@ -90,20 +95,24 @@ class TerraformAutomation(BaseAutomation):
|
|||
'init',
|
||||
f'-lock-timeout={str(lock_timeout)}m',
|
||||
],
|
||||
cwd=working_dir)
|
||||
cwd=self.working_dir)
|
||||
|
||||
def tf_output(self, working_dir) -> Any:
|
||||
def tf_output(self) -> Any:
|
||||
if not self.working_dir:
|
||||
raise RuntimeError("No working directory specified.")
|
||||
# The following subprocess call does not take any user input.
|
||||
tfcmd = subprocess.run( # nosec
|
||||
['terraform', 'output', '-json'],
|
||||
cwd=working_dir,
|
||||
cwd=self.working_dir,
|
||||
stdout=subprocess.PIPE)
|
||||
return json.loads(tfcmd.stdout)
|
||||
|
||||
def tf_plan(self, working_dir: str, *,
|
||||
def tf_plan(self, *,
|
||||
refresh: bool = True,
|
||||
parallelism: Optional[int] = None,
|
||||
lock_timeout: int = 15) -> Tuple[int, str]:
|
||||
if not self.working_dir:
|
||||
raise RuntimeError("No working directory specified.")
|
||||
# The following subprocess call takes external input, but is providing
|
||||
# the argument list as an array such that argument injection would be
|
||||
# ineffective.
|
||||
|
@ -115,7 +124,7 @@ class TerraformAutomation(BaseAutomation):
|
|||
f'-parallelism={str(parallelism)}',
|
||||
f'-lock-timeout={str(lock_timeout)}m',
|
||||
],
|
||||
cwd=working_dir)
|
||||
cwd=self.working_dir)
|
||||
return tfcmd.returncode, tfcmd.stdout.decode('utf-8')
|
||||
|
||||
def tf_posthook(self, *, prehook_result: Any = None) -> None:
|
||||
|
@ -140,13 +149,15 @@ class TerraformAutomation(BaseAutomation):
|
|||
:return: state that is useful to :func:`tf_posthook`, if required
|
||||
"""
|
||||
|
||||
def tf_show(self, working_dir) -> Any:
|
||||
def tf_show(self) -> Any:
|
||||
# This subprocess call doesn't take any user input.
|
||||
if not self.working_dir:
|
||||
raise RuntimeError("No working directory specified.")
|
||||
terraform = subprocess.run( # nosec
|
||||
['terraform', 'show', '-json'],
|
||||
cwd=working_dir,
|
||||
cwd=self.working_dir,
|
||||
stdout=subprocess.PIPE)
|
||||
return json.loads(terraform.stdout)
|
||||
|
||||
def tf_write(self, template: str, working_dir: str, **kwargs: Any) -> None:
|
||||
self.tmpl_write("main.tf", template, working_dir, **kwargs)
|
||||
def tf_write(self, template: str, **kwargs: Any) -> None:
|
||||
self.tmpl_write("main.tf", template, **kwargs)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue