Module: Sfn::MonkeyPatch::Stack
- Defined in:
- lib/sfn/monkey_patch/stack.rb,
lib/sfn/monkey_patch/stack/azure.rb,
lib/sfn/monkey_patch/stack/google.rb
Overview
Expand stack model functionality
Defined Under Namespace
Instance Method Summary collapse
-
#apply_stack(remote_stack, opts = {}, ignore_params = nil) ⇒ self
Apply stack outputs to current stack parameters.
-
#color_state ⇒ Symbol
Provides color of stack state.
-
#complete? ⇒ TrueClass, FalseClass
Stack is in complete state.
-
#creating? ⇒ TrueClass, FalseClass
Stack is creating.
-
#deleting? ⇒ TrueClass, FalseClass
Stack is deleting.
-
#encoded_id ⇒ String
URL safe encoded stack id.
-
#failed? ⇒ TrueClass, FalseClass
Stack is failed state.
-
#green? ⇒ TrueClass, FalseClass
Stack is in green state.
-
#in_progress? ⇒ TrueClass, FalseClass
Stack is in progress.
-
#nested? ⇒ TrueClass, FalseClass
Stack contains nested stacks.
-
#nested_stacks(recurse = true) ⇒ Array<Miasma::Models::Orchestration::Stack>
Return all stacks contained within this stack.
-
#nesting_style ⇒ Symbol, NilClass
Detect the nesting style in use by the stack.
-
#percent_complete(min = 5) ⇒ Integer
Whole number representation of current completion.
-
#performing ⇒ String
Action currently being performed.
-
#policy ⇒ Smash, NilClass
Return stack policy if available.
-
#red? ⇒ TrueClass, FalseClass
Stack is in red state.
-
#rollbacking? ⇒ TrueClass, FalseClass
Stack is rolling back.
-
#root_parameters ⇒ Hash
Provide easy parameters override.
-
#sparkleish_template(*args) ⇒ Hash
Reformat template data structure to SparkleFormation style structure.
-
#status_ends_with?(*args) ⇒ TrueClass, FalseClass
Check for state suffix.
-
#status_includes?(*args) ⇒ TrueClass, FalseClass
Check for state inclusion.
-
#status_starts_with?(*args) ⇒ TrueClass, FalseClass
Check for state prefix.
-
#success? ⇒ TrueClass, FalseClass
Stack is in success state.
-
#text_state ⇒ Symbol
Provides text of stack state.
-
#updating? ⇒ TrueClass, FalseClass
Stack is updating.
-
#yellow? ⇒ TrueClass, FalseClass
Stack is in yellow state.
Methods included from Google
#nested_stacks_google, #root_parameters_google, #sparkleish_template_google
Methods included from Azure
Instance Method Details
#apply_stack(remote_stack, opts = {}, ignore_params = nil) ⇒ self
setting ‘DisableApply` within parameter hash will prevent parameters being overridden
Apply stack outputs to current stack parameters
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/sfn/monkey_patch/stack.rb', line 169 def apply_stack(remote_stack, opts={}, ignore_params=nil) if(self.respond_to?("apply_stack_#{api.provider}")) self.send("apply_stack_#{api.provider}", remote_stack, opts, ignore_params) else unless(opts[:mapping]) opts[:mapping] = {} end if(opts[:parameter_key]) stack_parameters = template[opts[:parameter_key]] default_key = opts.fetch( :default_key, opts[:parameter_key].to_s[0,1].match(/[a-z]/) ? 'default' : 'Default' ) else if(template['Parameters']) default_key = 'Default' stack_parameters = template['Parameters'] else default_key = 'default' stack_parameters = template['parameters'] end end if(stack_parameters) valid_parameters = stack_parameters.find_all do |key, val| !val['DisableApply'] && !val['disable_apply'] end.map(&:first) if(ignore_params) valid_parameters.reject! do |key| ignore_params.include?(key) end end remote_stack.outputs.each do |output| o_key = output.key.downcase.tr('_', '') p_key = valid_parameters.detect do |v_param| v_param.downcase.tr('_', '') == o_key end unless(p_key) map_key = opts[:mapping].keys.detect do |map_key| map_key.downcase.tr('_', '') == o_key end if(map_key) p_key = valid_parameters.detect do |v_param| v_param.downcase.tr('_', '') == opts[:mapping][map_key].downcase.tr('_', '') end end end if(p_key) parameters.merge!(p_key => output.value) end end end end self end |
#color_state ⇒ Symbol
Provides color of stack state. Red is an error state, yellow is a warning state and green is a success state
121 122 123 |
# File 'lib/sfn/monkey_patch/stack.rb', line 121 def color_state red? ? :red : green? ? :green : :yellow end |
#complete? ⇒ TrueClass, FalseClass
Returns stack is in complete state.
58 59 60 |
# File 'lib/sfn/monkey_patch/stack.rb', line 58 def complete? status_ends_with?(:complete, :failed) end |
#creating? ⇒ TrueClass, FalseClass
Returns stack is creating.
74 75 76 |
# File 'lib/sfn/monkey_patch/stack.rb', line 74 def creating? in_progress? && status_starts_with?(:create) end |
#deleting? ⇒ TrueClass, FalseClass
Returns stack is deleting.
79 80 81 |
# File 'lib/sfn/monkey_patch/stack.rb', line 79 def deleting? in_progress? && status_starts_with?(:delete) end |
#encoded_id ⇒ String
Returns URL safe encoded stack id.
134 135 136 |
# File 'lib/sfn/monkey_patch/stack.rb', line 134 def encoded_id Base64.urlsafe_encode64(id) end |
#failed? ⇒ TrueClass, FalseClass
Returns stack is failed state.
63 64 65 66 |
# File 'lib/sfn/monkey_patch/stack.rb', line 63 def failed? status_ends_with?(:failed) || (status_includes?(:rollback) && status_ends_with?(:complete)) end |
#green? ⇒ TrueClass, FalseClass
Returns stack is in green state.
108 109 110 |
# File 'lib/sfn/monkey_patch/stack.rb', line 108 def green? success? end |
#in_progress? ⇒ TrueClass, FalseClass
Returns stack is in progress.
53 54 55 |
# File 'lib/sfn/monkey_patch/stack.rb', line 53 def in_progress? status_ends_with?(:in_progress) end |
#nested? ⇒ TrueClass, FalseClass
Returns stack contains nested stacks.
263 264 265 266 267 268 269 270 271 |
# File 'lib/sfn/monkey_patch/stack.rb', line 263 def nested? if(self.respond_to?("nested_#{api.provider}?")) self.send("nested_#{api.provider}?") else !!resources.detect do |resource| api.data.fetch(:stack_types, []).include?(resource.type) end end end |
#nested_stacks(recurse = true) ⇒ Array<Miasma::Models::Orchestration::Stack>
Return all stacks contained within this stack
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/sfn/monkey_patch/stack.rb', line 228 def nested_stacks(recurse=true) if(self.respond_to?("nested_stacks_#{api.provider}")) self.send("nested_stacks_#{api.provider}", recurse) else resources.reload.all.map do |resource| if(api.data.fetch(:stack_types, []).include?(resource.type)) # Custom remote load support if(resource.type == 'Custom::JackalStack') location, stack_id = resource.id.to_s.split('-', 2) if(l_conf = api.data[:locations][location]) n_stack = Miasma.api( :type => :orchestration, :provider => l_conf[:provider], :credentials => l_conf ).stacks.get(stack_id) end else n_stack = resource. end if(n_stack) n_stack.data[:logical_id] = resource.name n_stack.data[:parent_stack] = self n_stack.api.data[:stack_types] = api.data[:stack_types] if(recurse) [n_stack] + n_stack.nested_stacks(recurse) else n_stack end end end end.flatten.compact end end |
#nesting_style ⇒ Symbol, NilClass
in shallow nesting style, stack resources will not contain any direct values for parameters (which is what we are testing for)
Detect the nesting style in use by the stack
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/sfn/monkey_patch/stack.rb', line 309 def nesting_style if(self.respond_to?("nesting_style_#{api.provider}")) self.send("nesting_style_#{api.provider}") else if(nested?) self.template['Resources'].find_all do |t_resource| t_resource['Type'] == self.api.class.const_get(:RESOURCE_MAPPING).key(self.class) end.detect do |t_resource| t_resource['Properties'].fetch('Parameters', {}).values.detect do |t_value| !t_value.is_a?(Hash) end end ? :deep : :shallow end end end |
#percent_complete(min = 5) ⇒ Integer
Whole number representation of current completion
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/sfn/monkey_patch/stack.rb', line 142 def percent_complete(min = 5) if(self.respond_to?("percent_complete_#{api.provider}")) self.send("percent_complete_#{api.provider}", min) else if(in_progress?) total_resources = template.fetch('Resources', []).size total_complete = resources.all.find_all do |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 end |
#performing ⇒ String
Returns action currently being performed.
94 95 96 97 98 |
# File 'lib/sfn/monkey_patch/stack.rb', line 94 def performing if(in_progress?) status.to_s.downcase.split('_').first.to_sym end end |
#policy ⇒ Smash, NilClass
Return stack policy if available
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 |
# File 'lib/sfn/monkey_patch/stack.rb', line 276 def policy if(self.respond_to?("policy_#{api.provider}")) self.send("policy_#{api.provider}") else if(self.api.provider == :aws) # cause this is the only one begin result = self.api.request( :path => '/', :form => Smash.new( 'Action' => 'GetStackPolicy', 'StackName' => self.id ) ) serialized_policy = result.get(:body, 'GetStackPolicyResult', 'StackPolicyBody') MultiJson.load(serialized_policy).to_smash rescue Miasma::Error::ApiError::RequestError => e if(e.response.code == 404) nil else raise end end end end end |
#red? ⇒ TrueClass, FalseClass
Returns stack is in red state.
103 104 105 |
# File 'lib/sfn/monkey_patch/stack.rb', line 103 def red? failed? || deleting? end |
#rollbacking? ⇒ TrueClass, FalseClass
Returns stack is rolling back.
89 90 91 |
# File 'lib/sfn/monkey_patch/stack.rb', line 89 def rollbacking? in_progress? && status_starts_with?(:rollback) end |
#root_parameters ⇒ Hash
Provide easy parameters override
339 340 341 342 343 344 345 |
# File 'lib/sfn/monkey_patch/stack.rb', line 339 def root_parameters if(self.respond_to?("root_parameters_#{api.provider}")) self.send("root_parameters_#{api.provider}") else parameters end end |
#sparkleish_template(*args) ⇒ Hash
Reformat template data structure to SparkleFormation style structure
328 329 330 331 332 333 334 |
# File 'lib/sfn/monkey_patch/stack.rb', line 328 def sparkleish_template(*args) if(self.respond_to?("sparkleish_template_#{api.provider}")) self.send("sparkleish_template_#{api.provider}", *args) else template end end |
#status_ends_with?(*args) ⇒ TrueClass, FalseClass
Check for state suffix
23 24 25 26 27 28 |
# File 'lib/sfn/monkey_patch/stack.rb', line 23 def status_ends_with?(*args) stat = status.to_s.downcase !!args.map(&:to_s).map(&:downcase).detect do |suffix| stat.end_with?(suffix) || state.to_s.end_with?(suffix) end end |
#status_includes?(*args) ⇒ TrueClass, FalseClass
Check for state inclusion
45 46 47 48 49 50 |
# File 'lib/sfn/monkey_patch/stack.rb', line 45 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
34 35 36 37 38 39 |
# File 'lib/sfn/monkey_patch/stack.rb', line 34 def status_starts_with?(*args) stat = status.to_s.downcase !!args.map(&:to_s).map(&:downcase).detect do |prefix| stat.start_with?(prefix) || state.to_s.start_with?(prefix) end end |
#success? ⇒ TrueClass, FalseClass
Returns stack is in success state.
69 70 71 |
# File 'lib/sfn/monkey_patch/stack.rb', line 69 def success? !failed? && complete? end |
#text_state ⇒ Symbol
Provides text of stack state. Danger is an error state, warning is a warning state and success is a success state
129 130 131 |
# File 'lib/sfn/monkey_patch/stack.rb', line 129 def text_state red? ? :danger : green? ? :success : :warning end |
#updating? ⇒ TrueClass, FalseClass
Returns stack is updating.
84 85 86 |
# File 'lib/sfn/monkey_patch/stack.rb', line 84 def updating? in_progress? && status_starts_with?(:update) end |
#yellow? ⇒ TrueClass, FalseClass
Returns stack is in yellow state.
113 114 115 |
# File 'lib/sfn/monkey_patch/stack.rb', line 113 def yellow? !red? && !green? end |