Module: Sfn::CommandModule::Stack::InstanceMethods
- Defined in:
- lib/sfn/command_module/stack.rb
Constant Summary collapse
- MAX_PARAMETER_ATTEMPTS =
maximum number of attempts to get valid parameter value
5
- TEMPLATE_PARAMETER_LOCATIONS =
Template parameter locations
["Parameters", "parameters"]
- TEMPLATE_PARAMETER_DEFAULTS =
Template parameter default value locations
["Default", "defaultValue", "default"]
- TEMPLATE_PARAMETER_NOECHO =
Template parameter no echo locations
["NoEcho"]
- TEMPLATE_PARAMETER_SFN_NOECHO =
Template parameter no echo custom
["Quiet", "quiet"]
Instance Method Summary collapse
-
#apply_nested_stacks!(remote_stack, stack) ⇒ Miasma::Models::Orchestration::Stack
Detect nested stacks and apply.
-
#apply_stacks!(stack) ⇒ Miasma::Models::Orchestration::Stack
Apply any defined remote stacks.
-
#config_root_parameters ⇒ Hash
Parameters for root stack create/update.
-
#execute_apply_stack(provider_stack, receiver_stack, mappings) ⇒ TrueClass
Apply provider stack outputs to receiver stack parameters.
-
#format_config_parameters! ⇒ Object
Format config defined parameters to ensure expected layout.
-
#function_set_parameter?(val) ⇒ TrueClass, FalseClass
Determine if parameter was set via intrinsic function.
-
#generate_custom_apply_mappings(provider_stack) ⇒ Hash
Build apply mappings valid for given provider stack.
-
#locate_config_parameter_key(parameter_prefix, parameter_name, root_name) ⇒ Array<String>
Determine correct configuration parameter key.
-
#populate_parameters!(sparkle, opts = {}) ⇒ Hash
Prompt for parameter values and store result.
-
#prefix_parameters_setup(sparkle) ⇒ Array<Array<String>, Smash>
Generate name prefix for config parameter based on location and extract template parameters.
-
#set_parameter(sparkle, ns_key, param_name, param_value, current_parameters, param_banner) ⇒ TrueClass, FalseClass
Populate stack parameter value via user interaction.
-
#validate_stack_parameter(c_stack, p_key, p_ns_key, c_value) ⇒ TrueClass, FalseClass
Validate stack parameter is properly set via stack resource from parent stack.
Instance Method Details
#apply_nested_stacks!(remote_stack, stack) ⇒ Miasma::Models::Orchestration::Stack
Detect nested stacks and apply
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/sfn/command_module/stack.rb', line 49 def apply_nested_stacks!(remote_stack, stack) remote_stack.resources.all.each do |resource| if valid_stack_types.include?(resource.type) nested_stack = resource. apply_nested_stacks!(nested_stack, stack) mappings = generate_custom_apply_mappings(nested_stack) execute_apply_stack(nested_stack, stack, mappings) end end stack end |
#apply_stacks!(stack) ⇒ Miasma::Models::Orchestration::Stack
Apply any defined remote stacks
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/sfn/command_module/stack.rb', line 25 def apply_stacks!(stack) remote_stacks = [config[:apply_stack]].flatten.compact remote_stacks.each do |stack_name| stack_info = stack_name.split("__") stack_info.unshift(nil) if stack_info.size == 1 stack_location, stack_name = stack_info remote_stack = provider_for(stack_location).stack(stack_name) if remote_stack apply_nested_stacks!(remote_stack, stack) mappings = generate_custom_apply_mappings(remote_stack) execute_apply_stack(remote_stack, stack, mappings) else ui.error "Failed to apply requested stack. Unable to locate. (#{stack_name})" raise "Failed to locate stack: #{stack}" end end stack end |
#config_root_parameters ⇒ Hash
Returns parameters for root stack create/update.
300 301 302 303 304 305 306 |
# File 'lib/sfn/command_module/stack.rb', line 300 def config_root_parameters Hash[ config.fetch(:parameters, {}).find_all do |k, v| !k.include?("__") end ] end |
#execute_apply_stack(provider_stack, receiver_stack, mappings) ⇒ TrueClass
Apply provider stack outputs to receiver stack parameters
101 102 103 104 |
# File 'lib/sfn/command_module/stack.rb', line 101 def execute_apply_stack(provider_stack, receiver_stack, mappings) receiver_stack.apply_stack(provider_stack, :mapping => mappings) true end |
#format_config_parameters! ⇒ Object
Format config defined parameters to ensure expected layout
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/sfn/command_module/stack.rb', line 132 def format_config_parameters! if config.get(:parameter).is_a?(Array) config[:parameter] = Smash[ *config.get(:parameter).map(&:to_a).flatten ] end if config.get(:parameters) config.set(:parameters, config.get(:parameters).merge(config.fetch(:parameter, Smash.new))) else config.set(:parameters, config.fetch(:parameter, Smash.new)) end end |
#function_set_parameter?(val) ⇒ TrueClass, FalseClass
Determine if parameter was set via intrinsic function
295 296 297 |
# File 'lib/sfn/command_module/stack.rb', line 295 def function_set_parameter?(val) val.is_a?(Hash) end |
#generate_custom_apply_mappings(provider_stack) ⇒ Hash
Build apply mappings valid for given provider stack
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/sfn/command_module/stack.rb', line 65 def generate_custom_apply_mappings(provider_stack) if config[:apply_mapping] valid_keys = config[:apply_mapping].keys.find_all do |a_key| a_key = a_key.to_s key_parts = a_key.split("__") case key_parts.size when 3 provider_stack.api.data[:location] == key_parts[0] && provider_stack.name == key_parts[1] when 2 provider_stack.name == key_parts[0] when 1 true else raise ArgumentError, "Invalid name format for apply stack mapping (`#{a_key}`)" end end to_remove = valid_keys.find_all do |key| valid_keys.any? { |v_key| v_key.match(/__#{Regexp.escape(key)}$/) } end valid_keys -= to_remove Hash[ valid_keys.map do |a_key| cut_key = a_key.split("__").last [cut_key, config[:apply_mapping][a_key]] end ] end end |
#locate_config_parameter_key(parameter_prefix, parameter_name, root_name) ⇒ Array<String>
Determine correct configuration parameter key
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/sfn/command_module/stack.rb', line 151 def locate_config_parameter_key(parameter_prefix, parameter_name, root_name) check_name = parameter_name.downcase.tr("-_", "") check_prefix = parameter_prefix.map { |i| i.downcase.tr("-_", "") } key_match = config[:parameters].keys.detect do |cp_key| cp_key = cp_key.to_s.downcase.split("__").map { |i| i.tr("-_", "") }.join("__") non_root_matcher = (check_prefix + [check_name]).join("__") root_matcher = ([root_name] + check_prefix + [check_name]).join("__") cp_key == non_root_matcher || cp_key == root_matcher end actual_key = (parameter_prefix + [parameter_name]).compact.join("__") if key_match ui.debug "Remapping configuration runtime parameter `#{key_match}` -> `#{actual_key}`" config[:parameters][actual_key] = config[:parameters].delete(key_match) end actual_key end |
#populate_parameters!(sparkle, opts = {}) ⇒ Hash
Prompt for parameter values and store result
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/sfn/command_module/stack.rb', line 238 def populate_parameters!(sparkle, opts = {}) current_parameters = opts[:current_parameters] || {} current_stack = opts[:stack] parameter_prefix, stack_parameters = prefix_parameters_setup(sparkle) sparkle_root_name = sparkle.is_a?(SparkleFormation) ? sparkle.root.name : nil unless stack_parameters.empty? format_config_parameters! = false stack_parameters.each do |param_name, param_value| ns_key = locate_config_parameter_key(parameter_prefix, param_name, sparkle_root_name) # When parameter is a hash type, it is being set via # intrinsic function and we don't modify if function_set_parameter?(current_parameters[param_name]) if !config[:parameters][ns_key].nil? ui.warn "Overriding mapped parameter value with explicit assignment `#{ns_key}`!" else if current_stack enable_set = validate_stack_parameter(current_stack, param_name, ns_key, current_parameters[param_name]) else enable_set = true end end if enable_set # NOTE: direct set dumps the stack (nfi). Smash will # auto dup it, and works, so yay i guess. config[:parameters][ns_key] = current_parameters[param_name].is_a?(Hash) ? Smash.new(current_parameters[param_name]) : current_parameters[param_name].dup valid = true end else if current_stack && current_stack.data[:parent_stack] use_expected = validate_stack_parameter(current_stack, param_name, ns_key, current_parameters[param_name]) unless use_expected current_parameters[param_name] = current_stack.parameters[param_name] end end end unless valid = set_parameter(sparkle, ns_key, param_name, param_value, current_parameters, ) end end end Smash[ config.fetch(:parameters, {}).map do |k, v| strip_key = parameter_prefix ? k.sub(/#{parameter_prefix.join("__")}_{2}?/, "") : k unless strip_key.include?("__") [strip_key, v] end end.compact ] end |
#prefix_parameters_setup(sparkle) ⇒ Array<Array<String>, Smash>
Generate name prefix for config parameter based on location and extract template parameters
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sfn/command_module/stack.rb', line 111 def prefix_parameters_setup(sparkle) case sparkle when SparkleFormation parameter_prefix = sparkle.root? ? [] : (sparkle.root_path - [sparkle.root]).map do |s| Bogo::Utility.camel(s.name) end stack_parameters = sparkle.compile.parameters stack_parameters = stack_parameters.nil? ? Smash.new : stack_parameters._dump when Hash parameter_prefix = [] stack_parameters = TEMPLATE_PARAMETER_LOCATIONS.map do |loc_key| sparkle[loc_key] end.compact.first || Smash.new else parameter_prefix = [] stack_parameters = Smash.new end [parameter_prefix, stack_parameters] end |
#set_parameter(sparkle, ns_key, param_name, param_value, current_parameters, param_banner) ⇒ TrueClass, FalseClass
Populate stack parameter value via user interaction
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 223 224 225 226 227 228 229 |
# File 'lib/sfn/command_module/stack.rb', line 178 def set_parameter(sparkle, ns_key, param_name, param_value, current_parameters, ) valid = false attempt = 0 if !valid && ! if sparkle.is_a?(SparkleFormation) ui.info "#{ui.color("Stack runtime parameters:", :bold)} - template: #{ui.color(sparkle.root_path.map(&:name).map(&:to_s).join(" > "), :green, :bold)}" else ui.info ui.color("Stack runtime parameters:", :bold) end = true end until valid attempt += 1 default = config[:parameters].fetch( ns_key, current_parameters.fetch( param_name, TEMPLATE_PARAMETER_DEFAULTS.map { |loc_key| param_value[loc_key] }.compact.first ) ) if config[:interactive_parameters] no_echo = !!TEMPLATE_PARAMETER_NOECHO.detect { |loc_key| param_value[loc_key].to_s.downcase == "true" } sfn_no_echo = TEMPLATE_PARAMETER_SFN_NOECHO.map do |loc_key| res = param_value.delete(loc_key).to_s.downcase res if !res.empty? && res != "false" end.compact.first no_echo = sfn_no_echo if sfn_no_echo answer = ui.ask_question( "#{param_name.split(/([A-Z]+[^A-Z]*)/).find_all { |s| !s.empty? }.join(" ")}", :default => default, :hide_default => sfn_no_echo == "all", :no_echo => !!no_echo, ) else answer = default end validation = validate_parameter(answer, param_value) if validation == true config[:parameters][ns_key] = answer valid = true else validation.each do |validation_error| ui.error "#{param_name}: #{validation_error.last}" end end if attempt > MAX_PARAMETER_ATTEMPTS ui.fatal "Failed to receive allowed parameter!" exit 1 end end end |
#validate_stack_parameter(c_stack, p_key, p_ns_key, c_value) ⇒ TrueClass, FalseClass
Validate stack parameter is properly set via stack resource from parent stack. If not properly set, prompt user for expected behavior. This accounts for states encountered when a nested stack’s parameters are adjusted directly but the resource sets value via intrinsic function.
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
# File 'lib/sfn/command_module/stack.rb', line 319 def validate_stack_parameter(c_stack, p_key, p_ns_key, c_value) stack_value = c_stack.parameters[p_key] p_stack = c_stack.data[:parent_stack] unless config[:parameter_validation] == "none" if c_value.is_a?(Hash) case c_value.keys.first when "Ref" current_value = p_stack.parameters[c_value.values.first] when "Fn::Att" resource_name, output_name = c_value.values.first.split(".", 2) ref_stack = p_stack.nested_stacks.detect { |i| i.data[:logical_id] == resource_name } if ref_stack output = ref_stack.outputs.detect do |o| o.key == output_name end if output current_value = output.value end end end else current_value = c_value end if current_value && current_value.to_s != stack_value.to_s if config[:parameter_validation] == "default" ui.warn "Nested stack has been altered directly! " \ "This update may cause unexpected modifications!" ui.warn "Stack name: #{c_stack.name}. Parameter: #{p_key}. " \ "Current value: #{stack_value}. Expected value: #{current_value} " \ "(via: #{c_value.inspect})" if config[:interactive_parameters] answer = ui.ask_question("Use current value or expected value for #{p_key} " \ "[current/expected]?", :valid => ["current", "expected"]) else raise Error::InteractionDisabled end else answer = config[:parameter_validation] end answer == "expected" else true end else true end end |