Use disposable temporary directories to run automation jobs

This commit is contained in:
Owen 2022-11-16 15:47:36 +00:00 committed by irl
parent 9e5280280f
commit 0ebfe28b89
5 changed files with 36 additions and 42 deletions

View file

@ -18,34 +18,19 @@ class BaseAutomation(metaclass=ABCMeta):
"""
@abstractmethod
def automate(self, full: bool = False) -> Tuple[bool, str]:
def automate(self, working_dir: str, full: bool = False) -> Tuple[bool, str]:
raise NotImplementedError()
def working_directory(self, filename: Optional[str] = None) -> str:
"""
Provides a filesystem path that can be used during the automation run.
This is currently a persistent path, but this should not be relied upon
as future versions may use disposable temporary paths instead. State that
is needed in subsequent runs should be stored elsewhere.
:param filename: the filename inside the working directory to create a path for
:return: filesystem path for that filename
"""
return os.path.join(
app.config['TERRAFORM_DIRECTORY'],
self.short_name or self.__class__.__name__.lower(),
filename or ""
)
def tmpl_write(self, filename: str, template: str, **kwargs: Any) -> None:
def tmpl_write(self, filename: str, template: str, working_dir: str, **kwargs: Any) -> None:
"""
Write a Jinja2 template to the working directory for use by an automation module.
:param filename: filename to write to
:param template: Jinja2 template
:param working_dir: temporary directory for running the Terraform automation
:param kwargs: variables for use with the template
:return: None
"""
tmpl = jinja2.Template(template)
with open(self.working_directory(filename), 'w', encoding="utf-8") as tfconf:
with open(os.path.join(working_dir, filename), 'w', encoding="utf-8") as tfconf:
tfconf.write(tmpl.render(**kwargs))