Class: Hiera::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/config.rb

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object



86
87
88
# File 'lib/hiera/config.rb', line 86

def [](key)
  @config[key]
end

.include?(key) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/hiera/config.rb', line 82

def include?(key)
  @config.include?(key)
end

.load(source) ⇒ Hash

load takes a string or hash as input, strings are treated as filenames hashes are stored as data that would have been in the config file

Unless specified it will only use YAML as backend with a single ‘common’ hierarchy and console logger

Returns:

  • (Hash)

    representing the configuration. e.g. => “yaml”, :hierarchy => “common”



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hiera/config.rb', line 12

def load(source)
  @config = {:backends => "yaml",
             :hierarchy => "common",
             :merge_behavior => :native }

  if source.is_a?(String)
    if File.exist?(source)
      config = begin
                 yaml_load_file(source)
               rescue TypeError => detail
                 case detail.message
                 when /no implicit conversion from nil to integer/
                   false
                 else
                   raise detail
                 end
               end
      @config.merge! config if config
    else
      raise "Config file #{source} not found"
    end
  elsif source.is_a?(Hash)
    @config.merge! source
  end

  @config[:backends] = [ @config[:backends] ].flatten

  if @config.include?(:logger)
    Hiera.logger = @config[:logger].to_s
  else
    @config[:logger] = "console"
    Hiera.logger = "console"
  end

  self.validate!

  @config
end

.load_backendsObject



72
73
74
75
76
77
78
79
80
# File 'lib/hiera/config.rb', line 72

def load_backends
  @config[:backends].each do |backend|
    begin
      require "hiera/backend/#{backend.downcase}_backend"
    rescue LoadError => e
      Hiera.warn "Cannot load backend #{backend}: #{e}"
    end
  end
end

.validate!Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/hiera/config.rb', line 51

def validate!
  case @config[:merge_behavior]
  when :deep,'deep',:deeper,'deeper'
    begin
      require "deep_merge"
    rescue LoadError
      raise Hiera::Error, "Must have 'deep_merge' gem installed for the configured merge_behavior."
    end
  end
end

.yaml_load_file(source) ⇒ Object

yaml_load_file directly delegates to YAML.load_file and is intended to be a private, internal method suitable for stubbing and mocking.

Returns:

  • (Object)

    return value of YAML.load_file



67
68
69
# File 'lib/hiera/config.rb', line 67

def yaml_load_file(source)
  YAML.load_file(source)
end