-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. possibly being pedantic but could you simplify to |
||
tag_value = tag.split('=')[1] | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handle --stack-tag input if format is not equal to |
||
cf_resource = Aws::CloudFormation::Resource.new() | ||
stack_collection = cf_resource.stacks.each { |stack| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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
orSTACK_TAG
isn't supplied