Class: Cfhighlander::Dsl::HighlanderTemplate

Inherits:
DslBase
  • Object
show all
Defined in:
lib/cfhighlander.dsl.template.rb

Instance Attribute Summary collapse

Attributes inherited from DslBase

#config

Instance Method Summary collapse

Methods inherited from DslBase

#AWSAccountId, #AWSNoValue, #AWSNotificationARNs, #AWSPartition, #AWSStackName, #AWSStackRegion, #AWSURLSuffix, #FindInMap, #FnAnd, #FnBase64, #FnCidr, #FnEquals, #FnFindInMap, #FnGetAZs, #FnGetAtt, #FnIf, #FnImportValue, #FnJoin, #FnNot, #FnOr, #FnSelect, #FnSplit, #FnSub, #GetAtt, #Ref, #cfmap, #cfout, #method_missing

Constructor Details

#initializeHighlanderTemplate

Returns a new instance of HighlanderTemplate.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/cfhighlander.dsl.template.rb', line 48

def initialize
  @mappings = []
  @subcomponents = []
  @config = { 'mappings' => {}, 'component_version' => 'latest' }
  @component_configs = {}
  @version = 'latest'
  @distribute_url = nil
  @distribution_prefix = ''
  @component_sources = []
  @parameters = Parameters.new
  @lambda_functions_keys = []
  @dependson_components_templates = []
  @dependson_components = []
  @conditions = []
  @config_overrides = {}
  @extended_template = nil
  # execution blocks for subcomponents
  @subcomponents_exec = {}
  @template_dir = nil
  @publish_artifacts = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cfhighlander::Dsl::DslBase

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



43
44
45
# File 'lib/cfhighlander.dsl.template.rb', line 43

def conditions
  @conditions
end

#config_overridesObject (readonly)

Returns the value of attribute config_overrides.



43
44
45
# File 'lib/cfhighlander.dsl.template.rb', line 43

def config_overrides
  @config_overrides
end

#dependson_componentsObject

Returns the value of attribute dependson_components.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def dependson_components
  @dependson_components
end

#descriptionObject

Returns the value of attribute description.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def description
  @description
end

#distribute_urlObject

Returns the value of attribute distribute_url.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def distribute_url
  @distribute_url
end

#distribution_bucketObject

Returns the value of attribute distribution_bucket.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def distribution_bucket
  @distribution_bucket
end

#distribution_prefixObject

Returns the value of attribute distribution_prefix.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def distribution_prefix
  @distribution_prefix
end

#extended_templateObject (readonly)

Returns the value of attribute extended_template.



43
44
45
# File 'lib/cfhighlander.dsl.template.rb', line 43

def extended_template
  @extended_template
end

#lambda_functions_keysObject

Returns the value of attribute lambda_functions_keys.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def lambda_functions_keys
  @lambda_functions_keys
end

#mappingsObject

Returns the value of attribute mappings.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def mappings
  @mappings
end

#nameObject

Returns the value of attribute name.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def name
  @name
end

#parametersObject

Returns the value of attribute parameters.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def parameters
  @parameters
end

#publish_artifactsObject

Returns the value of attribute publish_artifacts.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def publish_artifacts
  @publish_artifacts
end

#subcomponentsObject (readonly)

Returns the value of attribute subcomponents.



43
44
45
# File 'lib/cfhighlander.dsl.template.rb', line 43

def subcomponents
  @subcomponents
end

#template_dirObject

Returns the value of attribute template_dir.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def template_dir
  @template_dir
end

#versionObject

Returns the value of attribute version.



30
31
32
# File 'lib/cfhighlander.dsl.template.rb', line 30

def version
  @version
end

Instance Method Details

#addMapping(name, map) ⇒ Object

DSL statements



72
73
74
75
76
# File 'lib/cfhighlander.dsl.template.rb', line 72

def addMapping(name, map)
  @mappings << name
  @config['mappings'] = {} unless @config.key?('mappings')
  @config['mappings'][name] = map
end

#apply_config_exportsObject



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/cfhighlander.dsl.template.rb', line 344

def apply_config_exports
  # first export from master to all children
  if ((@config.key? 'config_export') and (@config['config_export']['global']))
    @config['config_export']['global'].each { |global_export_key|
      if @config.key? global_export_key
        @config_overrides.each { |cname, co|
          co[global_export_key] = @config[global_export_key]
        }
      end
    }
  end

  @subcomponents.each { |component|
    cl = component.component_loaded
    if ((not cl.config.nil?) and (cl.config.key? 'config_export'))

      # global config
      if cl.config['config_export'].key? 'global'
        cl.config['config_export']['global'].each { |global_export_key|

          # global config is exported to parent and every component
          if cl.config.key? global_export_key

            # cname is for component name, co for component override
            @config_overrides.each { |cname, co|

              # if templates are different e.g don't export from vpc to vpc
              config_receiver_component = @named_components[cname]
              if config_receiver_component.template != component.template
                if (not config_receiver_component.export_config.nil?) and (config_receiver_component.export_config.key? component.template)
                  allow_from_component_name = config_receiver_component.export_config[component.template]
                  if allow_from_component_name == component.name
                    puts("INFO: Exporting key #{global_export_key} from component #{component.name} to #{cname}")
                    co[global_export_key] = cl.config[global_export_key]
                  end
                else
                  puts("INFO: Exporting key #{global_export_key} from component #{component.name} to #{cname}")
                  co[global_export_key] = cl.config[global_export_key]
                end
              end
            }


          else
            STDERR.puts("Trying to export non-existent configuration key #{global_export_key}")
          end
        }
      end

      if cl.config['config_export'].key? 'component'
        cl.config['config_export']['component'].each { |component_name, export_keys|
          # check if there is configuration of export from this component
          # and if there is export configuration for given component name

          if (not component.export_config.nil?) and (component.export_config.key? component_name)
            # if there is component with such name
            if @config_overrides.key? component.export_config[component_name]
              # override the config
              real_component_name = component.export_config[component_name]
              export_keys.each { |export_component_key|
                puts("Exporting config for key=#{export_component_key} from #{component.name} to #{real_component_name}")
                if not @config_overrides[real_component_name].key? export_component_key
                  @config_overrides[real_component_name][export_component_key] = {}
                end
                @config_overrides[real_component_name][export_component_key].extend(cl.config[export_component_key])
              }
            else
              STDERR.puts("Trying to export configuration for non-existant component #{component.export_config[component_name]}")
            end
          elsif @config_overrides.key? component_name
            export_keys.each { |export_component_key|
              puts("Exporting config for key=#{export_component_key} from #{component.name} to #{component_name}")
              if not @config_overrides[component_name].key? export_component_key
                @config_overrides[component_name][export_component_key] = {}
              end
              @config_overrides[component_name][export_component_key].extend(cl.config[export_component_key])
            }
          else
            STDERR.puts("Trying to export configuration for non-existant component #{component_name}")
          end
        }
      end
      # component config
      # loop over keys
      # check if there
    end
  }
end

#apply_config_overridesObject



324
325
326
327
328
# File 'lib/cfhighlander.dsl.template.rb', line 324

def apply_config_overrides
  @config_overrides.each { |component_name, component_override|
    @named_components[component_name].component_loaded.config.extend(component_override)
  }
end

#build_distribution_urlObject

def name=(value)

self.Name(value)

end



454
455
456
457
458
459
460
461
462
463
464
465
# File 'lib/cfhighlander.dsl.template.rb', line 454

def build_distribution_url
  if not (@distribution_bucket.nil? or @distribution_prefix.nil?)
    @distribute_url = "https://#{@distribution_bucket}.s3.amazonaws.com/#{@distribution_prefix}"
    @distribute_url = "#{@distribute_url}/#{@version}" unless @version.nil?
    @subcomponents.each { |component|
      component.distribute_bucket = @distribution_bucket unless @distribution_bucket.nil?
      component.distribute_prefix = @distribution_prefix unless @distribution_prefix.nil?
      component.version = @version unless @version.nil?
      component.build_distribution_url
    }
  end
end

#Component(template_arg = nil, template: nil, name: template, param_values: {}, config: {}, export_config: {}, conditional: false, condition: nil, enabled: true, dependson: [], render: Cfhighlander::Model::Component::Substack, &block) ⇒ Object



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/cfhighlander.dsl.template.rb', line 123

def Component(template_arg = nil, template: nil,
    name: template,
    param_values: {},
    config: {},
    export_config: {},
    conditional: false,
    condition: nil,
    enabled: true,
    dependson: [],
    render: Cfhighlander::Model::Component::Substack,
    &block)
  puts "INFO: Requested subcomponent #{name} with template #{template}"
  if ((not template_arg.nil?) and template.nil?)
    template = template_arg
  elsif (template_arg.nil? and template.nil?)
    raise 'Component must be defined with template name at minimum,' +
        ' passed as first argument, or template: named arguent'
  end

  name = template if name.nil?

  # load component
  component = Cfhighlander::Dsl::Subcomponent.new(self,
      name,
      template,
      param_values,
      @component_sources,
      config,
      export_config,
      conditional,
      condition,
      enabled,
      dependson,
      render == Cfhighlander::Model::Component::Inline
  )
  # component.instance_eval(&block) unless block.nil?
  @subcomponents_exec[name] = block unless block.nil?
  component.distribute_bucket = @distribution_bucket unless @distribution_bucket.nil?
  component.distribute_prefix = @distribution_prefix unless @distribution_prefix.nil?
  component.version = @version unless @version.nil?
  @component_configs[name] = config
  @subcomponents << component
end

#ComponentDistribution(s3_url) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/cfhighlander.dsl.template.rb', line 183

def ComponentDistribution(s3_url)
  if s3_url.start_with? 's3://'
    if s3_url.split('/').length < 4
      raise 'Unrecognised distribution url, only supporting s3://bucket/prefix urls'
    end
    parts = s3_url.split('/')
    @distribution_bucket = parts[2]
    @distribution_prefix = parts[3]
    i = 4
    while i < parts.size()
      @distribution_prefix += "/#{parts[i]}"
      i += 1
    end
    @distribution_prefix = @distribution_prefix.chomp('/')

    build_distribution_url
  else
    raise 'Unrecognised distribution url, only supporting s3://bucket/prefix urls'
  end
end

#ComponentSources(sources_array) ⇒ Object



204
205
206
# File 'lib/cfhighlander.dsl.template.rb', line 204

def ComponentSources(sources_array)
  @component_sources = sources_array
end

#ComponentVersion(version) ⇒ Object



167
168
169
170
171
# File 'lib/cfhighlander.dsl.template.rb', line 167

def ComponentVersion(version)
  @version = version
  @config['component_version'] = version
  build_distribution_url
end

#Condition(name, expression) ⇒ Object

def ComponentParam(argc*, argv)

@parameters.ComponentParam(argc, argv)

end



106
107
108
# File 'lib/cfhighlander.dsl.template.rb', line 106

def Condition(name, expression)
  @conditions << Condition.new(name, expression)
end

#DependsOn(template) ⇒ Object



115
116
117
# File 'lib/cfhighlander.dsl.template.rb', line 115

def DependsOn(template)
  @dependson_components_templates << template
end

#Description(description) ⇒ Object



87
88
89
90
# File 'lib/cfhighlander.dsl.template.rb', line 87

def Description(description)
  @description = description
  @config['description'] = description
end

#distribute_bucket=(value) ⇒ Object



440
441
442
443
# File 'lib/cfhighlander.dsl.template.rb', line 440

def distribute_bucket=(value)
  @distribution_bucket = value
  build_distribution_url
end

#distribute_prefix=(value) ⇒ Object



445
446
447
448
# File 'lib/cfhighlander.dsl.template.rb', line 445

def distribute_prefix=(value)
  @distribution_prefix = value
  build_distribution_url
end

#DistributionBucket(bucket_name) ⇒ Object



178
179
180
181
# File 'lib/cfhighlander.dsl.template.rb', line 178

def DistributionBucket(bucket_name)
  @distribution_bucket = bucket_name
  build_distribution_url
end

#DistributionPrefix(prefix) ⇒ Object



173
174
175
176
# File 'lib/cfhighlander.dsl.template.rb', line 173

def DistributionPrefix(prefix)
  @distribution_prefix = prefix
  build_distribution_url
end

#DynamicMappings(providerName) ⇒ Object



110
111
112
113
# File 'lib/cfhighlander.dsl.template.rb', line 110

def DynamicMappings(providerName)
  maps = mappings_provider_maps(providerName, self.config)
  maps.each { |name, map| addMapping(name, map) } unless maps.nil?
end

#Extends(template) ⇒ Object



119
120
121
# File 'lib/cfhighlander.dsl.template.rb', line 119

def Extends(template)
  @extended_template = template
end

#LambdaFunctions(config_key) ⇒ Object



208
209
210
# File 'lib/cfhighlander.dsl.template.rb', line 208

def LambdaFunctions(config_key)
  @lambda_functions_keys << config_key
end

#load_configfile_component_configObject



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/cfhighlander.dsl.template.rb', line 330

def load_configfile_component_config
  if (@config.key? 'components')
    @config['components'].each { |component_name, component_config|
      if component_config.key?('config')
        if @config_overrides.key? component_name
          @config_overrides[component_name].extend(component_config['config'])
        else
          STDERR.puts("WARN: Overriding config for non-existing component #{component_name}")
        end
      end
    }
  end
end

#load_explicit_component_configObject



433
434
435
436
437
438
# File 'lib/cfhighlander.dsl.template.rb', line 433

def load_explicit_component_config
  @component_configs.each { |component_name, component_config|
    @config_overrides[component_name].extend(component_config)
  }

end

#load_extension_exportsObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/cfhighlander.dsl.template.rb', line 304

def load_extension_exports
  @subcomponents.each do |c|
    component = c.component_loaded
    config = component.config
    if ((config.key? 'lib_export') and (config['lib_export'].key? 'global'))

      global_export_config = config['lib_export']['global']
      if global_export_config.key? 'cfndsl'
        global_export_config['cfndsl'].each do |exported_extension|
          extension_file_path = "#{component.component_dir}/ext/cfndsl/#{exported_extension}.rb"
          @subcomponents.each do |cr|
            cr.component_loaded.cfndsl_ext_files << extension_file_path unless cr == c
          end
        end
      end

    end
  end
end

#loadComponentsObject

Internal and interface functions



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/cfhighlander.dsl.template.rb', line 219

def loadComponents

  # empty config overrides to start with
  @config_overrides = Hash[@subcomponents.collect { |c| [c.name, { 'nested_component' => true }] }]
  @named_components = Hash[@subcomponents.collect { |c| [c.name, c] }]

  # populate overrides with master config defined overrides
  load_configfile_component_config

  # populate overrides with config defined explictily
  load_explicit_component_config

  # apply configuration exports
  apply_config_overrides
  apply_config_exports


  # component exports may have overriden some of explicit / configfile configuration, reapply
  load_configfile_component_config
  load_explicit_component_config

  # apply extension exports
  load_extension_exports


  # load components and extract parent stack parameters and mappings
  @subcomponents.each do |component|

    component.load @config_overrides[component.name]

    # add all of it's stack parameters unless same template has been already processed
    component
        .component_loaded
        .highlander_dsl
        .parameters
        .param_list.each do |param|

      # for map parameters add maps
      if param.class == Cfhighlander::Dsl::MappingParam
        if not param.mapProvider.nil?
          maps = param.mapProvider.getMaps(component.component_loaded.config)
          maps.each do |name, map|
            if not @mappings.include? name
              #1. add mapping name to model
              @mappings << name
              #2. add mapping to config to be rendered via cfndsl
              @config['mappings'] = {} if @config['mappings'].nil?
              @config['mappings'][name] = map
            end
          end unless maps.nil?
        end
      end

    end
    if @subcomponents_exec.key? component.name
      component.instance_eval &@subcomponents_exec[component.name]
    end
    component.component_loaded.eval_cfndsl
  end

  # in 2nd pass, load parameters
  # 2nd pass is required as all of cfndsl models need to be populated
  available_outputs = {}
  @subcomponents.each do |c|
    c.component_loaded.outputs.each do |out|
      available_outputs[out.name] = out
    end
  end

  @subcomponents.each do |component|
    component.resolve_parameter_values available_outputs
  end

  @dependson_components_templates.each do |template|
    component = Cfhighlander::Dsl::Subcomponent.new(self,
        template,
        template,
        {},
        @component_sources
    )
    component.load
    @dependson_components << component
  end
end

#Name(name) ⇒ Object



78
79
80
81
82
83
84
85
# File 'lib/cfhighlander.dsl.template.rb', line 78

def Name(name)
  # nested components always have their name dictated by parent
  # component, defaulting to template name
  if (not @config.key? 'nested_component')
    @name = name
    @config['component_name'] = name
  end
end

#Parameters(&block) ⇒ Object



97
98
99
100
# File 'lib/cfhighlander.dsl.template.rb', line 97

def Parameters(&block)
  @parameters.config = @config
  @parameters.instance_eval(&block) unless block.nil?
end

#PublishArtifact(file: nil, key: key=nil) ⇒ Object



212
213
214
215
# File 'lib/cfhighlander.dsl.template.rb', line 212

def PublishArtifact(file: nil, key: key=nil)
  puts "INFO: adding artifact #{file} to publishing list"
  @publish_artifacts << {file: file, key: key}
end