Module: KnifeCloudformation::MonkeyPatch::Stack

Includes:
Utils::AnimalStrings
Defined in:
lib/knife-cloudformation/monkey_patch/stack.rb

Overview

Expand stack model functionality

Instance Method Summary collapse

Methods included from Utils::AnimalStrings

#camel, #snake

Instance Method Details

#apply_stack(remote_stack) ⇒ self

Note:

setting ‘DisableApply` within parameter hash will prevent parameters being overridden

Apply stack outputs to current stack parameters

Parameters:

  • remote_stack (Miasma::Orchestration::Stack)

Returns:

  • (self)


156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 156

def apply_stack(remote_stack)
  default_key = 'Default'
  stack_parameters = template['Parameters']
  valid_parameters = Hash[
    stack_parameters.map do |key, val|
      unless(val['DisableApply'])
        [snake(key), key]
      end
    end.compact
  ]
  if(defined?(Chef::Config) && Chef::Config[:knife][:cloudformation][:ignore_parameters])
    valid_parameters = Hash[
      valid_parameters.map do |snake_param, camel_param|
        unless(Chef::Config[:knife][:cloudformation][:ignore_parameters].include?(camel_param))
          [snake_param, camel_param]
        end
      end.compact
    ]
  end
  if(persisted?)
    remote_stack.outputs.each do |output|
      if(param_key = valid_parameters[snake(output.key)])
        parameters.merge!(param_key => output.value)
      end
    end
  else
    remote_stack.outputs.each do |output|
      if(param_key = valid_parameters[snake(output.key)])
        stack_parameters[param_key][default_key] = output.value
      end
    end
  end
end

#color_stateSymbol

Provides color of stack state. Red is an error state, yellow is a warning state and green is a success state

Returns:

  • (Symbol)

    color of state (:red, :yellow, :green)



116
117
118
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 116

def color_state
  red? ? :red : green? ? :green : :yellow
end

#complete?TrueClass, FalseClass

Returns stack is in complete state.

Returns:

  • (TrueClass, FalseClass)

    stack is in complete state



53
54
55
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 53

def complete?
  status_ends_with?(:complete, :failed)
end

#creating?TrueClass, FalseClass

Returns stack is creating.

Returns:

  • (TrueClass, FalseClass)

    stack is creating



69
70
71
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 69

def creating?
  in_progress? && status_starts_with?(:create)
end

#deleting?TrueClass, FalseClass

Returns stack is deleting.

Returns:

  • (TrueClass, FalseClass)

    stack is deleting



74
75
76
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 74

def deleting?
  in_progress? && status_starts_with?(:delete)
end

#encoded_idString

Returns URL safe encoded stack id.

Returns:

  • (String)

    URL safe encoded stack id



129
130
131
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 129

def encoded_id
  Base64.urlsafe_encode64(id)
end

#failed?TrueClass, FalseClass

Returns stack is failed state.

Returns:

  • (TrueClass, FalseClass)

    stack is failed state



58
59
60
61
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 58

def failed?
  status_ends_with?(:failed) ||
    (status_includes?(:rollback) && status_ends_with?(:complete))
end

#green?TrueClass, FalseClass

Returns stack is in green state.

Returns:

  • (TrueClass, FalseClass)

    stack is in green state



103
104
105
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 103

def green?
  success?
end

#in_progress?TrueClass, FalseClass

Returns stack is in progress.

Returns:

  • (TrueClass, FalseClass)

    stack is in progress



48
49
50
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 48

def in_progress?
  status_ends_with?(:in_progress)
end

#percent_complete(min = 5) ⇒ Integer

Whole number representation of current completion

Parameters:

  • min (Integer) (defaults to: 5)

    lowest allowed return value (defaults 5)

Returns:

  • (Integer)

    percent complete (0..100)



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 137

def percent_complete(min = 5)
  if(in_progress?)
    total_resources = load_template.fetch('Resources', []).size
    total_complete = resources.all.find_all do |resource|
      resource.resource_status.downcase.end_with?('complete')
    end.size
    result = ((total_complete.to_f / total_resources) * 100).to_i
    result > min.to_i ? result : min
  else
    100
  end
end

#performingString

Returns action currently being performed.

Returns:

  • (String)

    action currently being performed



89
90
91
92
93
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 89

def performing
  if(in_progress?)
    status.to_s.downcase.split('_').first.to_sym
  end
end

#red?TrueClass, FalseClass

Returns stack is in red state.

Returns:

  • (TrueClass, FalseClass)

    stack is in red state



98
99
100
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 98

def red?
  failed? || deleting?
end

#rollbacking?TrueClass, FalseClass

Returns stack is rolling back.

Returns:

  • (TrueClass, FalseClass)

    stack is rolling back



84
85
86
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 84

def rollbacking?
  in_progress? && status_starts_with?(:rollback)
end

#status_ends_with?(*args) ⇒ TrueClass, FalseClass

Check for state suffix

Parameters:

  • args (String, Symbol)

    state suffix to check for (multiple allowed)

Returns:

  • (TrueClass, FalseClass)

    true if any matches found in argument list



18
19
20
21
22
23
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 18

def status_ends_with?(*args)
  stat = status.to_s.downcase
  !!args.map(&:to_s).map(&:downcase).detect do |suffix|
    stat.end_with?(suffix)
  end
end

#status_includes?(*args) ⇒ TrueClass, FalseClass

Check for state inclusion

Parameters:

  • args (String, Symbol)

    state string to check for (multiple allowed)

Returns:

  • (TrueClass, FalseClass)

    true if any matches found in argument list



40
41
42
43
44
45
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 40

def status_includes?(*args)
  stat = status.to_s.downcase
  !!args.map(&:to_s).map(&:downcase).detect do |string|
    stat.include?(string)
  end
end

#status_starts_with?(*args) ⇒ TrueClass, FalseClass

Check for state prefix

Parameters:

  • args (String, Symbol)

    state prefix to check for (multiple allowed)

Returns:

  • (TrueClass, FalseClass)

    true if any matches found in argument list



29
30
31
32
33
34
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 29

def status_starts_with?(*args)
  stat = status.to_s.downcase
  !!args.map(&:to_s).map(&:downcase).detect do |prefix|
    stat.start_with?(prefix)
  end
end

#success?TrueClass, FalseClass

Returns stack is in success state.

Returns:

  • (TrueClass, FalseClass)

    stack is in success state



64
65
66
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 64

def success?
  !failed? && complete?
end

#text_stateSymbol

Provides text of stack state. Danger is an error state, warning is a warning state and success is a success state

Returns:

  • (Symbol)

    color of state (:danger, :warning, :success)



124
125
126
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 124

def text_state
  red? ? :danger : green? ? :success : :warning
end

#updating?TrueClass, FalseClass

Returns stack is updating.

Returns:

  • (TrueClass, FalseClass)

    stack is updating



79
80
81
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 79

def updating?
  in_progress? && status_starts_with?(:update)
end

#yellow?TrueClass, FalseClass

Returns stack is in yellow state.

Returns:

  • (TrueClass, FalseClass)

    stack is in yellow state



108
109
110
# File 'lib/knife-cloudformation/monkey_patch/stack.rb', line 108

def yellow?
  !red? && !green?
end