cfhighlander 0.2.1.alpha.43 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/cfndsl_ext/sg.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'netaddr'
2
1
 
3
2
  def sg_create_rules (x, ip_blocks={})
4
3
  rules = []
@@ -16,38 +15,12 @@ end
16
15
 
17
16
 
18
17
  def lookup_ips_for_sg (ips, ip_block_name={})
19
- cidr = []
20
18
  if ip_block_name == 'stack'
21
19
  cidr = [FnJoin( "", [ "10.", Ref('StackOctet'), ".", "0.0/16" ] )]
22
20
  elsif ips.has_key? ip_block_name
23
- ips[ip_block_name].each do |ip|
24
- if (ips.include?(ip) || ip_block_name == 'stack')
25
- cidr += lookup_ips_for_sg(ips, ip) unless ip == ip_block_name
26
- else
27
- if ip == 'stack'
28
- cidr << [FnJoin( "", [ "10.", Ref('StackOctet'), ".", "0.0/16" ] )]
29
- elsif(isCidr(ip))
30
- cidr << ip
31
- else
32
- STDERR.puts("WARN: ip #{ip} is not a valid CIDR. Ignoring IP")
33
- end
34
- end
35
- end
21
+ cidr = ips[ip_block_name]
36
22
  else
37
- if isCidr(ip_block_name)
38
- cidr = [ip_block_name]
39
- else
40
- STDERR.puts("WARN: ip #{ip_block_name} is not a valid CIDR. Ignoring IP")
41
- end
23
+ cidr = [ip_block_name]
42
24
  end
43
25
  cidr
44
26
  end
45
-
46
- def isCidr(block)
47
- begin
48
- NetAddr::CIDR.create(block)
49
- return block.include?('/')
50
- rescue NetAddr::ValidationError
51
- return false
52
- end
53
- end
@@ -1,16 +1,14 @@
1
- require_relative '../lib/cfhighlander.mapproviders'
1
+ require_relative '../lib/highlander.mapproviders'
2
2
 
3
3
  # Return mapping provider as class
4
- def mappings_provider(provider_name, is_legacy = false)
4
+ def mappings_provider(provider_name)
5
5
  return nil if provider_name.nil?
6
6
  provider = nil
7
- module_name = is_legacy ? 'Highlander': 'Cfhighlander'
8
- providers = Object.const_get(module_name).const_get('MapProviders')
7
+ providers = Object.const_get('Highlander').const_get('MapProviders')
9
8
  begin
10
9
  providers.const_get(provider_name)
11
10
  rescue NameError => e
12
- if e.to_s.include? "uninitialized constant #{module_name}::MapProviders::"
13
- return mappings_provider(provider_name, true) unless is_legacy
11
+ if e.to_s.include? 'uninitialized constant Highlander::MapProviders::'
14
12
  return nil
15
13
  end
16
14
  STDERR.puts(e.to_s)
@@ -12,7 +12,7 @@ require 'highline/import'
12
12
  require 'zip'
13
13
  require_relative './util/zip.util'
14
14
 
15
- module Cfhighlander
15
+ module Highlander
16
16
 
17
17
  module Compiler
18
18
 
@@ -33,7 +33,7 @@ module Cfhighlander
33
33
 
34
34
  def initialize(component)
35
35
 
36
- @workdir = ENV['CFHIGHLANDER_WORKDIR']
36
+ @workdir = ENV['HIGHLANDER_WORKDIR']
37
37
  @component = component
38
38
  @sub_components = []
39
39
  @component_name = component.highlander_dsl.name.downcase
@@ -43,16 +43,14 @@ module Cfhighlander
43
43
  @lambdas_processed = false
44
44
  @silent_mode = false
45
45
  @lambda_src_paths = []
46
- @config_yaml_path = nil
47
- @cfn_model = nil
48
46
 
49
47
  if @@global_extensions_paths.empty?
50
48
  global_extensions_folder = "#{File.dirname(__FILE__)}/../cfndsl_ext"
51
49
  Dir["#{global_extensions_folder}/*.rb"].each { |f| @@global_extensions_paths << f }
52
50
  end
53
51
 
54
- @component.highlander_dsl.subcomponents.each do |sub_component|
55
- sub_component_compiler = Cfhighlander::Compiler::ComponentCompiler.new(sub_component.component_loaded)
52
+ @component.highlander_dsl.components.each do |sub_component|
53
+ sub_component_compiler = Highlander::Compiler::ComponentCompiler.new(sub_component.component_loaded)
56
54
  sub_component_compiler.component_name = sub_component.name
57
55
  @sub_components << sub_component_compiler
58
56
  end
@@ -69,7 +67,7 @@ module Cfhighlander
69
67
  dsl = @component.highlander_dsl
70
68
  component_cfndsl = @component.cfndsl_content
71
69
 
72
- @component.highlander_dsl.subcomponents.each { |sc|
70
+ @component.highlander_dsl.components.each { |sc|
73
71
  sc.distribution_format = out_format
74
72
  }
75
73
 
@@ -102,38 +100,33 @@ module Cfhighlander
102
100
 
103
101
  end
104
102
 
105
- def evaluateCloudFormation(format = 'yaml')
106
- #compile cfndsl templates first
107
- compileCfnDsl format unless @cfndsl_compiled
108
-
109
- # write config
110
- cfndsl_opts = []
111
- cfndsl_opts.push([:yaml, @config_yaml_path])
103
+ def compileCloudFormation(format = 'yaml')
112
104
 
113
- # grab cfndsl model
114
- model = CfnDsl.eval_file_with_extras(@cfndsl_compiled_path, cfndsl_opts, false)
115
- @cfn_model = model
116
- return model
117
- end
118
105
 
119
- def compileCloudFormation(format = 'yaml')
106
+ #compile cfndsl templates first
107
+ compileCfnDsl format unless @cfndsl_compiled
120
108
 
121
109
  dsl = @component.highlander_dsl
110
+ component_cfndsl = @component.cfndsl_content
122
111
 
123
112
  # create out dir if not there
124
113
  @cfn_output_location = "#{@workdir}/out/#{format}"
125
114
  output_dir = @cfn_output_location
126
115
  FileUtils.mkdir_p(output_dir) unless Dir.exist?(output_dir)
127
116
 
117
+ # write config
118
+ config_yaml_path = writeConfig
119
+
128
120
 
129
121
  # compile templates
130
- output_path = "#{output_dir}/#{@component_name}.compiled.#{format}"
122
+ output_path = "#{output_dir}/#{component_name}.compiled.#{format}"
131
123
  @cfn_template_paths << output_path
132
124
  # configure cfndsl
133
-
125
+ cfndsl_opts = []
126
+ cfndsl_opts.push([:yaml, config_yaml_path])
134
127
 
135
128
  # grab cfndsl model
136
- model = evaluateCloudFormation
129
+ model = CfnDsl.eval_file_with_extras(@cfndsl_compiled_path, cfndsl_opts, false)
137
130
 
138
131
  # write resulting cloud formation template
139
132
  if format == 'json'
@@ -172,8 +165,7 @@ module Cfhighlander
172
165
  end
173
166
  end
174
167
  @config_written = true
175
- @config_yaml_path = config_yaml_path
176
- return @config_yaml_path
168
+ config_yaml_path
177
169
  end
178
170
 
179
171
  def processLambdas()
@@ -289,7 +281,7 @@ module Cfhighlander
289
281
  end
290
282
  end
291
283
  File.delete full_destination_path if File.exist? full_destination_path
292
- zip_generator = Cfhighlander::Util::ZipFileGenerator.new(lambda_source_dir, full_destination_path)
284
+ zip_generator = Highlander::Util::ZipFileGenerator.new(lambda_source_dir, full_destination_path)
293
285
  zip_generator.write
294
286
 
295
287
  end
@@ -1,5 +1,5 @@
1
1
 
2
- module Cfhighlander
2
+ module Highlander
3
3
 
4
4
  module Dsl
5
5
  class DslBase
@@ -15,7 +15,7 @@ module Cfhighlander
15
15
  raise StandardError, "#{self} no config!"
16
16
  end
17
17
  return @config["#{method}"] unless @config["#{method}"].nil?
18
- raise StandardError, "#{self} Unknown method or variable #{method} in Cfhighlander template"
18
+ raise StandardError, "#{self}Unknown method or variable #{method} in Highlander template"
19
19
  end
20
20
 
21
21
  end
@@ -1,9 +1,8 @@
1
- require_relative './cfhighlander.helper'
2
- require_relative './cfhighlander.dsl.base'
3
- require_relative './cfhighlander.factory'
4
- require 'cfndsl'
1
+ require_relative './highlander.helper'
2
+ require_relative './highlander.dsl.base'
3
+ require_relative './highlander.factory'
5
4
 
6
- module Cfhighlander
5
+ module Highlander
7
6
 
8
7
  module Dsl
9
8
 
@@ -16,7 +15,8 @@ module Cfhighlander
16
15
 
17
16
  end
18
17
 
19
- class Subcomponent < DslBase
18
+
19
+ class Component < DslBase
20
20
 
21
21
  attr_accessor :name,
22
22
  :template,
@@ -65,11 +65,10 @@ module Cfhighlander
65
65
  build_distribution_url
66
66
 
67
67
  # load component
68
- factory = Cfhighlander::Factory::ComponentFactory.new(@component_sources)
69
- @component_loaded = factory.loadComponentFromTemplate(
68
+ factory = Highlander::Factory::ComponentFactory.new(@component_sources)
69
+ @component_loaded = factory.findComponent(
70
70
  @template,
71
- @template_version,
72
- @name
71
+ @template_version
73
72
  )
74
73
  @component_loaded.config.extend @config
75
74
 
@@ -100,6 +99,9 @@ module Cfhighlander
100
99
  end
101
100
 
102
101
  def load(component_config_override = {})
102
+ # check for component config on parent
103
+ parent = @parent
104
+
103
105
  # Highest priority is DSL defined configuration
104
106
  component_config_override.extend @config
105
107
 
@@ -108,22 +110,17 @@ module Cfhighlander
108
110
  @component_loaded.load @component_config_override
109
111
  end
110
112
 
111
- def parameter(name:, value:)
112
- @param_values[name] = value
113
- end
114
-
115
113
  # Parameters should be lazy loaded, that is late-binding should happen once
116
114
  # all parameters and mappings are known
117
- def resolve_parameter_values(available_outputs)
115
+ def load_parameters
118
116
  component_dsl = @component_loaded.highlander_dsl
119
117
  component_dsl.parameters.param_list.each do |component_param|
120
- param = Cfhighlander::Dsl::SubcomponentParameter.new
118
+ param = Highlander::Dsl::SubcomponentParameter.new
121
119
  param.name = component_param.name
122
120
  param.cfndsl_value = SubcomponentParamValueResolver.resolveValue(
123
121
  @parent,
124
122
  self,
125
- component_param,
126
- available_outputs)
123
+ component_param)
127
124
  @parameters << param
128
125
  end
129
126
  end
@@ -131,77 +128,40 @@ module Cfhighlander
131
128
  end
132
129
 
133
130
  class SubcomponentParamValueResolver
134
- def self.resolveValue(component, sub_component, param, available_outputs)
135
-
136
- print("INFO Resolving parameter #{component.name} -> #{sub_component.name}.#{param.name}: ")
137
-
138
- # rule 0: this rule is here for legacy reasons and OutputParam. It should be deprecated
139
- # once all hl-components- repos remove any references to OutputParam
140
- if not param.provided_value.nil?
141
- component_name = param.provided_value.split('.')[0]
142
- output_name = param.provided_value.split('.')[1]
143
- source_component = component.subcomponents.find {|c| c.name == component_name}
144
- if source_component.nil?
145
- source_component = component.subcomponents.find {|c| c.component_loaded.template.template_name == component_name}
146
- end
147
- return CfnDsl::Fn.new('GetAtt', [
148
- source_component.name,
149
- "Outputs.#{output_name}"
150
- ]).to_json
151
- end
131
+ def self.resolveValue(component, sub_component, param)
152
132
 
153
- # rule 1: check if there are values defined on component itself
133
+ puts("Resolving parameter #{component.name} -> #{sub_component.name}.#{param.name}")
134
+
135
+ # check if there are values defined on component itself
154
136
  if sub_component.param_values.key?(param.name)
155
- puts " parameter value provided "
156
-
157
- param_value = sub_component.param_values[param.name]
158
- if param_value.is_a? String and param_value.include? '.'
159
- source_component_name = param_value.split('.')[0]
160
- source_output = param_value.split('.')[1]
161
- source_component = component.subcomponents.find {|sc| sc.name == source_component_name}
162
- # if source component exists
163
- if not source_component.nil?
164
- if source_component_name == sub_component.name
165
- STDERR.puts "WARNING: Parameter value on component #{source_component_name} references component itself: #{param_value}"
166
- else
167
- return CfnDsl::Fn.new('GetAtt', [
168
- source_component_name,
169
- "Outputs.#{source_output}"
170
- ]).to_json
171
- end
172
- else
173
- return Cfhighlander::Helper.parameter_cfndsl_value(param_value)
174
- end
175
- else
176
- return Cfhighlander::Helper.parameter_cfndsl_value(sub_component.param_values[param.name])
177
- end
137
+ return Highlander::Helper.parameter_cfndsl_value(sub_component.param_values[param.name])
178
138
  end
179
139
 
180
- # rule 1.1 mapping parameters are handled differently.
181
- # TODO wire mapping parameters outside of component
182
- if param.class == Cfhighlander::Dsl::MappingParam
183
- puts " mapping parameter"
140
+ if param.class == Highlander::Dsl::StackParam
141
+ return self.resolveStackParamValue(component, sub_component, param)
142
+ elsif param.class == Highlander::Dsl::ComponentParam
143
+ return self.resolveComponentParamValue(component, sub_component, param)
144
+ elsif param.class == Highlander::Dsl::MappingParam
184
145
  return self.resolveMappingParamValue(component, sub_component, param)
146
+ elsif param.class == Highlander::Dsl::OutputParam
147
+ return self.resolveOutputParamValue(component, sub_component, param)
148
+ else
149
+ raise "#{param.class} not resolvable to parameter value"
185
150
  end
151
+ end
186
152
 
187
- # rule #2: match output values from other components
188
- # by parameter name
189
- if available_outputs.key? param.name
190
- component_name = available_outputs[param.name].component.name
191
- puts " resolved as output of #{component_name}"
192
- return CfnDsl::Fn.new('GetAtt', [
193
- component_name,
194
- "Outputs.#{param.name}"
195
- ]).to_json
196
- end
197
-
198
- # by default bubble parameter and resolve as reference on upper level
199
- propagated_param = param.clone
200
- propagated_param.name = "#{sub_component.name}#{param.name}" unless param.is_global
201
- component.parameters.addParam propagated_param
202
- puts " no autowiring candidates, propagate parameter to parent"
203
- return CfnDsl::RefDefinition.new(propagated_param.name).to_json
153
+ def self.resolveStackParamValue(component, sub_component, param)
154
+ param_name = param.is_global ? param.name : "#{sub_component.name}#{param.name}"
155
+ return "Ref('#{param_name}')"
156
+ end
204
157
 
158
+ def self.resolveComponentParamValue(component, sub_component, param)
159
+ # check component config for param value
160
+ # TODO
161
+ # check stack config for param value
162
+ # TODO
163
+ # return default value
164
+ return "'#{param.default_value}'"
205
165
  end
206
166
 
207
167
  def self.resolveMappingParamValue(component, sub_component, param)
@@ -216,7 +176,7 @@ module Cfhighlander
216
176
  key_name = nil
217
177
 
218
178
  # priority 0: stack-level parameter of map name
219
- stack_param_mapname = component.parameters.param_list.find {|p| p.name == mappings_name}
179
+ stack_param_mapname = component.parameters.param_list.find { |p| p.name == mappings_name }
220
180
  unless stack_param_mapname.nil?
221
181
  key_name = "Ref('#{mappings_name}')"
222
182
  end
@@ -235,7 +195,7 @@ module Cfhighlander
235
195
  # could still be nil after this line
236
196
  end
237
197
 
238
- value = mapping_value(component: component,
198
+ value = mapping_value(component: component,
239
199
  provider_name: mappings_name,
240
200
  value_name: param.mapAttribute,
241
201
  key_name: key_name
@@ -246,9 +206,37 @@ module Cfhighlander
246
206
  return "''"
247
207
  end
248
208
 
209
+ return value
210
+
211
+
249
212
  return value
250
213
  end
251
214
 
215
+ def self.resolveOutputParamValue(component, sub_component, param)
216
+ component_name = param.component
217
+ resource_name = nil
218
+ if not sub_component.export_config.nil?
219
+ if sub_component.export_config.key? component_name
220
+ resource_name = sub_component.export_config[component_name]
221
+ end
222
+ end
223
+
224
+ if resource_name.nil?
225
+ # find by component
226
+ resource = component.components.find { |c| c.name == component_name }
227
+ resource_name = resource.name unless resource.nil?
228
+ if resource_name.nil?
229
+ resource = component.components.find { |c| c.template == component_name }
230
+ resource_name = resource.name unless resource.nil?
231
+ end
232
+ end
233
+
234
+ if resource_name.nil?
235
+ raise "#{sub_component.name}.Params.#{param.name}: Failed to resolve OutputParam '#{param.name}' with source '#{component_name}'. Component not found!"
236
+ end
237
+
238
+ return "FnGetAtt('#{resource_name}','Outputs.#{param.name}')"
239
+ end
252
240
  end
253
241
 
254
242
  end
@@ -0,0 +1,113 @@
1
+ require_relative './highlander.dsl.base'
2
+
3
+ module Highlander
4
+
5
+ module Dsl
6
+ class Parameters < DslBase
7
+
8
+ attr_accessor :param_list
9
+
10
+ def initialize()
11
+ @param_list = []
12
+ end
13
+
14
+ def addParam(param)
15
+ existing_param = @param_list.find { |p| p.name == param.name }
16
+ if not existing_param.nil?
17
+ puts "Parameter being overwritten. Updating parameter #{param.name} with new definition..."
18
+ @param_list[@param_list.index(existing_param)] = param
19
+ else
20
+ @param_list << param
21
+ end
22
+ end
23
+
24
+ def StackParam(name, defaultValue='', isGlobal: false, noEcho: false)
25
+ param = StackParam.new(name, 'String', defaultValue)
26
+ param.is_global = isGlobal
27
+ param.config = @config
28
+ param.no_echo = noEcho
29
+ addParam param
30
+ end
31
+
32
+ def ComponentParam(name, defaultValue='')
33
+ param = ComponentParam.new(name, 'String', defaultValue)
34
+ param.config = @config
35
+ addParam param
36
+ end
37
+
38
+ def MappingParam(name, defaultValue='', &block)
39
+ param = MappingParam.new(name, 'String', defaultValue)
40
+ param.config = @config
41
+ param.instance_eval(&block)
42
+ addParam param
43
+ end
44
+
45
+ def OutputParam(component:, name:, default: '')
46
+ param = OutputParam.new(component, name, default)
47
+ param.config = @config
48
+ addParam param
49
+ end
50
+ end
51
+
52
+ class Parameter < DslBase
53
+ attr_accessor :name, :type, :default_value, :no_echo
54
+
55
+ def initialize(name, type, defaultValue, noEcho = false)
56
+ @no_echo = noEcho
57
+ @name = name
58
+ @type = type
59
+ @default_value = defaultValue
60
+ end
61
+ end
62
+
63
+ class StackParam < Parameter
64
+ attr_accessor :is_global
65
+ end
66
+
67
+ class ComponentParam < Parameter
68
+
69
+ end
70
+
71
+ class OutputParam < Parameter
72
+ attr_accessor :component
73
+
74
+ def initialize(component, name, default)
75
+ @component = component
76
+ @name = name
77
+ @default_value = default
78
+ @type = 'String'
79
+ end
80
+ end
81
+
82
+ class MappingParam < Parameter
83
+
84
+ attr_accessor :mapName, :mapKey, :mapAttribute
85
+
86
+ def method_missing(method, *args)
87
+ smethod = "#{method}"
88
+ if smethod.start_with?('Map')
89
+ puts smethod
90
+ end
91
+
92
+ super.method_missing(method)
93
+ end
94
+
95
+ def key(map_key)
96
+ @mapKey = map_key
97
+ end
98
+
99
+ def attribute(key)
100
+ @mapAttribute = key
101
+ end
102
+
103
+ def map(mapName)
104
+ @mapName = mapName
105
+ end
106
+
107
+ def mapProvider
108
+ mappings_provider(@mapName)
109
+ end
110
+
111
+ end
112
+ end
113
+ end