From cd8f302e209c68768aa6a687a38a4ff60acf366c Mon Sep 17 00:00:00 2001 From: Craig Dodd Date: Fri, 29 Jan 2016 10:55:11 +0000 Subject: [PATCH 1/2] Fix spacing in error message --- cumulus/MegaStack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cumulus/MegaStack.py b/cumulus/MegaStack.py index 9ea7aca..f4bcef2 100644 --- a/cumulus/MegaStack.py +++ b/cumulus/MegaStack.py @@ -171,7 +171,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.") From 41b2a732c234588c929aba9604ae97dbc0242a48 Mon Sep 17 00:00:00 2001 From: Craig Dodd Date: Fri, 29 Jan 2016 11:04:07 +0000 Subject: [PATCH 2/2] Add ability to specify key profile --- cumulus/CFStack.py | 19 ++++++++++++++++--- cumulus/MegaStack.py | 24 ++++++++++++++++++++++-- examples/cumulus_example_stack.yaml | 1 + 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/cumulus/CFStack.py b/cumulus/CFStack.py index d686888..22ac0ba 100644 --- a/cumulus/CFStack.py +++ b/cumulus/CFStack.py @@ -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 @@ -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 @@ -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] diff --git a/cumulus/MegaStack.py b/cumulus/MegaStack.py index f4bcef2..a9e3484 100644 --- a/cumulus/MegaStack.py +++ b/cumulus/MegaStack.py @@ -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] @@ -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( @@ -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 diff --git a/examples/cumulus_example_stack.yaml b/examples/cumulus_example_stack.yaml index ac19e6e..420704f 100644 --- a/examples/cumulus_example_stack.yaml +++ b/examples/cumulus_example_stack.yaml @@ -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