-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.py
361 lines (288 loc) · 10.5 KB
/
deploy.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
import argparse
import json
import os
import time
from dataclasses import dataclass
from pprint import pprint
from typing import List
import boto3
import botocore
import requests
from awscli.customizations.cloudformation.yamlhelper import yaml_parse
ENV = os.getenv("ENV", "")
PROJECT_NAME = os.getenv("ProjectName", "")
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", "")
URL = os.getenv("URL", "")
SIGNED_URL_TIMEOUT_SECONDS = 60 * 5 # 5 minutes
YML_DIR = "./cfn"
PARAM_DIR = "./param"
@dataclass
class YmlItem:
yml_filename: str
region: str
def __str__(self) -> str:
return f"{self.yml_filename} @ {self.region}"
# Execute from the top item of YML_ORDER
YML_ORDER = [
YmlItem("vpc.yml", "ap-northeast-1"),
YmlItem("s3.yml", "ap-northeast-1"),
]
def main(is_dryrun=False):
"""main function
Args:
is_dryrun (bool, optional): dryrun or not. Defaults to False.
"""
param_path = os.path.join(PARAM_DIR, f"{ENV}-parameters.json")
with open(param_path, "r", encoding='UTF-8') as f:
all_param_dic = json.load(f)["Parameters"]
if is_dryrun:
runstr = "dryrun"
result = dryrun(all_param_dic)
print("result:")
pprint(result, indent=4)
else:
runstr = "deploy"
result = deploy(all_param_dic)
print("result:")
pprint(result, indent=4)
message_prefix = f"***** {ENV} {runstr} result *****"
message = f"""{message_prefix}
{json.dumps(result, indent=4)}
"""
if URL != "":
# remove past dryrun results on PR
clean_before_ci_pull_request_comments(message_prefix)
# post dryrun results to PR
post_to_pull_request(message)
def dryrun(all_param_dic: dict) -> dict:
"""dryrun(just create changeset)
Args:
all_param_dic (dict): parameter dictionary
Returns:
dict: result dictionary(yml_filename to result)
"""
result = {}
for yml_item in YML_ORDER:
print(f"***** {yml_item} start *****")
result[str(yml_item)] = process_yml(
yml_item, all_param_dic, deploys=False)
print(f"***** {yml_item} finished *****")
return result
def deploy(all_param_dic: dict) -> dict:
"""deploy(create changeset and execute it)
Args:
all_param_dic (dict): parameter dictionary
Returns:
dict: result dictionary(yml_filename to result)
"""
result = {}
for yml_item in YML_ORDER:
print(f"***** {yml_item} start *****")
result[str(yml_item)] = process_yml(
yml_item, all_param_dic, deploys=True)
print(f"***** {yml_item} finished *****")
return result
def process_yml(yml_item: YmlItem, all_param_dic: dict, deploys: bool) -> List:
"""process a yml file
Args:
yml_item (YmlItem): yml information
all_param_dic (dict): parameter dictionary
deploys (bool): whether it is deploy or dryrun
Returns:
List: _description_
"""
client = boto3.client('cloudformation', region_name=yml_item.region)
yml_path = os.path.join(YML_DIR, yml_item.yml_filename)
with open(yml_path, "r", encoding='UTF-8') as file:
yml_str = file.read()
parsed = yaml_parse(yml_str)
use_params = []
if "Parameters" in parsed:
use_params = list(parsed["Parameters"])
formatted_param = create_param(all_param_dic, use_params)
print("formatted_param:")
pprint(formatted_param, indent=4)
stack_suffix, _ = os.path.splitext(yml_item.yml_filename)
stack_name = f'{ENV}-{PROJECT_NAME}-{stack_suffix}'
middle_name = "deploy" if deploys else "dryrun"
change_set_name = f"{stack_suffix}-{middle_name}-{int(time.time())}"
yml_url = upload_yml_to_s3(yml_path, yml_item.region)
if not is_stack_exists(client, stack_name):
if deploys:
create_stack(client, stack_name, yml_url, formatted_param)
else:
return [f"{stack_name} Stack does not exist"]
# https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudformation.html#CloudFormation.Client.create_change_set
create_response = client.create_change_set(
StackName=stack_name,
TemplateURL=yml_url,
UsePreviousTemplate=False,
Parameters=formatted_param,
Capabilities=[
'CAPABILITY_NAMED_IAM'
],
ChangeSetName=change_set_name,
ChangeSetType='UPDATE',
)
print(f"create_change_set response: {create_response.get('Id', '')}")
waiter = client.get_waiter('change_set_create_complete')
try:
waiter.wait(
ChangeSetName=change_set_name,
StackName=stack_name,
WaiterConfig={
'Delay': 3,
'MaxAttempts': 50
}
)
desc_response = client.describe_change_set(
ChangeSetName=change_set_name,
StackName=stack_name,
)
print("describe_change_set response Changes:")
pprint(desc_response["Changes"], indent=4)
if not deploys:
return [x["ResourceChange"] for x in desc_response["Changes"]]
exec_response = client.execute_change_set(
ChangeSetName=change_set_name,
StackName=stack_name
)
print("execute_change_set response:")
pprint(exec_response, indent=4)
waiter = client.get_waiter('stack_update_complete')
waiter.wait(
StackName=stack_name,
WaiterConfig={
'Delay': 3,
'MaxAttempts': 50
}
)
return [x["ResourceChange"] for x in desc_response["Changes"]]
except botocore.exceptions.WaiterError as err:
print("fail-on-empty-changeset. err: ", err)
return []
except Exception as err:
print(f"err: {err}")
return [{"error": str(err)}]
def upload_yml_to_s3(yml_filename: str, region: str) -> str:
"""upload yml file to s3 and get presigned url
Args:
yml_filename (str): yml file name to upload
region (str): target s3 region
Returns:
str: yml presigned url
"""
s3_client = boto3.client('s3', region_name=region)
s3_destination_bucket = f"{ENV}-{PROJECT_NAME}-infra-cfn"
s3_client.upload_file(
yml_filename, s3_destination_bucket, yml_filename)
s3_source_signed_url = s3_client.generate_presigned_url('get_object',
Params={
'Bucket': s3_destination_bucket,
'Key': yml_filename
},
ExpiresIn=SIGNED_URL_TIMEOUT_SECONDS)
return s3_source_signed_url
def create_param(param_master: dict, params: List[str]) -> List[dict]:
"""create parameter dictionary from parameter master and parameter list
Args:
param_master (dict): includes all parameters
params (List[str]): parameters to use for a yml file
Returns:
List[dict]: list of parameter key-value dictionary to use for a yml file
"""
result = []
for param in params:
if param in param_master:
result.append({
'ParameterKey': param,
'ParameterValue': param_master[param],
})
return result
def is_stack_exists(client, stack_name: str) -> bool:
"""check whether a stack exists
Args:
client (client): boto3 cloudformation client
stack_name (str): stack name
Returns:
bool: whether the stack exists
"""
try:
client.describe_stacks(StackName=stack_name)
return True
except botocore.exceptions.ClientError as err:
if err.response['Error']['Code'] == 'ValidationError':
return False
raise
def create_stack(client, stack_name: str, template_url: str, parameters: List) -> None:
"""create a stack
Args:
client (clinet): boto3 cloudformation client
stack_name (str): stack name
template_url (str): CFn template url
parameters (List): parameters to use for a yml file
"""
client.create_stack(
StackName=stack_name,
TemplateURL=template_url,
Capabilities=['CAPABILITY_NAMED_IAM'],
Parameters=parameters
)
print(f"create_stack started: {stack_name}")
waiter = client.get_waiter('stack_create_complete')
waiter.wait(
StackName=stack_name,
WaiterConfig={
'Delay': 3,
'MaxAttempts': 50
}
)
print(f"create_stack finished: {stack_name}")
def clean_before_ci_pull_request_comments(message_prefix: str) -> None:
"""delete all ci PR comments before the latest commits
"""
comments = fetch_pull_request_comments()
for comment in comments:
if should_delete_comment(comment, message_prefix):
delete_pull_request_comment(comment["url"])
def fetch_pull_request_comments() -> dict:
"""fetch pull request comments
"""
response = requests.get(URL, headers={
"Authorization": f"token {GITHUB_TOKEN}"})
# print("fetch_pull_request_comments response:")
# pprint(response.json(), indent=4) # too long to print
return response.json()
def delete_pull_request_comment(comment_url: str) -> None:
"""delete_pull_request_comment by id
Args:
id (str): pull request comment id
"""
response = requests.delete(comment_url, headers={
"Authorization": f"token {GITHUB_TOKEN}"})
print("delete_pull_request_comment response:")
print(response)
def should_delete_comment(comment: dict, message_prefix: str) -> bool:
"""check whether a comment should be deleted
Args:
comment (dict): pull request comment
Returns:
bool: whether the comment should be deleted
"""
is_user_bot = comment["user"]["login"] == "github-actions[bot]"
is_env_match = comment["body"].startswith(message_prefix)
return is_user_bot and is_env_match
def post_to_pull_request(body: str) -> None:
"""post to pull_request_comments
Args:
body (str): comment body
"""
response = requests.post(URL, json={"body": body}, headers={
"Authorization": f"token {GITHUB_TOKEN}"})
print("post_to_pull_request response:")
pprint(response.json(), indent=4)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--dryrun', action='store_true')
args = parser.parse_args()
main(args.dryrun)