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

Tag-based stack start/stop #68

Open
wants to merge 1 commit 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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ Order of operations is supported at this point as hardcoded weights per resource
for supporting dynamic discovery of order of execution - local configuration file override is one of
the possible sources.

Both start and stop environment commands can take one of two arguments.

Supplying `--stack-name` and the name of an existing CloudFormation stack will run the operation against it

Supplying `--stack-tag` and a key value pair (`Key=Value`) will cause a lookup of any root CloudFormation stacks with a matching tag. These stacks will then be looped over and have the operation ran against them.


## Start - stop cloudformation stack

Expand Down
16 changes: 14 additions & 2 deletions exe/cfn_manage
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ OptionParser.new do |opts|
$options['STACK_NAME'] = stack
end

opts.on('--stack-tag [STACK_TAG]') do |stack_tag|
$options['STACK_TAG'] = stack_tag
end

opts.on('--asg-name [ASG]') do |asg|
$options['ASG'] = asg
end
Expand Down Expand Up @@ -251,7 +255,15 @@ when 'start-transfer-server'

# stack commands
when 'stop-environment'
CfnManage::CloudFormation::EnvironmentRunStop.new().stop_environment($options['STACK_NAME'])
if $options['STACK_NAME']
CfnManage::CloudFormation::EnvironmentRunStop.new().stop_environment($options['STACK_NAME'], 'stack')
elsif $options['STACK_TAG']
CfnManage::CloudFormation::EnvironmentRunStop.new().stop_environment($options['STACK_TAG'], 'tag')
end
Copy link
Contributor

@Guslington Guslington Dec 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

show the user an error if either STACK_NAME or STACK_TAG isn't supplied

when 'start-environment'
CfnManage::CloudFormation::EnvironmentRunStop.new().start_environment($options['STACK_NAME'])
if $options['STACK_NAME']
CfnManage::CloudFormation::EnvironmentRunStop.new().start_environment($options['STACK_NAME'], 'stack')
elsif $options['STACK_TAG']
CfnManage::CloudFormation::EnvironmentRunStop.new().start_environment($options['STACK_TAG'], 'tag')
end
Copy link
Contributor

@Guslington Guslington Dec 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

show the user an error if either STACK_NAME or STACK_TAG isn't supplied

end
4 changes: 4 additions & 0 deletions exe/usage.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ cfn_manage version

cfn_manage stop-environment --stack-name [STACK_NAME]

cfn_manage stop-environment --stack-tag [STACK_TAG]

cfn_manage start-environment --stack-name [STACK_NAME]

cfn_manage start-environment --stack-tag [STACK_TAG]

cfn_manage stop-asg --asg-name [ASG]

cfn_manage start-asg --asg-name [ASG]
Expand Down
62 changes: 59 additions & 3 deletions lib/cfn_manage/cf_start_stop_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,64 @@ def initialize()
end


def start_environment(stack_name)
def start_environment(arg, arg_type)

stacks = []

if arg_type == 'tag'
$log.info("Stack Tag #{arg} provided, searching for stacks with matching tag")
lookup_results = stack_tag_lookup(arg)
stacks.concat(lookup_results)
elsif arg_type == 'stack'
stacks.push(arg)
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

throw an error if no stacks are found

$log.info("List of CloudFormation stacks to be started: #{stacks}")
stacks.each { |stack|
start_stack(stack)
}
end


def stop_environment(arg, arg_type)

stacks = []

if arg_type == 'tag'
$log.info("Stack Tag #{arg} provided, searching for stacks with matching tag")
lookup_results = stack_tag_lookup(arg)
stacks.concat(lookup_results)
elsif arg_type == 'stack'
stacks.push(arg)
end

$log.info("List of CloudFormation stacks to be stopped: #{stacks}")
stacks.each { |stack|
stop_stack(stack)
}
end

def stack_tag_lookup(tag)

lookup_results = []

tag_key = tag.split('=')[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly being pedantic but could you simplify to
tag_key, tag_value = tag.split('=')
Unless you're expecting more than two elements?

tag_value = tag.split('=')[1]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

handle --stack-tag input if format is not equal to key=value and return error to user

cf_resource = Aws::CloudFormation::Resource.new()
stack_collection = cf_resource.stacks.each { |stack|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why assign the loop to a variable?

next if !stack.root_id.nil?
stack.tags.each { |tag|
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you use .select here instead of .each

if tag.key == tag_key and tag.value == tag_value
$log.info("Stack found with matching tag: #{stack.stack_name}")
lookup_results.push(stack.stack_name)
end
}
}
return lookup_results
end

def start_stack(stack_name)
$log.info("Starting environment #{stack_name}")
Common.visit_stack(@cf_client, stack_name, method(:collect_resources), true)
do_start_assets
Expand All @@ -52,8 +109,7 @@ def start_environment(stack_name)
$log.info("Environment #{stack_name} started")
end


def stop_environment(stack_name)
def stop_stack(stack_name)
$log.info("Stopping environment #{stack_name}")
Common.visit_stack(@cf_client, stack_name, method(:collect_resources), true)
do_stop_assets
Expand Down