Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify key profile #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions cumulus/CFStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CFStack(object):
region, template and what other stacks it depends on.
"""
def __init__(self, mega_stack_name, name, params, template_name, region,
sns_topic_arn, tags=None, depends_on=None):
key_profile, sns_topic_arn, tags=None, depends_on=None):
self.logger = logging.getLogger(__name__)
if mega_stack_name == name:
self.cf_stack_name = name
Expand All @@ -33,6 +33,7 @@ def __init__(self, mega_stack_name, name, params, template_name, region,
self.depends_on.append(dep)
else:
self.depends_on.append("%s-%s" % (mega_stack_name, dep))
self.key_profile = key_profile
self.region = region
self.sns_topic_arn = sns_topic_arn

Expand Down Expand Up @@ -148,12 +149,24 @@ def get_cf_stack(self, stack, resources=False):
if stack not in self.cf_stacks:
# We don't have this stack in the cache already
# so we need to pull it from CF
cfconn = cloudformation.connect_to_region(self.region)
if self.key_profile is not None:
cfconn = cloudformation.connect_to_region(
self.region,
profile_name=self.key_profile,
)
else:
cfconn = cloudformation.connect_to_region(self.region)
self.cf_stacks[stack] = cfconn.describe_stacks(stack)[0]
return self.cf_stacks[stack]
else:
if stack not in self.cf_stacks_resources:
cfconn = cloudformation.connect_to_region(self.region)
if self.key_profile is not None:
cfconn = cloudformation.connect_to_region(
self.region,
profile_name=self.key_profile,
)
else:
cfconn = cloudformation.connect_to_region(self.region)
the_stack = self.get_cf_stack(stack=stack, resources=False)
self.cf_stacks_resources[stack] = the_stack.list_resources()
return self.cf_stacks_resources[stack]
Expand Down
26 changes: 23 additions & 3 deletions cumulus/MegaStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,22 @@ def __init__(self, yamlFile):
" don't know where to build it.")
exit(1)

# Find and set the mega stacks key profile. Use None if it has not been
# defined in the stack
if 'key_profile' in self.stackDict[self.name]:
self.key_profile = self.stackDict[self.name]['key_profile']
else:
self.key_profile = None

if 'account_id' in self.stackDict[self.name]:
# Get the account ID for the current AWS credentials
iamconn = iam.connect_to_region(self.region)
if self.key_profile is not None:
iamconn = iam.connect_to_region(
self.region,
profile_name=self.key_profile,
)
else:
iamconn = iam.connect_to_region(self.region)
user_response = iamconn.get_user()['get_user_response']
user_result = user_response['get_user_result']
account_id = user_result['user']['arn'].split(':')[4]
Expand Down Expand Up @@ -79,7 +92,13 @@ def __init__(self, yamlFile):
# currently in our region stops us making lots of calls to
# CloudFormation API for each stack
try:
self.cfconn = cloudformation.connect_to_region(self.region)
if self.key_profile is not None:
self.cfconn = cloudformation.connect_to_region(
self.region,
profile_name=self.key_profile,
)
else:
self.cfconn = cloudformation.connect_to_region(self.region)
self.cf_desc_stacks = self._describe_all_stacks()
except boto.exception.NoAuthHandlerFound as exception:
self.logger.critical(
Expand Down Expand Up @@ -120,6 +139,7 @@ def __init__(self, yamlFile):
params=the_stack.get('params'),
template_name=the_stack['cf_template'],
region=self.region,
key_profile=self.key_profile,
sns_topic_arn=local_sns_arn,
depends_on=the_stack.get('depends'),
tags=merged_tags
Expand Down Expand Up @@ -171,7 +191,7 @@ def check(self, stack_name=None):
continue
self.logger.info("Starting check of stack %s" % stack.name)
if not stack.populate_params(self.cf_desc_stacks):
info_message = ("Could not determine correct parameters for" +
info_message = ("Could not determine correct parameters for " +
"CloudFormation stack %s\n\tMost likely " +
"because stacks it depends on haven't been " +
"created yet.")
Expand Down
1 change: 1 addition & 0 deletions examples/cumulus_example_stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ example-stack:
highlight-output: true
#Limit the account in which this stack can be run
account_id: 972549067366
key_profile: prod
stacks:
#Base stack, has the same name as the top level. Cumulus knows not to call it examplestack-example-stack.
#This template has no parameters to pass in
Expand Down