majuna/app/terraform/__init__.py

37 lines
1.2 KiB
Python
Raw Normal View History

from abc import ABCMeta, abstractmethod
2022-03-10 14:26:22 +00:00
import os
from typing import Tuple, Optional, Any
import jinja2
2022-03-10 14:26:22 +00:00
from app import app
class BaseAutomation(metaclass=ABCMeta):
2022-05-15 18:47:46 +01:00
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))