Skip to content

Commit

Permalink
Merge pull request #41 from RightBrain-Networks/bugfix/stackset-role-…
Browse files Browse the repository at this point in the history
…calculation

Set the path for stack set roles to be /service-role/ insead of /
  • Loading branch information
yeslayla authored Jan 2, 2020
2 parents 07ba7c1 + 25d6590 commit 47529fb
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions deployer/stack_sets.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#!/usr/bin/python

import tabulate
import time, pytz
import time

from botocore.exceptions import ClientError, WaiterError
from botocore.exceptions import ClientError
from collections import defaultdict

from .cloudformation import Stack
from .logger import logger

from datetime import datetime


class StackSet(Stack):

Expand All @@ -34,10 +32,10 @@ def current_instances(self):
current = current + result['Summaries']
return current


@property
def current_accounts(self):
return set([x['Account'] for x in self.current_instances])

@property
def current_regions(self):
return set([x['Region'] for x in self.current_instances])
Expand All @@ -48,7 +46,7 @@ def administration_role(self):
if config_value is None or config_value.startswith('arn:'):
return config_value
else:
return "arn:aws:iam::{}:role/{}".format(self.current_account, config_value)
return "arn:aws:iam::{}:role/service-role/{}".format(self.current_account, config_value)

@property
def current_account(self):
Expand Down Expand Up @@ -103,8 +101,7 @@ def create(self):
result = self.client.create_stack_set(**args)
self.stack_set_id = result['StackSetId']

self.stack_set_waiter(self.create_stack_instances(None, self.accounts, self.regions), "Creation")

self.stack_set_waiter(self.create_stack_instances(self.accounts, self.regions), "Creation")

def upsert(self):
if not self.accounts or not self.regions:
Expand All @@ -117,16 +114,16 @@ def delete_stack(self):
return super(StackSet, self).delete_stack()

# Delete existing stack instances
if(len(self.current_accounts) > 0):
self.stack_set_waiter(self.delete_stack_instances(None, list(self.current_accounts), list(self.current_regions)), "Deletion")
if len(self.current_accounts) > 0:
self.stack_set_waiter(self.delete_stack_instances(list(self.current_accounts), list(self.current_regions)), "Deletion")

# Delete stack set
args = {'StackSetName': self.stack_name}
self.client.delete_stack_set(**args)

logger.info("Delete complete!")

def stack_set_waiter(self, operation_id, verb = "Update"):
def stack_set_waiter(self, operation_id, verb="Update"):
logger.info("Stack Set " + verb + " Started")

args = {
Expand Down Expand Up @@ -171,7 +168,6 @@ def update(self):
if self.template_body:
logger.info("Using local template due to null template bucket")


# Generate API calls based upon what is currently deployed
api_calls = self.generate_instance_calls()

Expand All @@ -183,10 +179,9 @@ def update(self):
# Delete or create as needed
for call in api_calls:
if call['type'] == "create":
self.stack_set_waiter(self.create_stack_instances(None, call['accounts'], call['regions']))
self.stack_set_waiter(self.create_stack_instances(call['accounts'], call['regions']))
if call['type'] == "delete":
self.stack_set_waiter(self.delete_stack_instances(None, call['accounts'], call['regions']))

self.stack_set_waiter(self.delete_stack_instances(call['accounts'], call['regions']))

def generate_instance_calls(self):
api_calls = []
Expand All @@ -213,9 +208,9 @@ def generate_instance_calls(self):
# Get all account based calls
for account in range(0, len(self.accounts)):
api_call = {
'type' : call_type,
'accounts' : [self.accounts[account]],
'regions' : []
'type': call_type,
'accounts': [self.accounts[account]],
'regions': []
}
for region in range(0, len(self.regions)):
if graph[account][region] == call_type:
Expand All @@ -227,8 +222,8 @@ def generate_instance_calls(self):
# Get all region based calls
for region in range(0, len(self.regions)):
api_call = {
'type' : call_type,
'accounts' : [],
'type': call_type,
'accounts': [],
'regions': [self.regions[region]]
}
for account in range(0, len(self.accounts)):
Expand Down Expand Up @@ -257,12 +252,12 @@ def generate_instance_calls(self):
api_calls = api_calls + type_calls
return api_calls

def create_stack_instances(self, operation_id, accounts, regions):
def create_stack_instances(self, accounts, regions):
logger.info("Creating " + str(len(accounts) * len(regions)) + " stack instances...")
result = self.client.create_stack_instances(StackSetName=self.stack_name, Accounts=accounts, Regions=regions)
return result['OperationId']

def delete_stack_instances(self, operation_id, accounts, regions):
def delete_stack_instances(self, accounts, regions):
logger.info("Deleting " + str(len(accounts) * len(regions)) + " stack instances...")
result = self.client.delete_stack_instances(StackSetName=self.stack_name, Accounts=accounts, Regions=regions, RetainStacks=False)
return result['OperationId']

0 comments on commit 47529fb

Please sign in to comment.