Class: Bora::Cfn::Stack

Inherits:
Object
  • Object
show all
Defined in:
lib/bora/cfn/stack.rb

Constant Summary collapse

NO_UPDATE_MESSAGE =
'No updates are to be performed'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(stack_name, region = nil) ⇒ Stack

Returns a new instance of Stack.



15
16
17
18
19
# File 'lib/bora/cfn/stack.rb', line 15

def initialize(stack_name, region = nil)
  @stack_name = stack_name
  @region = region
  @processed_events = Set.new
end

Instance Method Details

#create(options, &block) ⇒ Object



21
22
23
# File 'lib/bora/cfn/stack.rb', line 21

def create(options, &block)
  call_cfn_action(:create_stack, options, &block)
end

#create_change_set(change_set_name, options) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/bora/cfn/stack.rb', line 78

def create_change_set(change_set_name, options)
  change_set_options = {
    stack_name: @stack_name,
    change_set_name: change_set_name
  }
  cloudformation.create_change_set(change_set_options.merge(options))
  loop do
    change_set = ChangeSet.new(cloudformation.describe_change_set(change_set_options))
    return change_set if change_set.status_complete?
    sleep 5
  end
end

#create_or_update(options, &block) ⇒ Object



32
33
34
# File 'lib/bora/cfn/stack.rb', line 32

def create_or_update(options, &block)
  exists? ? update(options, &block) : create(options, &block)
end

#delete(&block) ⇒ Object



41
42
43
# File 'lib/bora/cfn/stack.rb', line 41

def delete(&block)
  call_cfn_action(:delete_stack, &block)
end

#delete_change_set(change_set_name) ⇒ Object



100
101
102
# File 'lib/bora/cfn/stack.rb', line 100

def delete_change_set(change_set_name)
  cloudformation.delete_change_set(stack_name: @stack_name, change_set_name: change_set_name)
end

#describe_change_set(change_set_name) ⇒ Object



96
97
98
# File 'lib/bora/cfn/stack.rb', line 96

def describe_change_set(change_set_name)
  ChangeSet.new(cloudformation.describe_change_set(stack_name: @stack_name, change_set_name: change_set_name))
end

#eventsObject



45
46
47
48
49
# File 'lib/bora/cfn/stack.rb', line 45

def events
  return unless exists?
  events = cloudformation.describe_stack_events(stack_name: underlying_stack.stack_id).stack_events
  events.reverse.map { |e| Event.new(e) }
end

#execute_change_set(change_set_name, &block) ⇒ Object



104
105
106
# File 'lib/bora/cfn/stack.rb', line 104

def execute_change_set(change_set_name, &block)
  call_cfn_action(:execute_change_set, { change_set_name: change_set_name }, &block)
end

#exists?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/bora/cfn/stack.rb', line 74

def exists?
  status.exists?
end

#list_change_setsObject



91
92
93
94
# File 'lib/bora/cfn/stack.rb', line 91

def list_change_sets
  cfn_change_sets = cloudformation.list_change_sets(stack_name: @stack_name)
  cfn_change_sets.summaries.map { |cs| ChangeSet.new(cs, true) }
end

#outputsObject



51
52
53
54
# File 'lib/bora/cfn/stack.rb', line 51

def outputs
  return unless exists?
  underlying_stack.outputs.map { |output| Output.new(output) }
end

#parametersObject



56
57
58
59
# File 'lib/bora/cfn/stack.rb', line 56

def parameters
  return unless exists?
  underlying_stack.parameters.map { |parameter| Parameter.new(parameter) }
end

#recreate(options, &block) ⇒ Object



36
37
38
39
# File 'lib/bora/cfn/stack.rb', line 36

def recreate(options, &block)
  delete(&block) if exists?
  create(options, &block) unless exists?
end

#statusObject



70
71
72
# File 'lib/bora/cfn/stack.rb', line 70

def status
  StackStatus.new(underlying_stack)
end

#templateObject



61
62
63
64
# File 'lib/bora/cfn/stack.rb', line 61

def template
  return unless exists?
  cloudformation.get_template(stack_name: @stack_name).template_body
end

#update(options, &block) ⇒ Object



25
26
27
28
29
30
# File 'lib/bora/cfn/stack.rb', line 25

def update(options, &block)
  # Parameters that are not valid for the update_stack api
  invalid_update_stack_options = %i(on_failure disable_rollback)
  update_options = options.select { |key| !invalid_update_stack_options.include?(key) }
  call_cfn_action(:update_stack, update_options, &block)
end

#validate(options) ⇒ Object



66
67
68
# File 'lib/bora/cfn/stack.rb', line 66

def validate(options)
  cloudformation.validate_template(options.select { |k| [:template_body, :template_url].include?(k) })
end