Class: Aws::Cfn::Compiler::Main

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

Returns a new instance of Main.



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

def initialize
  @items = {}
end

Instance Attribute Details

#itemsObject

Returns the value of attribute items.



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

def items
  @items
end

#optsObject

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#loadObject



82
83
84
85
86
# File 'lib/aws/cfn/compiler.rb', line 82

def load
  %w{params mappings resources outputs}.each do |dir|
    load_dir(dir)
  end
end

#runObject



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

def run ()

  @opts = Slop.parse(help: true) do
    on :d, :directory=, 'The directory to look in', as: String
    on :o, :output=, 'The JSON file to output', as: String
  end

  unless @opts[:directory]
    puts @opts
    exit
  end

  load

  compiled = {
      AWSTemplateFormatVersion: "2010-09-09",
      Description:              "Description goes here",
      Parameters:               @items['params'],
      Mappings:                 @items['mappings'],
      Resources:                @items['resources'],
      Outputs:                  @items['outputs'],
  }

  output_file = @opts[:output] || "compiled.json"
  puts
  puts "Writing compiled file to #{output_file}..."
  save(compiled, output_file)

  puts
  puts "Validating compiled file..."

  validate(compiled)

  puts
  puts "*** Compiled Successfully ***"
end

#save(compiled, output_file) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/aws/cfn/compiler.rb', line 70

def save(compiled, output_file)
  begin
    File.open output_file, 'w' do |f|
      f.write JSON.pretty_generate(compiled)
    end
    puts "  Compiled file written."
  rescue
    puts "!!! Could not write compiled file: #{$!}"
    abort!
  end
end

#validate(compiled) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/aws/cfn/compiler.rb', line 56

def validate(compiled)
  names = compiled["Resources"].keys + compiled["Parameters"].keys
  refs = find_refs(compiled).select { |a| !(a =~ /^AWS::/) }

  unless (refs-names).empty?
    puts "!!! Unknown references !!!"
    (refs-names).each do |name|
      puts "  #{name}"
    end
    abort!
  end
  puts "  References validated"
end