Class: Hieracles::Node

Inherits:
Object
  • Object
show all
Includes:
Interpolate, Utils
Defined in:
lib/hieracles/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Interpolate

#ask_about, #extract, #parse, #regex, #setio

Methods included from Utils

#deep_sort, #local_merge, #local_merge!, #max_key_length, #sym_keys, #to_deep_hash, #to_shallow_hash

Constructor Details

#initialize(fqdn, config) ⇒ Node

Returns a new instance of Node.



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

def initialize(fqdn, config)
  @fqdn = fqdn
  @config = config
  @hiera = Hieracles::Hiera.new @config
  @hiera_params = { fqdn: @fqdn }.
    merge(get_hiera_params(@fqdn)).
    merge(@config.extraparams)
  @facts = deep_sort(@hiera_params.
    merge(@config.scope).
    merge(puppet_facts))
end

Instance Attribute Details

#factsObject (readonly)

Returns the value of attribute facts.



11
12
13
# File 'lib/hieracles/node.rb', line 11

def facts
  @facts
end

#hieraObject (readonly)

Returns the value of attribute hiera.



11
12
13
# File 'lib/hieracles/node.rb', line 11

def hiera
  @hiera
end

#hiera_paramsObject (readonly)

Returns the value of attribute hiera_params.



11
12
13
# File 'lib/hieracles/node.rb', line 11

def hiera_params
  @hiera_params
end

#notificationsObject (readonly)

Returns the value of attribute notifications.



11
12
13
# File 'lib/hieracles/node.rb', line 11

def notifications
  @notifications
end

Instance Method Details

#_get_infoObject



115
116
117
118
119
120
121
# File 'lib/hieracles/node.rb', line 115

def _get_info
  extra = {}
  if @config.usedb
    extra = puppetdb_info
  end
  @hiera_params.merge extra
end

#_get_modulesObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/hieracles/node.rb', line 95

def _get_modules
  modules = {}
  classfiles.each do |c|
    if File.exist?(c)
      f = File.open(c, "r")
      f.each_line do |line|
        modules = add_modules(line, modules)
      end
      f.close
    else
      raise "Class file #{c} not found."
    end
  end
  modules
end

#add_modules(line, modules) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/hieracles/node.rb', line 133

def add_modules(line, modules)
  if /^\s*include\s*([-_:a-zA-Z0-9]*)\s*/.match(line)
    mod = $1
    mainmod = mod[/^[^:]*/]
    if Dir.exists? modulepath(mainmod)
      modules[mod] = File.join(@config.modulepath, mainmod)
    else
      modules[mod] = nil
    end
  end
  modules
end

#classfilesObject



123
124
125
126
127
# File 'lib/hieracles/node.rb', line 123

def classfiles
  @hiera_params[:classes].map do |cl|
    format(@config.classpath, cl)
  end
end

#error(message) ⇒ Object



202
203
204
205
206
207
208
# File 'lib/hieracles/node.rb', line 202

def error(message)
  if @notifications
    @notifications << Notification.new('node', message, 'error')
  else
    @notifications = [ Notification.new('node', message, 'error') ]
  end
end

#files(without_common = true) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/hieracles/node.rb', line 34

def files(without_common = true)
  @__files ||= @hiera.hierarchy.reduce([]) do |a, f|
    file = parse("#{f}.yaml", @facts, @config.interactive)
    if file && 
       File.exist?(File.join(@hiera.datapath, file)) &&
       (!without_common ||
       !file[/common/])
      a << File.join(@hiera.datadir, file)
    end
    a
  end
end

#get_hiera_params(fqdn) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/hieracles/node.rb', line 25

def get_hiera_params(fqdn)
  @__hiera_params ||= if File.exist?(File.join(@config.encpath, "#{fqdn}.yaml"))
    load = YAML.load_file(File.join(@config.encpath, "#{fqdn}.yaml"))
    sym_keys(load['parameters']).merge({ classes: load['classes']})
  else
    raise "Node not found"
  end
end

#infoObject



111
112
113
# File 'lib/hieracles/node.rb', line 111

def info
  @_info ||= _get_info
end

#merge_trees(left, right) ⇒ Object



169
170
171
172
173
174
175
176
177
178
# File 'lib/hieracles/node.rb', line 169

def merge_trees(left, right)
  case @hiera.merge_behavior
  when :deeper
    left.deep_merge!(right)
  when :deep
    left.deep_merge(right)
  else
    local_merge!(left, right)
  end
end

#merge_value(previous, value) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/hieracles/node.rb', line 180

def merge_value(previous, value)
  if value.is_a? Array
    if previous == []
      { merged: deep_sort(value) }
    else
      left = { merged: previous.last[:merged] }
      right = { merged: value }
      case @hiera.merge_behavior
      # TODO: handle the case where right is not an array
      when :deeper
        deep_sort(left.deep_merge!(right))
      when :deep
        deep_sort(left.deep_merge(right))
      else
        deep_sort(right)
      end
    end
  else
    { merged: value }
  end
end

#modulepath(path) ⇒ Object



129
130
131
# File 'lib/hieracles/node.rb', line 129

def modulepath(path)
  File.join(@config.modulepath, path)
end

#modulesObject



91
92
93
# File 'lib/hieracles/node.rb', line 91

def modules
  @_modules ||= _get_modules
end

#params(without_common = true) ⇒ Object



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
# File 'lib/hieracles/node.rb', line 51

def params(without_common = true)
  params = {}
  files(without_common).each do |f|
    data = YAML.load_file(File.join(@config.basepath, f))
    if data
      s = to_shallow_hash(data)
      s.each do |k,v|
        if params[k]
          case @hiera.merge_behavior
          when :deeper
            params[k] = { value: v }.deep_merge!(params[k])
          when :deep
            params[k].deep_merge({ value: v })
          end
          params[k][:file] = '-'
          params[k][:overriden] = true
          params[k][:found_in].push({ value: v, file: f })
        else
          params[k] = {
            value: v,
            file: f,
            overriden: false,
            found_in: [{ value: v, file: f }]
          }
        end
      end
    end
  end
  params.sort.to_h
end

#params_tree(without_common = true) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/hieracles/node.rb', line 82

def params_tree(without_common = true)
  params = {}
  paths(without_common).reverse.each do |f|
    data = YAML.load_file(f) || {}
    merge_trees params, data
  end
  deep_sort(params)
end

#paths(without_common = true) ⇒ Object



47
48
49
# File 'lib/hieracles/node.rb', line 47

def paths(without_common = true)
  files(without_common).map { |p| File.join(@config.basepath, p) }
end

#puppet_factsObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/hieracles/node.rb', line 150

def puppet_facts
  if @config.usedb
    resp = request_db.node_facts(@fqdn)
    @notifications = resp.notifications
    if resp.total_records > 0
      resp.data
    else
      error "not found in puppetdb."
      {}
    end
  else
    {}
  end
end

#puppetdb_infoObject



146
147
148
# File 'lib/hieracles/node.rb', line 146

def puppetdb_info
  request_db.node_info(@fqdn).data
end

#request_dbObject



165
166
167
# File 'lib/hieracles/node.rb', line 165

def request_db
  @_request ||= Hieracles::Puppetdb::Request.new @config.puppetdb
end