Class: Hieracles::Node

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

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

Constructor Details

#initialize(fqdn, options) ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
16
17
18
# File 'lib/hieracles/node.rb', line 11

def initialize(fqdn, options)
  Config.load(options)
  @hiera = Hieracles::Hiera.new
  @hiera_params = { fqdn: fqdn }.
    merge(get_hiera_params(fqdn)).
    merge(Config.extraparams)
  @fqdn = fqdn
end

Instance Attribute Details

#hieraObject (readonly)

Returns the value of attribute hiera.



9
10
11
# File 'lib/hieracles/node.rb', line 9

def hiera
  @hiera
end

#hiera_paramsObject (readonly)

Returns the value of attribute hiera_params.



9
10
11
# File 'lib/hieracles/node.rb', line 9

def hiera_params
  @hiera_params
end

Instance Method Details

#add_modules(line, modules) ⇒ Object



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

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



88
89
90
91
92
# File 'lib/hieracles/node.rb', line 88

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

#files(without_common = true) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hieracles/node.rb', line 29

def files(without_common = true)
  @hiera.hierarchy.reduce([]) do |a, f|
    file = format("#{f}.yaml", @hiera_params) rescue nil
    if file && 
       File.exist?(File.join(@hiera.datadir, file)) &&
       (!without_common ||
       !file[/common/])
      a << file
    end
    a
  end
end

#get_hiera_params(fqdn) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/hieracles/node.rb', line 20

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

#infoObject



84
85
86
# File 'lib/hieracles/node.rb', line 84

def info
  @hiera_params
end

#modulepath(path) ⇒ Object



94
95
96
# File 'lib/hieracles/node.rb', line 94

def modulepath(path)
  File.join(Config.path('modulepath'), path)
end

#modulesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/hieracles/node.rb', line 68

def 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

#params(without_common = true) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/hieracles/node.rb', line 46

def params(without_common = true)
  params = {}
  files(without_common).each do |f|
    data = YAML.load_file(File.join(@hiera.datadir, f))
    s = to_shallow_hash(data)
    s.each do |k,v|
      params[k] ||= []
      params[k] << { value: v, file: f}
    end
  end
  params.sort
end

#params_tree(without_common = true) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/hieracles/node.rb', line 59

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

#paths(without_common = true) ⇒ Object



42
43
44
# File 'lib/hieracles/node.rb', line 42

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