Module: CORL::Mixin::Lookup

Defined in:
lib/core/mixin/lookup.rb

Constant Summary collapse

@@facts =

Facter lookup

nil
@@hiera =

Hiera configuration

nil

Instance Method Summary collapse

Instance Method Details

#config_initialized?Boolean


Configuration lookup interface

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/core/mixin/lookup.rb', line 168

def config_initialized?
  ready = false
  if hiera
    if CORL.admin?
      if network_path = fact(:corl_network)
        ready = File.directory?(File.join(network_path, 'config'))
      end
    else
      ready = hiera_override_dir && File.directory?(hiera_override_dir)
    end
  end
  ready
end

#create_facts(data, reset = false) ⇒ Object




29
30
31
32
33
34
35
36
37
# File 'lib/core/mixin/lookup.rb', line 29

def create_facts(data, reset = false)
  facts(reset, false)

  data = hash(data)
  data.each do |name, value|
    fact_var[name.to_sym] = value
  end
  create_facts_post(fact_var.clone, data.keys)
end

#create_facts_post(data, names) ⇒ Object



39
40
41
# File 'lib/core/mixin/lookup.rb', line 39

def create_facts_post(data, names)
  data
end

#debug_lookup(config, property, value, label) ⇒ Object




363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/core/mixin/lookup.rb', line 363

def debug_lookup(config, property, value, label)
  if config.get(:debug, false)
    CORL.ui_group(Util::Console.cyan(property.to_s)) do |ui|
      dump = Util::Console.green(Util::Data.to_json(value, true))

      if dump.match(/\n+/)
        ui.info("#{label}:\n#{dump}")
      else
        ui.info("#{label}: #{dump}")
      end
    end
  end
end

#delete_facts(names, reset = false) ⇒ Object




45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/core/mixin/lookup.rb', line 45

def delete_facts(names, reset = false)
  facts(reset, false)

  names = [ names ] unless names.is_a?(Array)
  data  = {}

  names.each do |name|
    name       = name.to_sym
    data[name] = fact_var.delete(name)
  end
  delete_facts_post(fact_var.clone, data)
end

#delete_facts_post(data, old_data) ⇒ Object



58
59
60
# File 'lib/core/mixin/lookup.rb', line 58

def delete_facts_post(data, old_data)
  data
end

#fact(name, reset = false) ⇒ Object




64
65
66
67
# File 'lib/core/mixin/lookup.rb', line 64

def fact(name, reset = false)
  facts(reset, false)
  fact_var[name.to_sym]
end

#fact_varObject



11
12
13
# File 'lib/core/mixin/lookup.rb', line 11

def fact_var
  @@facts
end

#fact_var=(facts) ⇒ Object



15
16
17
# File 'lib/core/mixin/lookup.rb', line 15

def fact_var=facts
  @@facts = facts
end

#facts(reset = false, clone = true) ⇒ Object




21
22
23
24
25
# File 'lib/core/mixin/lookup.rb', line 21

def facts(reset = false, clone = true) # Override if needed. (See node plugin)
  self.fact_var = CORL.facts if reset || fact_var.nil?
  return fact_var.clone if clone
  fact_var
end

#hiera(reset = false) ⇒ Object




160
161
162
163
# File 'lib/core/mixin/lookup.rb', line 160

def hiera(reset = false)
  self.hiera_var = Hiera.new(:config => hiera_configuration(facts(reset))) if reset || hiera_var.nil?
  hiera_var
end

#hiera_configuration(local_facts = {}) ⇒ Object




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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/core/mixin/lookup.rb', line 96

def hiera_configuration(local_facts = {})
  Kernel.load File.join(File.dirname(__FILE__), '..', 'mod', 'hiera_backend.rb')

  backends    = CORL.value(:hiera_backends, [ "json", "yaml" ])
  config_file = CORL.value(:hiera_config_file, File.join(Hiera::Util.config_dir, 'hiera.yaml'))
  config      = {}

  if CORL.admin? && config_file && File.exist?(config_file)
    config = Hiera::Config.load(config_file)
  end

  # Need some way to tell if we have installed our own Hiera configuration.
  # TODO: Figure something else out.  This is bad.
  config = {} if config[:merge_behavior] == :native

  config[:logger] = :corl

  unless config[:merge_behavior]
    config[:merge_behavior] = "deeper"
  end

  unless config[:backends]
    config[:backends] = backends
  end

  if override_dir = hiera_override_dir
    backends.each do |backend|
      if config[:backends].include?(backend)
        backend = backend.to_sym
        config[backend] = {} unless config[backend]
        config[backend][:datadir] = override_dir
      end
    end
  end

  hiera_config = CORL.config(:hiera, config)
  loaded_facts = Util::Data.prefix(hiera_lookup_prefix, local_facts, '')

  if hiera_config[:hierarchy]
    hiera_config[:hierarchy].delete('common')
  end

  unless loaded_facts.empty?
    hiera_config[:hierarchy].collect! do |search|
      Hiera::Interpolate.interpolate(search, loaded_facts, {})
    end
  end

  unless hiera_config[:hierarchy]
    hiera_config[:hierarchy] = [ 'common' ]
  end
  unless hiera_config[:hierarchy].include?('common')
    hiera_config[:hierarchy] << 'common'
  end

  hiera_config[:hierarchy].each_with_index do |path, index|
    hiera_config[:hierarchy][index] = path.gsub('//', '/')
  end

  hiera_config.export
end

#hiera_lookup_prefixObject




90
91
92
# File 'lib/core/mixin/lookup.rb', line 90

def hiera_lookup_prefix
  '::'
end

#hiera_override_dirObject




84
85
86
# File 'lib/core/mixin/lookup.rb', line 84

def hiera_override_dir
  nil # Override if needed. (See network and node plugins)
end

#hiera_varObject



74
75
76
# File 'lib/core/mixin/lookup.rb', line 74

def hiera_var
  @@hiera
end

#hiera_var=(hiera) ⇒ Object



78
79
80
# File 'lib/core/mixin/lookup.rb', line 78

def hiera_var=hiera
  @@hiera = hiera
end

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




262
263
264
# File 'lib/core/mixin/lookup.rb', line 262

def lookup(properties, default = nil, options = {})
  lookup_base(properties, default, options)
end

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




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
# File 'lib/core/mixin/lookup.rb', line 268

def lookup_array(properties, default = [], options = {})
  config          = Config.ensure(options).defaults({ :basic => false })
  value, property = lookup(properties, nil, config.import({ :return_property => true, :context => :array }))

  if Util::Data.undef?(value)
    value = default
    debug_lookup(config, property, value, "Array default value")

  elsif ! Util::Data.empty?(default)
    if config.get(:merge, false)
      value = Util::Data.merge([default, value], config)
      debug_lookup(config, property, value, "Merged array value with default")
    end
  end

  unless value.is_a?(Array)
    value = ( Util::Data.empty?(value) ? [] : [ value ] )
  end

  value = Util::Data.value(value, config.get(:undefined_value, :undefined))
  debug_lookup(config, property, value, "Final array value")

  Config.set_property(property, value)
  CORL.ui.info("\n", { :prefix => false }) if config.get(:debug, false)
  value
end

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




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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/core/mixin/lookup.rb', line 184

def lookup_base(properties, default = nil, options = {})
  config      = Config.new(Config.ensure(options).export)
  value       = nil

  node        = config.delete(:node, nil)

  provisioner = config.get(:provisioner, nil)

  hiera_scope = config.get(:hiera_scope, {})
  override    = config.get(:override, nil)
  context     = config.get(:context, :priority)
  debug       = config.get(:debug, false)

  return_property = config.get(:return_property, false)

  return node.lookup(properties, default, config) if node

  unless properties.is_a?(Array)
    properties = [ properties ].flatten
  end

  first_property = nil
  properties.each do |property|
    property       = property.to_sym
    first_property = property unless first_property

    if debug
      CORL.ui.info("\n", { :prefix => false })
      CORL.ui_group(Util::Console.purple(property)) do |ui|
        ui.info("-----------------------------------------------------")
      end
    end

    # Try to load facts first (these can not be overridden)
    value = fact(property)
    debug_lookup(config, property, value, "Fact lookup")

    unless value
      if config_initialized?
        # Try to find in Hiera data store
        unless hiera_scope.respond_to?('[]')
          hiera_scope = Hiera::Scope.new(hiera_scope)
        end

        hiera_scope = string_map(hiera_scope) if hiera_scope.is_a?(Hash)
        value       = hiera.lookup(property.to_s, nil, hiera_scope, override, context.to_sym)

        debug_lookup(config, property, value, "Hiera lookup")
      end

      if provisioner && value.nil?
        # Search the provisioner scope (only admins can provision a machine)
        value = CORL.provisioner({ :name => :lookup }, provisioner).lookup(property, default, config)
        debug_lookup(config, property, value, "Provisioner lookup")
      end
    end
  end
  if value.nil? # Resort to default
    value = default
    debug_lookup(config, first_property, value, "Default value")
  end
  value = Util::Data.value(value, config.get(:undefined_value, :undefined))

  if Config.get_property(first_property).nil? || value
    Config.set_property(first_property, value)
  end

  debug_lookup(config, first_property, value, 'Internalized value')

  if return_property
    return value, first_property
  end
  CORL.ui.info("\n", { :prefix => false }) if debug
  value
end

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




297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/core/mixin/lookup.rb', line 297

def lookup_hash(properties, default = {}, options = {})
  config          = Config.ensure(options).defaults({ :basic => false })
  value, property = lookup(properties, nil, config.import({ :return_property => true, :context => :hash }))

  if Util::Data.undef?(value)
    value = default
    debug_lookup(config, property, value, "Hash default value")

  elsif ! Util::Data.empty?(default)
    if config.get(:merge, false)
      value = Util::Data.merge([default, value], config)
      debug_lookup(config, property, value, "Merged hash value with default")
    end
  end

  unless value.is_a?(Hash)
    value = ( Util::Data.empty?(value) ? {} : { :value => value } )
  end

  value = Util::Data.value(value, config.get(:undefined_value, :undefined))
  debug_lookup(config, property, value, "Final hash value")

  Config.set_property(property, value)
  CORL.ui.info("\n", { :prefix => false }) if config.get(:debug, false)
  value
end

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




326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
# File 'lib/core/mixin/lookup.rb', line 326

def normalize(data, override = nil, options = {})
  config  = Config.ensure(options).import({ :context => :hash }).defaults({ :basic => false })
  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
  results
end