Class: HieraExaminer

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

Instance Method Summary collapse

Constructor Details

#initialize(conf) ⇒ HieraExaminer

Returns a new instance of HieraExaminer.



8
9
10
11
# File 'lib/hiera-examiner.rb', line 8

def initialize(conf)
  puts conf
  @conf = conf
end

Instance Method Details

#explain(key, scope) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/hiera-examiner.rb', line 31

def explain(key, scope)
  puts 'key ' + (key or "UNKNOWN")
  print 'scope ' 
  puts scope
  
  puts "#{key.red}\n#{explainDetail(key, scope, '', true)}"
end

#explainDetail(key, scope, prefix = '', showSource = false, yamlFileParam = '') ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
# File 'lib/hiera-examiner.rb', line 39

def explainDetail(key, scope, prefix = '', showSource = false, yamlFileParam = '')
  startExp = /%{hiera\('/
  endExp = /'\)}/
  
  firstIndex = key.index(startExp)
  
  if (firstIndex)
    newIndex = firstIndex + 9
    endIndex = key.index(endExp) - 1
    padding = "#{prefix}#{' '.rjust((firstIndex + 2) - prefix.length)}"
    yamlMap = yamlLookup(key[newIndex..endIndex], scope)
    yamlVal = yamlMap[:val]
    yamlFile = yamlMap[:file]
    
    newVal = ''
    keyVal = key
    if (firstIndex == 0)
      keyVal = "#{key[0..endIndex + 3].bold.red}#{key[endIndex + 4..-1]}"
      newVal += "#{yamlVal}#{key[endIndex + 4..-1]}"
    else
      keyVal = "#{key[0..firstIndex - 1]}#{key[firstIndex..endIndex + 3].bold.red}#{key[endIndex + 4..-1]}"
      newVal += "#{key[0..firstIndex - 1]}#{yamlVal}#{key[endIndex + 4..-1]}"
    end
    
    if (showSource && !yamlFileParam.empty?())
      yamlFileString = " \u2794 #{yamlFileParam}"
      res = "#{keyVal}#{yamlFileString.cyan}\n"
    else
      res = "#{keyVal}\n"
    end
    
    res += "#{padding}#{"\u2B07".yellow}\n"
    if yamlVal
      yamlFileString = " \u2794 #{yamlFile}"
      yamlVal = "#{yamlVal.bold.green}#{yamlFileString.cyan}"
    else
      yamlVal = 'MISSING'.magenta.blink
    end
    res += "#{padding}#{yamlVal}"
    if (newVal.index(startExp))
      res += "\n#{explainDetail(newVal, scope, prefix)}"
    end
    
    return res
  else
    hieraVal = hieraLookup(key, scope)
    yamlMap = yamlLookup(key, scope)
    yamlVal = yamlMap[:val]
    yamlFile = yamlMap[:file]
    
    if (hieraVal.empty?() || (!yamlVal || yamlVal.empty?()))
      return 'MISSING'.magenta.blink
    elsif (yamlVal.index(startExp))
      if (showSource)
        res = explainDetail(yamlVal, scope, "#{prefix}|", true, yamlFile)
      else
        res = explainDetail(yamlVal, scope, "#{prefix}|")
      end
      res = "|\n#{res}\n#{hieraVal.green}"
      return res
    else
      res = "|\n#{yamlVal}"
      return res
    end
  end
end

#hieraLookup(key, scope) ⇒ Object



25
26
27
28
29
# File 'lib/hiera-examiner.rb', line 25

def hieraLookup(key, scope)
  hiera = Hiera.new(:config => @conf) 
  puts "Looking up '#{key}' in '#{@conf}' with '#{scope}'"
  return hiera.lookup(key, @conf, scope)
end

#yamlLookup(key, scope) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/hiera-examiner.rb', line 13

def yamlLookup(key, scope) 
  hiera = Hiera.new(:config => @conf) 
  value = {val: nil, file: nil}
  Hiera::Backend.datasourcefiles('yaml', scope, 'yaml') { |source, file| 
    yaml = YAML.load_file(file)
    if (yaml.has_key?(key) && value[:val] == nil)
      value = {val: yaml[key], file: file}
    end
  }
  return value
end