Module: Hiera::Backend
- Defined in:
- lib/hiera/backend.rb,
lib/hiera/backend/json_backend.rb,
lib/hiera/backend/yaml_backend.rb
Defined Under Namespace
Classes: Backend1xWrapper, Json_backend, Yaml_backend
Class Method Summary collapse
- .clear! ⇒ Object
-
.datadir(backend, scope) ⇒ Object
Data lives in /var/lib/hiera by default.
-
.datafile(backend, scope, source, extension) ⇒ Object
Finds the path to a datafile based on the Backend#datadir and extension.
- .datafile_in(datadir, source, extension) ⇒ Object private
-
.datasourcefiles(backend, scope, extension, override = nil, hierarchy = nil) {|String, String| ... } ⇒ Object
Constructs a list of data files to search.
-
.datasources(scope, override = nil, hierarchy = nil) ⇒ Object
Constructs a list of data sources to search.
- .find_backend(backend_constant) ⇒ Object
-
.lookup(key, default, scope, order_override, resolution_type, context = {:recurse_guard => nil}) ⇒ Object
The value that corresponds to the given key or nil if no such value cannot be found.
-
.merge_answer(left, right, resolution_type = nil) ⇒ Hash
Merges two hashes answers with the given or configured merge behavior.
-
.parse_answer(data, scope, extra_data = {}, context = {:recurse_guard => nil, :order_override => nil}) ⇒ Object
Parses a answer received from data files.
-
.parse_string(data, scope, extra_data = {}, context = {:recurse_guard => nil, :order_override => nil}) ⇒ String
Parse a string like
'%{foo}'
against a supplied scope and additional scope. - .qualified_lookup(segments, hash) ⇒ Object
- .resolve_answer(answer, resolution_type) ⇒ Object
Class Method Details
.clear! ⇒ Object
297 298 299 |
# File 'lib/hiera/backend.rb', line 297 def clear! @backends = {} end |
.datadir(backend, scope) ⇒ Object
Data lives in /var/lib/hiera by default. If a backend supplies a datadir in the config it will be used and subject to variable expansion based on scope
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/hiera/backend.rb', line 31 def datadir(backend, scope) backend = backend.to_sym if Config[backend] && Config[backend][:datadir] dir = Config[backend][:datadir] else dir = Hiera::Util.var_dir end if !dir.is_a?(String) raise(Hiera::InvalidConfigurationError, "datadir for #{backend} cannot be an array") end parse_string(dir, scope) end |
.datafile(backend, scope, source, extension) ⇒ Object
Finds the path to a datafile based on the Backend#datadir and extension
If the file is not found nil is returned
52 53 54 |
# File 'lib/hiera/backend.rb', line 52 def datafile(backend, scope, source, extension) datafile_in(datadir(backend, scope), source, extension) end |
.datafile_in(datadir, source, extension) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/hiera/backend.rb', line 57 def datafile_in(datadir, source, extension) file = File.join(datadir, "#{source}.#{extension}") if File.exist?(file) file else Hiera.debug("Cannot find datafile #{file}, skipping") nil end end |
.datasourcefiles(backend, scope, extension, override = nil, hierarchy = nil) {|String, String| ... } ⇒ Object
Constructs a list of data files to search
If you give it a specific hierarchy it will just use that else it will use the global configured one, failing that it will just look in the ‘common’ data source.
An override can be supplied that will be pre-pended to the hierarchy.
The source names will be subject to variable expansion based on scope
Only files that exist will be returned. If the file is missing, then the block will not receive the file.
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/hiera/backend.rb', line 113 def datasourcefiles(backend, scope, extension, override=nil, hierarchy=nil) datadir = Backend.datadir(backend, scope) Backend.datasources(scope, override, hierarchy) do |source| Hiera.debug("Looking for data source #{source}") file = datafile_in(datadir, source, extension) if file yield source, file end end end |
.datasources(scope, override = nil, hierarchy = nil) ⇒ Object
Constructs a list of data sources to search
If you give it a specific hierarchy it will just use that else it will use the global configured one, failing that it will just look in the ‘common’ data source.
An override can be supplied that will be pre-pended to the hierarchy.
The source names will be subject to variable expansion based on scope
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/hiera/backend.rb', line 79 def datasources(scope, override=nil, hierarchy=nil) if hierarchy hierarchy = [hierarchy] elsif Config.include?(:hierarchy) hierarchy = [Config[:hierarchy]].flatten else hierarchy = ["common"] end hierarchy.insert(0, override) if override hierarchy.flatten.map do |source| source = parse_string(source, scope, {}, :order_override => override) yield(source) unless source == "" or source =~ /(^\/|\/\/|\/$)/ end end |
.find_backend(backend_constant) ⇒ Object
318 319 320 321 |
# File 'lib/hiera/backend.rb', line 318 def find_backend(backend_constant) backend = Backend.const_get(backend_constant).new return backend.method(:lookup).arity == 4 ? Backend1xWrapper.new(backend) : backend end |
.lookup(key, default, scope, order_override, resolution_type, context = {:recurse_guard => nil}) ⇒ Object
Returns The value that corresponds to the given key or nil if no such value cannot be found.
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 |
# File 'lib/hiera/backend.rb', line 240 def lookup(key, default, scope, order_override, resolution_type, context = {:recurse_guard => nil}) @backends ||= {} answer = nil # order_override is kept as an explicit argument for backwards compatibility, but should be specified # in the context for internal handling. context ||= {} order_override ||= context[:order_override] context[:order_override] ||= order_override strategy = resolution_type.is_a?(Hash) ? :hash : resolution_type segments = key.split('.') subsegments = nil if segments.size > 1 raise ArgumentError, "Resolution type :#{strategy} is illegal when doing segmented key lookups" unless strategy.nil? || strategy == :priority subsegments = segments.drop(1) end found = false Config[:backends].each do |backend| backend_constant = "#{backend.capitalize}_backend" if constants.include?(backend_constant) || constants.include?(backend_constant.to_sym) backend = (@backends[backend] ||= find_backend(backend_constant)) found_in_backend = false new_answer = catch(:no_such_key) do value = backend.lookup(segments[0], scope, order_override, resolution_type, context) value = qualified_lookup(subsegments, value) unless subsegments.nil? found_in_backend = true value end next unless found_in_backend found = true case strategy when :array raise Exception, "Hiera type mismatch for key '#{key}': expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String answer ||= [] answer << new_answer when :hash raise Exception, "Hiera type mismatch for key '#{key}': expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash answer ||= {} answer = merge_answer(new_answer, answer, resolution_type) else answer = new_answer break end end end answer = resolve_answer(answer, strategy) unless answer.nil? answer = parse_string(default, scope, {}, context) if !found && default.is_a?(String) return default if !found && answer.nil? return answer end |
.merge_answer(left, right, resolution_type = nil) ⇒ Hash
Merges two hashes answers with the given or configured merge behavior. Behavior can be given by passing resolution_type as a Hash
:merge_behavior: {:native|:deep|:deeper}
Deep merge options use the Hash utility function provided by [deep_merge](github.com/danielsdeleo/deep_merge)
:native => Native Hash.merge
:deep => Use Hash.deep_merge
:deeper => Use Hash.deep_merge!
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/hiera/backend.rb', line 200 def merge_answer(left,right,resolution_type=nil) behavior, = if resolution_type.is_a?(Hash) merge = resolution_type.clone [merge.delete(:behavior), merge] else [Config[:merge_behavior], Config[:deep_merge_options] || {}] end case behavior when :deeper,'deeper' left.deep_merge!(right, ) when :deep,'deep' left.deep_merge(right, ) else # Native and undefined left.merge(right) end end |
.parse_answer(data, scope, extra_data = {}, context = {:recurse_guard => nil, :order_override => nil}) ⇒ Object
Parses a answer received from data files
Ultimately it just pass the data through parse_string but it makes some effort to handle arrays of strings as well
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/hiera/backend.rb', line 149 def parse_answer(data, scope, extra_data={}, context={:recurse_guard => nil, :order_override => nil}) if data.is_a?(Numeric) or data.is_a?(TrueClass) or data.is_a?(FalseClass) return data elsif data.is_a?(String) return parse_string(data, scope, extra_data, context) elsif data.is_a?(Hash) answer = {} data.each_pair do |key, val| interpolated_key = parse_string(key, scope, extra_data, context) answer[interpolated_key] = parse_answer(val, scope, extra_data, context) end return answer elsif data.is_a?(Array) answer = [] data.each do |item| answer << parse_answer(item, scope, extra_data, context) end return answer end end |
.parse_string(data, scope, extra_data = {}, context = {:recurse_guard => nil, :order_override => nil}) ⇒ String
Parse a string like '%{foo}'
against a supplied scope and additional scope. If either scope or extra_scope includes the variable ‘foo’, then it will be replaced else an empty string will be placed.
If both scope and extra_data has “foo”, then the value in scope will be used.
141 142 143 |
# File 'lib/hiera/backend.rb', line 141 def parse_string(data, scope, extra_data={}, context={:recurse_guard => nil, :order_override => nil}) Hiera::Interpolate.interpolate(data, scope, extra_data, context) end |
.qualified_lookup(segments, hash) ⇒ Object
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/hiera/backend.rb', line 301 def qualified_lookup(segments, hash) value = hash segments.each do |segment| throw :no_such_key if value.nil? if segment =~ /^[0-9]+$/ segment = segment.to_i raise Exception, "Hiera type mismatch: Got #{value.class.name} when Array was expected enable lookup using key '#{segment}'" unless value.instance_of?(Array) throw :no_such_key unless segment < value.size else raise Exception, "Hiera type mismatch: Got #{value.class.name} when a non Array object that responds to '[]' was expected to enable lookup using key '#{segment}'" unless value.respond_to?(:'[]') && !value.instance_of?(Array); throw :no_such_key unless value.include?(segment) end value = value[segment] end value end |
.resolve_answer(answer, resolution_type) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/hiera/backend.rb', line 172 def resolve_answer(answer, resolution_type) case resolution_type when :array [answer].flatten.uniq.compact when :hash answer # Hash structure should be preserved else answer end end |