Module: Coral::Mixin::Lookup

Included in:
Config
Defined in:
lib/coral_core/mixin/lookup.rb

Constant Summary collapse

@@hiera =

Hiera configuration

nil

Instance Method Summary collapse

Instance Method Details

#hieraObject




33
34
35
36
# File 'lib/coral_core/mixin/lookup.rb', line 33

def hiera
  @@hiera = Hiera.new(:config => hiera_config) unless @@hiera
  return @@hiera
end

#hiera_configObject




17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/coral_core/mixin/lookup.rb', line 17

def hiera_config
  config_file = Puppet.settings[:hiera_config]
  config      = {}

  if File.exist?(config_file)
    config = Hiera::Config.load(config_file)
  else
    ui.warn("Config file #{config_file} not found, using Hiera defaults")
  end

  config[:logger] = 'puppet'
  return config
end

#initialized?(options = {}) ⇒ Boolean


Configuration lookup interface

Returns:

  • (Boolean)


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/coral_core/mixin/lookup.rb', line 41

def initialized?(options = {})
  config   = Config.ensure(options)
  
  begin
    require 'hiera'
    puppet_scope = config.get(:puppet_scope, scope)
  
    prefix_text = config.get(:prefix_text, '::')  
    init_fact   = prefix_text + config.get(:init_fact, 'hiera_ready')
    
    if Puppet::Parser::Functions.function('hiera') && puppet_scope.respond_to?('[]')
      return true if Util::Data.true?(puppet_scope[init_fact])
    end
    return false
  
  rescue Exception # Prevent abortions.
  end    
  return false
end

#lookup(properties, default = nil, options = {}) ⇒ Object




63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/coral_core/mixin/lookup.rb', line 63

def lookup(properties, default = nil, options = {})
  config          = Config.ensure(options)
  value           = nil
  
  hiera_scope     = config.get(:hiera_scope, {})
  context         = config.get(:context, :priority)    
  override        = config.get(:override, nil)
  
  puppet_scope    = config.get(:puppet_scope, scope)
  
  base_names      = config.get(:search, nil)
   
  search_name     = config.get(:search_name, true)
  reverse_lookup  = config.get(:reverse_lookup, true)
  
  return_property = config.get(:return_property, false)
  
  unless properties.is_a?(Array)
    properties = [ properties ].flatten
  end

  first_property = nil
  properties.each do |property|
    first_property = property unless first_property
        
    if initialized?(config)
      unless hiera_scope.respond_to?('[]')
        hiera_scope = Hiera::Scope.new(hiera_scope)
      end
      value = hiera.lookup(property, nil, hiera_scope, override, context)
    end 
  
    if Util::Data.undef?(value)    
      log_level = Puppet::Util::Log.level
      Puppet::Util::Log.level = :err # Don't want failed parameter lookup warnings here.
    
      if base_names
        if base_names.is_a?(String)
          base_names = [ base_names ]
        end
        base_names = base_names.reverse if reverse_lookup
      
        base_names.each do |base|
          value = puppet_scope.lookupvar("::#{base}::#{property}")
          break unless Util::Data.undef?(value)  
        end
      end
      if Util::Data.undef?(value) && search_name
        value = puppet_scope.lookupvar("::#{property}")
      end
      Puppet::Util::Log.level = log_level
    end
  end
  value = default if Util::Data.undef?(value)
  value = Util::Data.value(value)
  
  if ! @@properties.has_key?(first_property) || ! Util::Data.undef?(value)
    Config::Collection.set(first_property, value)
  end
  return value, first_property if return_property
  return value
end

#lookup_array(properties, default = [], options = {}) ⇒ Object




128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/coral_core/mixin/lookup.rb', line 128

def lookup_array(properties, default = [], options = {})
  config          = Config.ensure(options) 
  value, property = lookup(properties, nil, config.import({ :return_property => true }))
  
  if Util::Data.undef?(value)
    value = default
      
  elsif ! Util::Data.empty?(default)
    if config.get(:merge, false)
      value = Util::Data.merge([default, value], config)
    end
  end
  
  unless value.is_a?(Array)
    value = ( Util::Data.empty?(value) ? [] : [ value ] )
  end
  
  Config::Collection.set(property, value)
  return value
end

#lookup_hash(properties, default = {}, options = {}) ⇒ Object




151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/coral_core/mixin/lookup.rb', line 151

def lookup_hash(properties, default = {}, options = {})
  config          = Config.ensure(options) 
  value, property = lookup(properties, nil, config.import({ :return_property => true }))
  
  if Util::Data.undef?(value)
    value = default
      
  elsif ! Util::Data.empty?(default)
    if config.get(:merge, false)
      value = Util::Data.merge([default, value], config)
    end
  end
  
  unless value.is_a?(Hash)
    value = ( Util::Data.empty?(value) ? {} : { :value => value } )
  end
  
  Config::Collection.set(property, value)
  return value
end

#normalize(data, override = nil, options = {}) ⇒ Object




174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/coral_core/mixin/lookup.rb', line 174

def normalize(data, override = nil, options = {})
  config  = Config.ensure(options)
  results = {}
  
  unless Util::Data.undef?(override)
    case data
    when String, Symbol
      data = [ data, override ] if data != override
    when Array
      data << override unless data.include?(override)
    when Hash
      data = [ data, override ]
    end
  end
  
  case data
  when String, Symbol
    results = lookup(data.to_s, {}, config)
    
  when Array
    data.each do |item|
      if item.is_a?(String) || item.is_a?(Symbol)
        item = lookup(item.to_s, {}, config)
      end
      unless Util::Data.undef?(item)
        results = Util::Data.merge([ results, item ], config)
      end
    end

  when Hash
    results = data
  end
  
  return results
end