2022-03-10 14:26:22 +00:00
|
|
|
import os
|
2022-11-28 18:55:10 +00:00
|
|
|
from typing import Tuple, Any, Optional
|
2022-05-24 19:51:38 +01:00
|
|
|
|
|
|
|
import jinja2
|
2022-03-10 14:26:22 +00:00
|
|
|
|
|
|
|
|
2022-11-28 21:18:56 +00:00
|
|
|
class BaseAutomation:
|
2022-05-15 18:47:46 +01:00
|
|
|
short_name: str = "base"
|
|
|
|
description: str = "Abstract base automation."
|
|
|
|
frequency: int
|
2022-11-28 18:55:10 +00:00
|
|
|
working_dir: Optional[str]
|
2022-05-15 18:47:46 +01:00
|
|
|
|
2022-05-08 13:01:15 +01:00
|
|
|
"""
|
|
|
|
The short name of the automation provider. This is used as an opaque token throughout
|
|
|
|
the portal system.
|
|
|
|
"""
|
|
|
|
|
2022-11-28 18:55:10 +00:00
|
|
|
def __init__(self, working_dir: Optional[str] = None):
|
|
|
|
super().__init__()
|
|
|
|
self.working_dir = working_dir
|
|
|
|
|
|
|
|
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
2022-05-08 13:01:15 +01:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-11-28 18:55:10 +00:00
|
|
|
def tmpl_write(self, filename: str, template: str, **kwargs: Any) -> None:
|
2022-05-24 19:51:38 +01:00
|
|
|
"""
|
|
|
|
Write a Jinja2 template to the working directory for use by an automation module.
|
|
|
|
|
|
|
|
:param filename: filename to write to
|
|
|
|
:param template: Jinja2 template
|
2022-11-16 15:47:36 +00:00
|
|
|
:param working_dir: temporary directory for running the Terraform automation
|
2022-05-24 19:51:38 +01:00
|
|
|
:param kwargs: variables for use with the template
|
|
|
|
:return: None
|
|
|
|
"""
|
2022-11-28 18:55:10 +00:00
|
|
|
if not self.working_dir:
|
|
|
|
raise RuntimeError("No working directory specified.")
|
2022-05-24 19:51:38 +01:00
|
|
|
tmpl = jinja2.Template(template)
|
2022-11-28 18:55:10 +00:00
|
|
|
with open(os.path.join(self.working_dir, filename), 'w', encoding="utf-8") as tfconf:
|
2022-06-23 13:42:45 +01:00
|
|
|
tfconf.write(tmpl.render(**kwargs))
|