majuna/app/terraform/__init__.py

39 lines
1.3 KiB
Python

import os
from typing import Tuple, Any, Optional
import jinja2
class BaseAutomation:
short_name: str = "base"
description: str = "Abstract base automation."
frequency: int
working_dir: Optional[str]
"""
The short name of the automation provider. This is used as an opaque token throughout
the portal system.
"""
def __init__(self, working_dir: Optional[str] = None):
super().__init__()
self.working_dir = working_dir
def automate(self, full: bool = False) -> Tuple[bool, str]:
raise NotImplementedError()
def tmpl_write(self, filename: str, template: 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
"""
if not self.working_dir:
raise RuntimeError("No working directory specified.")
tmpl = jinja2.Template(template)
with open(os.path.join(self.working_dir, filename), 'w', encoding="utf-8") as tfconf:
tfconf.write(tmpl.render(**kwargs))