cfhighlander 0.2.1 → 0.3.0.alpha.1528936037

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,26 +2,33 @@
2
2
 
3
3
  extensions_folder = "#{File.dirname(__FILE__)}/../hl_ext"
4
4
 
5
- Dir["#{extensions_folder}/*.rb"].each { |f|
5
+ Dir["#{extensions_folder}/*.rb"].each {|f|
6
6
  require f
7
7
  }
8
8
 
9
9
  # require libraries
10
10
 
11
- require_relative './highlander.dsl.base'
12
- require_relative './highlander.dsl.params'
13
- require_relative './highlander.dsl.component'
11
+ require_relative './cfhighlander.dsl.base'
12
+ require_relative './cfhighlander.dsl.params'
13
+ require_relative './cfhighlander.dsl.subcomponent'
14
14
 
15
- module Highlander
15
+ module Cfhighlander
16
16
 
17
17
  module Dsl
18
+ class Condition < DslBase
18
19
 
19
- class Template < DslBase
20
+ attr_reader :name, :expression
21
+
22
+ def initialize(name, expression)
23
+ @name = name
24
+ @expression = expression
25
+ end
26
+ end
27
+ class HighlanderTemplate < DslBase
20
28
 
21
29
  attr_accessor :mappings,
22
30
  :parameters,
23
31
  :name,
24
- :components,
25
32
  :version,
26
33
  :distribute_url,
27
34
  :distribution_bucket,
@@ -30,9 +37,11 @@ module Highlander
30
37
  :description,
31
38
  :dependson_components
32
39
 
40
+ attr_reader :conditions, :subcomponents
41
+
33
42
  def initialize
34
43
  @mappings = []
35
- @components = []
44
+ @subcomponents = []
36
45
  @config = { 'mappings' => {}, 'component_version' => 'latest' }
37
46
  @component_configs = {}
38
47
  @version = 'latest'
@@ -43,6 +52,7 @@ module Highlander
43
52
  @lambda_functions_keys = []
44
53
  @dependson_components_templates = []
45
54
  @dependson_components = []
55
+ @conditions = []
46
56
  end
47
57
 
48
58
  # DSL statements
@@ -54,8 +64,12 @@ module Highlander
54
64
  end
55
65
 
56
66
  def Name(name)
57
- @name = name
58
- @config['component_name'] = name
67
+ # nested components always have their name dictated by parent
68
+ # component, defaulting to template name
69
+ if (not @config.key? 'nested_component')
70
+ @name = name
71
+ @config['component_name'] = name
72
+ end
59
73
  end
60
74
 
61
75
  def Description(description)
@@ -68,9 +82,13 @@ module Highlander
68
82
  @parameters.instance_eval(&block)
69
83
  end
70
84
 
85
+ def Condition(name, expression)
86
+ @conditions << Condition.new(name, expression)
87
+ end
88
+
71
89
  def DynamicMappings(providerName)
72
90
  maps = mappings_provider_maps(providerName, self.config)
73
- maps.each { |name, map| addMapping(name, map) } unless maps.nil?
91
+ maps.each {|name, map| addMapping(name, map)} unless maps.nil?
74
92
  end
75
93
 
76
94
  def DependsOn(template)
@@ -78,23 +96,41 @@ module Highlander
78
96
  end
79
97
 
80
98
 
81
- def Component(name:, template:, param_values: {}, config: {}, export_config: {}, &block)
82
- puts "Initialize #{name} with template #{template}"
99
+ def Component(template_arg = nil, template: nil,
100
+ name: template,
101
+ param_values: {},
102
+ config: {},
103
+ export_config: {},
104
+ conditional: false,
105
+ enabled: true,
106
+ &block)
107
+ puts "INFO: Requested subcomponent #{name} with template #{template}"
108
+ if ((not template_arg.nil?) and template.nil?)
109
+ template = template_arg
110
+ elsif (template_arg.nil? and template.nil?)
111
+ raise 'Component must be defined with template name at minimum,' +
112
+ ' passed as first argument, or template: named arguent'
113
+ end
114
+
115
+ name = template if name.nil?
83
116
 
84
117
  # load component
85
- component = Highlander::Dsl::Component.new(self,
118
+ component = Cfhighlander::Dsl::Subcomponent.new(self,
86
119
  name,
87
120
  template,
88
121
  param_values,
89
122
  @component_sources,
90
123
  config,
91
- export_config
124
+ export_config,
125
+ conditional,
126
+ enabled
92
127
  )
128
+ component.instance_eval(&block) unless block.nil?
93
129
  component.distribute_bucket = @distribution_bucket unless @distribution_bucket.nil?
94
130
  component.distribute_prefix = @distribution_prefix unless @distribution_prefix.nil?
95
131
  component.version = @version unless @version.nil?
96
132
  @component_configs[name] = config
97
- @components << component
133
+ @subcomponents << component
98
134
  end
99
135
 
100
136
  def ComponentVersion(version)
@@ -147,8 +183,8 @@ module Highlander
147
183
  def loadComponents()
148
184
 
149
185
  # empty config overrides to start with
150
- @config_overrides = Hash[@components.collect { |c| [c.name, {}] }]
151
- @named_components = Hash[@components.collect { |c| [c.name, c] }]
186
+ @config_overrides = Hash[@subcomponents.collect {|c| [c.name, { 'nested_component' => true }]}]
187
+ @named_components = Hash[@subcomponents.collect {|c| [c.name, c]}]
152
188
 
153
189
  # populate overrides with master config defined overrides
154
190
  load_configfile_component_config
@@ -170,7 +206,7 @@ module Highlander
170
206
 
171
207
 
172
208
  # load components and extract parent stack parameters and mappings
173
- @components.each { |component|
209
+ @subcomponents.each do |component|
174
210
 
175
211
  component.load @config_overrides[component.name]
176
212
  # add all of it's stack parameters unless same template has been already processed
@@ -180,21 +216,8 @@ module Highlander
180
216
  .parameters
181
217
  .param_list.each do |param|
182
218
 
183
- # add stack parameters
184
- if param.class == Highlander::Dsl::StackParam
185
- # sub-component stack param becomes top-level component param
186
- param_name = param.is_global ? param.name : "#{component.name}#{param.name}"
187
- stack_param = Highlander::Dsl::ComponentParam.new(
188
- param_name,
189
- param.type,
190
- param.default_value,
191
- param.no_echo
192
- )
193
- @parameters.addParam stack_param
194
- end unless component.param_values.key? param.name
195
-
196
219
  # for map parameters add maps
197
- if param.class == Highlander::Dsl::MappingParam
220
+ if param.class == Cfhighlander::Dsl::MappingParam
198
221
  if not param.mapProvider.nil?
199
222
  maps = param.mapProvider.getMaps(component.component_loaded.config)
200
223
  maps.each do |name, map|
@@ -211,12 +234,24 @@ module Highlander
211
234
 
212
235
  end
213
236
 
214
- # late bind parameter values, once mappings and top level params are extracted
215
- component.load_parameters
216
- }
237
+ component.component_loaded.eval_cfndsl()
238
+ end
239
+
240
+ # in 2nd pass, load parameters
241
+ # 2nd pass is required as all of cfndsl models need to be populated
242
+ available_outputs = {}
243
+ @subcomponents.each do |c|
244
+ c.component_loaded.outputs.each do |out|
245
+ available_outputs[out.name] = out
246
+ end
247
+ end
248
+
249
+ @subcomponents.each do |component|
250
+ component.resolve_parameter_values available_outputs
251
+ end
217
252
 
218
253
  @dependson_components_templates.each do |template|
219
- component = Highlander::Dsl::Component.new(self,
254
+ component = Cfhighlander::Dsl::Subcomponent.new(self,
220
255
  template,
221
256
  template,
222
257
  {},
@@ -228,7 +263,7 @@ module Highlander
228
263
  end
229
264
 
230
265
  def load_extension_exports
231
- @components.each do |c|
266
+ @subcomponents.each do |c|
232
267
  component = c.component_loaded
233
268
  config = component.config
234
269
  if ((config.key? 'lib_export') and (config['lib_export'].key? 'global'))
@@ -237,7 +272,7 @@ module Highlander
237
272
  if global_export_config.key? 'cfndsl'
238
273
  global_export_config['cfndsl'].each do |exported_extension|
239
274
  extension_file_path = "#{component.component_dir}/ext/cfndsl/#{exported_extension}.rb"
240
- @components.each do |cr|
275
+ @subcomponents.each do |cr|
241
276
  cr.component_loaded.cfndsl_ext_files << extension_file_path unless cr == c
242
277
  end
243
278
  end
@@ -248,16 +283,20 @@ module Highlander
248
283
  end
249
284
 
250
285
  def apply_config_overrides
251
- @config_overrides.each { |component_name, component_override|
286
+ @config_overrides.each {|component_name, component_override|
252
287
  @named_components[component_name].component_loaded.config.extend(component_override)
253
288
  }
254
289
  end
255
290
 
256
291
  def load_configfile_component_config
257
292
  if (@config.key? 'components')
258
- @config['components'].each { |component_name, component_config|
293
+ @config['components'].each {|component_name, component_config|
259
294
  if component_config.key?('config')
260
- @config_overrides[component_name].extend(component_config['config'])
295
+ if @config_overrides.key? component_name
296
+ @config_overrides[component_name].extend(component_config['config'])
297
+ else
298
+ STDERR.puts("WARN: Overriding config for non-existing component #{component_name}")
299
+ end
261
300
  end
262
301
  }
263
302
  end
@@ -266,28 +305,28 @@ module Highlander
266
305
  def apply_config_exports
267
306
  # first export from master to all children
268
307
  if ((@config.key? 'config_export') and (@config['config_export']['global']))
269
- @config['config_export']['global'].each { |global_export_key|
308
+ @config['config_export']['global'].each {|global_export_key|
270
309
  if @config.key? global_export_key
271
- @config_overrides.each { |cname, co|
310
+ @config_overrides.each {|cname, co|
272
311
  co[global_export_key] = @config[global_export_key]
273
312
  }
274
313
  end
275
314
  }
276
315
  end
277
316
 
278
- @components.each { |component|
317
+ @subcomponents.each {|component|
279
318
  cl = component.component_loaded
280
319
  if ((not cl.config.nil?) and (cl.config.key? 'config_export'))
281
320
 
282
321
  # global config
283
322
  if cl.config['config_export'].key? 'global'
284
- cl.config['config_export']['global'].each { |global_export_key|
323
+ cl.config['config_export']['global'].each {|global_export_key|
285
324
 
286
325
  # global config is exported to parent and every component
287
326
  if cl.config.key? global_export_key
288
327
 
289
328
  # cname is for component name, co for component override
290
- @config_overrides.each { |cname, co|
329
+ @config_overrides.each {|cname, co|
291
330
 
292
331
  # if templates are different e.g don't export from vpc to vpc
293
332
  config_receiver_component = @named_components[cname]
@@ -295,11 +334,11 @@ module Highlander
295
334
  if (not config_receiver_component.export_config.nil?) and (config_receiver_component.export_config.key? component.template)
296
335
  allow_from_component_name = config_receiver_component.export_config[component.template]
297
336
  if allow_from_component_name == component.name
298
- puts("Exporting key #{global_export_key} from component #{component.name} to #{cname}")
337
+ puts("INFO: Exporting key #{global_export_key} from component #{component.name} to #{cname}")
299
338
  co[global_export_key] = cl.config[global_export_key]
300
339
  end
301
340
  else
302
- puts("Exporting key #{global_export_key} from component #{component.name} to #{cname}")
341
+ puts("INFO: Exporting key #{global_export_key} from component #{component.name} to #{cname}")
303
342
  co[global_export_key] = cl.config[global_export_key]
304
343
  end
305
344
  end
@@ -313,7 +352,7 @@ module Highlander
313
352
  end
314
353
 
315
354
  if cl.config['config_export'].key? 'component'
316
- cl.config['config_export']['component'].each { |component_name, export_keys|
355
+ cl.config['config_export']['component'].each {|component_name, export_keys|
317
356
  # check if there is configuration of export from this component
318
357
  # and if there is export configuration for given component name
319
358
 
@@ -322,7 +361,7 @@ module Highlander
322
361
  if @config_overrides.key? component.export_config[component_name]
323
362
  # override the config
324
363
  real_component_name = component.export_config[component_name]
325
- export_keys.each { |export_component_key|
364
+ export_keys.each {|export_component_key|
326
365
  puts("Exporting config for key=#{export_component_key} from #{component.name} to #{real_component_name}")
327
366
  if not @config_overrides[real_component_name].key? export_component_key
328
367
  @config_overrides[real_component_name][export_component_key] = {}
@@ -333,7 +372,7 @@ module Highlander
333
372
  STDERR.puts("Trying to export configuration for non-existant component #{component.export_config[component_name]}")
334
373
  end
335
374
  elsif @config_overrides.key? component_name
336
- export_keys.each { |export_component_key|
375
+ export_keys.each {|export_component_key|
337
376
  puts("Exporting config for key=#{export_component_key} from #{component.name} to #{component_name}")
338
377
  if not @config_overrides[component_name].key? export_component_key
339
378
  @config_overrides[component_name][export_component_key] = {}
@@ -353,7 +392,7 @@ module Highlander
353
392
  end
354
393
 
355
394
  def load_explicit_component_config
356
- @component_configs.each { |component_name, component_config|
395
+ @component_configs.each {|component_name, component_config|
357
396
  @config_overrides[component_name].extend(component_config)
358
397
  }
359
398
  end
@@ -368,15 +407,15 @@ module Highlander
368
407
  build_distribution_url
369
408
  end
370
409
 
371
- def name=(value)
372
- self.Name(value)
373
- end
410
+ # def name=(value)
411
+ # self.Name(value)
412
+ # end
374
413
 
375
414
  def build_distribution_url
376
415
  if not (@distribution_bucket.nil? or @distribution_prefix.nil?)
377
416
  @distribute_url = "https://#{@distribution_bucket}.s3.amazonaws.com/#{@distribution_prefix}"
378
417
  @distribute_url = "#{@distribute_url}/#{@version}" unless @version.nil?
379
- @components.each { |component|
418
+ @subcomponents.each {|component|
380
419
  component.distribute_bucket = @distribution_bucket unless @distribution_bucket.nil?
381
420
  component.distribute_prefix = @distribution_prefix unless @distribution_prefix.nil?
382
421
  component.version = @version unless @version.nil?
@@ -391,13 +430,12 @@ module Highlander
391
430
 
392
431
  end
393
432
 
394
- def HighlanderComponent(&block)
395
- instance = Highlander::Dsl::Template.new
433
+ def CfhighlanderTemplate(&block)
434
+ instance = Cfhighlander::Dsl::HighlanderTemplate.new
396
435
 
397
436
  puts "Processing higlander component #{@name}\n\tLocation:#{@highlander_dsl_path}" +
398
437
  "\n\tConfig:#{@config}"
399
438
 
400
- component_config = @config
401
439
 
402
440
  instance.config = @config
403
441
 
@@ -407,6 +445,7 @@ def HighlanderComponent(&block)
407
445
 
408
446
  unless @version.nil?
409
447
  instance.version = @version
448
+ instance.config['component_version'] = @version
410
449
  end
411
450
 
412
451
  unless @distribution_bucket.nil?
@@ -416,13 +455,22 @@ def HighlanderComponent(&block)
416
455
  instance.DistributionPrefix(@distribution_prefix)
417
456
  end
418
457
 
458
+ instance.name = @template.template_name
419
459
  instance.instance_eval(&block)
420
- if instance.name.nil?
421
- instance.name = @name
422
- end
460
+
423
461
 
424
462
  # load sub-components
425
463
  instance.loadComponents
426
464
 
427
465
  return instance
428
466
  end
467
+
468
+ def CfhighlanderComponent(&block)
469
+ STDERR.puts("DEPRECATED: #{@template.template_name}: use CfhighlanderTemplate instead of CfhighlanderComponent")
470
+ CfhighlanderTemplate(&block)
471
+ end
472
+
473
+ def HighlanderComponent(&block)
474
+ STDERR.puts("DEPRECATED: #{@template.template_name}: use CfhighlanderTemplate instead of HighlanderComponent")
475
+ CfhighlanderTemplate(&block)
476
+ end
@@ -0,0 +1,45 @@
1
+ require_relative './cfhighlander.dsl.template'
2
+ require_relative './cfhighlander.factory.templatefinder'
3
+ require_relative './cfhighlander.model.component'
4
+ require 'fileutils'
5
+ require 'git'
6
+
7
+
8
+ module Cfhighlander
9
+
10
+ module Factory
11
+
12
+ class ComponentFactory
13
+
14
+ attr_accessor :component_sources
15
+
16
+ def initialize(component_sources = [])
17
+ @template_finder = Cfhighlander::Factory::TemplateFinder.new(component_sources)
18
+ @component_sources = component_sources
19
+ end
20
+
21
+ # Find component and given list of sources
22
+ # @return [Cfhighlander::Factory::Component]
23
+ def loadComponentFromTemplate(template_name, template_version = nil, component_name = nil)
24
+
25
+ template_meta = @template_finder.findTemplate(template_name, template_version)
26
+
27
+ raise StandardError, "highlander template #{template_name}@#{template_version} not located" +
28
+ " in sources #{@component_sources}" if template_meta.nil?
29
+
30
+ component_name = template_name if component_name.nil?
31
+ return buildComponentFromLocation(template_meta, component_name)
32
+
33
+ end
34
+
35
+
36
+ def buildComponentFromLocation(template_meta, component_name)
37
+ component = Model::Component.new(template_meta, component_name)
38
+ component.load_config
39
+ return component
40
+ end
41
+
42
+ end
43
+ end
44
+
45
+ end