2022-05-16 10:08:18 +01:00
|
|
|
from abc import ABCMeta, abstractmethod
|
2022-03-10 14:26:22 +00:00
|
|
|
import os
|
2022-05-24 19:51:38 +01:00
|
|
|
from typing import Tuple, Optional, Any
|
|
|
|
|
|
|
|
import jinja2
|
2022-03-10 14:26:22 +00:00
|
|
|
|
|
|
|
from app import app
|
|
|
|
|
|
|
|
|
2022-05-16 10:08:18 +01:00
|
|
|
class BaseAutomation(metaclass=ABCMeta):
|
2022-05-15 18:47:46 +01:00
|
|
|
short_name: str = "base"
|
|
|
|
description: str = "Abstract base automation."
|
|
|
|
frequency: int
|
|
|
|
|
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-05-16 10:08:18 +01:00
|
|
|
@abstractmethod
|
2022-05-08 17:20:04 +01:00
|
|
|
def automate(self, full: bool = False) -> Tuple[bool, str]:
|
2022-05-08 13:01:15 +01:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-05-16 11:44:03 +01:00
|
|
|
def working_directory(self, filename: Optional[str] = None) -> str:
|
2022-05-08 13:01:15 +01:00
|
|
|
"""
|
|
|
|
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
|
|
|
|
"""
|
2022-03-10 14:26:22 +00:00
|
|
|
return os.path.join(
|
|
|
|
app.config['TERRAFORM_DIRECTORY'],
|
|
|
|
self.short_name or self.__class__.__name__.lower(),
|
|
|
|
filename or ""
|
|
|
|
)
|
2022-05-24 19:51:38 +01:00
|
|
|
|
|
|
|
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 kwargs: variables for use with the template
|
|
|
|
:return: None
|
|
|
|
"""
|
|
|
|
tmpl = jinja2.Template(template)
|
2022-06-23 13:42:45 +01:00
|
|
|
with open(self.working_directory(filename), 'w', encoding="utf-8") as tfconf:
|
|
|
|
tfconf.write(tmpl.render(**kwargs))
|