This repository has been archived by the owner on May 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtasks.py
132 lines (91 loc) · 3.14 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
"""Call various Terraform actions."""
import os
import os.path
from invoke import task
import jinja2
import yaml
TERRAFORM_VERSION = '0.11.7'
@task
def install(c, ostype='linux', version=TERRAFORM_VERSION):
"""Download a local version of Terraform."""
if ostype == 'mac':
ostype = 'darwin'
file = f'terraform_{version}_{ostype}_amd64.zip'
if os.path.exists('terraform'):
print('Terraform file found')
return
print(f'Downloading Terraform {version}')
c.run(f'wget -q https://releases.hashicorp.com/terraform/{version}/{file}')
c.run(f'unzip {file}')
c.run(f'rm {file}')
MAIN_TF_FILE = 'stellar-network.tf'
@task
def template(c, vars_file='vars.yml'):
"""Process Terraform file taht require templating.
Terraform and HCL has limitations that can be easily solved using template
languages like Jinja.
For example, avoiding redundancy when calling a module multiple times with
just a single different variable value every time.
"""
print('generating terraform files from templates')
with open(vars_file) as f:
variables = yaml.load(f)
for root, _, files in os.walk("."):
for file in files:
stripped_file, ext = os.path.splitext(file)
if ext != '.j2':
continue
out_file = f'{root}/{stripped_file}'
print(f'processing file {root}/{file}')
with open(f'{root}/{file}') as f:
tmplate = jinja2.Template(f.read(), extensions=['jinja2.ext.do'])
out = tmplate.render(variables, env_vars=os.environ)
with open(out_file, 'w') as f:
f.write(out)
c.run(f'./terraform fmt {out_file}')
@task(template)
def init(c):
"""Call terraform init."""
print('initializing')
c.run('./terraform init')
@task(init)
def new_workspace(c, vars_file='vars.yml'):
"""Set terraform workspace."""
print('setting workspace')
with open(vars_file) as f:
variables = yaml.load(f)
workspace = variables['stellar']['network_name']
c.run(f'./terraform workspace new {workspace}')
@task(init)
def workspace(c, vars_file='vars.yml'):
"""Set terraform workspace."""
print('setting workspace')
with open(vars_file) as f:
variables = yaml.load(f)
workspace = variables['stellar']['network_name']
c.run(f'./terraform workspace select {workspace}')
@task(workspace)
def modules(c):
"""Call terraform get."""
print('getting modules')
c.run('./terraform get')
@task(modules)
def plan(c, destroy=False):
"""Call terraform plan."""
print('planning')
c.run('./terraform plan {}'.format('-destroy' if destroy else ''))
@task(modules)
def apply(c, yes=False):
"""Call terraform destroy."""
print('applying')
c.run('./terraform apply {}'.format('-auto-approve' if yes else ''))
@task(modules)
def destroy(c, yes=False):
"""Call terraform destroy."""
print('destroying')
c.run('./terraform destroy {}'.format('-auto-approve' if yes else ''))
@task(modules)
def output(c):
"""Call terraform output."""
print('printing output')
c.run('./terraform output')