Created
November 16, 2015 15:55
-
-
Save cbare/d549514a02775bc93af1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################################################ | |
## | |
## Create EC2 Machines | |
## | |
## Start up EC2 instances for each member of a Synapse | |
## team, emailing them the details of their assigned | |
## machine. | |
## | |
## chris.bare@sagebase.org, November 2015 | |
## | |
############################################################ | |
import boto.ec2 | |
import os | |
import synapseclient | |
import sys | |
import time | |
from collections import OrderedDict | |
## connect to Amazon web services EC2, credentials in ~/.boto | |
ec2 = boto.ec2.connect_to_region('us-east-1') | |
## connect to Synapse | |
syn = synapseclient.Synapse() | |
syn.login('chris.bare') | |
TEAM_ID = 3332509 | |
PROJECT_ID = 'syn4957270' | |
AMI_ID = 'ami-21cdb64b' | |
KEY = 'synapse-demo' | |
# To use Jupyter / IPython notebooks, point your browser to: | |
# http://{ip_address}:8888/ | |
# ...and log in with the password "madscientiststu". | |
# Finally, t | |
msg = """\ | |
Hello {user_name}({user_id}), | |
Thanks for participating in the 2015 DREAM Conference Hackathon! | |
We've provisioned a machine with R and Python environments on Amazon's \ | |
cloud your use during the hackathon. | |
To use RStudio on your machine, point your browser to: | |
http://{ip_address}:8787/ | |
...and log in with the username 'XXXXXXXX' and the password 'XXXXXXXX'. | |
To access your machine by SSH, there's a key file on Synapse in \ | |
syn4957270/Files/Logistics/synapse-demo.pem. Download that key to your local \ | |
file system, then: | |
chmod 600 synapse-demo.pem | |
ssh -i synapse-demo.pem ubuntu@{ip_address} | |
These are your machines. Feel free to install what you like. But, be sure to \ | |
save any work you'd like to keep elsewhere as these machines will go away after Monday. | |
Thanks and happy hacking, | |
- Chris and the 2015 DREAM Conference Hackathon team | |
""" | |
def provision_instances(n): | |
## Give the machines a 30GB EBS volume | |
## http://stackoverflow.com/a/13604274/199166 | |
dev_sda1 = boto.ec2.blockdevicemapping.EBSBlockDeviceType() | |
dev_sda1.size = 30 | |
bdm = boto.ec2.blockdevicemapping.BlockDeviceMapping() | |
bdm['/dev/sda1'] = dev_sda1 | |
## Ask EC2 for n instances | |
## http://boto.readthedocs.org/en/latest/ref/ec2.html#boto.ec2.connection.EC2Connection.run_instances | |
reservation = ec2.run_instances(image_id=AMI_ID, | |
key_name=KEY, | |
instance_type='m3.large', | |
security_groups = ['RStudioAndIPythonNonVPC'], | |
block_device_map = bdm, | |
min_count=n, | |
max_count=n) | |
return reservation | |
## read in existing reservations | |
assignments = OrderedDict() | |
with open('instances.txt') as f: | |
for line in f: | |
line = line.strip() | |
if line: | |
fields = line.split('|') | |
record = OrderedDict() | |
record['user_id'] = fields[0] | |
record['user_name'] = fields[1] | |
record['ip_address'] = fields[2] | |
record['reservation'] = fields[3] | |
assignments[record['user_id']] = record | |
## create new machines | |
team = syn.getTeam(TEAM_ID) | |
profiles = [] | |
for i, member in enumerate(syn.getTeamMembers(team)): | |
profile = syn.getUserProfile(member) | |
if profile.ownerId not in assignments: | |
profiles.append(profile) | |
else: | |
print ".", assignments[profile.ownerId] | |
if len(profiles) > 0: | |
reservation = provision_instances(len(profiles)) | |
sys.stdout.write("waiting for instances to become available") | |
sys.stdout.flush() | |
while not all([instance.state=='running' for instance in reservation.instances]): | |
for instance in reservation.instances: | |
instance.update() | |
sys.stdout.write("."); | |
sys.stdout.flush() | |
time.sleep(1) | |
sys.stdout.write("\n") | |
sys.stdout.flush() | |
for instance, profile in zip(reservation.instances, profiles): | |
record = OrderedDict() | |
record['user_id'] = profile.ownerId | |
record['user_name'] = profile.userName | |
record['ip_address'] = instance.ip_address | |
record['reservation'] = reservation.id | |
print "+", record | |
assignments[record['user_id']] = record | |
print msg.format(**record) | |
sent_msg = syn.sendMessage([profile.ownerId], "2015 DREAM Conference Hackathon logistics", msg.format(**record)) | |
## write reservations file | |
with open('instances.txt', 'wt') as f: | |
for record in assignments.values(): | |
f.write('|'.join(record.values()) + "\n") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment