-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_terminate_imagebuilder_gendo.py
executable file
·148 lines (114 loc) · 5.18 KB
/
run_terminate_imagebuilder_gendo.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
#!/usr/bin/env python3.12
from run_common import AWSCli
from run_common import print_message
from run_common import print_session
from run_terminate_imagebuilder_iam import \
terminate_iam_profile_for_imagebuilder
options, args = dict(), list()
if __name__ == "__main__":
from run_common import parse_args
options, args = parse_args()
def filter_gendo_arn_list(target_resouce_list):
arn_list = list()
for r in target_resouce_list:
if r['name'] and r['name'].startswith('gendo_'):
arn_list.append(r['arn'])
return arn_list
def run_terminate_image(name):
aws_cli = AWSCli()
account_id = aws_cli.get_caller_account_id()
print_message(f'delete imagebuilder {name} ami')
cmd = ['imagebuilder', 'list-images']
rr = aws_cli.run(cmd)
ami_arn_list = filter_gendo_arn_list(rr['imageVersionList'])
for ami_arn in ami_arn_list:
cmd = ['imagebuilder', 'list-image-build-versions']
cmd += ['--image-version-arn', ami_arn]
rr = aws_cli.run(cmd)
for r in rr['imageSummaryList']:
cmd = ['imagebuilder', 'list-image-build-versions']
cmd += ['--image-version-arn', ami_arn]
arn_version_list = aws_cli.run(cmd, ignore_error=True)
if arn_version_list['imageSummaryList']:
cmd = ['imagebuilder', 'delete-image']
cmd += ['--image-build-version-arn', r['arn']]
aws_cli.run(cmd, ignore_error=True)
print_message(f'delete ec2 {name} ami and snapshot')
cmd = ['ec2', 'describe-images']
cmd += ['--filters=Name=name,Values="Gendo*"']
cmd += ['--owners', account_id]
rr = aws_cli.run(cmd)
for r in rr['Images']:
ami = r['ImageId']
snapshot_id = r['BlockDeviceMappings'][0]['Ebs']['SnapshotId']
cmd = ['ec2', 'deregister-image']
cmd += ['--image-id', ami]
aws_cli.run(cmd)
cmd = ['ec2', 'delete-snapshot']
cmd += ['--snapshot-id', snapshot_id]
aws_cli.run(cmd)
print_message('delete cloudwatch image builder logs')
cmd = ['logs', 'describe-log-groups']
cmd += ['--log-group-name-prefix', '/aws/imagebuilder/gendo_']
rr = aws_cli.run(cmd, ignore_error=True)
for log_group in rr['logGroups']:
cmd = ['logs', 'delete-log-group']
cmd += ['--log-group-name', log_group['logGroupName']]
aws_cli.run(cmd, ignore_error=True)
print_message(f'delete imagebuilder {name} pipe lines')
cmd = ['imagebuilder', 'list-image-pipelines']
rr = aws_cli.run(cmd)
pipe_line_list = filter_gendo_arn_list(rr['imagePipelineList'])
for pipe_line in pipe_line_list:
cmd = ['imagebuilder', 'delete-image-pipeline']
cmd += ['--image-pipeline-arn', pipe_line]
aws_cli.run(cmd)
print_message(f'delete imagebuilder {name} distributions')
cmd = ['imagebuilder', 'list-distribution-configurations']
rr = aws_cli.run(cmd)
distribution_list = filter_gendo_arn_list(rr['distributionConfigurationSummaryList'])
for distribution in distribution_list:
cmd = ['imagebuilder', 'delete-distribution-configuration']
cmd += ['--distribution-configuration-arn', distribution]
aws_cli.run(cmd)
print_message(f'delete imagebuilder {name} infrastructures')
cmd = ['imagebuilder', 'list-infrastructure-configurations']
rr = aws_cli.run(cmd)
infrastructure_list = filter_gendo_arn_list(rr['infrastructureConfigurationSummaryList'])
for infrastructure in infrastructure_list:
cmd = ['imagebuilder', 'delete-infrastructure-configuration']
cmd += ['--infrastructure-configuration-arn', infrastructure]
aws_cli.run(cmd)
print_message(f'delete imagebuilder {name} image-recipes')
cmd = ['imagebuilder', 'list-image-recipes']
rr = aws_cli.run(cmd)
image_recipe_list = filter_gendo_arn_list(rr['imageRecipeSummaryList'])
for image_recipe in image_recipe_list:
cmd = ['imagebuilder', 'delete-image-recipe']
cmd += ['--image-recipe-arn', image_recipe]
aws_cli.run(cmd)
print_message(f'delete imagebuilder {name} components')
cmd = ['imagebuilder', 'list-components']
rr = aws_cli.run(cmd)
component_list = filter_gendo_arn_list(rr['componentVersionList'])
for component_arn in component_list:
cmd = ['imagebuilder', 'list-component-build-versions']
cmd += ['--component-version-arn', component_arn]
rr = aws_cli.run(cmd)
for r in rr['componentSummaryList']:
cmd = ['imagebuilder', 'list-component-build-versions']
cmd += ['--component-version-arn', component_arn]
arn_version_list = aws_cli.run(cmd, ignore_error=True)
if arn_version_list['componentSummaryList']:
cmd = ['imagebuilder', 'delete-component']
cmd += ['--component-build-version-arn', r['arn']]
aws_cli.run(cmd, ignore_error=True)
terminate_iam_profile_for_imagebuilder(name)
################################################################################
#
# start
#
################################################################################
print_session('terminate imagebuilder gendo')
name = 'gendo'
run_terminate_image(name)