Module: KnifeCloudformation::Knife::Template::InstanceMethods

Defined in:
lib/knife-cloudformation/knife/template.rb

Instance Method Summary collapse

Instance Method Details

#load_template_file(*args) ⇒ Hash

Load the template file

Parameters:

  • args (Symbol)

    options (:allow_missing)

Returns:

  • (Hash)

    loaded template



18
19
20
21
22
23
24
25
26
27
28
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
# File 'lib/knife-cloudformation/knife/template.rb', line 18

def load_template_file(*args)
  unless(Chef::Config[:knife][:cloudformation][:template])
    set_paths_and_discover_file!
    unless(File.exists?(Chef::Config[:knife][:cloudformation][:file].to_s))
      unless(args.include?(:allow_missing))
        ui.fatal "Invalid formation file path provided: #{Chef::Config[:knife][:cloudformation][:file]}"
        exit 1
      end
    end
  end
  if(Chef::Config[:knife][:cloudformation][:template])
    Chef::Config[:knife][:cloudformation][:template]
  elsif(Chef::Config[:knife][:cloudformation][:file])
    if(Chef::Config[:knife][:cloudformation][:processing])
      sf = SparkleFormation.compile(Chef::Config[:knife][:cloudformation][:file], :sparkle)
      if(sf.nested? && Chef::Config[:knife][:cloudformation][:apply_nesting])
        sf.apply_nesting do |stack_name, stack_definition|
          bucket = provider.connection.api_for(:storage).buckets.get(
            Chef::Config[:knife][:cloudformation][:nesting_bucket]
          )
          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(KnifeCloudformation::Utils::StackParameterScrubber.scrub!(stack_definition))
          file.save
          # TODO: what if we need extra params?
          url = URI.parse(file.url)
          "#{url.scheme}://#{url.host}#{url.path}"
        end
      else
        if(sf.nested? && !sf.isolated_nests?)
          raise TypeError.new('Template does not contain isolated stack nesting! Cannot process in existing state.')
        end
        sf.dump
      end
    else
      _from_json(File.read(Chef::Config[:knife][:cloudformation][:file]))
    end
  end
end

#set_paths_and_discover_file!TrueClass

Set SparkleFormation paths and locate tempate

Returns:

  • (TrueClass)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/knife-cloudformation/knife/template.rb', line 90

def set_paths_and_discover_file!
  if(Chef::Config[:knife][:cloudformation][:base_directory])
    SparkleFormation.components_path = File.join(
      Chef::Config[:knife][:cloudformation][:base_directory], 'components'
    )
    SparkleFormation.dynamics_path = File.join(
      Chef::Config[:knife][:cloudformation][:base_directory], 'dynamics'
    )
  end
  if(!Chef::Config[:knife][:cloudformation][:file] && Chef::Config[:knife][:cloudformation][:file_path_prompt])
    root = File.expand_path(
      Chef::Config[:knife][:cloudformation].fetch(:base_directory,
        File.join(Dir.pwd, 'cloudformation')
      )
    ).split('/')
    bucket = root.pop
    root = root.join('/')
    directory = File.join(root, bucket)
    Chef::Config[:knife][:cloudformation][:file] = prompt_for_file(directory,
      :directories_name => 'Collections',
      :files_name => 'Templates',
      :ignore_directories => TEMPLATE_IGNORE_DIRECTORIES
    )
  else
    unless(Pathname(Chef::Config[:knife][:cloudformation][:file].to_s).absolute?)
      base_dir = Chef::Config[:knife][:cloudformation][:base_directory].to_s
      file = Chef::Config[:knife][:cloudformation][:file].to_s
      pwd = Dir.pwd
      Chef::Config[:knife][:cloudformation][:file] = [
        File.join(base_dir, file),
        File.join(pwd, file),
        File.join(pwd, 'cloudformation', file)
      ].detect do |file_path|
        File.file?(file_path)
      end
    end
  end
  true
end

#translate_template(template) ⇒ Hash

Apply template translation

Parameters:

  • template (Hash)

Returns:

  • (Hash)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/knife-cloudformation/knife/template.rb', line 66

def translate_template(template)
  if(klass_name = Chef::Config[:knife][:cloudformation][:translate])
    klass = SparkleFormation::Translation.const_get(camel(klass_name))
    args = {
      :parameters => Chef::Config[:knife][:cloudformation][:options][:parameters]
    }
    if(chunk_size = Chef::Config[:knife][:cloudformation][: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