-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrun_terminate_appstream_stack_fleet.py
executable file
·156 lines (117 loc) · 4.32 KB
/
run_terminate_appstream_stack_fleet.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
#!/usr/bin/env python3
from time import sleep
from env import env
from run_common import AWSCli
from run_common import print_message
from run_common import print_session
options, args = dict(), list()
if __name__ == "__main__":
from run_common import parse_args
options, args = parse_args()
def terminate_iam_for_appstream():
aws_cli = AWSCli()
role_name = 'AmazonAppStreamServiceAccess'
print_message('delete iam role')
# noinspection PyShadowingNames
cc = ['iam', 'detach-role-policy']
cc += ['--role-name', role_name]
cc += ['--policy-arn', 'arn:aws:iam::aws:policy/service-role/AmazonAppStreamServiceAccess']
aws_cli.run(cc, ignore_error=True)
# noinspection PyShadowingNames
cc = ['iam', 'delete-role']
cc += ['--role-name', role_name]
aws_cli.run(cc, ignore_error=True)
role_name = 'ApplicationAutoScalingForAmazonAppStreamAccess'
# noinspection PyShadowingNames
cc = ['iam', 'detach-role-policy']
cc += ['--role-name', role_name]
cc += ['--policy-arn', 'arn:aws:iam::aws:policy/service-role/ApplicationAutoScalingForAmazonAppStreamAccess']
aws_cli.run(cc, ignore_error=True)
# noinspection PyShadowingNames
cc = ['iam', 'delete-role']
cc += ['--role-name', role_name]
aws_cli.run(cc, ignore_error=True)
role_name = 'aws-appstream-naoko-fleet-role'
# noinspection PyShadowingNames
cc = ['iam', 'delete-role-policy']
cc += ['--role-name', role_name]
cc += ['--policy-name', 'aws-appstream-naoko-fleet-policy']
aws_cli.run(cc, ignore_error=True)
# noinspection PyShadowingNames
cc = ['iam', 'delete-role']
cc += ['--role-name', role_name]
aws_cli.run(cc, ignore_error=True)
def stop_fleet(fleet_name, fleet_region):
aws_cli = AWSCli(fleet_region)
cmd = ['appstream', 'stop-fleet']
cmd += ['--name', fleet_name]
aws_cli.run(cmd, ignore_error=True)
def delete_fleet(fleet_name, fleet_region):
aws_cli = AWSCli(fleet_region)
cmd = ['appstream', 'delete-fleet']
cmd += ['--name', fleet_name]
return aws_cli.run(cmd, ignore_error=True)
def delete_stack(stack_name, stack_region):
aws_cli = AWSCli(stack_region)
cmd = ['appstream', 'delete-stack']
cmd += ['--name', stack_name]
return aws_cli.run(cmd, ignore_error=True)
def disassociate_fleet(fleet_name, stack_name, fleet_region):
aws_cli = AWSCli(fleet_region)
cmd = ['appstream', 'disassociate-fleet']
cmd += ['--fleet-name', fleet_name]
cmd += ['--stack-name', stack_name]
rr = aws_cli.run(cmd, ignore_error=True)
return bool(rr)
def wait_state(name, fleet_region):
aws_cli = AWSCli(fleet_region)
elapsed_time = 0
is_waiting = True
while is_waiting:
cmd = ['appstream', 'describe-fleets']
cmd += ['--name', name]
rr = aws_cli.run(cmd)
for r in rr['Fleets']:
if 'STOPPED' == r['State']:
is_waiting = False
if elapsed_time > 1200:
raise Exception('timeout: terminating fleet (%s)' % name)
sleep(5)
print('waiting for fleet terminated... (elapsed time: \'%d\' seconds)' % elapsed_time)
elapsed_time += 5
################################################################################
#
# start
#
################################################################################
print_session('terminate appstream stack & fleet')
appstream = env['appstream']
target_name = None
region = options.get('region')
is_target_exists = False
if len(args) > 1:
target_name = args[1]
for settings in appstream.get('STACK', list()):
if target_name and settings['NAME'] != target_name:
continue
if region and settings['AWS_REGION'] != region:
continue
is_target_exists = True
fleet_name = settings['FLEET_NAME']
region = settings['AWS_REGION']
stack_name = settings['NAME']
disassociate_fleet(fleet_name, stack_name, region)
delete_stack(stack_name, region)
stop_fleet(fleet_name, region)
wait_state(fleet_name, region)
delete_fleet(fleet_name, region)
if not target_name and not region:
terminate_iam_for_appstream()
if is_target_exists is False:
mm = list()
if target_name:
mm.append(target_name)
if region:
mm.append(region)
mm = ' in '.join(mm)
print(f'appstream fleet & stack: {mm} is not found in config.json')