Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow configuration file named per component #31

Merged
merged 2 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source 'http://rubygems.org'
source 'https://rubygems.org'


gem 'cfndsl', '~> 0.16'
Expand All @@ -12,4 +12,4 @@ gem 'rubyzip', '>= 1.2.1'
gem 'git', '~> 1.4', '<2'
gem 'netaddr', '~> 1.5', '>= 1.5.1'
gem 'rspec'
gem 'octokit', '~> 4.0'
gem 'octokit', '~> 4.0'
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
GEM
remote: http://rubygems.org/
remote: https://rubygems.org/
specs:
addressable (2.5.2)
public_suffix (>= 2.0.2, < 4.0)
Expand Down Expand Up @@ -71,4 +71,4 @@ DEPENDENCIES
thor

BUNDLED WITH
1.16.1
1.16.2
12 changes: 9 additions & 3 deletions lib/cfhighlander.compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class ComponentCompiler
:cfn_output_location,
:cfn_template_paths,
:silent_mode,
:lambda_src_paths
:lambda_src_paths,
:process_lambdas

def initialize(component)

Expand All @@ -45,6 +46,7 @@ def initialize(component)
@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"
Expand All @@ -58,6 +60,11 @@ def initialize(component)
end
end

def process_lambdas=(value)
@process_lambdas = value
@sub_components.each { |scc| scc.process_lambdas=value }
end

def silent_mode=(value)
@silent_mode = value
@sub_components.each { |scc| scc.silent_mode=value }
Expand Down Expand Up @@ -177,14 +184,13 @@ def writeConfig(write_subcomponents_config = false)
end

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
@lambda_src_paths += resolver.generateSourceArchives if @process_lambdas
resolver.mergeComponentConfig
end

Expand Down
48 changes: 31 additions & 17 deletions lib/cfhighlander.dsl.template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

extensions_folder = "#{File.dirname(__FILE__)}/../hl_ext"

Dir["#{extensions_folder}/*.rb"].each {|f|
Dir["#{extensions_folder}/*.rb"].each { |f|
require f
}

Expand Down Expand Up @@ -37,7 +37,9 @@ class HighlanderTemplate < DslBase
:description,
:dependson_components

attr_reader :conditions, :subcomponents
attr_reader :conditions,
:subcomponents,
:config_overrides

def initialize
@mappings = []
Expand All @@ -53,6 +55,7 @@ def initialize
@dependson_components_templates = []
@dependson_components = []
@conditions = []
@config_overrides = {}
end

# DSL statements
Expand Down Expand Up @@ -88,7 +91,7 @@ def Condition(name, expression)

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

def DependsOn(template)
Expand Down Expand Up @@ -183,8 +186,8 @@ def LambdaFunctions(config_key)
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]}]
@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
Expand Down Expand Up @@ -283,14 +286,14 @@ def load_extension_exports
end

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

def load_configfile_component_config
if (@config.key? 'components')
@config['components'].each {|component_name, component_config|
@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'])
Expand All @@ -305,28 +308,28 @@ def load_configfile_component_config
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|
@config['config_export']['global'].each { |global_export_key|
if @config.key? global_export_key
@config_overrides.each {|cname, co|
@config_overrides.each { |cname, co|
co[global_export_key] = @config[global_export_key]
}
end
}
end

@subcomponents.each {|component|
@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|
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|
@config_overrides.each { |cname, co|

# if templates are different e.g don't export from vpc to vpc
config_receiver_component = @named_components[cname]
Expand All @@ -352,7 +355,7 @@ def apply_config_exports
end

if cl.config['config_export'].key? 'component'
cl.config['config_export']['component'].each {|component_name, export_keys|
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

Expand All @@ -361,7 +364,7 @@ def apply_config_exports
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|
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] = {}
Expand All @@ -372,7 +375,7 @@ def apply_config_exports
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|
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] = {}
Expand All @@ -392,9 +395,10 @@ def apply_config_exports
end

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

end

def distribute_bucket=(value)
Expand All @@ -415,7 +419,7 @@ 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|
@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?
Expand Down Expand Up @@ -458,6 +462,16 @@ def CfhighlanderTemplate(&block)
instance.name = @template.template_name
instance.instance_eval(&block)

# process convention over configuration componentname.config.yaml files
@potential_subcomponent_overrides.each do |name, config|
if (not instance.subcomponents.find{|s|s.name == name}.nil?)
instance.config['components'] = {} unless instance.config.key? 'components'
instance.config['components'][name] = {} unless instance.config['components'].key? name
instance.config['components'][name]['config'] = {} unless instance.config['components'][name].key? 'config'
instance.config['components'][name]['config'].extend config
end
end

# load sub-components
instance.loadComponents

Expand Down
13 changes: 11 additions & 2 deletions lib/cfhighlander.model.component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class Component
:cfndsl_ext_files,
:lambda_src_files

attr_reader :cfn_model, :outputs
attr_reader :cfn_model,
:outputs,
:potential_subcomponent_overrides

def initialize(template_meta, component_name)
@template = template_meta
Expand All @@ -35,6 +37,7 @@ def initialize(template_meta, component_name)
@component_files = []
@cfndsl_ext_files = []
@lambda_src_files = []
@potential_subcomponent_overrides = {}
end

# load component configuration files
Expand All @@ -43,10 +46,13 @@ def load_config()
Dir["#{@component_dir}/*.config.yaml"].each do |config_file|
puts "INFO Loading config for #{@name}: read file:#{config_file} "
partial_config = YAML.load(File.read(config_file))
unless partial_config.nil?
unless (partial_config.nil? or partial_config.key? 'subcomponent_config_file')
@config.extend(partial_config)
@component_files << config_file
end
fname = File.basename(config_file)
potential_component_name = fname.gsub('.config.yaml','')
@potential_subcomponent_overrides[potential_component_name] = partial_config
end
end

Expand Down Expand Up @@ -156,6 +162,9 @@ def load(config_override = nil)
# evaluates cfndsl with current config
def eval_cfndsl
compiler = Cfhighlander::Compiler::ComponentCompiler.new self
# there is no need for processing lambda source code during cloudformation evaluation,
# this version never gets published
compiler.process_lambdas = false
@cfn_model = compiler.evaluateCloudFormation().as_json
@outputs = (
if @cfn_model.key? 'Outputs'
Expand Down