Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
Allows to override the aws profile for route53
Browse files Browse the repository at this point in the history
In certain cases the aws stack belongs to a different sub account
than the route53 domains so certain operations could benefit by
crossing to a different sub account, using different credentials.

For now only the enter_maintenance and exit_maintenance tasks are
aware of this change.
  • Loading branch information
filipposc5 committed Mar 7, 2016
1 parent 291b797 commit 810d0a8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
43 changes: 37 additions & 6 deletions bootstrap_cfn/fab_tasks.py
Expand Up @@ -33,6 +33,7 @@
env.setdefault('stack_passwords')
env.setdefault('blocking', True)
env.setdefault('aws_region', 'eu-west-1')
env.setdefault('aws_r53', None)

# GLOBAL VARIABLES
TIMEOUT = 3600
Expand Down Expand Up @@ -66,6 +67,23 @@ def aws(profile_name):
boto3.setup_default_session(profile_name=env.aws)


@task
def aws_r53(profile_name):
"""
Set the AWS account to use for route53
Sets the environment variable 'aws_r53' to the name of the
account to use in the AWS config file (~/.aws/credentials.yaml)
and allows to override the 'aws' variable, for cases where the
Route53 zones exist under a different AWS account
Args:
profile_name(string): The string to set the environment
variable to
"""
env.aws_r53 = str(profile_name).lower()


@task
def environment(environment_name):
"""
Expand Down Expand Up @@ -214,15 +232,28 @@ def apply_maintenance_criteria(elb):
return elb['scheme'] == 'internet-facing'


def override_route53_if_needed():
'''
Allows to override AWS account for Route53 Maintenance modes
Returns a dictionary with the route53 account specified in aws_r53 from
command line, eg "fab aws:account1 aws_r53:account2"
'''
if aws_r53:
return dict(aws=env.aws_r53)
else:
return {}


@task
def enter_maintenance(maintenance_ip):
def enter_maintenance(maintenance_ip, dry_run=False):
'''
Puts stack into maintenance mode
Sets all internet facing elb hostnames to resolve to given maintenance_ip
'''
cfn_config = get_config()
r53_conn = get_connection(R53)
r53_conn = get_connection(R53, **override_route53_if_needed())

cached_zone_ids = {}
for elb in cfn_config.data['elb']:
Expand All @@ -232,7 +263,7 @@ def enter_maintenance(maintenance_ip):
record = "{name}.{hosted_zone}".format(**elb)
zone_id = get_cached_zone_id(r53_conn, cached_zone_ids, elb['hosted_zone'])
print green("Attempting to update: \"{0}\":\"{1}\"".format(record, maintenance_ip))
r53_conn.update_dns_record(zone_id, record, 'A', maintenance_ip)
r53_conn.update_dns_record(zone_id, record, 'A', maintenance_ip, dry_run=dry_run)


@task
Expand All @@ -243,7 +274,7 @@ def exit_maintenance():
Sets internet-facing elbs hostnames
back to the ELB DNS alias
"""
r53_conn = get_connection(R53)
r53_conn = get_connection(R53, **override_route53_if_needed())
elb_conn = get_connection(ELB)

cfn_config = get_config()
Expand Down Expand Up @@ -453,9 +484,9 @@ def get_config():
return cfn_config


def get_connection(klass):
def get_connection(klass, aws=env.aws, aws_region=env.aws_region):
_validate_fabric_env()
return klass(env.aws, env.aws_region)
return klass(aws, aws_region)


@task
Expand Down
7 changes: 5 additions & 2 deletions bootstrap_cfn/r53.py
Expand Up @@ -39,7 +39,7 @@ def get_hosted_zone_id(self, zone_name):
zone = zone['GetHostedZoneResponse']['HostedZone']['Id']
return zone.replace('/hostedzone/', '')

def update_dns_record(self, zone, record, record_type, record_value, is_alias=False):
def update_dns_record(self, zone, record, record_type, record_value, is_alias=False, dry_run=False):
'''
Updates a dns record in route53
Expand All @@ -62,7 +62,10 @@ def update_dns_record(self, zone, record, record_type, record_value, is_alias=Fa
change.set_alias(*record_value)
else:
change.add_value(record_value)
changes.commit()
if dry_run:
print changes
else:
changes.commit()
return True

def get_record(self, zone, zone_id, record, record_type):
Expand Down

0 comments on commit 810d0a8

Please sign in to comment.