forked from HardBoiledSmith/johanna
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_terminate_s3.py
executable file
·80 lines (63 loc) · 2.66 KB
/
run_terminate_s3.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
#!/usr/bin/env python3
import os
from env import env
from run_common import AWSCli
from run_common import print_message
from run_common import print_session
args = []
if __name__ == "__main__":
from run_common import parse_args
args = parse_args()
aws_cli = AWSCli()
def run_terminate_s3_webapp(name, settings):
deploy_bucket_name = settings['BUCKET_NAME']
bucket_prefix = settings.get('BUCKET_PREFIX', '')
deploy_bucket_prefix = os.path.normpath('%s/%s' % (deploy_bucket_name, bucket_prefix))
################################################################################
print_message('terminate %s' % name)
################################################################################
print_message('cleanup deploy bucket')
cmd = ['s3', 'rm', 's3://%s' % deploy_bucket_prefix, '--recursive']
delete_excluded_files = list(settings.get('DELETE_EXCLUDED_FILES', ''))
for ff in delete_excluded_files:
cmd += ['--exclude', '%s' % ff]
delete_result = aws_cli.run(cmd)
for ll in delete_result.split('\n'):
print(ll)
################################################################################
print_message('remove tag from deploy bucket')
cmd = ['s3api', 'delete-bucket-tagging', '--bucket', deploy_bucket_name]
aws_cli.run(cmd)
################################################################################
print_message('invalidate cache from cloudfront')
cf_dist_id = settings.get('CLOUDFRONT_DIST_ID', '')
if len(cf_dist_id) > 0:
path_list = list(settings['INVALIDATE_PATHS'])
cmd = ['cloudfront', 'create-invalidation', '--distribution-id', cf_dist_id, '--paths', ' '.join(path_list)]
invalidate_result = aws_cli.run(cmd)
print(invalidate_result)
################################################################################
#
# start
#
################################################################################
print_session('terminate s3')
s3_list = env.get('s3', list())
if len(args) == 2:
target_s3_name = args[1]
target_s3_name_exists = False
for s3_env in s3_list:
if s3_env['NAME'] == target_s3_name:
target_s3_name_exists = True
if s3_env['TYPE'] == 'angular-app':
run_terminate_s3_webapp(s3_env['NAME'], s3_env)
break
if not target_s3_name_exists:
print('"%s" is not exists in config.json' % target_s3_name)
else:
for s3_env in s3_list:
if s3_env['TYPE'] == 'angular-app':
run_terminate_s3_webapp(s3_env['NAME'], s3_env)
continue
print('"%s" is not supported' % s3_env['TYPE'])
raise Exception()