Class: Aws::Cfn::Compiler::Base

Inherits:
Dsl::Base
  • Object
show all
Defined in:
lib/aws/cfn/compiler/base.rb

Direct Known Subclasses

Main

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



17
18
19
20
# File 'lib/aws/cfn/compiler/base.rb', line 17

def initialize
  super
  @items = {}
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



13
14
15
# File 'lib/aws/cfn/compiler/base.rb', line 13

def items
  @items
end

#optsObject

Returns the value of attribute opts.



14
15
16
# File 'lib/aws/cfn/compiler/base.rb', line 14

def opts
  @opts
end

#specObject

Returns the value of attribute spec.



15
16
17
# File 'lib/aws/cfn/compiler/base.rb', line 15

def spec
  @spec
end

Instance Method Details

#load_spec(spec = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
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
# File 'lib/aws/cfn/compiler/base.rb', line 80

def load_spec(spec=nil)
  if spec
    abs = nil
    [spec, File.join(@config[:directory],spec)].each do |p|
      begin
        abs = File.realpath(File.absolute_path(File.expand_path(p)))
        break if File.exists?(abs)
      rescue => e
        @logger.debug e
        # pass
      end
    end

    if not abs.nil? and File.exists?(abs)
      logStep "Loading specification #{@config[:expandedpaths] ? abs : spec}..."
      unless abs =~ /\.(json|ya?ml|jts|yts)\z/i
        abort! "Unsupported specification file type: #{spec}=>#{abs}\n\tSupported types are: json,yaml,jts,yts\n"
      end

      spec = File.read(abs)

      case File.extname(File.basename(abs)).downcase
        when /json|jts/
          @spec = JSON.parse(spec)
        when /yaml|yts/
          @spec = YAML.load(spec)
        else
          abort! "Unsupported file type for specification: #{spec}"
      end
    else
      abort! "Unable to open specification"+ (abs.nil? ? " or {,#{@config[:directory]}/}#{spec} not found" : ": #{abs}")
    end
    @dsl ||= Aws::Cfn::Dsl::Template.new(@config[:directory])
    %w( Mappings Parameters Resources Outputs ).each do |dir|
      load_dir(dir,@spec)
    end
  else
    raise "No specification provided"
  end
end

#save_template(output_file, compiled) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/aws/cfn/compiler/base.rb', line 57

def save_template(output_file,compiled)
  output_file = File.realpath(File.expand_path(output_file)) if @config[:expandedpaths]
  logStep "Writing compiled file to #{output_file}..."
  begin
    hash = {}
    compiled.each do |item,value|
      unless value.nil?
        if (not value.is_a?(Hash)) or (value.count > 0)
          hash[item] = value
        end
      end
    end

    File.open output_file, 'w' do |f|
      f.write JSON.pretty_generate(hash, { indent: "\t", space: ' '})
    end
    @logger.info '  Compiled file written.'
  rescue
    @logger.error "!!! Could not write compiled file: #{$!}"
    abort!
  end
end

#validate(compiled) ⇒ Object



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

def validate(compiled)
  abort! 'No Resources!?' unless compiled['Resources']
  logStep 'Validating compiled file...'

  # Mappings => Resources
  maps  = find_maps(compiled) #.select { |a| !(a =~ /^AWS::/) }
  rscs  = compiled['Resources'].keys
  mpgs  = compiled['Mappings'].nil? ? [] : compiled['Mappings'].keys
  names = rscs+mpgs

  unless (maps-names).empty?
    @logger.error '!!! Unknown mappings !!!'
    (maps-names).each do |name|
      @logger.error "  #{name}"
    end
    abort!
  end
  @logger.info '  Mappings validated'

  # Parameters => Resources => Outputs
  refs  = find_refs(compiled).select { |a,_| !(a =~ /^AWS::/) }
  prms  = compiled['Parameters'].keys rescue []
  # outs  = compiled['Outputs'].keys rescue []
  names = rscs+prms

  unless (refs.keys-names).empty?
    @logger.error '!!! Unknown references !!!'
    (refs.keys-names).each do |name|
      @logger.error "  #{name} from #{refs[name][0]}:#{refs[name][1]}"
    end
    abort!
  end
  @logger.info '  References validated'
end