Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mikery committed Apr 2, 2012
0 parents commit 5c600d3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
bin
lib
lib64
include
*.swp
*.pyc
31 changes: 31 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Some helpers to make running Fabric tasks on EC2 instances a little easier.

Usage::

from fabric.api import run, env
from fabric_ec2 import get_instances_for_tags
# Define a roledefs mapping to build a mapping of roles:hosts, based on the host's
# EC2 tags
env.roledefs = {
# Search in a single, specified region...
'webserver': get_instances_for_tags({'role': 'webserver'}, ['eu-west-1', ]),
# ... or search all regions
'dbserver': get_instances_for_tags({'role': 'dbserver'}),
}

@roles('webserver')
def host_type():
run('hostname')

Notes:

* Make sure to set up AWS credentials, as described in the boto docs
* Checking in all regions can be slow, restrict the server to a smaller number
of regions if possible. Multiple roledefs can help here, e.g. create roledefs
for "EU web servers", "US-1 web servers" and so on.


TODO:

* setup.py
16 changes: 16 additions & 0 deletions fabfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from fabric.api import run, env, roles
from fabric_ec2 import get_instances_for_tags

# Define a roledefs mapping to build a mapping of roles:hosts, based on the host's
# EC2 tags
env.roledefs = {
# Search in a single, specified region...
'webserver': get_instances_for_tags({'Name': 'puppet.goteam.be'}, ['eu-west-1', ]),
# ... or search all regions
#'dbserver': get_instances_for_tags({'role': 'dbserver'}),
}

@roles('webserver')
def host_type():
run('hostname')

30 changes: 30 additions & 0 deletions fabric_ec2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from boto import ec2


def get_instances_for_tags(tags, ec2_regions=None):
""" Returns a list of hosts, matching the given tags/ec2_regions.
"""
# Use default regions (taken from boto) if user didn't specify a list
if not ec2_regions:
ec2_regions = ['us-east-1', 'us-west-1', 'us-west-2',
'sa-east-1',
'eu-west-1',
'ap-southeast-1',
'ap-northeast-1']

tag_filter = {}
for key, val in tags.iteritems():
tag_filter['tag:%s' % key] = val

host_list = []
for region in ec2_regions:
conn = ec2.connect_to_region(region)
reservations = conn.get_all_instances(None, tag_filter)
for res in reservations:
for instance in res.instances:
if instance.public_dns_name != '':
# Terminated/stopped instances will not have a public_dns_name
host_list.append(instance.public_dns_name)
return host_list


1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

0 comments on commit 5c600d3

Please sign in to comment.