Skip to content

Commit a6d148b

Browse files
author
Jeff
committed
new cloudbuilder mechanism
1 parent 2d25401 commit a6d148b

File tree

1 file changed

+177
-0
lines changed

1 file changed

+177
-0
lines changed

ec2/cloudbuilder.py

+177
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#!/usr/bin/python
2+
3+
4+
import os
5+
import sys
6+
import time
7+
import boto
8+
import boto.ec2
9+
import argparse
10+
11+
12+
# Define arguments
13+
parser = argparse.ArgumentParser(description='H2o Cloud Launch.')
14+
parser.add_argument('-n','--number',help='Number of Instances',type=int,required=True)
15+
parser.add_argument('-c','--cloudname',help='Name of Cloud',required=True)
16+
args = parser.parse_args()
17+
18+
# Print Output
19+
print ("Cloud size: %s" % args.number )
20+
print ("Cloud name: %s" % args.cloudname )
21+
# Environment variables you MUST set (either here or by passing them in).
22+
# -----------------------------------------------------------------------
23+
#
24+
os.environ['AWS_ACCESS_KEY_ID'] = 'AKIAJFIA2VEGUMLHD5VA'
25+
os.environ['AWS_SECRET_ACCESS_KEY'] = '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'
26+
os.environ['AWS_SSH_PRIVATE_KEY_FILE'] = '/opt/play-h2o-ai.pem'
27+
28+
29+
30+
# Launch EC2 instances with an IAM role
31+
# --------------------------------------
32+
#
33+
iam_profile_resource_name = None
34+
# or
35+
iam_profile_name = None
36+
37+
# Options you MUST tailor to your own AWS account.
38+
# ------------------------------------------------
39+
40+
# SSH key pair name.
41+
keyName = 'play-h2o-ai'
42+
43+
# AWS security group name.
44+
# Note:
45+
# H2O uses TCP and UDP ports 54321 and 54322.
46+
# RStudio uses TCP port 8787.
47+
securityGroupName = 'SecurityDisabled'
48+
49+
50+
# Options you might want to change.
51+
# ---------------------------------
52+
53+
numInstancesToLaunch = args.number
54+
instanceType = 'm1.large'
55+
instanceNameRoot = args.cloudname
56+
57+
58+
# Options to help debugging.
59+
# --------------------------
60+
61+
debug = 0
62+
# debug = 1
63+
dryRun = False
64+
# dryRun = True
65+
66+
67+
# Options you should not change unless you really mean to.
68+
# --------------------------------------------------------
69+
70+
regionName = 'us-east-1'
71+
amiId = 'ami-c0df5ea8'
72+
73+
74+
#--------------------------------------------------------------------------
75+
# No need to change anything below here.
76+
#--------------------------------------------------------------------------
77+
78+
# Note: this python script was initially developed with boto 2.13.3.
79+
def botoVersionMismatch():
80+
print 'WARNING: Unsupported boto version. Please upgrade boto to at least 2.13.x and try again.'
81+
print 'Comment this out to run anyway.'
82+
print 'Exiting.'
83+
sys.exit(1)
84+
85+
if not 'AWS_ACCESS_KEY_ID' in os.environ:
86+
print 'ERROR: You must set AWS_ACCESS_KEY_ID in the environment.'
87+
sys.exit(1)
88+
89+
if not 'AWS_SECRET_ACCESS_KEY' in os.environ:
90+
print 'ERROR: You must set AWS_SECRET_ACCESS_KEY in the environment.'
91+
sys.exit(1)
92+
93+
if not 'AWS_SSH_PRIVATE_KEY_FILE' in os.environ:
94+
print 'ERROR: You must set AWS_SSH_PRIVATE_KEY_FILE in the environment.'
95+
sys.exit(1)
96+
97+
publicFileName = '/usr/share/nginx/html/temp/nodes-public'
98+
privateFileName = '/usr/share/nginx/html/temp/nodes-private'
99+
100+
if not dryRun:
101+
fpublic = open(publicFileName, 'w')
102+
fprivate = open(privateFileName, 'w')
103+
104+
print 'Using boto version', boto.Version
105+
if True:
106+
botoVersionArr = boto.Version.split(".")
107+
if (botoVersionArr[0] != 2):
108+
botoVersionMismatch
109+
if (botoVersionArr[1] < 13):
110+
botoVersionMismatch
111+
112+
if (debug):
113+
boto.set_stream_logger('h2o-ec2')
114+
ec2 = boto.ec2.connect_to_region(regionName, debug=debug)
115+
116+
print 'Launching', numInstancesToLaunch, 'instances.'
117+
118+
reservation = ec2.run_instances(
119+
image_id=amiId,
120+
min_count=numInstancesToLaunch,
121+
max_count=numInstancesToLaunch,
122+
key_name=keyName,
123+
instance_type=instanceType,
124+
security_groups=[securityGroupName],
125+
instance_profile_arn=iam_profile_resource_name,
126+
instance_profile_name=iam_profile_name,
127+
dry_run=dryRun
128+
)
129+
130+
for i in range(numInstancesToLaunch):
131+
instance = reservation.instances[i]
132+
print 'Waiting for instance', i+1, 'of', numInstancesToLaunch, '...'
133+
instance.update()
134+
while instance.state != 'running':
135+
print ' .'
136+
time.sleep(1)
137+
instance.update()
138+
print ' instance', i+1, 'of', numInstancesToLaunch, 'is up.'
139+
name = instanceNameRoot + str(i)
140+
instance.add_tag('Name', value=name)
141+
142+
print
143+
print 'Creating output files: ', publicFileName, privateFileName
144+
print
145+
146+
for i in range(numInstancesToLaunch):
147+
instance = reservation.instances[i]
148+
instanceName = ''
149+
if 'Name' in instance.tags:
150+
instanceName = instance.tags['Name'];
151+
print 'Instance', i+1, 'of', numInstancesToLaunch
152+
print ' Name: ', instanceName
153+
print ' PUBLIC: ', instance.public_dns_name
154+
print ' PRIVATE:', instance.private_ip_address
155+
print
156+
fpublic.write(instance.public_dns_name + '\n')
157+
fprivate.write(instance.private_ip_address + '\n')
158+
159+
fpublic.close()
160+
fprivate.close()
161+
162+
print 'Creating S3 Buckets'
163+
164+
boto.set_stream_logger('boto')
165+
s3 = boto.connect_s3()
166+
bucketName = instanceNameRoot
167+
168+
print 'Creating:', bucketName
169+
bucket = s3.create_bucket(bucketName)
170+
key = bucket.new_key('nodes-public')
171+
key.set_contents_from_filename(publicFileName)
172+
key = bucket.new_key('nodes-private')
173+
key.set_contents_from_filename(privateFileName)
174+
time.sleep(2)
175+
176+
print 'Complete'
177+

0 commit comments

Comments
 (0)