-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from crccheck/plumbum
add utility to help me generate config files
- Loading branch information
Showing
5 changed files
with
144 additions
and
1 deletion.
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
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,20 @@ | ||
{# This is a Jinja2 comment | ||
To use this template, you'd run something like: | ||
python plumbum.py config.yaml.j2.example ec2 | ||
#} | ||
# Sample config.yaml | ||
Metrics: | ||
{% for instance in resources %} | ||
- Namespace: "AWS/EC2" | ||
MetricName: "CPUUtilization" | ||
Statistics: | ||
- "Maximum" | ||
- "Average" | ||
Unit: "Percent" | ||
Dimensions: | ||
InstanceId: "{{ instance.id }}" | ||
Options: | ||
{# I'm assuming my tag names are safe to use as metric names here #} | ||
Formatter: 'cloudwatch.%(Namespace)s.{{ instance.tags['Name'] }}.%(MetricName)s.%(statistic)s.%(Unit)s' | ||
Period: 5 | ||
{% endfor %} |
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,114 @@ | ||
# -*- coding: UTF-8 -*- | ||
""" | ||
Usage: | ||
plumbum <template> <namespace> [options]... | ||
Examples: | ||
plumbum elb.yaml.j2 elb | ||
plumbum ec2.yaml.j2 ec2 environment=production | ||
Outputs to stdout. | ||
""" | ||
from __future__ import unicode_literals | ||
|
||
import sys | ||
|
||
import boto.ec2 | ||
import boto.ec2.elb | ||
import boto.rds | ||
import jinja2 | ||
|
||
|
||
def get_property_func(key): | ||
""" | ||
Get the accessor function for an instance to look for `key`. | ||
Look for it as an attribute, and if that does not work, look to see if it | ||
is a tag. | ||
""" | ||
def get_it(obj): | ||
try: | ||
return getattr(obj, key) | ||
except AttributeError: | ||
return obj.tags.get(key) | ||
return get_it | ||
|
||
|
||
def filter_key(filter_args): | ||
def filter_instance(instance): | ||
return all([value == get_property_func(key)(instance) | ||
for key, value in filter_args.items()]) | ||
return filter_instance | ||
|
||
|
||
def lookup(instances, filter_by=None): | ||
if filter_by: | ||
return filter(filter_key(filter_by), instances) | ||
return instances | ||
|
||
|
||
def get_options(input_args): | ||
filter_by_kwargs = {} | ||
for arg in input_args: | ||
if arg.startswith('-'): | ||
# ignore options | ||
continue | ||
if '=' in arg: | ||
key, value = arg.split('=', 2) | ||
filter_by_kwargs[key] = value | ||
return filter_by_kwargs | ||
|
||
|
||
def list_ec2(filter_by_kwargs): | ||
conn = boto.ec2.connect_to_region('us-east-1') # XXX magic constant | ||
instances = conn.get_only_instances() | ||
return lookup(instances, filter_by=filter_by_kwargs) | ||
|
||
|
||
def list_elb(filter_by_kwargs): | ||
conn = boto.ec2.elb.connect_to_region('us-east-1') # XXX magic constant | ||
instances = conn.get_all_load_balancers() | ||
return lookup(instances, filter_by=filter_by_kwargs) | ||
|
||
|
||
def list_rds(filter_by_kwargs): | ||
conn = boto.rds.connect_to_region('us-east-1') | ||
instances = conn.get_all_dbinstances() | ||
return lookup(instances, filter_by=filter_by_kwargs) | ||
|
||
|
||
def main(): | ||
if len(sys.argv) < 3: | ||
print __doc__ | ||
sys.exit() | ||
options = sys.argv[1:] | ||
template = options[0] | ||
namespace = options[1].lower() | ||
filters = get_options(options[2:]) | ||
|
||
# get the template first so this can fail before making a network request | ||
loader = jinja2.FileSystemLoader('.') | ||
jinja2_env = jinja2.Environment(loader=loader) | ||
template = jinja2_env.get_template(template) | ||
|
||
# should I be using ARNs? | ||
if namespace in ('ec2', 'aws/ec2'): | ||
resources = list_ec2(filters) | ||
elif namespace in ('elb', 'aws/elb'): | ||
resources = list_elb(filters) | ||
elif namespace in ('rds', 'aws/rds'): | ||
resources(filters) | ||
else: | ||
# TODO | ||
sys.exit(1) | ||
|
||
print template.render({ | ||
'filters': filters, | ||
'resources': resources, | ||
}) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
boto==2.33.0 | ||
PyYAML==3.11 | ||
docopt==0.6.2 | ||
Jinja2==2.7.3 | ||
|
||
# tests | ||
tox==1.8.0 | ||
|
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 |
---|---|---|
|
@@ -8,16 +8,18 @@ | |
author='Chris Chang', | ||
author_email='[email protected]', | ||
url='https://github.com/crccheck/cloudwatch-to-graphite', | ||
py_modules=['leadbutt'], | ||
py_modules=['leadbutt', 'plumbum'], | ||
entry_points={ | ||
'console_scripts': [ | ||
'leadbutt = leadbutt:main', | ||
'plumbum = plumbum:main', | ||
], | ||
}, | ||
install_requires=[ | ||
'boto', | ||
'PyYAML', | ||
'docopt', | ||
'Jinja2', | ||
], | ||
license='Apache License, Version 2.0', | ||
long_description=open('README.rst').read(), | ||
|