Class: Cfhighlander::Compiler::ComponentCompiler
- Inherits:
-
Object
- Object
- Cfhighlander::Compiler::ComponentCompiler
- Defined in:
- lib/cfhighlander.compiler.rb
Constant Summary collapse
- @@global_extensions_paths =
[]
Instance Attribute Summary collapse
-
#cfn_output_location ⇒ Object
Returns the value of attribute cfn_output_location.
-
#cfn_template_paths ⇒ Object
Returns the value of attribute cfn_template_paths.
-
#compiled_subcomponents ⇒ Object
Returns the value of attribute compiled_subcomponents.
-
#component ⇒ Object
Returns the value of attribute component.
-
#component_name ⇒ Object
Returns the value of attribute component_name.
-
#config_output_location ⇒ Object
Returns the value of attribute config_output_location.
-
#dsl_output_location ⇒ Object
Returns the value of attribute dsl_output_location.
-
#lambda_src_paths ⇒ Object
Returns the value of attribute lambda_src_paths.
-
#process_lambdas ⇒ Object
Returns the value of attribute process_lambdas.
-
#silent_mode ⇒ Object
Returns the value of attribute silent_mode.
-
#workdir ⇒ Object
Returns the value of attribute workdir.
Instance Method Summary collapse
- #clear_out_dir ⇒ Object
- #compileCfnDsl(out_format) ⇒ Object
- #compileCloudFormation(format = 'yaml') ⇒ Object
- #evaluateCloudFormation(format = 'yaml') ⇒ Object
-
#initialize(component) ⇒ ComponentCompiler
constructor
A new instance of ComponentCompiler.
- #processLambdas ⇒ Object
- #writeConfig(write_subcomponents_config = false) ⇒ Object
Constructor Details
#initialize(component) ⇒ ComponentCompiler
Returns a new instance of ComponentCompiler.
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 |
# File 'lib/cfhighlander.compiler.rb', line 36 def initialize(component) @workdir = ENV['CFHIGHLANDER_WORKDIR'] @component = component @sub_components = [] @component_name = component.highlander_dsl.name.downcase @cfndsl_compiled = false @config_compiled = false @cfn_template_paths = [] @lambdas_processed = false @silent_mode = false @lambda_src_paths = [] @config_yaml_path = nil @cfn_model = nil @process_lambdas = true if @@global_extensions_paths.empty? global_extensions_folder = "#{File.dirname(__FILE__)}/../cfndsl_ext" Dir["#{global_extensions_folder}/*.rb"].each { |f| @@global_extensions_paths << f } end @component.highlander_dsl.subcomponents.each do |sub_component| sub_component_compiler = Cfhighlander::Compiler::ComponentCompiler.new(sub_component.component_loaded) sub_component_compiler.component_name = sub_component.name @sub_components << sub_component_compiler end end |
Instance Attribute Details
#cfn_output_location ⇒ Object
Returns the value of attribute cfn_output_location.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def cfn_output_location @cfn_output_location end |
#cfn_template_paths ⇒ Object
Returns the value of attribute cfn_template_paths.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def cfn_template_paths @cfn_template_paths end |
#compiled_subcomponents ⇒ Object
Returns the value of attribute compiled_subcomponents.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def compiled_subcomponents @compiled_subcomponents end |
#component ⇒ Object
Returns the value of attribute component.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def component @component end |
#component_name ⇒ Object
Returns the value of attribute component_name.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def component_name @component_name end |
#config_output_location ⇒ Object
Returns the value of attribute config_output_location.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def config_output_location @config_output_location end |
#dsl_output_location ⇒ Object
Returns the value of attribute dsl_output_location.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def dsl_output_location @dsl_output_location end |
#lambda_src_paths ⇒ Object
Returns the value of attribute lambda_src_paths.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def lambda_src_paths @lambda_src_paths end |
#process_lambdas ⇒ Object
Returns the value of attribute process_lambdas.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def process_lambdas @process_lambdas end |
#silent_mode ⇒ Object
Returns the value of attribute silent_mode.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def silent_mode @silent_mode end |
#workdir ⇒ Object
Returns the value of attribute workdir.
24 25 26 |
# File 'lib/cfhighlander.compiler.rb', line 24 def workdir @workdir end |
Instance Method Details
#clear_out_dir ⇒ Object
64 65 66 67 |
# File 'lib/cfhighlander.compiler.rb', line 64 def clear_out_dir # Clear previous packages FileUtils.rmtree "#{@workdir}/out/" end |
#compileCfnDsl(out_format) ⇒ Object
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 115 116 117 |
# File 'lib/cfhighlander.compiler.rb', line 79 def compileCfnDsl(out_format) processLambdas unless @lambdas_processed writeConfig unless @config_written dsl = @component.highlander_dsl component_cfndsl = @component.cfndsl_content @component.highlander_dsl.subcomponents.each { |sc| sc.distribution_format = out_format } # indent component cfndsl component_cfndsl.gsub!("\n", "\n\t") component_cfndsl.gsub!("\r\n", "\r\n\t") # render cfndsl renderer = ERB.new(File.read("#{__dir__}/../templates/cfndsl.component.template.erb"), nil, '-') cfn_template = renderer.result(OpenStruct.new({ 'dsl' => dsl, 'component_cfndsl' => component_cfndsl, 'component_requires' => (@@global_extensions_paths + @component.cfndsl_ext_files), 'distribution_format' => out_format }).instance_eval { binding }) # write to output file output_dir = "#{@workdir}/out/cfndsl" @dsl_output_location = output_dir output_path = "#{output_dir}/#{@component_name}.compiled.cfndsl.rb" FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir) File.write(output_path, cfn_template) puts "cfndsl template for #{dsl.name} written to #{output_path}" @cfndsl_compiled_path = output_path @sub_components.each { |subcomponent_compiler| puts "Rendering sub-component cfndsl: #{subcomponent_compiler.component_name}" subcomponent_compiler.compileCfnDsl out_format } @cfndsl_compiled = true end |
#compileCloudFormation(format = 'yaml') ⇒ Object
133 134 135 136 137 138 139 140 141 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 177 178 179 180 181 182 |
# File 'lib/cfhighlander.compiler.rb', line 133 def compileCloudFormation(format = 'yaml') dsl = @component.highlander_dsl # create out dir if not there @cfn_output_location = "#{@workdir}/out/#{format}" if @cfn_output_location.nil? output_dir = @cfn_output_location FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir) # compile templates output_path = "#{output_dir}/#{@component_name}.compiled.#{format}" @cfn_template_paths << output_path # configure cfndsl # grab cfndsl model # 1st pass of cloudformation compiling - does the substacks normally model = evaluateCloudFormation @component.set_cfndsl_model model # compile sub-component templates @sub_components.each do |sub_component| sub_component.compileCloudFormation format @cfn_template_paths += sub_component.cfn_template_paths @lambda_src_paths += sub_component.lambda_src_paths end # 2nd pass will flatten any inlined components model = Cfhighlander::Util::CloudFormation.flattenCloudformation( component: @component ) # write resulting cloud formation template if format == 'json' output_content = JSON.pretty_generate(model) elsif format == 'yaml' output_content = JSON.parse(model.to_json).to_yaml else raise StandardError, "#{format} not supported for cfn generation" end File.write(output_path, output_content) # `cfndsl #{@cfndsl_compiled_path} -p -f #{format} -o #{output_path} --disable-binding` puts "CloudFormation #{format.upcase} template for #{dsl.name} written to #{output_path}" return JSON.parse(model.to_json) end |
#evaluateCloudFormation(format = 'yaml') ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/cfhighlander.compiler.rb', line 119 def evaluateCloudFormation(format = 'yaml') #compile cfndsl templates first compileCfnDsl format unless @cfndsl_compiled # write config cfndsl_opts = [] cfndsl_opts.push([:yaml, @config_yaml_path]) # grab cfndsl model model = CfnDsl.eval_file_with_extras(@cfndsl_compiled_path, cfndsl_opts, false) @cfn_model = model return model end |
#processLambdas ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/cfhighlander.compiler.rb', line 203 def processLambdas() @component.highlander_dsl.lambda_functions_keys.each do |lfk| resolver = LambdaResolver.new(@component, lfk, @workdir, (not @silent_mode) ) @lambda_src_paths += resolver.generateSourceArchives if @process_lambdas resolver.mergeComponentConfig end @lambdas_processed = true end |
#writeConfig(write_subcomponents_config = false) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/cfhighlander.compiler.rb', line 184 def writeConfig(write_subcomponents_config = false) @config_output_location = "#{@workdir}/out/config" config_yaml_path = "#{@config_output_location}/#{@component_name}.config.yaml" FileUtils.mkdir_p(@config_output_location) unless Dir.exist?(@config_output_location) File.write(config_yaml_path, @component.config.to_yaml) puts "Config for #{@component.highlander_dsl.name} written to #{config_yaml_path}" if write_subcomponents_config # compile sub-component templates @sub_components.each do |sub_component| sub_component.writeConfig write_subcomponents_config end end @config_written = true @config_yaml_path = config_yaml_path return @config_yaml_path end |