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

Commit

Permalink
Add support for RDS instance
Browse files Browse the repository at this point in the history
  • Loading branch information
yufangzhang committed Jul 19, 2016
1 parent 68a71f4 commit 20340e7
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 36 deletions.
6 changes: 6 additions & 0 deletions bootstrap_cfn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,12 @@ def rds(self, template):
if 'db-engine' in self.data['rds'] and self.data['rds']['db-engine'].startswith("sqlserver"):
required_fields.pop('db-name')

if 'identifier' in self.data['rds']:
# update identifier name
self.data['rds']['identifier'] = "{}-{}".format(self.data['rds']['identifier'], self.stack_id)
logging.info("identifier was updated to {}".format(self.data['rds']['identifier']))
print "identifier was updated to {}".format(self.data['rds']['identifier'])

# TEST FOR REQUIRED FIELDS AND EXIT IF MISSING ANY
for yaml_key, rds_prop in required_fields.iteritems():
if yaml_key not in self.data['rds']:
Expand Down
39 changes: 5 additions & 34 deletions bootstrap_cfn/fab_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,29 +552,17 @@ def cfn_delete(force=False, pre_delete_callbacks=None):
stack_id = stack_name.split('-')[-1]
zone_name = get_zone_name()
zone_id = r53_conn.get_hosted_zone_id(zone_name)
elb = get_first_public_elb()
if hasattr(env, "tag") and env.tag != 'active':
# delete inactive stack
stack_tag = env.tag
logger.info("Deleting {} inactive stack {}...".format(stack_tag, stack_name))
print green("\nSTACK {0} DELETING...\n").format(stack_name)

# delete Alias record
elb_name = "{}-{}".format(elb, stack_id)
alias_record_object = r53_conn.get_full_record(zone_name, zone_id, elb_name, 'A')
if alias_record_object:
alias_record_value = [alias_record_object.alias_hosted_zone_id,
alias_record_object.alias_dns_name,
alias_record_object.alias_evaluate_target_health]
alias_record_name = "{}.{}".format(elb_name, zone_name)
r53_conn.delete_dns_record(zone_id, alias_record_name, 'A', alias_record_value, is_alias=True)
# delete TXT record
# delete Alias and TXT records
txt_tag_record = get_tag_record_name(stack_tag)
txt_record_name = "{}.{}".format(txt_tag_record, zone_name)
txt_record_value = '"{}"'.format(r53_conn.get_record(
zone_name, zone_id, txt_tag_record, 'TXT'))
if txt_record_value:
r53_conn.delete_dns_record(zone_id, txt_record_name, 'TXT', txt_record_value)

r53_conn.delete_record(zone_name, zone_id, elb, stack_id, stack_tag, txt_tag_record)
# Wait for stacks to delete
print 'Waiting for stack to delete.'
cfn.delete(stack_name)
Expand All @@ -591,25 +579,8 @@ def cfn_delete(force=False, pre_delete_callbacks=None):

stack_tag = 'active'
print green("\nDELETING ACTIVE DNS RECORDS...\n")

# delete 'A' record
main_record_name = "{}.{}".format(elb, zone_name)
stack_record_name = "{}-{}".format(elb, stack_id)
stack_record_object = r53_conn.get_full_record(zone_name, zone_id, stack_record_name, 'A')
main_record_object = r53_conn.get_full_record(zone_name, zone_id, elb, 'A')
main_record_value = [main_record_object.alias_hosted_zone_id,
main_record_object.alias_dns_name,
main_record_object.alias_evaluate_target_health]
if stack_record_object and stack_record_object.to_print() == main_record_object.to_print():
r53_conn.delete_dns_record(zone_id, main_record_name, 'A', main_record_value, is_alias=True)

# delete 'TXT' record
tag_record_name = get_tag_record_name(stack_tag)
record_value = '"{}"'.format(r53_conn.get_record(
zone_name, zone_id, tag_record_name, 'TXT'))
record_name = '{}.{}'.format(tag_record_name, zone_name)
if stack_id and stack_id == record_value[1:-1]:
r53_conn.delete_dns_record(zone_id, record_name, 'TXT', record_value)
txt_tag_record = get_tag_record_name(stack_tag)
r53_conn.delete_record(zone_name, zone_id, elb, stack_id, stack_tag, txt_tag_record)

if 'ssl' in cfn_config.data:
iam = get_connection(IAM)
Expand Down
24 changes: 24 additions & 0 deletions bootstrap_cfn/r53.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,30 @@ def delete_dns_record(self, zone_id, record_name, record_type, record_value, is_
res = changes.commit()
return res

def delete_record(self, zone_name, zone_id, elb, stack_id, stack_tag, txt_tag_record):
elb_name = "{}-{}".format(elb, stack_id)
alias_record_object = self.get_full_record(zone_name, zone_id, elb_name, 'A')
stack_record_name = None
stack_record_object = None
if stack_tag == 'active':
# do matching before deleting active records
stack_record_name = "{}-{}".format(elb, stack_id)
stack_record_object = self.get_full_record(zone_name, zone_id, stack_record_name, 'A')

if alias_record_object:
alias_record_value = [alias_record_object.alias_hosted_zone_id,
alias_record_object.alias_dns_name,
alias_record_object.alias_evaluate_target_health]
alias_record_name = "{}.{}".format(elb_name, zone_name)
if stack_record_object and stack_record_object.to_print() == alias_record_object.to_print():
self.delete_dns_record(zone_id, alias_record_name, 'A', alias_record_value, is_alias=True)
# delete TXT record
txt_record_name = "{}.{}".format(txt_tag_record, zone_name)
txt_record_value = '"{}"'.format(self.get_record(
zone_name, zone_id, txt_tag_record, 'TXT'))
if txt_record_value:
self.delete_dns_record(zone_id, txt_record_name, 'TXT', txt_record_value)

def get_record(self, zone_name, zone_id, record_name, record_type):
"""
Expand Down
4 changes: 2 additions & 2 deletions tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ def test_rds(self):
ProjectConfig(
'tests/sample-project.yaml',
'dev',
'tests/sample-project-passwords.yaml').config, 'my-stack-name')
'tests/sample-project-passwords.yaml').config, 'my-stack-name-12345678')

template = Template()
config.rds(template)
Expand All @@ -313,7 +313,7 @@ def test_rds(self):
# Identifier can be optionally be defined in the yaml template for compatibility.
# We're only testing the case where it's defined. If left undefined AWS will
# generate a random one.
self.assertEquals(identifier, 'test-dev')
self.assertEquals(identifier, 'test-dev-12345678')
rds_dict["RDSInstance"]["Properties"].pop("DBInstanceIdentifier")
known = self._resources_to_dict(known)
compare(known, rds_dict)
Expand Down

0 comments on commit 20340e7

Please sign in to comment.