-
Notifications
You must be signed in to change notification settings - Fork 16
/
run_terminate_cloudfront.py
executable file
·119 lines (89 loc) · 3.21 KB
/
run_terminate_cloudfront.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
#!/usr/bin/env python3
import json
import re
from argparse import ArgumentParser
from time import sleep
from run_common import AWSCli
from run_common import _confirm_phase
def parse_args():
parser = ArgumentParser()
parser.add_argument('-f', '--force', action='store_true', help='pass confirm')
parser.add_argument('-c', '--cname', type=str, required=True, help='cname')
args = parser.parse_args()
if not args.force:
_confirm_phase()
return args
def find_cloudfront(domain_name):
aws_cli = AWSCli()
cmd = ['cloudfront', 'list-distributions']
rr = aws_cli.run(cmd)
for vv in rr['DistributionList']['Items']:
if 'Items' in vv['Aliases'] and domain_name in vv['Aliases']['Items']:
return vv
def delete_cloudfront(id):
aws_cli = AWSCli()
cmd = ['cloudfront', 'get-distribution']
cmd += ['--id', id]
rr = aws_cli.run(cmd)
e_tag = rr['ETag']
cmd = ['cloudfront', 'delete-distribution']
cmd += ['--id', id]
cmd += ['--if-match', e_tag]
rr = aws_cli.run(cmd)
def disable_cloudfront(id):
aws_cli = AWSCli()
cmd = ['cloudfront', 'get-distribution']
cmd += ['--id', id]
rr = aws_cli.run(cmd)
e_tag = rr['ETag']
if not rr['Distribution']['DistributionConfig']['Enabled']:
return
aws_cli = AWSCli()
cmd = ['cloudfront', 'update-distribution']
cmd += ['--id', id]
cmd += ['--if-match', e_tag]
dd = dict()
dd['PriceClass'] = 'PriceClass_200'
dd['CallerReference'] = domain_name
dd['Origins'] = rr['Distribution']['DistributionConfig']['Origins']
dd['DefaultCacheBehavior'] = rr['Distribution']['DistributionConfig']['DefaultCacheBehavior']
dd['DefaultRootObject'] = rr['Distribution']['DistributionConfig']['DefaultRootObject']
dd['Comment'] = rr['Distribution']['DistributionConfig']['Comment']
dd['Aliases'] = rr['Distribution']['DistributionConfig']['Aliases']
dd['Enabled'] = False
rr['Distribution']['DistributionConfig']['Enabled'] = False
cmd += ['--distribution-config', json.dumps(rr['Distribution']['DistributionConfig'])]
aws_cli.run(cmd)
def wait_cloudfront_status(id, status):
aws_cli = AWSCli()
elapsed_time = 0
is_not_terminate = True
while is_not_terminate:
cmd = ['cloudfront', 'get-distribution']
cmd += ['--id', id]
rr = aws_cli.run(cmd)
ss = rr['Distribution']['Status']
if status == ss:
is_not_terminate = False
if elapsed_time > 1200:
raise Exception('timeout: stop cloudfront(%s)' % id)
sleep(5)
print('wait cloudfront status(%s), but now %s (elapsed time: \'%d\' seconds)' % (status, ss, elapsed_time))
elapsed_time += 5
################################################################################
#
# start
#
################################################################################
if __name__ == "__main__":
args = parse_args()
domain_name = args.cname
cc = re.split('[-.]', domain_name)
if 'dv' not in cc:
print('only can delete dv')
exit(1)
cc = find_cloudfront(domain_name)
id = cc['Id']
disable_cloudfront(id)
wait_cloudfront_status(id, 'Deployed')
delete_cloudfront(id)