Created
July 29, 2013 22:23
-
-
Save SavvyGuard/6108458 to your computer and use it in GitHub Desktop.
Launch an ec2 instance and then wait until they're ready
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
import sys | |
import boto | |
import boto.ec2 | |
def _launchAWSInstances(): | |
connection = boto.ec2.connect_to_region(AWS_REGION, aws_access_key_id = AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_ACCESS_KEY_SECRET) | |
global reservation | |
reservation = connection.run_instances(AWS_AMI, | |
key_name=AWS_SSH_KEY_NAME, | |
instance_type=AWS_INSTANCE_SIZE, | |
security_groups=[AWS_SECURITY_GROUP]) | |
import time | |
unixtime = int(time.time()) | |
for index, instance in enumerate(reservation.instances): | |
instance.add_tag('Name', 'UDB Machine ' + str(unixtime)) | |
print "Awaiting Instances Ready" | |
for index, instance in enumerate(reservation.instances): | |
_awaitAWSInstanceReady(instance, 0) | |
print "All Instances Ready" | |
# takes an ec2 instance object | |
# recursively calls itself until it's ready | |
def _awaitAWSInstanceReady(instance, loops): | |
time_interval = 1 | |
if instance.update() == 'running': | |
time_elapsed = loops * time_interval | |
print '' | |
print 'Took %d seconds' % time_elapsed | |
return True | |
else: | |
_progressBar(loops) | |
time.sleep(1) | |
_awaitAWSInstanceReady(instance, loops + 1) | |
# simple progress bar | |
def _progressBar(state): | |
progress_bar = '=' * state | |
sys.stdout.write('\r'+progress_bar) | |
sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment