/
h2o-cluster-launch-instances.py
229 lines (179 loc) · 6.41 KB
/
h2o-cluster-launch-instances.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
#!/usr/bin/env python
import os
import sys
import time
import boto
import boto.ec2
# Environment variables you MUST set (either here or by passing them in).
# -----------------------------------------------------------------------
#
os.environ['AWS_ACCESS_KEY_ID'] = ''
os.environ['AWS_SECRET_ACCESS_KEY'] = ''
os.environ['AWS_SSH_PRIVATE_KEY_FILE'] = '/home/mohamed/Desktop/aws/created_key.pem'
# Launch EC2 instances with an IAM role
# --------------------------------------
#
iam_profile_resource_name = None
# or
iam_profile_name = None
# Options you MUST tailor to your own AWS account.
# ------------------------------------------------
# SSH key pair name.
keyName = 'created_key'
# AWS security group name.
# Note:
# H2O uses TCP and UDP ports 54321 and 54322.
# RStudio uses TCP port 8787.
securityGroupName = 'SecurityDisabled'
# Options you might want to change.
# ---------------------------------
numInstancesToLaunch = 2
instanceType = 'm4.4xlarge'
instanceNameRoot = 'h2o-instance'
# set to a string USD amount if you'd like to request spot instances
# read more about spot here: https://aws.amazon.com/ec2/spot/
spotBid = None
# Options to help debugging.
# --------------------------
#debug = 0
debug = 0
dryRun = False
# dryRun = True
# Options you should not change unless you really mean to.
# --------------------------------------------------------
regionName = 'eu-central-1'
amiId = 'ami-657bd20a'
#regionName = 'us-east-1'
#amiId = 'ami-bde90fc7'
#--------------------------------------------------------------------------
# No need to change anything below here.
#--------------------------------------------------------------------------
# Note: this python script was initially developed with boto 2.13.3.
def botoVersionMismatch():
print 'WARNING: Unsupported boto version. Please upgrade boto to at least 2.13.x and try again.'
print 'Comment this out to run anyway.'
print 'Exiting.'
sys.exit(1)
if not 'AWS_ACCESS_KEY_ID' in os.environ:
print 'ERROR: You must set AWS_ACCESS_KEY_ID in the environment.'
sys.exit(1)
if not 'AWS_SECRET_ACCESS_KEY' in os.environ:
print 'ERROR: You must set AWS_SECRET_ACCESS_KEY in the environment.'
sys.exit(1)
if not 'AWS_SSH_PRIVATE_KEY_FILE' in os.environ:
print 'ERROR: You must set AWS_SSH_PRIVATE_KEY_FILE in the environment.'
sys.exit(1)
publicFileName = 'nodes-public'
privateFileName = 'nodes-private'
if not dryRun:
fpublic = open(publicFileName, 'w')
fprivate = open(privateFileName, 'w')
print 'Using boto version', boto.Version
if True:
botoVersionArr = boto.Version.split(".")
if (botoVersionArr[0] != 2):
botoVersionMismatch
if (botoVersionArr[1] < 13):
botoVersionMismatch
if (debug):
boto.set_stream_logger('h2o-ec2')
ec2 = boto.ec2.connect_to_region(regionName, debug=debug)
print 'Launching', numInstancesToLaunch, 'instances.'
if spotBid is None:
reservation = ec2.run_instances(
image_id=amiId,
min_count=numInstancesToLaunch,
max_count=numInstancesToLaunch,
key_name=keyName,
instance_type=instanceType,
security_groups=[securityGroupName],
instance_profile_arn=iam_profile_resource_name,
instance_profile_name=iam_profile_name,
dry_run=dryRun
)
else:
spotRequests = ec2.request_spot_instances(
price=spotBid,
image_id=amiId,
count=numInstancesToLaunch,
key_name=keyName,
instance_type=instanceType,
security_groups=[securityGroupName],
instance_profile_arn=iam_profile_resource_name,
instance_profile_name=iam_profile_name,
dry_run=dryRun,
)
requestIDs = [request.id for request in spotRequests]
fulfilled = []
while requestIDs:
requests = ec2.get_all_spot_instance_requests(
request_ids=requestIDs,
)
for request in requests:
if request.instance_id:
requestIDs.remove(request.id)
fulfilled.append(request.instance_id)
# unrecoverable error without increasing the spot bid
elif request.status.code == u'price-too-low':
print request.status.message
print 'Cancelling Spot requests...'
# get original request ids since some requests may have already been fulfilled
ec2.cancel_spot_instance_requests(
request_ids=[request.id for request in spotRequests],
)
print 'Exiting...'
sys.exit(1)
print request.id, request.status.message
print '%s/%s requests fulfilled' % (len(fulfilled), numInstancesToLaunch)
if requestIDs:
print 'Waiting for remaining requests to be fulfilled...'
time.sleep(5)
reservation = ec2.get_all_instances(
instance_ids=fulfilled,
)
print '------------------------------------------------------------------'
#print len(reservation[0].instances)
#print reservation.instances
#instances = reservation[0].instances
instances = reservation.instances
for i, instance in enumerate(instances):
print 'Waiting for instance', i+1, 'of', numInstancesToLaunch, '...'
instance.update()
while instance.state != 'running':
print ' .'
time.sleep(1)
instance.update()
print ' instance', i+1, 'of', numInstancesToLaunch, 'is up.'
name = instanceNameRoot + str(i)
instance.add_tag('Name', value=name)
print
print 'Creating output files: ', publicFileName, privateFileName
print
for i, instance in enumerate(instances):
instanceName = instance.tags.get('Name', '')
print 'Instance', i+1, 'of', numInstancesToLaunch
print ' Name: ', instanceName
print ' PUBLIC: ', instance.public_dns_name
print ' PRIVATE:', instance.private_ip_address
print
fpublic.write(instance.public_dns_name + '\n')
fprivate.write(instance.private_ip_address + '\n')
fpublic.close()
fprivate.close()
print 'Sleeping for 60 seconds for ssh to be available...'
time.sleep(60)
d = os.path.dirname(os.path.realpath(__file__))
print 'Testing ssh access...'
cmd = d + '/' + 'h2o-cluster-test-ssh.sh'
rv = os.system(cmd)
if rv != 0:
print 'Failed.'
sys.exit(1)
print
print 'Distributing flatfile...'
cmd = d + '/' + 'h2o-cluster-distribute-flatfile.sh'
rv = os.system(cmd)
if rv != 0:
print 'Failed.'
sys.exit(1)
# Distribute flatfile script already prints success when it completes.