-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5c600d3
Showing
5 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
bin | ||
lib | ||
lib64 | ||
include | ||
*.swp | ||
*.pyc |
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
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 |
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
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') | ||
|
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
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 | ||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|