-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_ec2.py
64 lines (48 loc) · 2.12 KB
/
create_ec2.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
import boto3
import time
ec2 = boto3.resource('ec2')
IMAGE_ID = 'ami-8278baef'
def get_instances_ips():
return [instance.public_ip_address for instance in ec2.instances.filter()]
def create_master(image_id):
userdata = '''#cloud-config
runcmd:
- [ sh, -c, "systemctl enable docker.service" ]
- [ sh, -c, "systemctl start docker-storage-setup.service --ignore-dependencies" ]
- [ sh, -c, "systemctl start docker.service --ignore-dependencies" ]
- [ sh, -c, "docker pull mboustani/scispark_cluster_master" ]
- [ sh, -c, "docker run -d --name scispark_master -it -p 5050:5050 mboustani/scispark_cluster_master" ]
'''
ec2.create_instances(ImageId=image_id, \
MinCount=1, \
MaxCount=1, \
InstanceType='t2.large', \
SecurityGroups=['Spark_Cluster_Mesos'], \
KeyName='esip_workshop', \
UserData=userdata)
def create_slave(image_id, master_ip):
userdata = '''#cloud-config
runcmd:
- [ sh, -c, "systemctl enable docker.service" ]
- [ sh, -c, "systemctl start docker-storage-setup.service --ignore-dependencies" ]
- [ sh, -c, "systemctl start docker.service --ignore-dependencies" ]
- [ sh, -c, "docker pull mboustani/scispark_cluster_slave" ]
- [ sh, -c, "docker run -d --name scispark_slave -it --privileged -e master_ip={0}:5050 mboustani/scispark_cluster_slave" ]
'''.format(master_ip)
ec2.create_instances(ImageId=image_id, \
MinCount=2, \
MaxCount=2, \
InstanceType='t2.large', \
SecurityGroups=['Spark_Cluster_Mesos'], \
KeyName='esip_workshop', \
UserData=userdata)
old_instances = get_instances_ips()
create_master(IMAGE_ID)
time.sleep(2)
new_instances = get_instances_ips()
while new_instances == old_instances:
time.sleep(1)
new_instances = get_instances_ips()
master_ip = list(set(new_instances) - set(old_instances))[0]
print master_ip
create_slave(IMAGE_ID, master_ip)