Skip to content

Commit

Permalink
#5 first attempt to do DNS traffic routing (not working correctly yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Apr 17, 2015
1 parent 0f95ce6 commit 1f6e3ff
Show file tree
Hide file tree
Showing 7 changed files with 478 additions and 44 deletions.
1 change: 1 addition & 0 deletions release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ python3 setup.py clean
python3 setup.py test

python3 setup.py sdist upload
python3 setup-meta.py register

git tag ${version}
git push --tags
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
clickclick>=0.5
clickclick>=0.7
pystache
boto>=2.37.0
PyYAML
41 changes: 41 additions & 0 deletions senza/aws.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import collections
import datetime
import boto.cloudformation
import boto.ec2
import boto.iam
import time
Expand Down Expand Up @@ -74,3 +76,42 @@ def resolve_topic_arn(region, topic):
topic_arn = obj['TopicArn']

return topic_arn


def get_stacks(stack_refs: list, region, all=False):
cf = boto.cloudformation.connect_to_region(region)
if all:
status_filter = None
else:
status_filter = [st for st in cf.valid_states if st != 'DELETE_COMPLETE']
stacks = cf.list_stacks(stack_status_filters=status_filter)
for stack in stacks:
if not stack_refs or matches_any(stack.stack_name, stack_refs):
yield stack


def matches_any(cf_stack_name: str, stack_refs: list):
'''
>>> matches_any('foobar-1', [])
False
>>> matches_any('foobar-1', [StackReference(name='foobar', version=None)])
True
>>> matches_any('foobar-1', [StackReference(name='foobar', version='1')])
True
>>> matches_any('foobar-1', [StackReference(name='foobar', version='2')])
False
'''
for ref in stack_refs:
if ref.version and cf_stack_name == ref.cf_stack_name():
return True
elif not ref.version and cf_stack_name.rsplit('-', 1)[0] == ref.name:
return True
return False


class StackReference(collections.namedtuple('StackReference', 'name version')):
def cf_stack_name(self):
return '{}-{}'.format(self.name, self.version)
60 changes: 18 additions & 42 deletions senza/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@

from boto.exception import BotoServerError
import click
from clickclick import AliasedGroup, Action, choice, info
from clickclick import AliasedGroup, Action, choice, info, FloatRange
from clickclick.console import print_table
import collections
import yaml
import pystache
import boto.cloudformation
Expand All @@ -22,12 +21,13 @@
import boto.iam
import boto.sns
import boto.route53
from .aws import parse_time, get_required_capabilities, resolve_topic_arn

from .aws import parse_time, get_required_capabilities, resolve_topic_arn, get_stacks, StackReference
from .components import component_basic_configuration, component_stups_auto_configuration, \
component_auto_scaling_group, component_taupage_auto_scaling_group, \
component_load_balancer, component_weighted_dns_load_balancer
import senza
from .traffic import change_version_traffic
from .utils import named_value, camel_case_to_underscore


Expand Down Expand Up @@ -84,11 +84,6 @@ def convert(self, value, param, ctx):
return data


class StackReference(collections.namedtuple('StackReference', 'name version')):
def cf_stack_name(self):
return '{}-{}'.format(self.name, self.version)


class KeyValParamType(click.ParamType):
name = 'key_val'

Expand Down Expand Up @@ -252,40 +247,6 @@ def get_stack_refs(refs: list):
return stack_refs


def matches_any(cf_stack_name: str, stack_refs: list):
'''
>>> matches_any('foobar-1', [])
False
>>> matches_any('foobar-1', [StackReference(name='foobar', version=None)])
True
>>> matches_any('foobar-1', [StackReference(name='foobar', version='1')])
True
>>> matches_any('foobar-1', [StackReference(name='foobar', version='2')])
False
'''
for ref in stack_refs:
if ref.version and cf_stack_name == ref.cf_stack_name():
return True
elif not ref.version and cf_stack_name.rsplit('-', 1)[0] == ref.name:
return True
return False


def get_stacks(stack_refs: list, region, all=False):
cf = boto.cloudformation.connect_to_region(region)
if all:
status_filter = None
else:
status_filter = [st for st in cf.valid_states if st != 'DELETE_COMPLETE']
stacks = cf.list_stacks(stack_status_filters=status_filter)
for stack in stacks:
if not stack_refs or matches_any(stack.stack_name, stack_refs):
yield stack


@cli.command('list')
@click.option('--region', envvar='AWS_DEFAULT_REGION', metavar='AWS_REGION_ID', help='AWS region ID (e.g. eu-west-1)')
@click.option('--all', is_flag=True, help='Show all stacks, including deleted ones')
Expand Down Expand Up @@ -587,6 +548,21 @@ def domains(stack_ref, region):
rows, styles=STYLES, titles=TITLES)


@cli.command()
@click.argument('stack_ref', nargs=-1)
@click.argument('percentage', type=FloatRange(0, 100, clamp=True))
@click.option('--region', envvar='AWS_DEFAULT_REGION', metavar='AWS_REGION_ID', help='AWS region ID (e.g. eu-west-1)')
def traffic(stack_ref, percentage, region):
'''Route traffic to a specific stack (weighted DNS record)'''
stack_refs = get_stack_refs(stack_ref)
region = get_region(region)

for ref in stack_refs:
if not ref.version:
raise click.UsageError('You must specify the stack version')
change_version_traffic(ref, percentage, region)


def main():
try:
cli()
Expand Down
Loading

0 comments on commit 1f6e3ff

Please sign in to comment.