-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_deploy_stack.py
397 lines (349 loc) · 15.8 KB
/
create_deploy_stack.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
import os
import sys
import logging
import re
import json
from typing import List
from collections import defaultdict
import fnmatch
import github
import aurora_data_api
import boto3
sys.path.append(os.path.dirname(__file__) + "/..")
from common.utils import (
subprocess_run,
TerragruntException,
ClientException,
get_task_log_url,
)
log = logging.getLogger(__name__)
stream = logging.StreamHandler(sys.stdout)
stream.setFormatter(logging.Formatter("%(levelname)s %(message)s"))
log.addHandler(stream)
log.setLevel(logging.DEBUG)
ssm = boto3.client("ssm", endpoint_url=os.environ.get("SSM_ENDPOINT_URL"))
lb = boto3.client("lambda", endpoint_url=os.environ.get("LAMBDA_ENDPOINT_URL"))
rds_data_client = boto3.client(
"rds-data", endpoint_url=os.environ.get("METADB_ENDPOINT_URL")
)
class CreateStack:
def get_new_providers(self, path: str, role_arn: str) -> List[str]:
"""
Returns list of Terraform provider sources that are defined within the
`path` that are not within the Terraform state
Arguments:
path: Absolute path to a directory that contains atleast one Terragrunt *.hcl file
role_arn: Role used for running Terragrunt command
"""
log.debug(f"Path: {path}")
run = subprocess_run(
f"terragrunt providers --terragrunt-working-dir {path} --terragrunt-iam-role {role_arn}"
)
cfg_providers = re.findall(
r"(?<=─\sprovider\[).+(?=\])", run.stdout, re.MULTILINE
)
state_providers = re.findall(
r"(?<=\s\sprovider\[).+(?=\])", run.stdout, re.MULTILINE
)
log.debug(f"Config providers:\n{cfg_providers}")
log.debug(f"State providers:\n{state_providers}")
return list(set(cfg_providers).difference(state_providers))
def get_graph_deps(self, path: str, role_arn: str) -> dict:
"""
Returns a Python dictionary version of the `terragrunt graph-dependencies` command
Arguments:
path: Absolute path to a directory to run the Terragrunt command from
role_arn: Role used for running Terragrunt command
"""
graph_deps_run = subprocess_run(
f"terragrunt graph-dependencies --terragrunt-working-dir {path} --terragrunt-iam-role {role_arn}"
)
# parses output of command to create a map of directories with a list of their directory dependencies
# if directory has no dependency, map value default's to list
graph_deps = defaultdict(list)
for m in re.finditer(
r'\t"(.+?)("\s;|"\s->\s")(.+(?=";)|$)', graph_deps_run.stdout, re.MULTILINE
):
cfg = os.path.relpath(m.group(1), os.environ["SOURCE_REPO_PATH"])
dep = m.group(3)
if dep != "":
graph_deps[cfg].append(
os.path.relpath(dep, os.environ["SOURCE_REPO_PATH"])
)
else:
# initialize map key with emtpy list if dependency value is none
graph_deps[cfg]
return dict(graph_deps)
def get_github_diff_paths(self, graph_deps: dict, path: str) -> List[str]:
"""
Returns unique list of GitHub diff directories and the directories that
are dependent on them. Function uses the graph-dependencies dictionary
to find the directories that are dependent on the GitHub diff directories.
Arguments:
graph_deps: Terragrunt graph-dependencies Python version dictionary
path: Parent directory to search for differences within
"""
repo = github.Github(os.environ["GITHUB_TOKEN"], retry=3).get_repo(
os.environ["REPO_FULL_NAME"]
)
log.info("Running Graph Scan")
target_diff_paths = []
log.debug(
f"Getting git differences between commits: {os.environ['BASE_COMMIT_ID']} and {os.environ['COMMIT_ID']}"
)
for diff in repo.compare(
os.environ["BASE_COMMIT_ID"], os.environ["COMMIT_ID"]
).files:
# collects directories that contain new, modified and removed .hcl/.tf files
if diff.status in ["added", "modified", "removed"]:
if fnmatch.fnmatch(diff.filename, f"{path}/**.hcl") or fnmatch.fnmatch(
diff.filename, f"{path}/**.tf"
):
target_diff_paths.append(os.path.dirname(diff.filename))
target_diff_paths = list(set(target_diff_paths))
log.debug(f"Detected differences:\n{target_diff_paths}")
diff_paths = []
# recursively searches for the diff directories within the graph
# dependencies mapping to include chained dependencies
while len(target_diff_paths) > 0:
log.debug(f"Current diffs list:\n{target_diff_paths}")
path = target_diff_paths.pop()
for cfg_path, cfg_deps in graph_deps.items():
if cfg_path == path:
diff_paths.append(path)
if path in cfg_deps:
target_diff_paths.append(cfg_path)
return list(set(diff_paths))
def get_plan_diff_paths(self, path, role_arn):
"""
Returns unique list of Terragrunt directories that contain a difference
in their respective Terraform state file.
Arguments:
path: Parent directory to search for differences within
role_arn: Role used for running Terragrunt command
"""
log.info("Running Plan Scan")
# use the terraform exitcode for each directory found in the terragrunt
# run-all plan output to determine what directories to collect
# set check=False to prevent raising subprocess.CalledProcessError
# since the -detailed-exitcode flags causes a return code of 2 if diff
# in Terraform plan
run = subprocess_run(
f"terragrunt run-all plan --terragrunt-working-dir {path} --terragrunt-iam-role {role_arn} --terragrunt-non-interactive -detailed-exitcode",
check=False,
)
if run.returncode not in [0, 2]:
log.error(f"Stderr: {run.stderr}")
raise TerragruntException(
"Terragrunt run-all plan command failed -- Aborting task"
)
# selects directories that contains a exitcode of 2 meaning there is a
# difference in the terraform plan
return [
os.path.relpath(p, os.environ["SOURCE_REPO_PATH"])
for p in re.findall(
r"(?<=exit\sstatus\s2\n\n\sprefix=\[).+?(?=\])", run.stderr, re.DOTALL
)
]
def create_stack(self, path: str, role_arn: str) -> List[map]:
"""
Creates a list of dictionaries consisting of keys representing
Terragrunt paths that contain differences in their respective
Terraform tfstate file and values that contain the path's associated
Terragrunt dependencies and new providers
Arguments:
path: Parent directory to search for differences within. When
SCAN_TYPE is `plan`, terragrunt run-all plan will be used
to search for differences which means the parent directory must
contain a `terragrunt.hcl` file.
role_arn: AWS account role used for running Terragrunt commands
"""
if not os.path.isdir(path):
raise ClientException(f"Account path does not exist within repo: {path}")
graph_deps = self.get_graph_deps(path, role_arn)
log.debug(f"Graph Dependency mapping: \n{json.dumps(graph_deps, indent=4)}")
log.debug(f'Scan type: {os.environ["SCAN_TYPE"]}')
if os.environ["SCAN_TYPE"] == "graph":
diff_paths = self.get_github_diff_paths(graph_deps, path)
elif os.environ["SCAN_TYPE"] == "plan":
diff_paths = self.get_plan_diff_paths(path, role_arn)
else:
raise ClientException(f"Scan type is invalid: {os.environ['SCAN_TYPE']}")
if len(diff_paths) == 0:
log.debug("Detected no Terragrunt paths with differences")
return []
else:
log.debug(f"Detected new/modified Terragrunt paths:\n{diff_paths}")
stack = []
for cfg_path, cfg_deps in graph_deps.items():
if cfg_path in diff_paths:
stack.append(
{
"cfg_path": cfg_path,
# only selects dependencies that contain differences
"cfg_deps": [dep for dep in cfg_deps if dep in diff_paths],
"new_providers": self.get_new_providers(cfg_path, role_arn),
}
)
return stack
def update_executions_with_new_deploy_stack(self) -> None:
"""
Iterates through every parent account-level directory and insert it's
associated deployment stack within the metadb
"""
with aurora_data_api.connect(
aurora_cluster_arn=os.environ["AURORA_CLUSTER_ARN"],
secret_arn=os.environ["AURORA_SECRET_ARN"],
database=os.environ["METADB_NAME"],
rds_data_client=rds_data_client,
) as conn:
with conn.cursor() as cur:
cur.execute(
"""
SELECT
account_name,
account_path,
account_deps::VARCHAR,
min_approval_count,
min_rejection_count,
voters::VARCHAR,
plan_role_arn,
apply_role_arn
FROM account_dim
ORDER BY account_name
"""
)
cols = [desc[0] for desc in cur.description]
res = cur.fetchall()
log.debug(f"Raw accounts records:\n{json.dumps(res, indent=4)}")
accounts = []
for account in res:
record = dict(zip(cols, account))
record["account_deps"] = (
record["account_deps"]
.removeprefix("{")
.removesuffix("}")
.split(",")
)
record["voters"] = (
record["voters"].removeprefix("{").removesuffix("}").split(",")
)
accounts.append(record)
log.debug(f"Accounts:\n{json.dumps(accounts, indent=4)}")
if len(accounts) == 0:
Exception("No account paths are defined in account_dim")
try:
log.info("Getting account stacks")
for account in accounts:
log.info(f'Account path: {account["account_path"]}')
log.info(f'Account plan role ARN: {account["plan_role_arn"]}')
stack = self.create_stack(
account["account_path"], account["plan_role_arn"]
)
log.debug(f"Stack:\n{stack}")
if len(stack) == 0:
log.debug("Stack is empty -- skipping")
continue
# convert lists to comma-delimitted strings that will
# parsed to TEXT[] within query
stack_values = []
for cfg in stack:
stack_values.append(
str(
tuple(
[
v if type(v) != list else ",".join(v)
for v in cfg.values()
]
)
)
)
stack_values = ",".join(stack_values)
with open(
f"{os.path.dirname(os.path.realpath(__file__))}/sql/update_executions_with_new_deploy_stack.sql",
"r",
) as f:
query = f.read().format(
pr_id=os.environ["PR_ID"],
commit_id=os.environ["COMMIT_ID"],
base_ref=os.environ["BASE_REF"],
head_ref=os.environ["HEAD_REF"],
account_name=account["account_name"],
account_path=account["account_path"],
account_deps=",".join(account["account_deps"]),
min_approval_count=account["min_approval_count"],
min_rejection_count=account["min_rejection_count"],
voters=",".join(account["voters"]),
plan_role_arn=account["plan_role_arn"],
apply_role_arn=account["apply_role_arn"],
stack=stack_values,
)
log.debug(f"Query:\n{query}")
cur.execute(query)
except Exception as e:
log.info("Rolling back execution insertions")
conn.rollback()
raise e
return None
def main(self) -> None:
"""
When a PR that contains Terragrunt and/or Terraform changes is merged
within the base branch, each of those new/modified Terragrunt
directories will have an associated deployment record inserted into the
metadb. After all records are inserted, a downstream AWS Lambda
Function will choose which of those records to run through the
AWS Step Function deployment flow.
"""
log.info("Locking merge action within target branch")
ssm.put_parameter(
Name=os.environ["GITHUB_MERGE_LOCK_SSM_KEY"],
Value=os.environ["PR_ID"],
Type="String",
Overwrite=True,
)
try:
try:
log.info("Creating deployment execution records")
self.update_executions_with_new_deploy_stack()
except Exception as e:
log.error(e, exc_info=True)
log.info("Unlocking merge action within target branch")
ssm.put_parameter(
Name=os.environ["GITHUB_MERGE_LOCK_SSM_KEY"],
Value="none",
Type="String",
Overwrite=True,
)
# raise after sending failed commit status
raise e
log.info(
f'Invoking Lambda Function: {os.environ["TRIGGER_SF_FUNCTION_NAME"]}'
)
lb.invoke(
FunctionName=os.environ["TRIGGER_SF_FUNCTION_NAME"],
InvocationType="Event",
Payload=json.dumps({"pr_id": os.environ["PR_ID"]}),
)
state = "success"
except Exception as e:
log.error(e, exc_info=True)
state = "failure"
commit_status_config = json.loads(os.environ["COMMIT_STATUS_CONFIG"])
log.debug(
f"Commit status config:\n{json.dumps(commit_status_config, indent=4)}"
)
if commit_status_config[os.environ["STATUS_CHECK_NAME"]]:
commit = (
github.Github(os.environ["GITHUB_TOKEN"], retry=3)
.get_repo(os.environ["REPO_FULL_NAME"])
.get_commit(os.environ["COMMIT_ID"])
)
log.info("Sending commit status")
commit.create_status(
state=state,
context=os.environ["STATUS_CHECK_NAME"],
target_url=get_task_log_url(),
)
if __name__ == "__main__":
run = CreateStack()
run.main()