forked from puppetlabs/puppet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhiera_puppet.rb
87 lines (69 loc) · 2 KB
/
hiera_puppet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
require 'hiera'
require 'hiera/scope'
require 'puppet'
module HieraPuppet
module_function
def lookup(key, default, scope, override, resolution_type)
scope = Hiera::Scope.new(scope)
answer = hiera.lookup(key, default, scope, override, resolution_type)
if answer.nil?
raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied")
end
answer
end
def parse_args(args)
# Functions called from Puppet manifests like this:
#
# hiera("foo", "bar")
#
# Are invoked internally after combining the positional arguments into a
# single array:
#
# func = function_hiera
# func(["foo", "bar"])
#
# Functions called from templates preserve the positional arguments:
#
# scope.function_hiera("foo", "bar")
#
# Deal with Puppet's special calling mechanism here.
if args[0].is_a?(Array)
args = args[0]
end
if args.empty?
raise(Puppet::ParseError, "Please supply a parameter to perform a Hiera lookup")
end
key = args[0]
default = args[1]
override = args[2]
return [key, default, override]
end
private
module_function
def hiera
@hiera ||= Hiera.new(:config => hiera_config)
end
def hiera_config
config = {}
if config_file = hiera_config_file
config = Hiera::Config.load(config_file)
end
config[:logger] = 'puppet'
config
end
def hiera_config_file
config_file = nil
if Puppet.settings[:hiera_config].is_a?(String)
expanded_config_file = File.expand_path(Puppet.settings[:hiera_config])
if Puppet::FileSystem.exist?(expanded_config_file)
config_file = expanded_config_file
end
elsif Puppet.settings[:confdir].is_a?(String)
expanded_config_file = File.expand_path(File.join(Puppet.settings[:confdir], '/hiera.yaml'))
if Puppet::FileSystem.exist?(expanded_config_file)
config_file = expanded_config_file
end
end
config_file
end
end