Module: Sfn::CommandModule::Template::InstanceMethods

Defined in:
lib/sfn/command_module/template.rb

Instance Method Summary collapse

Instance Method Details

#format_nested_stack_results(provider, results) ⇒ Hash

Update the nested stack information for specific provider

Parameters:

  • provider (Symbol)
  • results (Hash)

Returns:

  • (Hash)


316
317
318
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
# File 'lib/sfn/command_module/template.rb', line 316

def format_nested_stack_results(provider, results)
  case provider
  when :aws
    if(results[:parameters])
      results['Parameters'] = results.delete(:parameters)
    end
    if(results[:url])
      url = URI.parse(results.delete(:url))
      results['TemplateURL'] = "#{url.scheme}://#{url.host}#{url.path}"
    end
    results
  when :heat, :rackspace
    results[:template] = results.delete(:url)
    results
  when :azure
    if(results[:parameters])
      results[:parameters] = Smash[
        results[:parameters].map do |key, value|
          [key,
            value.is_a?(Hash) ? value : Smash.new(:value => value)]
        end
      ]
    end
    if(results[:url])
      results[:templateLink] = Smash.new(
        :uri => results.delete(:url),
        :contentVersion => '1.0.0.0'
      )
    end
    results[:mode] = 'Incremental'
    results
  else
    raise "Unknown stack provider value given! `#{provider}`"
  end
end

#load_template_file(*args) ⇒ Hash

Load the template file

Parameters:

  • args (Symbol)

    options (:allow_missing)

Returns:

  • (Hash)

    loaded template



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/sfn/command_module/template.rb', line 125

def load_template_file(*args)
  c_stack = (args.detect{|i| i.is_a?(Hash)} || {})[:stack]
  unless(config[:template])
    set_paths_and_discover_file!
    unless(config[:file])
      unless(args.include?(:allow_missing))
        ui.fatal "Invalid formation file path provided: #{config[:file]}"
        raise IOError.new "Failed to locate file: #{config[:file]}"
      end
    end
  end
  if(config[:template])
    config[:template]
  elsif(config[:file])
    if(config[:processing])
      compile_state = config.fetch(:compile_parameters, Smash.new)
      sf = SparkleFormation.compile(config[:file], :sparkle)
      if(name_args.first)
        sf.name = name_args.first
      end
      sf.compile_time_parameter_setter do |formation|
        f_name = formation.root_path.map(&:name).map(&:to_s)
        pathed_name = f_name.join(' > ')
        f_name = f_name.join('__')
        current_state = compile_state[f_name]
        unless(current_state)
          f_name = f_name.split('__')
          f_name.shift
          f_name = f_name.join('__')
          current_state = f_name.empty? ? compile_state : compile_state[f_name]
        end
        if(formation.root? && compile_state[f_name].nil?)
          current_state = compile_state
        else
          current_state = compile_state.fetch(f_name, Smash.new)
        end
        if(formation.compile_state)
          current_state = current_state.merge(formation.compile_state)
        end
        unless(formation.parameters.empty?)
          ui.info "#{ui.color('Compile time parameters:', :bold)} - template: #{ui.color(pathed_name, :green, :bold)}" unless config[:print_only]
          formation.parameters.each do |k,v|
            current_state[k] = request_compile_parameter(k, v, current_state[k], !!formation.parent)
          end
          formation.compile_state = current_state
        end
      end
      sf.sparkle.apply sparkle_collection
      custom_stack_types.each do |s_type|
        unless(sf.stack_resource_types.include?(s_type))
          sf.stack_resource_types.push(s_type)
        end
      end
      if(sf.nested? && !sf.isolated_nests?)
        raise TypeError.new('Template does not contain isolated stack nesting! Sfn does not support mixed mixed resources within root stack!')
      end
      run_callbacks_for(:template, :stack_name => arguments.first, :sparkle_stack => sf)
      if(sf.nested? && config[:apply_nesting])
        if(config[:apply_nesting] == true)
          config[:apply_nesting] = :deep
        end
        case config[:apply_nesting].to_sym
        when :deep
          process_nested_stack_deep(sf, c_stack)
        when :shallow
          process_nested_stack_shallow(sf, c_stack)
        when :none
          sf.dump.merge('sfn_nested_stack' => !!sf.nested?)
        else
          raise ArgumentError.new "Unknown nesting style requested: #{config[:apply_nesting].inspect}!"
        end
      else
        sf.dump.merge('sfn_nested_stack' => !!sf.nested?)
      end
    else
      template = _from_json(File.read(config[:file]))
      run_callbacks_for(:template, :stack_name => arguments.first, :hash_stack => template)
      template
    end
  else
    raise ArgumentError.new 'Failed to locate template for processing!'
  end
end

#process_nested_stack_deep(sf, c_stack = nil) ⇒ Hash

Processes template using new deep workflow

Parameters:

  • sf (SparkleFormation)

    stack

  • c_stack (Miasma::Models::Orchestration::Stack) (defaults to: nil)

    existing stack

Returns:

  • (Hash)

    dumped stack



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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/sfn/command_module/template.rb', line 247

def process_nested_stack_deep(sf, c_stack=nil)
  sf.apply_nesting(:deep) do |stack_name, stack, resource|
    run_callbacks_for(:template, :stack_name => stack_name, :sparkle_stack => stack)

    stack_resource = resource._dump

    if(stack.parent)
      current_parameters = stack.parent.compile.resources.set!(stack_name).properties.parameters
      current_parameters = current_parameters.nil? ? Smash.new : current_parameters._dump
    else
      current_parameters = Smash.new
    end
    current_stack = c_stack ? c_stack.nested_stacks.detect{|s| s.data[:logical_id] == stack_name} : nil
    if(current_stack && current_stack.data[:parent_stack])
      current_parameters.merge!(
        current_stack.data[:parent_stack].template.fetch(
          'Resources', stack_name, 'Properties', 'Parameters', current_stack.data[:parent_stack].template.fetch(
            'resources', stack_name, 'properties', 'parameters', Smash.new
          )
        )
      )
    end
    full_stack_name = [
      config[:nesting_prefix],
      stack.root_path.map(&:name).map(&:to_s).join('_')
    ].compact.join('/')
    unless(config[:print_only])
      result = Smash.new(
        :parameters => populate_parameters!(stack,
          :stack => current_stack,
          :current_parameters => current_parameters
        )
      )
      hold_stack = resource.properties.delete!(:stack)
      stack_definition = stack.compile.dump!

      if(config[:plan])
        resource.properties.stack hold_stack
      end
      bucket = provider.connection.api_for(:storage).buckets.get(
        config[:nesting_bucket]
      )
      unless(bucket)
        raise "Failed to locate configured bucket for stack template storage (#{config[:nesting_bucket]})!"
      end
      file = bucket.files.build
      file.name = "#{full_stack_name}.json"
      file.content_type = 'text/json'
      file.body = MultiJson.dump(Sfn::Utils::StackParameterScrubber.scrub!(stack_definition))
      file.save
      result.merge!(
        :url => file.url
      )
    else
      result = Smash.new(
        :url => "http://example.com/bucket/#{full_stack_name}.json"
      )
    end
    format_nested_stack_results(resource._self.provider, result).each do |k,v|
      resource.properties.set!(k, v)
    end
  end
end

#process_nested_stack_shallow(sf, c_stack = nil) ⇒ Hash

Processes template using the original shallow workflow

Parameters:

  • sf (SparkleFormation)

    stack formation

  • c_stack (Miasma::Models::Orchestration::Stack) (defaults to: nil)

    existing stack

Returns:

  • (Hash)

    dumped stack



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/sfn/command_module/template.rb', line 214

def process_nested_stack_shallow(sf, c_stack=nil)
  sf.apply_nesting(:shallow) do |stack_name, stack, resource|
    run_callbacks_for(:template, :stack_name => stack_name, :sparkle_stack => stack)
    stack_definition = stack.compile.dump!
    bucket = provider.connection.api_for(:storage).buckets.get(
      config[:nesting_bucket]
    )
    if(config[:print_only])
      template_url = "http://example.com/bucket/#{name_args.first}_#{stack_name}.json"
    else
      unless(config[:plan])
        resource.properties.delete!(:stack)
      end
      unless(bucket)
        raise "Failed to locate configured bucket for stack template storage (#{bucket})!"
      end
      file = bucket.files.build
      file.name = "#{name_args.first}_#{stack_name}.json"
      file.content_type = 'text/json'
      file.body = MultiJson.dump(Sfn::Utils::StackParameterScrubber.scrub!(stack_definition))
      file.save
      url = URI.parse(file.url)
      template_url = "#{url.scheme}://#{url.host}#{url.path}"
    end
    resource.properties.set!('TemplateURL', template_url)
  end
end

#prompt_for_template(prefix = nil) ⇒ String

Prompt user for template selection

Parameters:

  • prefix (String) (defaults to: nil)

    prefix filter for names

Returns:

  • (String)

    path to template



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/sfn/command_module/template.rb', line 403

def prompt_for_template(prefix=nil)
  if(prefix)
    collection_name = prefix.split('__').map do |c_name|
      c_name.split('_').map(&:capitalize).join(' ')
    end.join(' / ')
    ui.info "Viewing collection: #{ui.color(collection_name, :bold)}"
    template_names = sparkle_collection.templates.keys.find_all do |t_name|
      t_name.to_s.start_with?(prefix.to_s)
    end
  else
    template_names = sparkle_collection.templates.keys
  end
  collections = template_names.map do |t_name|
    t_name = t_name.to_s.sub(/^#{Regexp.escape(prefix.to_s)}/, '')
    if(t_name.include?('__'))
      c_name = t_name.split('__').first
      [[prefix, c_name].compact.join('') + '__', c_name]
    end
  end.compact.uniq(&:first)
  templates = template_names.map do |t_name|
    t_name = t_name.to_s.sub(/^#{Regexp.escape(prefix.to_s)}/, '')
    unless(t_name.include?('__'))
      [[prefix, t_name].compact.join(''), t_name]
    end
  end.compact
  if(collections.empty? && templates.empty?)
    ui.error 'Failed to locate any templates!'
    return nil
  end
  ui.info "Please select an entry#{ '(or collection to list)' unless collections.empty?}:"
  output = []
  idx = 1
  valid = {}
  unless(collections.empty?)
    output << ui.color('Collections:', :bold)
    collections.each do |full_name, part_name|
      valid[idx] = {:name => full_name, :type => :collection}
      output << [idx, part_name.split('_').map(&:capitalize).join(' ')]
      idx += 1
    end
  end
  unless(templates.empty?)
    output << ui.color('Templates:', :bold)
    templates.each do |full_name, part_name|
      valid[idx] = {:name => full_name, :type => :template}
      output << [idx, part_name.split('_').map(&:capitalize).join(' ')]
      idx += 1
    end
  end
  max = idx.to_s.length
  output.map! do |line|
    if(line.is_a?(Array))
      "  #{line.first}.#{' ' * (max - line.first.to_s.length)} #{line.last}"
    else
      line
    end
  end
  ui.puts "#{output.join("\n")}\n"
  response = nil
  until(valid[response])
    response = ui.ask_question('Enter selection').to_i
  end
  entry = valid[response]
  if(entry[:type] == :collection)
    prompt_for_template(entry[:name])
  else
    sparkle_collection.get(:template, entry[:name])[:path]
  end
end

#request_compile_parameter(p_name, p_config, cur_val, nested = false) ⇒ Object

Request compile time parameter value

Parameters:

  • p_name (String, Symbol)

    name of parameter

  • p_config (Hash)

    parameter meta information

  • cur_val (Object, NilClass)

    current value assigned to parameter

  • nested (TrueClass, FalseClass) (defaults to: false)

    template is nested

Options Hash (p_config):

  • :type (String, Symbol)
  • :default (String, Symbol)
  • :description (String, Symbol)
  • :multiple (String, Symbol)

Returns:

  • (Object)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/sfn/command_module/template.rb', line 29

def request_compile_parameter(p_name, p_config, cur_val, nested=false)
  result = nil
  attempts = 0
  unless(cur_val || p_config[:default].nil?)
    cur_val = p_config[:default]
  end
  if(cur_val.is_a?(Array))
    cur_val = cur_val.map(&:to_s).join(',')
  end
  until(result && (!result.respond_to?(:empty?) || !result.empty?))
    attempts += 1
    if(config[:interactive_parameters] && (!nested || !p_config.key?(:prompt_when_nested) || p_config[:prompt_when_nested] == true))
      result = ui.ask_question(
        p_name.to_s.split('_').map(&:capitalize).join,
        :default => cur_val.to_s.empty? ? nil : cur_val.to_s
      )
    else
      result = cur_val.to_s
    end
    case p_config.fetch(:type, 'string').to_s.downcase.to_sym
    when :string
      if(p_config[:multiple])
        result = result.split(',').map(&:strip)
      end
    when :number
      if(p_config[:multiple])
        result = result.split(',').map(&:strip)
        new_result = result.map do |item|
          new_item = item.to_i
          new_item if new_item.to_s == item
        end
        result = new_result.size == result.size ? new_result : []
      else
        new_result = result.to_i
        result = new_result.to_s == result ? new_result : nil
      end
    else
      raise ArgumentError.new "Unknown compile time parameter type provided: `#{p_config[:type].inspect}` (Parameter: #{p_name})"
    end
    if(result.nil? || (result.respond_to?(:empty?) && result.empty?))
      if(attempts > MAX_PARAMETER_ATTEMPTS)
        ui.fatal "Failed to receive allowed parameter! (Parameter: #{p_name})"
        exit 1
      else
        ui.error "Invalid value provided for parameter. Must be type: `#{p_config[:type].to_s.capitalize}`"
      end
    end
  end
  result
end

#set_paths_and_discover_file!TrueClass

Set SparkleFormation paths and locate tempate

Returns:

  • (TrueClass)


380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'lib/sfn/command_module/template.rb', line 380

def set_paths_and_discover_file!
  if(config[:processing])
    if(!config[:file] && config[:file_path_prompt])
      config[:file] = prompt_for_template
    else
      config[:file] = sparkle_collection.get(:template, config[:file])[:path]
    end
  else
    if(config[:file])
      unless(File.exists?(config[:file]))
        raise Errno::ENOENT.new("No such file - #{config[:file]}")
      end
    else
      raise "Template processing is disabled. Path to serialized template via `--file` required!"
    end
  end
  true
end

#sparkle_collectionSparkleFormation::SparkleCollection

Returns:

  • (SparkleFormation::SparkleCollection)


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/sfn/command_module/template.rb', line 101

def sparkle_collection
  memoize(:sparkle_collection) do
    collection = SparkleFormation::SparkleCollection.new
    begin
      if(config[:base_directory])
        root_pack = SparkleFormation::SparklePack.new(:root => config[:base_directory])
      else
        root_pack = SparkleFormation::SparklePack.new
      end
      collection.set_root(root_pack)
    rescue Errno::ENOENT
      ui.warn 'No local SparkleFormation files detected'
    end
    sparkle_packs.each do |pack|
      collection.add_sparkle(pack)
    end
    collection
  end
end

#sparkle_packsArray<SparkleFormation::SparklePack>

Returns:

  • (Array<SparkleFormation::SparklePack>)


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sfn/command_module/template.rb', line 81

def sparkle_packs
  memoize(:sparkle_packs) do
    [config.fetch(:sparkle_pack, [])].flatten.compact.map do |sparkle_name|
      begin
        require sparkle_name
      rescue LoadError
        ui.fatal "Failed to locate sparkle pack `#{sparkle_name}` for loading!"
        raise
      end
      begin
        SparkleFormation::Sparkle.new(:name => sparkle_name)
      rescue ArgumentError
        ui.fatal "Failed to properly setup sparkle pack `#{sparkle_name}`. Check implementation."
        raise
      end
    end
  end
end

#translate_template(template) ⇒ Hash

Apply template translation

Parameters:

  • template (Hash)

Returns:

  • (Hash)


356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/sfn/command_module/template.rb', line 356

def translate_template(template)
  if(klass_name = config[:translate])
    klass = SparkleFormation::Translation.const_get(camel(klass_name))
    args = {
      :parameters => config.fetch(:options, :parameters, Smash.new)
    }
    if(chunk_size = config[:translate_chunk_size])
      args.merge!(
        :options => {
          :serialization_chunk_size => chunk_size
        }
      )
    end
    translator = klass.new(template, args)
    translator.translate!
    template = translator.translated
    ui.info "#{ui.color('Translation applied:', :bold)} #{ui.color(klass_name, :yellow)}"
  end
  template
end