parent
9b8ac493b1
commit
082de33b5d
7 changed files with 194 additions and 143 deletions
93
app/terraform/terraform.py
Normal file
93
app/terraform/terraform.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
import json
|
||||
import subprocess
|
||||
from typing import Dict, Any, Optional
|
||||
|
||||
import jinja2
|
||||
|
||||
from app.terraform import BaseAutomation
|
||||
|
||||
|
||||
class TerraformAutomation(BaseAutomation):
|
||||
"""
|
||||
An abstract class to be extended by automation plugins using Terraform
|
||||
providers to deploy resources.
|
||||
"""
|
||||
|
||||
parallelism = 10
|
||||
"""
|
||||
Default parallelism for remote API calls.
|
||||
"""
|
||||
|
||||
def automate(self):
|
||||
self.tf_prehook()
|
||||
self.tf_generate()
|
||||
self.tf_init()
|
||||
self.tf_apply(refresh=False)
|
||||
self.tf_posthook()
|
||||
|
||||
def tf_apply(self, refresh: bool = True, parallelism: Optional[int] = None):
|
||||
if not parallelism:
|
||||
parallelism = self.parallelism
|
||||
subprocess.run(
|
||||
['terraform', 'apply', f'-refresh={str(refresh).lower()}', '-auto-approve',
|
||||
f'-parallelism={str(parallelism)}'],
|
||||
cwd=self.working_directory())
|
||||
|
||||
def tf_generate(self):
|
||||
raise NotImplementedError()
|
||||
|
||||
def tf_init(self):
|
||||
subprocess.run(
|
||||
['terraform', 'init'],
|
||||
cwd=self.working_directory())
|
||||
|
||||
def tf_output(self) -> Dict[str, Any]:
|
||||
tf = subprocess.run(
|
||||
['terraform', 'output', '-json'],
|
||||
cwd=self.working_directory(),
|
||||
stdout=subprocess.PIPE)
|
||||
return json.loads(tf.stdout)
|
||||
|
||||
def tf_plan(self):
|
||||
tf = subprocess.run(
|
||||
['terraform', 'plan'],
|
||||
cwd=self.working_directory())
|
||||
# TODO: looks like terraform has a -json output mode here but it's
|
||||
# more like JSON-ND, task is to figure out how to yield those records
|
||||
# as plan runs, the same is probably also true for apply
|
||||
|
||||
def tf_posthook(self, prehook_result: Any = None) -> None:
|
||||
"""
|
||||
This hook function is called as part of normal automation, after the
|
||||
completion of :func:`tf_apply`.
|
||||
|
||||
The default, if not overridden by a subclass, is to do nothing.
|
||||
|
||||
:param prehook_result: the returned value of :func:`tf_prehook`
|
||||
:return: None
|
||||
"""
|
||||
pass
|
||||
|
||||
def tf_prehook(self) -> Optional[Any]:
|
||||
"""
|
||||
This hook function is called as part of normal automation, before generating
|
||||
the terraform configuration file. The return value will be passed to
|
||||
:func:`tf_posthook` but is otherwise ignored.
|
||||
|
||||
The default, if not overridden by a subclass, is to do nothing.
|
||||
|
||||
:return: state that is useful to :func:`tf_posthook`, if required
|
||||
"""
|
||||
pass
|
||||
|
||||
def tf_show(self) -> Dict[str, Any]:
|
||||
terraform = subprocess.run(
|
||||
['terraform', 'show', '-json'],
|
||||
cwd=self.working_directory(),
|
||||
stdout=subprocess.PIPE)
|
||||
return json.loads(terraform.stdout)
|
||||
|
||||
def tf_write(self, template: str, **kwargs):
|
||||
tmpl = jinja2.Template(template)
|
||||
with open(self.working_directory("main.tf"), 'w') as tf:
|
||||
tf.write(tmpl.render(**kwargs))
|
Loading…
Add table
Add a link
Reference in a new issue