hiera-router 0.3.3 → 0.3.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/hiera/backend/router_backend.rb +163 -163
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46665fadd9ea12e6890a66c4525b2d86ab21487c
|
4
|
+
data.tar.gz: 79f78ce15b8018a4652b26f0b502b4918a813589
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de1dcade846df314da397119fb5a519d267288737037e1492dafd6adf13d4f914c5d309687bec760e4445e6c87598de2ce6bb21bc0875f131377232d813b8d31
|
7
|
+
data.tar.gz: 08cd3d6d6bd6e0ccc81ec6853fb2c50f984b2d8dfa3b5d3d73a69d2b46c34728d5cd58096d7c80b9727ce8017015683d5fa1d7127b9efda2d73d896d51feaf36
|
@@ -2,167 +2,167 @@ require 'hiera/filecache'
|
|
2
2
|
require 'yaml'
|
3
3
|
|
4
4
|
class Hiera
|
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
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
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
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
5
|
+
class Config
|
6
|
+
class << self
|
7
|
+
def config
|
8
|
+
@config
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module Backend
|
14
|
+
class Router_backend
|
15
|
+
attr_reader :backends
|
16
|
+
attr_accessor :config
|
17
|
+
|
18
|
+
def initialize(cache = nil)
|
19
|
+
@cache = cache || Filecache.new
|
20
|
+
@backends = {}
|
21
|
+
Hiera.debug("[hiera-router] I'm here!")
|
22
|
+
self.config = Config.config
|
23
|
+
|
24
|
+
if backend_list = Config[:router][:backends]
|
25
|
+
Hiera.debug("[hiera-router] Initializing backends: #{backend_list.keys.join(',')}")
|
26
|
+
backend_list.each do |backend, backend_config|
|
27
|
+
Hiera.debug("[hiera-router] Initializing backend '#{backend}'")
|
28
|
+
backend_classname = backend_config['backend_class'] || backend_config[:backend_class] || backend
|
29
|
+
full_backend_classname = "#{backend_classname.capitalize}_backend"
|
30
|
+
backend_config_override = backend_config['backend_key'] || backend_config[:backend_key] || backend_classname
|
31
|
+
Hiera.debug("[hiera-router] Backend class for '#{backend}' will be '#{backend_classname}'")
|
32
|
+
|
33
|
+
backend_config_override_config = Config[backend_config_override.to_sym] || Config[:router][backend_config_override.to_sym] || {}
|
34
|
+
|
35
|
+
backend_config = self.config.clone
|
36
|
+
backend_config.delete(:router)
|
37
|
+
if backend_config_override_config[:hierarchy]
|
38
|
+
backend_config[:hierarchy] = backend_config_override_config[:hierarchy]
|
39
|
+
end
|
40
|
+
|
41
|
+
backend_config[:backends] = [backend_classname]
|
42
|
+
backend_config[backend_classname.to_sym] = backend_config_override_config
|
43
|
+
|
44
|
+
Config.load(backend_config)
|
45
|
+
require "hiera/backend/#{full_backend_classname.downcase}"
|
46
|
+
backend_inst = Hiera::Backend.const_get(full_backend_classname).new
|
47
|
+
Config.load(config)
|
48
|
+
@backends[backend.to_sym] = {
|
49
|
+
:instance => backend_inst,
|
50
|
+
:config => backend_config,
|
51
|
+
}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
Hiera.debug("[hiera-router] hiera router initialized")
|
56
|
+
end
|
57
|
+
def lookup(key, scope, order_override, resolution_type)
|
58
|
+
options = {
|
59
|
+
:key => key,
|
60
|
+
:scope => scope,
|
61
|
+
:order_override => order_override,
|
62
|
+
:resolution_type => resolution_type,
|
63
|
+
}
|
64
|
+
answer = nil
|
65
|
+
|
66
|
+
Hiera.debug("[hiera-router] Looking up #{key} in yaml backend")
|
67
|
+
|
68
|
+
Backend.datasources(scope, order_override) do |source|
|
69
|
+
yaml_file = Backend.datafile(:router, scope, source, 'yaml') || next
|
70
|
+
|
71
|
+
data = @cache.read(yaml_file, Hash) do |cached_data|
|
72
|
+
begin
|
73
|
+
Hiera.debug("[hiera-router] Looking + loading data source #{source} ('#{yaml_file}')")
|
74
|
+
YAML.load(cached_data) || {}
|
75
|
+
rescue
|
76
|
+
Hiera.debug("[hiera-router] something wrong with source #{source} '#{yaml_file}' -- returning an empty result")
|
77
|
+
{}
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
next if data.empty?
|
82
|
+
next unless data.include?(key)
|
83
|
+
|
84
|
+
Hiera.debug("[hiera-router] Found #{key} in #{source}")
|
85
|
+
|
86
|
+
new_answer = parse_answer(data[key], scope, options)
|
87
|
+
next if new_answer.nil?
|
88
|
+
|
89
|
+
case resolution_type
|
90
|
+
when :array
|
91
|
+
raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
|
92
|
+
answer ||= []
|
93
|
+
answer << new_answer
|
94
|
+
when :hash
|
95
|
+
raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
|
96
|
+
answer ||= {}
|
97
|
+
answer = Backend.merge_answer(new_answer,answer)
|
98
|
+
else
|
99
|
+
answer = new_answer
|
100
|
+
break
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
return answer
|
105
|
+
end
|
106
|
+
|
107
|
+
def recursive_key_from_hash(hash, path)
|
108
|
+
focus = hash
|
109
|
+
path.each do |key|
|
110
|
+
if focus.is_a?(Hash) and focus.include?(key)
|
111
|
+
focus = focus[key]
|
112
|
+
else
|
113
|
+
return nil
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
return focus
|
118
|
+
end
|
119
|
+
|
120
|
+
def parse_answer(data, scope, options, path = [])
|
121
|
+
if data.is_a?(Numeric) or data.is_a?(TrueClass) or data.is_a?(FalseClass)
|
122
|
+
return data
|
123
|
+
elsif data.is_a?(String)
|
124
|
+
return parse_string(data, scope, options, path)
|
125
|
+
elsif data.is_a?(Hash)
|
126
|
+
answer = {}
|
127
|
+
data.each_pair do |key, val|
|
128
|
+
interpolated_key = Backend.parse_string(key, scope)
|
129
|
+
subpath = path + [interpolated_key]
|
130
|
+
answer[interpolated_key] = parse_answer(val, scope, options, subpath)
|
131
|
+
end
|
132
|
+
|
133
|
+
return answer
|
134
|
+
elsif data.is_a?(Array)
|
135
|
+
answer = []
|
136
|
+
data.each do |item|
|
137
|
+
answer << parse_answer(item, scope, options)
|
138
|
+
end
|
139
|
+
|
140
|
+
return answer
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
def parse_string(data, scope, options, path = [])
|
145
|
+
if match = data.match(/^backend\[([^,]+)(?:,(.*))?\]$/)
|
146
|
+
backend_name, backend_parameters = match.captures
|
147
|
+
backend_options = options
|
148
|
+
backend_options = backend_options.merge(backend_parameters) if backend_parameters
|
149
|
+
Hiera.debug("[hiera-router] Calling hiera with '#{backend_name}'...")
|
150
|
+
if backend = self.backends[backend_name.to_sym]
|
151
|
+
backend_instance = backend[:instance]
|
152
|
+
Hiera.debug("[hiera-router] Backend class: #{backend_instance.class.name}")
|
153
|
+
Config.load(backend[:config])
|
154
|
+
result = backend_instance.lookup(backend_options[:key], backend_options[:scope], nil, backend_options[:resolution_type])
|
155
|
+
Config.load(self.config)
|
156
|
+
else
|
157
|
+
Hiera.warn "Backend '#{backend_name}' was not configured; returning the data as-is."
|
158
|
+
result = data
|
159
|
+
end
|
160
|
+
Hiera.debug("[hiera-router] Call to '#{backend_name}' finished.")
|
161
|
+
return recursive_key_from_hash(result, path)
|
162
|
+
else
|
163
|
+
Backend.parse_string(data, scope)
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
168
|
end
|