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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
require 'json/pure'
require 'yaml'
module LeapCli
module Config
#
# A class to manage all the objects in all the configuration files.
#
class Manager
attr_reader :services, :tags, :nodes, :provider
##
## IMPORT EXPORT
##
#
# load .json configuration files
#
def load(provider_dir=Path.provider)
@services = load_all_json(Path.named_path( [:service_config, '*'], provider_dir ))
@tags = load_all_json(Path.named_path( [:tag_config, '*'], provider_dir ))
@common = load_all_json(Path.named_path( :common_config, provider_dir ))['common']
@provider = load_all_json(Path.named_path( :provider_config, provider_dir ))['provider']
@nodes = load_all_json(Path.named_path( [:node_config, '*'], provider_dir ))
Util::assert!(@provider, "Failed to load provider.json")
Util::assert!(@common, "Failed to load common.json")
@nodes.each do |name, node|
@nodes[name] = apply_inheritance(node)
end
end
#
# save compiled hiera .yaml files
#
def export(dir=Path.named_path(:hiera_dir))
existing_files = Dir.glob(dir + '/*.yaml')
updated_files = []
@nodes.each do |name, node|
filepath = "#{dir}/#{name}.yaml"
updated_files << filepath
Util::write_file!(filepath, node.to_yaml)
end
(existing_files - updated_files).each do |filepath|
Util::remove_file!(filepath)
end
end
##
## FILTERING
##
#
# returns a node list consisting only of nodes that satisfy the filter criteria.
#
# filter: condition [condition] [condition] [+condition]
# condition: [node_name | service_name | tag_name]
#
# if conditions is prefixed with +, then it works like an AND. Otherwise, it works like an OR.
#
def filter(filters)
if filters.empty?
return nodes
end
if filters[0] =~ /^\+/
# don't let the first filter have a + prefix
filters[0] = filters[0][1..-1]
end
node_list = Config::ObjectList.new
filters.each do |filter|
if filter =~ /^\+/
keep_list = nodes_for_name(filter[1..-1])
node_list.delete_if do |name, node|
if keep_list[name]
false
else
true
end
end
else
node_list.merge!(nodes_for_name(filter))
end
end
return node_list
end
#
# same as filter(), but exits if there is no matching nodes
#
def filter!(filters)
node_list = filter(filters)
Util::assert! node_list.any?, "Could not match any nodes from '#{filters}'"
return node_list
end
#
# returns a single Config::Object that corresponds to a Node.
#
def node(name)
nodes[name]
end
private
def load_all_json(pattern, config_type = :class)
results = Config::ObjectList.new
Dir.glob(pattern).each do |filename|
obj = load_json(filename, config_type)
if obj
name = File.basename(filename).sub(/\.json$/,'')
obj['name'] ||= name
results[name] = obj
end
end
results
end
def load_json(filename, config_type)
#log2 { filename.sub(/^#{Regexp.escape(Path.root)}/,'') }
#
# read file, strip out comments
# (File.read(filename) would be faster, but we like ability to have comments)
#
buffer = StringIO.new
File.open(filename) do |f|
while (line = f.gets)
next if line =~ /^\s*#/
buffer << line
end
end
# parse json, and flatten hash
begin
#hash = Oj.load(buffer.string) || {}
hash = JSON.parse(buffer.string, :object_class => Hash, :array_class => Array) || {}
rescue SyntaxError => exc
log0 'Error in file "%s":' % filename
log0 exc.to_s
return nil
end
object = Config::Object.new(self)
object.deep_merge!(hash)
return object
end
#
# remove all the nesting from a hash.
#
# def flatten_hash(input = {}, output = {}, options = {})
# input.each do |key, value|
# key = options[:prefix].nil? ? "#{key}" : "#{options[:prefix]}#{options[:delimiter]||"_"}#{key}"
# if value.is_a? Hash
# flatten_hash(value, output, :prefix => key, :delimiter => options[:delimiter])
# else
# output[key] = value
# end
# end
# output.replace(input)
# output
# end
#
# makes a node inherit options from appropriate the common, service, and tag json files.
#
def apply_inheritance(node)
new_node = Config::Object.new(self)
name = node.name
# inherit from common
new_node.deep_merge!(@common)
# inherit from services
if node['services']
node['services'].to_a.sort.each do |node_service|
service = @services[node_service]
if service.nil?
log0('Error in node "%s": the service "%s" does not exist.' % [node['name'], node_service])
else
new_node.deep_merge!(service)
service.node_list.add(name, new_node)
end
end
end
# inherit from tags
if node['tags']
node['tags'].to_a.sort.each do |node_tag|
tag = @tags[node_tag]
if tag.nil?
log0('Error in node "%s": the tag "%s" does not exist.' % [node['name'], node_tag])
else
new_node.deep_merge!(tag)
tag.node_list.add(name, new_node)
end
end
end
# inherit from node
new_node.deep_merge!(node)
return new_node
end
#
# returns a set of nodes corresponding to a single name, where name could be a node name, service name, or tag name.
#
def nodes_for_name(name)
if node = self.nodes[name]
Config::ObjectList.new(node)
elsif service = self.services[name]
service.node_list
elsif tag = self.tags[name]
tag.node_list
end
end
end
end
end
|