majuna/app/terraform/__init__.py

36 lines
1.2 KiB
Python

from abc import ABCMeta, abstractmethod
import os
from typing import Tuple, Optional, Any
import jinja2
from app import app
class BaseAutomation(metaclass=ABCMeta):
short_name: str = "base"
description: str = "Abstract base automation."
frequency: int
"""
The short name of the automation provider. This is used as an opaque token throughout
the portal system.
"""
@abstractmethod
def automate(self, working_dir: str, full: bool = False) -> Tuple[bool, str]:
raise NotImplementedError()
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(os.path.join(working_dir, filename), 'w', encoding="utf-8") as tfconf:
tfconf.write(tmpl.render(**kwargs))