-
Notifications
You must be signed in to change notification settings - Fork 2
/
__init__.py
462 lines (382 loc) · 14.1 KB
/
__init__.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
"""Helper functions to run terraform in CI or workstation."""
import json
import logging
import sys
from contextlib import contextmanager
from os import environ, path as osp
from subprocess import Popen, PIPE, CalledProcessError
from urllib.parse import urlparse
import boto3
__version__ = "0.9.0"
DEFAULT_TERRAFORM_VARS = ".env/tf_env.json"
LOG = logging.getLogger(__name__)
class LessThanFilter(logging.Filter): # pylint: disable=too-few-public-methods
"""Filters out log messages of a lower level."""
def __init__(self, exclusive_maximum, name=""):
super(LessThanFilter, self).__init__(name)
self.max_level = exclusive_maximum
def filter(self, record):
# non-zero return means we log this message
return 1 if record.levelno < self.max_level else 0
def render_comment(status):
"""
Format status with markdown syntax to publish it as a comment.
:param status: Status generated by a series of terraform plan.
For example::
{'cloudflare': {'add': 0,
'change': 0,
'destroy': 0,
'stderr': b'',
'success': True},
'github': {'add': 0,
'change': 0,
'destroy': 0,
'stderr': b'',
'success': True},
'management_app': {'add': 0,
'change': 0,
'destroy': 0,
'stderr': b'',
'success': True},
'prod/recovery_app': {'add': 0,
'change': 0,
'destroy': 0,
'stderr': b'',
'success': True},
'prod/web_app': {'add': 0,
'change': 0,
'destroy': 0,
'stderr': b'',
'success': True},
'stage/recovery_app2': {'add': 0,
'change': 0,
'destroy': 0,
'stderr': b'',
'success': True},
'stage/web_app': {'add': 0,
'change': 0,
'destroy': 0,
'stderr': b'',
'success': True}}
:type status: dict
:return: Markdown formatted comment
:rtype: str
"""
print(status)
# 1/0
map_change = {
"add": "![#c5f015](https://placehold.it/15/c5f015/000000?text=+) ",
"change": "![#1589F0](https://placehold.it/15/1589F0/000000?text=+) ",
"destroy": "![#f03c15](https://placehold.it/15/f03c15/000000?text=+) ",
None: "![#FFFF00](https://placehold.it/15/FFFF00/000000?text=+) ",
}
def flag(local_change):
for k in status.keys():
try:
if status[k][local_change] > 0:
return map_change[local_change]
except TypeError:
return map_change[None]
return ""
comment = " | ".join(
[
"Module",
"Success",
flag("add") + "Add",
flag("change") + "Change",
flag("destroy") + "Destroy",
]
)
comment += "\n" + "--- | --- | ---: | ---: | ---:" + "\n"
tag_map = {
True: "![#c5f015](https://placehold.it/15/c5f015/000000?text=+)",
False: "![#f03c15](https://placehold.it/15/f03c15/000000?text=+)",
}
for key in status.keys():
changes = {}
for change in ["add", "change", "destroy"]:
try:
if status[key][change] > 0:
changes[change] = "**%d**" % status[key][change]
else:
changes[change] = status[key][change]
except TypeError:
changes[change] = "Unknown"
line = "**{module}** | {tag} `{success}` " "| {add} | {change} | {destroy}"
line = line.format(
module=key,
tag=tag_map[status[key]["success"]],
success=status[key]["success"],
add=changes["add"],
change=changes["change"],
destroy=changes["destroy"],
)
comment += line + "\n"
for key in status.keys():
outs = {}
for out in ["stdout", "stderr"]:
if isinstance(status[key][out], bytes):
outs[out] = status[key][out].decode("utf-8")
else:
outs[out] = status[key][out]
line = """
# **{module}**
## stdout
{cout}
## stderr
{cerr}
"""
line = line.format(
module=key,
cout="```" + outs["stdout"] + "```" if outs["stdout"] else "_no output_",
cerr="```" + outs["stderr"] + "```" if outs["stderr"] else "_no output_",
)
comment += line
return comment
def get_action(branch=None, pull_request=False):
"""
Detect terraform action based on input branch and pull_request flag.
If it cannot detect the action (branch is not given or error) the action
will be ``plan``.
:param branch: Branch name.
:type branch: str
:param pull_request: Whether the branch is a pull request.
:type pull_request: bool
:return: "apply" or "plan". It will return "apply" only if the branch is
"master" and not a pull request.
:rtype: str
"""
if branch == "master" and not pull_request:
return "apply"
return "plan"
def parse_plan(output):
"""
Parse a string given by output and return a tuple with execution plan.
:param output: Output of terraform plan command.
:type output: str
:return: Tuple with number of changes (add, change, destroy)
:rtype: tuple
"""
add = None
change = None
destroy = None
try:
for line in output.splitlines():
if line.startswith("Plan: "):
split_line = line.split()
# Plan: 4 to add, 11 to change, 7 to destroy.
add = int(split_line[1])
change = int(split_line[4])
destroy = int(split_line[7])
elif line == "No changes. Infrastructure is up-to-date.":
return 0, 0, 0
except AttributeError:
pass
return add, change, destroy
def run_job(path, action):
"""
Run a job for a given module specified by path.
:param path: Path to terraform module.
:type path: str
:param action: "apply" or "plan". Other action are not supported.
:return: Dictionary with run report:
{
'success': True or False
'add': x,
'change': x,
'destroy': x,
'raw': <original content of the plan output>
}
:rtype: dict
"""
returncode, cout, cerr = execute(["make", "-C", path, action])
status = {"success": returncode == 0, "stderr": cerr, "stdout": cout}
parse_tree = parse_plan(cout.decode("utf-8"))
status["add"] = parse_tree[0]
status["change"] = parse_tree[1]
status["destroy"] = parse_tree[2]
return status
def execute(cmd, stdout=PIPE, stderr=PIPE, cwd=None):
"""
Execute a command and return a tuple with return code, STDOUT and STDERR.
:param cmd: Command.
:type cmd: list
:param stdout: Where to send stdout. Default PIPE.
:type stdout: int, None
:param stderr: Where to send stdout. Default PIPE.
:type stderr: int, None
:param cwd: Working directory.
:type cwd: str
:return: Tuple (return code, STDOUT, STDERR)
:rtype: tuple
"""
LOG.info("Executing: %s", " ".join(cmd))
proc = Popen(cmd, stdout=stdout, stderr=stderr, cwd=cwd)
cout, cerr = proc.communicate()
return proc.returncode, cout, cerr
def read_from_secretsmanager(url):
"""
Read a secret from AWS secrets manager.
``url`` is where the secret value is stored and has format:
secretsmanager://<secret name>:<json key>
"secret name" is the secret identifier as AWS calls it in Secrets Manager.
It is assumed the secret stores a JSON string. The function returns
value of the "json key".
:param url: URL to a secret value.
:type url: str
:return: Secret value that is stored in a JSON key "json key".
:rtype: str
"""
client = boto3.client("secretsmanager")
location = urlparse(url)
full_path = location.netloc + location.path
aws_response = client.get_secret_value(SecretId=full_path.split(":")[0])
try:
return json.loads(aws_response["SecretString"])[full_path.split(":")[1]]
except json.JSONDecodeError:
return aws_response["SecretString"]
def setup_environment(config_path=DEFAULT_TERRAFORM_VARS):
"""
Read AWS variables from Terraform config and set them
as environment variables
"""
with open(config_path) as f_descr:
tf_vars = json.loads(f_descr.read())
var_map = {
"TF_VAR_aws_access_key": ["AWS_ACCESS_KEY_ID", "TF_VAR_aws_access_key_id"],
"TF_VAR_aws_secret_key": [
"AWS_SECRET_ACCESS_KEY",
"TF_VAR_aws_secret_access_key",
],
}
for key in var_map:
try:
for eq_key in var_map[key]:
environ[eq_key] = tf_vars[key]
except KeyError:
pass
for variable in ["AWS_ACCESS_KEY_ID", "AWS_SECRET_ACCESS_KEY"]:
try:
environ[variable] = tf_vars["TF_VAR_{var}".format(var=variable.lower())]
except KeyError as err:
LOG.debug("Key %s is missing in %s", err, config_path)
for key, value in tf_vars.items():
if value.startswith("secretsmanager://"):
environ[key] = read_from_secretsmanager(value)
else:
environ[key] = value
for variable in ["GITHUB_TOKEN"]:
try:
environ[variable] = environ["TF_VAR_{var}".format(var=variable.lower())]
except KeyError as err:
LOG.debug("Key %s is missing in %s", err, config_path)
def module_name_from_path(path):
"""
Get one level up directory and return it as module name
:param path: Path to directory
:return: parent directory
:rtype: str
"""
abspath = osp.abspath(path)
if abspath == "/":
return "root"
return osp.basename(abspath)
def convert_to_newlines(text):
"""
Convert \n in the bytes ``text`` into actual new lines.
:param text: Input string where new lines are encoded as ``\n``
:type text: bytes
:return: Text where \n are replaced with actual new lines.
:rtype: str
"""
return text.replace(b"\\n", b"\n").decode("UTF-8")
def setup_logging(logger, debug=False): # pragma: no cover
"""Configures logging for the module"""
fmt_str = (
"%(asctime)s: %(levelname)s:"
" %(module)s.%(funcName)s():%(lineno)d: %(message)s"
)
console_handler = logging.StreamHandler(stream=sys.stdout)
console_handler.addFilter(LessThanFilter(logging.WARNING))
console_handler.setLevel(logging.INFO)
console_handler.setFormatter(logging.Formatter(fmt_str))
# Log errors and warnings to stderr
console_handler_err = logging.StreamHandler(stream=sys.stderr)
console_handler_err.setLevel(logging.WARNING)
console_handler_err.setFormatter(logging.Formatter(fmt_str))
# Log debug to stderr
console_handler_debug = logging.StreamHandler(stream=sys.stderr)
console_handler_debug.addFilter(LessThanFilter(logging.INFO))
console_handler_debug.setLevel(logging.DEBUG)
console_handler_debug.setFormatter(logging.Formatter(fmt_str))
logger.handlers = []
logger.addHandler(console_handler)
logger.addHandler(console_handler_err)
if debug:
logger.addHandler(console_handler_debug)
logger.debug_enabled = True
logger.setLevel(logging.DEBUG)
def terraform_output(path):
"""
Run terraform output and return the json results as a dict.
:param path: Path to directory with terraform module.
:type path: str
:return: dict from terraform output
:rtype: dict
"""
cmd = "terraform output -json"
ret, cout, cerr = execute(cmd.split(), stdout=PIPE, stderr=None, cwd=path)
if ret:
raise CalledProcessError(returncode=ret, cmd=cmd, output=cout, stderr=cerr)
return json.loads(cout)
@contextmanager
def terraform_apply(
path, # pylint: disable=bad-continuation
destroy_after=True, # pylint: disable=bad-continuation
json_output=False, # pylint: disable=bad-continuation
var_file="configuration.tfvars", # pylint: disable=bad-continuation
):
"""
Run terraform init and apply, then return a generator.
If destroy_after is True, run terraform destroy afterwards.
:param path: Path to directory with terraform module.
:type path: str
:param destroy_after: Run terraform destroy after context it returned back.
:type destroy_after: bool
:param json_output: Yield terraform output result as a dict (available in the context)
:type json_output: bool
:param var_file: Path to a file with terraform variables.
:type var_file: str
:return: If json_output is true then yield the result from terraform_output otherwise nothing.
Use it in the ``with`` block.
:raise CalledProcessError: if either of terraform commands (except ``terraform destroy``)
exits with non-zero.
"""
cmds = [
"terraform init -no-color",
"terraform get -update=true -no-color",
(
"terraform apply -var-file={var_file} -input=false "
"-auto-approve".format(var_file=var_file)
),
]
try:
for cmd in cmds:
ret, cout, cerr = execute(cmd.split(), stdout=None, stderr=None, cwd=path)
if ret:
raise CalledProcessError(
returncode=ret, cmd=cmd, output=cout, stderr=cerr
)
if json_output:
yield terraform_output(path)
else:
yield
finally:
if destroy_after:
execute(
"terraform destroy -var-file={var_file} "
"-input=false -auto-approve".format(var_file=var_file).split(),
stdout=None,
stderr=None,
cwd=path,
)