diff --git a/README.md b/README.md index bc29300..24bb553 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/exe/cfn_manage b/exe/cfn_manage index 9022c07..18fc772 100755 --- a/exe/cfn_manage +++ b/exe/cfn_manage @@ -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 @@ -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 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 end diff --git a/exe/usage.txt b/exe/usage.txt old mode 100644 new mode 100755 index 56e6a78..e60b4da --- a/exe/usage.txt +++ b/exe/usage.txt @@ -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] diff --git a/lib/cfn_manage/cf_start_stop_environment.rb b/lib/cfn_manage/cf_start_stop_environment.rb index 4ee0a58..0265605 100644 --- a/lib/cfn_manage/cf_start_stop_environment.rb +++ b/lib/cfn_manage/cf_start_stop_environment.rb @@ -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 + + $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] + tag_value = tag.split('=')[1] + + cf_resource = Aws::CloudFormation::Resource.new() + stack_collection = cf_resource.stacks.each { |stack| + next if !stack.root_id.nil? + stack.tags.each { |tag| + 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 @@ -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