forked from rll/rllab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_ec2_for_rllab.py
358 lines (302 loc) · 11 KB
/
setup_ec2_for_rllab.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
import boto3
import re
import sys
import json
import botocore
import os
from rllab.misc import console
from rllab import config
from string import Template
ACCESS_KEY = os.environ["AWS_ACCESS_KEY"]
ACCESS_SECRET = os.environ["AWS_ACCESS_SECRET"]
S3_BUCKET_NAME = os.environ["RLLAB_S3_BUCKET"]
ALL_REGION_AWS_SECURITY_GROUP_IDS = {}
ALL_REGION_AWS_KEY_NAMES = {}
CONFIG_TEMPLATE = Template("""
import os.path as osp
import os
USE_GPU = False
USE_TF = True
AWS_REGION_NAME = "us-west-1"
if USE_GPU:
DOCKER_IMAGE = "dementrock/rllab3-shared-gpu"
else:
DOCKER_IMAGE = "dementrock/rllab3-shared"
DOCKER_LOG_DIR = "/tmp/expt"
AWS_S3_PATH = "s3://$s3_bucket_name/rllab/experiments"
AWS_CODE_SYNC_S3_PATH = "s3://$s3_bucket_name/rllab/code"
ALL_REGION_AWS_IMAGE_IDS = {
"ap-northeast-1": "ami-002f0167",
"ap-northeast-2": "ami-590bd937",
"ap-south-1": "ami-77314318",
"ap-southeast-1": "ami-1610a975",
"ap-southeast-2": "ami-9dd4ddfe",
"eu-central-1": "ami-63af720c",
"eu-west-1": "ami-41484f27",
"sa-east-1": "ami-b7234edb",
"us-east-1": "ami-83f26195",
"us-east-2": "ami-66614603",
"us-west-1": "ami-576f4b37",
"us-west-2": "ami-b8b62bd8"
}
AWS_IMAGE_ID = ALL_REGION_AWS_IMAGE_IDS[AWS_REGION_NAME]
if USE_GPU:
AWS_INSTANCE_TYPE = "g2.2xlarge"
else:
AWS_INSTANCE_TYPE = "c4.2xlarge"
ALL_REGION_AWS_KEY_NAMES = $all_region_aws_key_names
AWS_KEY_NAME = ALL_REGION_AWS_KEY_NAMES[AWS_REGION_NAME]
AWS_SPOT = True
AWS_SPOT_PRICE = '0.5'
AWS_ACCESS_KEY = os.environ.get("AWS_ACCESS_KEY", None)
AWS_ACCESS_SECRET = os.environ.get("AWS_ACCESS_SECRET", None)
AWS_IAM_INSTANCE_PROFILE_NAME = "rllab"
AWS_SECURITY_GROUPS = ["rllab-sg"]
ALL_REGION_AWS_SECURITY_GROUP_IDS = $all_region_aws_security_group_ids
AWS_SECURITY_GROUP_IDS = ALL_REGION_AWS_SECURITY_GROUP_IDS[AWS_REGION_NAME]
FAST_CODE_SYNC_IGNORES = [
".git",
"data/local",
"data/s3",
"data/video",
"src",
".idea",
".pods",
"tests",
"examples",
"docs",
".idea",
".DS_Store",
".ipynb_checkpoints",
"blackbox",
"blackbox.zip",
"*.pyc",
"*.ipynb",
"scratch-notebooks",
"conopt_root",
"private/key_pairs",
]
FAST_CODE_SYNC = True
""")
def setup_iam():
iam_client = boto3.client(
"iam",
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=ACCESS_SECRET,
)
iam = boto3.resource('iam', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=ACCESS_SECRET)
# delete existing role if it exists
try:
existing_role = iam.Role('rllab')
existing_role.load()
# if role exists, delete and recreate
if not query_yes_no(
"There is an existing role named rllab. Proceed to delete everything rllab-related and recreate?",
default="no"):
sys.exit()
print("Listing instance profiles...")
inst_profiles = existing_role.instance_profiles.all()
for prof in inst_profiles:
for role in prof.roles:
print("Removing role %s from instance profile %s" % (role.name, prof.name))
prof.remove_role(RoleName=role.name)
print("Deleting instance profile %s" % prof.name)
prof.delete()
for policy in existing_role.policies.all():
print("Deleting inline policy %s" % policy.name)
policy.delete()
for policy in existing_role.attached_policies.all():
print("Detaching policy %s" % policy.arn)
existing_role.detach_policy(PolicyArn=policy.arn)
print("Deleting role")
existing_role.delete()
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'NoSuchEntity':
pass
else:
raise e
print("Creating role rllab")
iam_client.create_role(
Path='/',
RoleName='rllab',
AssumeRolePolicyDocument=json.dumps({'Version': '2012-10-17', 'Statement': [
{'Action': 'sts:AssumeRole', 'Effect': 'Allow', 'Principal': {'Service': 'ec2.amazonaws.com'}}]})
)
role = iam.Role('rllab')
print("Attaching policies")
role.attach_policy(PolicyArn='arn:aws:iam::aws:policy/AmazonS3FullAccess')
role.attach_policy(PolicyArn='arn:aws:iam::aws:policy/ResourceGroupsandTagEditorFullAccess')
print("Creating inline policies")
iam_client.put_role_policy(
RoleName=role.name,
PolicyName='CreateTags',
PolicyDocument=json.dumps({
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["ec2:CreateTags"],
"Resource": ["*"]
}
]
})
)
iam_client.put_role_policy(
RoleName=role.name,
PolicyName='TerminateInstances',
PolicyDocument=json.dumps({
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1458019101000",
"Effect": "Allow",
"Action": [
"ec2:TerminateInstances"
],
"Resource": [
"*"
]
}
]
})
)
print("Creating instance profile rllab")
iam_client.create_instance_profile(
InstanceProfileName='rllab',
Path='/'
)
print("Adding role rllab to instance profile rllab")
iam_client.add_role_to_instance_profile(
InstanceProfileName='rllab',
RoleName='rllab'
)
def setup_s3():
print("Creating S3 bucket at s3://%s" % S3_BUCKET_NAME)
s3_client = boto3.client(
"s3",
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=ACCESS_SECRET,
)
try:
s3_client.create_bucket(
ACL='private',
Bucket=S3_BUCKET_NAME,
)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'BucketAlreadyExists':
raise ValueError("Bucket %s already exists. Please reconfigure S3_BUCKET_NAME" % S3_BUCKET_NAME) from e
elif e.response['Error']['Code'] == 'BucketAlreadyOwnedByYou':
print("Bucket already created by you")
else:
raise e
print("S3 bucket created")
def setup_ec2():
for region in ["us-east-1", "us-west-1", "us-west-2"]:
print("Setting up region %s" % region)
ec2 = boto3.resource(
"ec2",
region_name=region,
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=ACCESS_SECRET,
)
ec2_client = boto3.client(
"ec2",
region_name=region,
aws_access_key_id=ACCESS_KEY,
aws_secret_access_key=ACCESS_SECRET,
)
existing_vpcs = list(ec2.vpcs.all())
assert len(existing_vpcs) >= 1
vpc = existing_vpcs[0]
print("Creating security group in VPC %s" % str(vpc.id))
try:
security_group = vpc.create_security_group(
GroupName='rllab-sg', Description='Security group for rllab'
)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'InvalidGroup.Duplicate':
sgs = list(vpc.security_groups.filter(GroupNames=['rllab-sg']))
security_group = sgs[0]
else:
raise e
ALL_REGION_AWS_SECURITY_GROUP_IDS[region] = [security_group.id]
ec2_client.create_tags(Resources=[security_group.id], Tags=[{'Key': 'Name', 'Value': 'rllab-sg'}])
try:
security_group.authorize_ingress(FromPort=22, ToPort=22, IpProtocol='tcp', CidrIp='0.0.0.0/0')
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'InvalidPermission.Duplicate':
pass
else:
raise e
print("Security group created with id %s" % str(security_group.id))
key_name = 'rllab-%s' % region
try:
print("Trying to create key pair with name %s" % key_name)
key_pair = ec2_client.create_key_pair(KeyName=key_name)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == 'InvalidKeyPair.Duplicate':
if not query_yes_no("Key pair with name %s exists. Proceed to delete and recreate?" % key_name, "no"):
sys.exit()
print("Deleting existing key pair with name %s" % key_name)
ec2_client.delete_key_pair(KeyName=key_name)
print("Recreating key pair with name %s" % key_name)
key_pair = ec2_client.create_key_pair(KeyName=key_name)
else:
raise e
key_pair_folder_path = os.path.join(config.PROJECT_PATH, "private", "key_pairs")
file_name = os.path.join(key_pair_folder_path, "%s.pem" % key_name)
print("Saving keypair file")
console.mkdir_p(key_pair_folder_path)
with os.fdopen(os.open(file_name, os.O_WRONLY | os.O_CREAT, 0o600), 'w') as handle:
handle.write(key_pair['KeyMaterial'] + '\n')
# adding pem file to ssh
os.system("ssh-add %s" % file_name)
ALL_REGION_AWS_KEY_NAMES[region] = key_name
def write_config():
print("Writing config file...")
content = CONFIG_TEMPLATE.substitute(
all_region_aws_key_names=json.dumps(ALL_REGION_AWS_KEY_NAMES, indent=4),
all_region_aws_security_group_ids=json.dumps(ALL_REGION_AWS_SECURITY_GROUP_IDS, indent=4),
s3_bucket_name=S3_BUCKET_NAME,
)
config_personal_file = os.path.join(config.PROJECT_PATH, "rllab/config_personal.py")
if os.path.exists(config_personal_file):
if not query_yes_no("rllab/config_personal.py exists. Override?", "no"):
sys.exit()
with open(config_personal_file, "wb") as f:
f.write(content.encode("utf-8"))
def setup():
setup_s3()
setup_iam()
setup_ec2()
write_config()
def query_yes_no(question, default="yes"):
"""Ask a yes/no question via raw_input() and return their answer.
"question" is a string that is presented to the user.
"default" is the presumed answer if the user just hits <Enter>.
It must be "yes" (the default), "no" or None (meaning
an answer is required of the user).
The "answer" return value is True for "yes" or False for "no".
"""
valid = {"yes": True, "y": True, "ye": True,
"no": False, "n": False}
if default is None:
prompt = " [y/n] "
elif default == "yes":
prompt = " [Y/n] "
elif default == "no":
prompt = " [y/N] "
else:
raise ValueError("invalid default answer: '%s'" % default)
while True:
sys.stdout.write(question + prompt)
choice = input().lower()
if default is not None and choice == '':
return valid[default]
elif choice in valid:
return valid[choice]
else:
sys.stdout.write("Please respond with 'yes' or 'no' "
"(or 'y' or 'n').\n")
if __name__ == "__main__":
setup()