Class: CfnStatus::RollbackStack

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn_status/rollback_stack.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stack_name, options = {}) ⇒ RollbackStack

Returns a new instance of RollbackStack.



8
9
10
11
12
# File 'lib/cfn_status/rollback_stack.rb', line 8

def initialize(stack_name, options={})
  @stack_name = stack_name
  @cfn = options[:cfn]
  @status = CfnStatus.new(@stack_name, cfn: @cfn)
end

Instance Attribute Details

#cfnObject (readonly)

Returns the value of attribute cfn.



7
8
9
# File 'lib/cfn_status/rollback_stack.rb', line 7

def cfn
  @cfn
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/cfn_status/rollback_stack.rb', line 7

def status
  @status
end

Class Method Details

.handle!(stack_name, options = {}) ⇒ Object



3
4
5
# File 'lib/cfn_status/rollback_stack.rb', line 3

def self.handle!(stack_name, options={})
  new(stack_name, options).run
end

Instance Method Details

#find_stack(stack_name) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cfn_status/rollback_stack.rb', line 29

def find_stack(stack_name)
  return if ENV['CFN_STATUS_TEST']
  resp = cfn.describe_stacks(stack_name: stack_name)
  resp.stacks.first
rescue Aws::CloudFormation::Errors::ValidationError => e
  # example: Stack with id demo-web does not exist
  if e.message =~ /Stack with/ && e.message =~ /does not exist/
    nil
  else
    raise
  end
end

#rollback_complete?(stack) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/cfn_status/rollback_stack.rb', line 25

def rollback_complete?(stack)
  stack.stack_status == 'ROLLBACK_COMPLETE'
end

#runObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/cfn_status/rollback_stack.rb', line 14

def run
  @stack = find_stack(@stack_name)
  if @stack && rollback_complete?(@stack)
    puts "Existing stack in ROLLBACK_COMPLETE state. Deleting stack before continuing."
    cfn.delete_stack(stack_name: @stack_name)
    status.wait
    status.reset
    @stack = nil # at this point stack has been deleted
  end
end