Class: Cfhighlander::Compiler::ComponentCompiler

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

Constant Summary collapse

@@global_extensions_paths =
[]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component) ⇒ ComponentCompiler

Returns a new instance of ComponentCompiler.



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
# File 'lib/cfhighlander.compiler.rb', line 37

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
  @lambda_mock_resolve = false

  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_locationObject

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_pathsObject

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_subcomponentsObject

Returns the value of attribute compiled_subcomponents.



24
25
26
# File 'lib/cfhighlander.compiler.rb', line 24

def compiled_subcomponents
  @compiled_subcomponents
end

#componentObject

Returns the value of attribute component.



24
25
26
# File 'lib/cfhighlander.compiler.rb', line 24

def component
  @component
end

#component_nameObject

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_locationObject

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_locationObject

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_mock_resolveObject

Returns the value of attribute lambda_mock_resolve.



24
25
26
# File 'lib/cfhighlander.compiler.rb', line 24

def lambda_mock_resolve
  @lambda_mock_resolve
end

#lambda_src_pathsObject

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_lambdasObject

Returns the value of attribute process_lambdas.



24
25
26
# File 'lib/cfhighlander.compiler.rb', line 24

def process_lambdas
  @process_lambdas
end

#silent_modeObject

Returns the value of attribute silent_mode.



24
25
26
# File 'lib/cfhighlander.compiler.rb', line 24

def silent_mode
  @silent_mode
end

#workdirObject

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_dirObject



66
67
68
69
# File 'lib/cfhighlander.compiler.rb', line 66

def clear_out_dir
  # Clear previous packages
  FileUtils.rmtree "#{@workdir}/out/"
end

#compileCfnDsl(out_format) ⇒ Object



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
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/cfhighlander.compiler.rb', line 86

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
  }

  # figure out cfndsl version
  cfndsl_version = CfnDsl::VERSION
  legacy_cfndsl = cfndsl_version.to_f < 1

  # 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,
      'legacy_cfndsl' => legacy_cfndsl,
      'cfndsl_version' => cfndsl_version
  }).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



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
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cfhighlander.compiler.rb', line 146

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



132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/cfhighlander.compiler.rb', line 132

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

#processLambdasObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/cfhighlander.compiler.rb', line 218

def processLambdas()
  @component.highlander_dsl.lambda_functions_keys.each do |lfk|
    resolver = LambdaResolver.new(@component,
        lfk,
        @workdir,
        (not @silent_mode),
        @lambda_mock_resolve
    )
    @lambda_src_paths += resolver.generateSourceArchives if @process_lambdas
    resolver.mergeComponentConfig
  end

  @lambdas_processed = true

end

#writeConfig(write_subcomponents_config = false) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/cfhighlander.compiler.rb', line 197

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)

  config_yaml = JSON.parse(@component.config.to_json).to_yaml
  File.write(config_yaml_path, config_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