Skip to content

Instantly share code, notes, and snippets.

@etchalon
Created August 6, 2013 15:29
Show Gist options
  • Save etchalon/6165524 to your computer and use it in GitHub Desktop.
Save etchalon/6165524 to your computer and use it in GitHub Desktop.
EC2 + Boto + Fabric
def create_new_fetcher():
conn = EC2Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
image = conn.get_image(settings.AWS_FETCHER_IMAGE_ID)
new_reservation = image.run(key_name='zumur', security_groups=['zumur.fetcher'], instance_type='t1.micro')
new_instance = new_reservation.instances[0]
conn.create_tags([new_instance.id], {"Name": 'Fetcher', 'zumur.role':'fetcher'})
return True
@task()
def cycle_fetchers():
print "Checking fetchers"
key_name = os.path.join(settings.PROJECT_ROOT, 'keys', 'zumur.pem')
conn = EC2Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
reservations = conn.get_all_instances()
one_down = False
valid_fetchers = 0
for reservation in reservations:
for instance in reservation.instances:
if 'zumur.role' in instance.tags and instance.tags['zumur.role'] == 'fetcher' and instance.tags['Name'] == 'Fetcher' and instance.state == 'running':
# Make sure the fetcher is running. Restart it.
valid_fetchers += 1
with fabric_settings(user='ubuntu', key_filename=key_name, host_string=instance.public_dns_name):
run('/home/ubuntu/zumur/zumur-fetcher/bin/startup.sh -daemon', warn_only=True)
one_hour_ago = datetime.datetime.now() - datetime.timedelta(minutes=60)
if date_parse(instance.launch_time).replace(tzinfo=None) < one_hour_ago.replace(tzinfo=None) and one_down == False:
print "... Spinning up/down a fetcher"
create_new_fetcher()
one_down = True
if 'zumur.role' in instance.tags and instance.tags['zumur.role'] == 'fetcher' and instance.tags['Name'] == 'Fetcher':
if instance.state == 'pending':
valid_fetchers += 1
if valid_fetchers < settings.AWS_FETCHER_COUNT:
print "... need to launch %s fetchers" % (settings.AWS_FETCHER_COUNT - valid_fetchers)
for i in xrange(0, settings.AWS_FETCHER_COUNT - valid_fetchers, 1):
print "... Spinning up a fetcher"
create_new_fetcher()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment