-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathrun_create_appstream_stack_fleet.py
executable file
·286 lines (217 loc) · 8.62 KB
/
run_create_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
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
#!/usr/bin/env python3
import json
import time
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 create_iam_for_appstream(settings):
aws_cli = AWSCli()
sleep_required = False
role_name = 'AmazonAppStreamServiceAccess'
if not aws_cli.get_iam_role(role_name):
print_message(f'create iam role: {role_name}')
cc = ['iam', 'create-role']
cc += ['--role-name', role_name]
cc += ['--path', '/service-role/']
cc += ['--assume-role-policy-document', 'file://aws_iam/aws-appstream-role.json']
aws_cli.run(cc)
cc = ['iam', 'attach-role-policy']
cc += ['--role-name', role_name]
cc += ['--policy-arn', 'arn:aws:iam::aws:policy/service-role/AmazonAppStreamServiceAccess']
aws_cli.run(cc)
sleep_required = True
role_name = 'ApplicationAutoScalingForAmazonAppStreamAccess'
if not aws_cli.get_iam_role(role_name):
print_message(f'create iam role: {role_name}')
cc = ['iam', 'create-role']
cc += ['--role-name', role_name]
cc += ['--assume-role-policy-document', 'file://aws_iam/aws-appstream-role.json']
aws_cli.run(cc)
cc = ['iam', 'attach-role-policy']
cc += ['--role-name', role_name]
cc += ['--policy-arn', 'arn:aws:iam::aws:policy/service-role/ApplicationAutoScalingForAmazonAppStreamAccess']
aws_cli.run(cc)
sleep_required = True
role_name = 'aws-appstream-naoko-fleet-role'
if not aws_cli.get_iam_role(role_name):
print_message(f'create iam role: {role_name}')
cc = ['iam', 'create-role']
cc += ['--role-name', role_name]
cc += ['--assume-role-policy-document', 'file://aws_iam/aws-appstream-role.json']
aws_cli.run(cc)
sleep(5)
dd = {
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListObjectsInBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": f"arn:aws:s3:::{settings['AWS_S3_SCRIPT_BUCKET']}"
},
{
"Sid": "PutAndGetObjectActions",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:GetObject",
"s3:DeleteObject"
],
"Resource": f"arn:aws:s3:::{settings['AWS_S3_SCRIPT_BUCKET']}/*"
}
]
}
cmd = ['iam', 'put-role-policy']
cmd += ['--role-name', role_name]
cmd += ['--policy-name', 'aws-appstream-naoko-fleet-policy']
cmd += ['--policy-document', json.dumps(dd)]
aws_cli.run(cmd)
sleep_required = True
if sleep_required:
print_message('wait 30 seconds to let iam role and policy propagated to all regions...')
time.sleep(30)
def create_fleet(name, image_name, subnet_ids, security_group_id, desired_instances, fleet_region):
vpc_config = f'SubnetIds={subnet_ids},SecurityGroupIds={security_group_id}'
aws_cli = AWSCli(fleet_region)
fleet_role_arn = aws_cli.get_role_arn('aws-appstream-naoko-fleet-role')
cmd = ['appstream', 'create-fleet']
cmd += ['--name', name]
cmd += ['--instance-type', 'stream.standard.medium']
cmd += ['--fleet-type', 'ON_DEMAND']
cmd += ['--compute-capacity', f'DesiredInstances={desired_instances}']
cmd += ['--image-name', image_name]
cmd += ['--vpc-config', vpc_config]
cmd += ['--no-enable-default-internet-access']
cmd += ['--idle-disconnect-timeout-in-seconds', '600']
cmd += ['--iam-role-arn', fleet_role_arn]
# cmd += ["--disconnect-timeout-in-seconds", '60']
# cmd += ["--max-user-duration-in-seconds", '60~360000']
aws_cli.run(cmd)
sleep(10)
cmd = ['appstream', 'start-fleet']
cmd += ['--name', name]
aws_cli.run(cmd)
def create_stack(stack_name, embed_host_domains, stack_region):
name = stack_name
user_settings = list()
user_settings.append('Action=CLIPBOARD_COPY_FROM_LOCAL_DEVICE,Permission=ENABLED')
user_settings.append('Action=CLIPBOARD_COPY_TO_LOCAL_DEVICE,Permission=ENABLED')
user_settings.append('Action=FILE_UPLOAD,Permission=ENABLED')
user_settings.append('Action=FILE_DOWNLOAD,Permission=ENABLED')
aws_cli = AWSCli(stack_region)
cmd = ['appstream', 'create-stack']
cmd += ['--name', name]
cmd += ['--user-settings'] + user_settings
cmd += ['--embed-host-domains', embed_host_domains]
aws_cli.run(cmd)
def associate_fleet(stack_name, fleet_name, fleet_region):
aws_cli = AWSCli(fleet_region)
cmd = ['appstream', 'associate-fleet']
cmd += ['--fleet-name', fleet_name]
cmd += ['--stack-name', stack_name]
return aws_cli.run(cmd)
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 'RUNNING' == r['State']:
is_waiting = False
if elapsed_time > 1200:
raise Exception('timeout: creating fleet (%s)' % name)
sleep(5)
print('waiting for fleet ready... (elapsed time: \'%d\' seconds)' % elapsed_time)
elapsed_time += 5
def get_subnet_and_security_group_id(vpc_region):
service_name = env['common'].get('SERVICE_NAME', '')
name_prefix = '%s_' % service_name if service_name else ''
aws_cli = AWSCli(vpc_region)
cidr_subnet = aws_cli.cidr_subnet
print_message('get vpc id')
rds_vpc_id, eb_vpc_id = aws_cli.get_vpc_id()
if not eb_vpc_id:
print('ERROR!!! No VPC found')
raise Exception()
print_message('get subnet id')
subnet_id_1 = None
subnet_id_2 = None
cmd = ['ec2', 'describe-subnets']
rr = aws_cli.run(cmd)
for r in rr['Subnets']:
if r['VpcId'] != eb_vpc_id:
continue
if r['CidrBlock'] == cidr_subnet['eb']['private_1']:
subnet_id_1 = r['SubnetId']
if r['CidrBlock'] == cidr_subnet['eb']['private_2']:
subnet_id_2 = r['SubnetId']
print_message('get security group id')
security_group_id = None
cmd = ['ec2', 'describe-security-groups']
rr = aws_cli.run(cmd)
for r in rr['SecurityGroups']:
if r['VpcId'] != eb_vpc_id:
continue
if r['GroupName'] == '%seb_private' % name_prefix:
security_group_id = r['GroupId']
break
return [subnet_id_1, subnet_id_2], security_group_id
def get_latest_image(image_region):
aws_cli = AWSCli(image_region)
ll = list()
cmd = ['appstream', 'describe-images']
rr = aws_cli.run(cmd)
for r in rr['Images']:
if not r['Name'].startswith('naoko-windows'):
continue
ll.append(r['Name'])
if len(ll) < 1:
raise Exception('image not found: naoko-windows*')
return sorted(ll, reverse=True)[0]
################################################################################
#
# start
#
################################################################################
print_session('create appstream image 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
create_iam_for_appstream(settings)
desired_instances = settings.get('DESIRED_INSTANCES', 1)
embed_host_domains = settings['EMBED_HOST_DOMAINS']
fleet_name = settings['FLEET_NAME']
region = settings['AWS_REGION']
stack_name = settings['NAME']
image_name = get_latest_image(region)
subnet_ids, security_group_id = get_subnet_and_security_group_id(region)
create_fleet(fleet_name, image_name, ','.join(subnet_ids), security_group_id, desired_instances, region)
create_stack(stack_name, embed_host_domains, region)
wait_state(fleet_name, region)
associate_fleet(stack_name, fleet_name, region)
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')