Module: Aws::Cfn::Compiler::Compile

Included in:
Base
Defined in:
lib/aws/cfn/compiler/mixins/compile.rb

Instance Method Summary collapse

Instance Method Details

#compile_specObject



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
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 99

def compile_spec
  desc =  if @config[:description]
            @config[:description]
          elsif @spec and @spec['Description']
            @spec['Description']
          elsif @config[:template]
            File.basename(@config[:template]).gsub(%r/\..*$/, '')
          else
            'compiled template'
          end

  vers =  if @config[:formatversion]
            @config[:formatversion]
          else
            if @spec and @spec['AWSTemplateFormatVersion']
              @spec['AWSTemplateFormatVersion']
            else
              '2010-09-09'
            end
          end

  desc = compile_value(desc)

  # [2014-06-29 Christo] IIRC it is important that the section names be strings instead of symbols ...
  # noinspection RubyStringKeysInHashInspection
  compiled =
      {
          'AWSTemplateFormatVersion' => vers,
          'Description'              => desc,
          'Mappings'                 => @items['Mappings'],
          'Parameters'               => @items['Parameters'],
          'Conditions'               => @items['Conditions'],
          'Resources'                => @items['Resources'],
          'Outputs'                  => @items['Outputs'],
      }

  @all_sections.each do |section|
    value = compile_value(@items[section])
    compiled[section] = value if value
  end
  compiled
end

#compile_value(expr) ⇒ Object



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
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 142

def compile_value(expr)
  begin
    if expr.is_a?(Hash)
      val = expr
      expr.each do |k,v|
        val[k] = compile_value(v)
      end
      val
    elsif expr.is_a?(Array)
      expr.map{ |e|
        compile_value(e)
      }
    elsif expr.is_a?(Symbol)
      expr
    elsif expr.is_a?(NilClass)
      expr
    elsif expr.is_a?(TrueClass)
      expr
    elsif expr.is_a?(FalseClass)
      expr
    elsif expr.is_a?(Fixnum)
      expr
    elsif expr.is_a?(String)
      val = expr
      if expr.match(%r'#\{.+?\}')
        eval %(val = "#{expr}")
      end
      val
    else
      raise "The expression type #{expr.class.name} cannot be compiled!\n#{expr}"
    end
  rescue Exception => e
    abort! "Specification expression error: #{e.message} on '#{expr}'"
  end
end

#find_conditions(hash, level = 0) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 269

def find_conditions(hash,level=0)
  if hash.is_a? Hash
    hash.keys.collect do |key|
      if 'Condition' == key and level <= 4
        if hash[key].is_a?(Array)
          hash[key].first
        else
          hash[key]
        end
      else
        find_conditions(hash[key],level+1)
      end
    end.flatten.compact.uniq
  elsif hash.is_a? Array
    hash.collect{|a| find_conditions(a,level+1)}.flatten.compact.uniq
  end
end

#find_fns(hash) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 237

def find_fns(hash)
  a = []
  if hash.is_a? Hash
    hash.each do |key,val|
      if key.match %r'^Fn::'
        a << key
      end
      a << find_fns(val)
    end
  elsif hash.is_a? Array
    hash.collect{|e|
      a << find_fns(e)
    }
  end
  r = a.flatten.compact.uniq
end

#find_maps(hash, source = []) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 254

def find_maps(hash,source=[])
  if hash.is_a? Hash
    hash.keys.collect do |key|
      if 'Fn::FindInMap' == key
        { :mapping => hash[key].first, :source => source }
      else
        find_maps(hash[key], [ source, key ].flatten!)
      end
    end.flatten.compact.uniq
  elsif hash.is_a? Array
    hash.collect{|a|
      find_maps(a,source)}.flatten.compact.uniq
  end
end

#find_refs(hash, type = 'Reference', parent = '') ⇒ Object



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
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 178

def find_refs(hash, type='Reference', parent='')
  h = {}
  newparent = parent
  if hash.is_a? Hash
    hash.keys.collect do |key|
      if @all_sections.include? key
        type = key#.gsub(/s$/, '')
        newparent = key
      elsif @all_sections.include? parent
        newparent = key
      end
      if %w{Ref}.include? key
        # h = { hash[key] => [type,newparent] }
        h[hash[key]] = [type,newparent]
      elsif 'Fn::GetAtt' == key
        # h = { hash[key].first => [type,newparent] }
        h[hash[key].first] = [type,newparent]
      elsif 'DependsOn' == key
        if hash[key].is_a?(Array)
          # h = {}
          hash[key].map { |dep|
            h[dep] = [type,newparent]
          }
        else
          # h = { hash[key] => [type,newparent] }
          h[hash[key]] = [type,newparent]
        end
      else
        a = find_refs(hash[key],type,newparent)
        h = merge(h, a, *[type,newparent])
      end
    end.flatten.compact.uniq
  elsif hash.is_a? Array
    a = hash.map{|i| find_refs(i,type,newparent) }
    h = merge(h, a, type, *[type,newparent])
  end
  h
end

#get_meta(spec, args) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
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
61
62
63
64
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
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 6

def get_meta(spec,args)

  if spec.is_a?(Hash)
    if args.is_a?(Array)
      args.flatten!
      if args.size > 0
        a = args.shift
        get_meta(get_meta(spec, a), args)
      else
        spec
      end
    elsif args.is_a?(Hash)
      h = {}
      args.map { |e, v| h[e] = get_meta(spec, v) }
      h
    elsif args.is_a?(Symbol)
      # noinspection RubyStringKeysInHashInspection
      case args
        when :Compiler
          {
              'Name'    => ::Aws::Cfn::Compiler.name,
              'Version' => ::Aws::Cfn::Compiler::VERSION,
          }
        when :Specification
          File.basename(@config[:specification])
        when :Template
          File.basename(@config[:template])
        when :DescriptionString
          v = nil
          begin
            dependson = meta(:DependsOn) || []
          rescue
            dependson = []
          end
          begin
            required = meta(:Require)['Template']
            ok = true
            required.map { |e| ok = (ok and e.is_a?(Hash)) }
            if ok
              required = required.map { |e| e.keys }.flatten
            else
              required = {}
            end
          rescue
            required = {}
          end
          raise "Bad Require:Template: meta-data ...\n#{meta(:Require).ai}. Must resolve to a Hash!\nFor example:\nRequire:\n  Template:\n  - my-template: '>= 0.0.0'" unless required
          begin
            # if dependson or required
              # noinspection RubyHashKeysTypesInspection
              parents = {}
              dependson.each { |i| parents[i] = true }
              required.each { |i| parents[i] = true }
              parents = "Parents: [#{parents.keys.join(',')}] "
            # else
            #   parents = ''
            # end
          rescue
            parents = ''
          end
          # noinspection RubyExpressionInStringInspection
          template = '#{meta(:Project,:Description)}(#{meta(:Project,:Name)}) - #{meta(:Name)} v#{meta(:Version)}; #{parents} [Compiled with #{meta(:Compiler,:Name)} v#{meta(:Compiler,:Version)}]'
          begin
            eval %(v = "#{template}" )
          rescue Exception => e
            raise e.message + "\nIn:\n" + template
          end
          v
        else
          get_meta(spec, args.to_s)
      end
    elsif args.is_a?(String)
      if spec[args]
        spec[args]
      else
        nil # raise "Meta:'#{args}' not set"
      end
    else
      nil
    end
  else
    spec
  end
end

#map_resource_reference(rsrc) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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
351
352
353
354
355
356
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 287

def map_resource_reference(rsrc)
  path = nil
  sub  = nil
  ref  = nil
  rel  = false
  # noinspection RubyParenthesesAroundConditionInspection
  if rsrc.match %r'^(\.\./.*?)::(.*)$'
    # Relative path stack reference
    path,sub,ref,rel  = map_resource_reference(File.basename(rsrc))
  elsif rsrc.match %r'^(~/.*?)$'
    # Relative to HOME
    path,sub,ref,rel  = map_resource_reference(File.expand_path(rsrc))
  elsif rsrc.match %r'^(\.\./[^:]*?)$'
    # Relative path
    path = File.dirname(rsrc)
    sub  = File.basename(path)
    path = File.dirname(path)
    ref  = File.basename(rsrc)
    rel  = true
  elsif rsrc.match %r'(^/.*?)::(.*)$'
    # Absolute path
    _,sub,ref,rel  = map_resource_reference(File.basename(rsrc))
    path = File.realpath(File.join(File.dirname(rsrc),_))
  elsif rsrc.match %r'(^/.*?[^:]*?)$'
    # Absolute path
    path = File.dirname(rsrc)
    sub  = File.basename(path)
    path = File.dirname(path)
    ref  = File.basename(rsrc)
  elsif (match = rsrc.match %r'^(.*?)::(.*)$')
    # Inherited stack reference
    ref = match[2]
    # noinspection RubyParenthesesAroundConditionInspection
    if (subm = match[1].match(%r'^(.+?)/(.+)$'))
      # Stack referenced with a path
      @config[:brick_path_list].each do |p|
        path = File.join(p,subm[1]) # File.dirname(@config[:directory])
        if File.directory?(path)
          break
        else
          path = nil
        end
      end
      sub = subm[2]
    else
      # Stack referenced on stack_path ...
      pstk = @config[:stack_path_list].map{ |p|
        if File.basename(p) == match[1]
          p
        else
          []
        end
      }.flatten.shift
      if pstk
        parseList(@config[:brick_path],':').each do |p|
          path = File.join(pstk,p) # File.dirname(@config[:directory])
          if File.directory?(path)
            break
          else
            path = nil
          end
        end
      end
    end
  else
    # Otherwise it is what it seems ;)
    ref  = rsrc
  end
  [path,sub,ref,rel]
end

#merge(h, a, *type) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 217

def merge(h, a, *type)
  if a.is_a? Hash
    if a.size > 0
      h.merge! a
    end
  else
    a.flatten.compact.uniq.map { |i|
      if i.is_a? Hash
        if i.size > 0
          h.merge! i
          h
        end
      else
        h[i] = type
      end
    }
  end
  h
end

#meta(*args) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 91

def meta(*args)
  if @spec['Meta']
    get_meta(@spec['Meta'],args)
  else
    raise "Specification contained no metadata while expanding #{args}"
  end
end