Class: Aws::Cfn::Compiler::Base
- Inherits:
-
Dsl::Base
- Object
- Dsl::Base
- Aws::Cfn::Compiler::Base
- Defined in:
- lib/aws/cfn/compiler/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#items ⇒ Object
Returns the value of attribute items.
-
#opts ⇒ Object
Returns the value of attribute opts.
-
#spec ⇒ Object
Returns the value of attribute spec.
Instance Method Summary collapse
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #load_spec(spec = nil) ⇒ Object
- #save_template(output_file, compiled) ⇒ Object
- #validate(compiled) ⇒ Object
Methods included from Save
Methods included from Compile
#compile_spec, #find_maps, #find_refs, #map_resource_reference, #merge
Methods included from Parse
#parse, #parse_rb_file, #parse_section, #sym_to_s
Methods included from Load
Methods included from Options
#parse_options, #set_config_options
Constructor Details
#initialize ⇒ Base
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
#items ⇒ Object
Returns the value of attribute items.
13 14 15 |
# File 'lib/aws/cfn/compiler/base.rb', line 13 def items @items end |
#opts ⇒ Object
Returns the value of attribute opts.
14 15 16 |
# File 'lib/aws/cfn/compiler/base.rb', line 14 def opts @opts end |
#spec ⇒ Object
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
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 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/aws/cfn/compiler/base.rb', line 116 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.(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 else abort! '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 79 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 |
# File 'lib/aws/cfn/compiler/base.rb', line 57 def save_template(output_file,compiled) filn = output_file file = output_file file = File.(output_file) if @config[:expandedpaths] if File.exists?(file) file = File.realpath(file) end logStep "Writing compiled file to #{filn}..." 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 dir = File.dirname(output_file) file = File.basename(output_file) sect = dir == '.' ? Dir.pwd : dir sect = File.basename(sect) unless @config[:expandedpaths] save_section(dir, file, @config[:format], sect, hash, '', 'template') parameters = [] if (@config[:parametersfile] or @config[:stackinifile]) and hash.has_key?('Parameters') hash['Parameters'].each do |par,hsh| # noinspection RubyStringKeysInHashInspection parameters << { 'ParameterKey' => par, 'ParameterValue' => hsh.has_key?('Default') ? hsh['Default'] : '', # 'UsePreviousValue' => false, } end end if @config[:parametersfile] and parameters.size > 0 dir = File.dirname(@config[:parametersfile]) file = File.basename(@config[:parametersfile]) sect = dir == '.' ? Dir.pwd : dir sect = File.basename(sect) unless @config[:expandedpaths] save_section(dir, file, @config[:format], sect, parameters, '', 'parameters') end if @config[:stackinifile] and parameters.size > 0 dir = File.dirname(@config[:stackinifile]) file = File.basename(@config[:stackinifile]) sect = dir == '.' ? Dir.pwd : dir sect = File.basename(sect) unless @config[:expandedpaths] save_inifile(dir, file, sect, parameters, 'parameters') end @logger.info ' Compiled file written.' rescue abort! "!!! Could not write compiled file: #{$!}" 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 template' # 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 |