coral_core 0.2.19 → 0.2.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,347 +0,0 @@
1
-
2
- module Coral
3
- class Config
4
-
5
- #-----------------------------------------------------------------------------
6
- # Global configuration
7
-
8
- @@options = {}
9
-
10
- #---
11
-
12
- def self.options(contexts, force = true)
13
- options = {}
14
-
15
- unless contexts.is_a?(Array)
16
- contexts = [ contexts ]
17
- end
18
- contexts.each do |name|
19
- if @@options.has_key?(name)
20
- options = Util::Data.merge([ options, @@options[name] ], force)
21
- end
22
- end
23
- return options
24
- end
25
-
26
- #---
27
-
28
- def self.set_options(context, options, force = true)
29
- current_options = ( @@options.has_key?(context) ? @@options[context] : {} )
30
- @@options[context] = Util::Data.merge([ current_options, options ], force)
31
- end
32
-
33
- #---
34
-
35
- def self.clear_options(contexts)
36
- unless contexts.is_a?(Array)
37
- contexts = [ contexts ]
38
- end
39
- contexts.each do |name|
40
- @@options.delete(name)
41
- end
42
- end
43
-
44
- #-----------------------------------------------------------------------------
45
-
46
- @@properties = {}
47
- @@mutex = false
48
-
49
- #---
50
-
51
- def self.properties
52
- return @@properties
53
- end
54
-
55
- #---
56
-
57
- def self.set_property(name, value)
58
- #dbg(value, "result -> #{name}")
59
- @@properties[name] = value
60
- save_properties
61
- end
62
-
63
- #---
64
-
65
- def self.clear_property(name)
66
- @@properties.delete(name)
67
- save_properties
68
- end
69
-
70
- #---
71
-
72
- def self.save_properties
73
- log_options = options('coral_log')
74
-
75
- unless Util::Data.empty?(log_options['config_log'])
76
- config_log = log_options['config_log']
77
-
78
- Util::Disk.write(config_log, JSON.generate(@@properties))
79
- Util::Disk.close(config_log)
80
- end
81
- end
82
-
83
- #-----------------------------------------------------------------------------
84
- # Hiera configuration
85
-
86
- @@hiera = nil
87
-
88
- #---
89
-
90
- def self.hiera_config
91
- config_file = Puppet.settings[:hiera_config]
92
- config = {}
93
-
94
- if File.exist?(config_file)
95
- config = Hiera::Config.load(config_file)
96
- else
97
- Coral.ui.warn("Config file #{config_file} not found, using Hiera defaults")
98
- end
99
-
100
- config[:logger] = 'puppet'
101
- return config
102
- end
103
-
104
- #---
105
-
106
- def self.hiera
107
- @@hiera = Hiera.new(:config => hiera_config) unless @@hiera
108
- return @@hiera
109
- end
110
-
111
- #---
112
-
113
- def hiera
114
- return self.class.hiera
115
- end
116
-
117
- #-----------------------------------------------------------------------------
118
- # Configuration lookup
119
-
120
- def self.initialized?(options = {})
121
- config = Config.ensure(options)
122
- begin
123
- require 'hiera_puppet'
124
-
125
- scope = config.get(:scope, {})
126
-
127
- sep = config.get(:sep, '::')
128
- prefix = config.get(:prefix, true)
129
- prefix_text = prefix ? sep : ''
130
-
131
- init_fact = prefix_text + config.get(:init_fact, 'hiera_ready')
132
- coral_fact = prefix_text + config.get(:coral_fact, 'coral_exists')
133
-
134
- if Puppet::Parser::Functions.function('hiera')
135
- if scope.respond_to?('lookupvar')
136
- return true if Util::Data.true?(scope.lookupvar(init_fact)) && Util::Data.true?(scope.lookupvar(coral_fact))
137
- else
138
- return true
139
- end
140
- end
141
-
142
- rescue Exception # Prevent abortions.
143
- end
144
- return false
145
- end
146
-
147
- #---
148
-
149
- def self.lookup(name, default = nil, options = {})
150
- config = Config.ensure(options)
151
- value = nil
152
-
153
- context = config.get(:context, :priority)
154
- scope = config.get(:scope, {})
155
- override = config.get(:override, nil)
156
-
157
- base_names = config.get(:search, nil)
158
- sep = config.get(:sep, '::')
159
- prefix = config.get(:prefix, true)
160
- prefix_text = prefix ? sep : ''
161
-
162
- search_name = config.get(:search_name, true)
163
-
164
- #dbg(default, "lookup -> #{name}")
165
-
166
- if Config.initialized?(options)
167
- unless scope.respond_to?("[]")
168
- scope = Hiera::Scope.new(scope)
169
- end
170
- value = hiera.lookup(name, default, scope, override, context)
171
- #dbg(value, "hiera -> #{name}")
172
- end
173
-
174
- if Util::Data.undef?(value) && scope.respond_to?('lookupvar')
175
- log_level = Puppet::Util::Log.level
176
- Puppet::Util::Log.level = :err # Don't want failed parameter lookup warnings here.
177
-
178
- if base_names
179
- if base_names.is_a?(String)
180
- base_names = [ base_names ]
181
- end
182
- base_names.each do |item|
183
- value = scope.lookupvar("#{prefix_text}#{item}#{sep}#{name}")
184
- #dbg(value, "#{prefix_text}#{item}#{sep}#{name}")
185
- break unless Util::Data.undef?(value)
186
- end
187
- end
188
- if Util::Data.undef?(value) && search_name
189
- value = scope.lookupvar("#{prefix_text}#{name}")
190
- #dbg(value, "#{prefix_text}#{name}")
191
- end
192
- Puppet::Util::Log.level = log_level
193
- end
194
- value = default if Util::Data.undef?(value)
195
- value = Util::Data.value(value)
196
-
197
- set_property(name, value)
198
-
199
- #dbg(value, "result -> #{name}")
200
- return value
201
- end
202
-
203
- #---
204
-
205
- def self.normalize(data, override = nil, options = {})
206
- config = Config.ensure(options)
207
- results = {}
208
-
209
- unless Util::Data.undef?(override)
210
- case data
211
- when String, Symbol
212
- data = [ data, override ] if data != override
213
- when Array
214
- data << override unless data.include?(override)
215
- when Hash
216
- data = [ data, override ]
217
- end
218
- end
219
-
220
- case data
221
- when String, Symbol
222
- results = Config.lookup(data.to_s, {}, config)
223
-
224
- when Array
225
- data.each do |item|
226
- if item.is_a?(String) || item.is_a?(Symbol)
227
- item = Config.lookup(item.to_s, {}, config)
228
- end
229
- unless Util::Data.undef?(item)
230
- results = Util::Data.merge([ results, item ], config)
231
- end
232
- end
233
-
234
- when Hash
235
- results = data
236
- end
237
-
238
- return results
239
- end
240
-
241
- #-----------------------------------------------------------------------------
242
- # Instance generators
243
-
244
- def self.ensure(config)
245
- case config
246
- when Coral::Config
247
- return config
248
- when Hash
249
- return Config.new(config)
250
- end
251
- return Config.new
252
- end
253
-
254
- #---
255
-
256
- def self.init(options, contexts = [], defaults = {})
257
- config = Coral::Config.new(Coral::Config.options(contexts), defaults)
258
- config.import(options) unless Coral::Util::Data.empty?(options)
259
- return config
260
- end
261
-
262
- #-----------------------------------------------------------------------------
263
- # Configuration instance
264
-
265
- def initialize(data = {}, defaults = {}, force = true)
266
- @force = force
267
- @options = {}
268
-
269
- if defaults.is_a?(Hash) && ! defaults.empty?
270
- symbolized = {}
271
- defaults.each do |key, value|
272
- symbolized[key.to_sym] = value
273
- end
274
- defaults = symbolized
275
- end
276
-
277
- case data
278
- when Coral::Config
279
- @options = Util::Data.merge([ defaults, data.options ], force)
280
- when Hash
281
- @options = {}
282
- if data.is_a?(Hash)
283
- symbolized = {}
284
- data.each do |key, value|
285
- symbolized[key.to_sym] = value
286
- end
287
- @options = Util::Data.merge([ defaults, symbolized ], force)
288
- end
289
- end
290
- end
291
-
292
- #---
293
-
294
- def import(data, options = {})
295
- config = Config.new(options, { :force => @force }).set(:context, :hash)
296
-
297
- case data
298
- when Hash
299
- symbolized = {}
300
- data.each do |key, value|
301
- symbolized[key.to_sym] = value
302
- end
303
- @options = Util::Data.merge([ @options, symbolized ], config)
304
-
305
- when String
306
- data = Util::Data.lookup(data, {}, config)
307
- Util::Data.merge([ @options, data ], config)
308
-
309
- when Array
310
- data.each do |item|
311
- import(item, config)
312
- end
313
- end
314
-
315
- return self
316
- end
317
-
318
- #---
319
-
320
- def set(name, value)
321
- @options[name.to_sym] = value
322
- return self
323
- end
324
-
325
- def []=(name, value)
326
- set(name, value)
327
- end
328
-
329
- #---
330
-
331
- def get(name, default = nil)
332
- name = name.to_sym
333
- return @options[name] if @options.has_key?(name)
334
- return default
335
- end
336
-
337
- def [](name, default = nil)
338
- get(name, default)
339
- end
340
-
341
- #---
342
-
343
- def options
344
- return @options
345
- end
346
- end
347
- end
@@ -1,212 +0,0 @@
1
-
2
- module Coral
3
- class Core
4
-
5
- #-----------------------------------------------------------------------------
6
- # Properties
7
-
8
- @@ui = Interface.new("coral")
9
-
10
- #-----------------------------------------------------------------------------
11
- # Constructor / Destructor
12
-
13
- def initialize(options = {})
14
- config = Config.ensure(options)
15
-
16
- @ui = Interface.new(config)
17
- end
18
-
19
- #-----------------------------------------------------------------------------
20
- # Accessor / Modifiers
21
-
22
- attr_accessor :ui
23
-
24
- #-----------------------------------------------------------------------------
25
-
26
- def self.ui
27
- return @@ui
28
- end
29
-
30
- #---
31
-
32
- def self.logger
33
- return @@ui.logger
34
- end
35
-
36
- #---
37
-
38
- def logger
39
- return self.class.logger
40
- end
41
-
42
- #---
43
-
44
- def logger=logger
45
- self.class.logger = logger
46
- end
47
-
48
- #-----------------------------------------------------------------------------
49
- # General utilities
50
-
51
- def self.symbol_map(data)
52
- results = {}
53
- return data unless data
54
-
55
- case data
56
- when Hash
57
- data.each do |key, value|
58
- results[key.to_sym] = symbol_map(value)
59
- end
60
- else
61
- results = data
62
- end
63
- return results
64
- end
65
-
66
- #---
67
-
68
- def symbol_map(data)
69
- return self.class.symbol_map(data)
70
- end
71
-
72
- #---
73
-
74
- def self.string_map(data)
75
- results = {}
76
- return data unless data
77
-
78
- case data
79
- when Hash
80
- data.each do |key, value|
81
- results[key.to_s] = string_map(value)
82
- end
83
- else
84
- results = data
85
- end
86
- return results
87
- end
88
-
89
- #---
90
-
91
- def string_map(data)
92
- return self.class.string_map(data)
93
- end
94
-
95
- #-----------------------------------------------------------------------------
96
-
97
- def self.filter(data, method = false)
98
- if method && method.is_a?(Symbol) &&
99
- [ :array, :hash, :string, :symbol, :test ].include?(method.to_sym)
100
- return send(method, data)
101
- end
102
- return data
103
- end
104
-
105
- #---
106
-
107
- def filter(data, method = false)
108
- return self.class.filter(data, method)
109
- end
110
-
111
- #-----------------------------------------------------------------------------
112
-
113
- def self.array(data, default = [], split_string = false)
114
- result = default
115
- if data
116
- case data
117
- when Array
118
- result = data
119
- when String
120
- result = [ ( split_string ? data.split(/\s*,\s*/) : data ) ]
121
- else
122
- result = [ data ]
123
- end
124
- end
125
- return result
126
- end
127
-
128
- #---
129
-
130
- def array(data, default = [], split_string = false)
131
- return self.class.array(data, default, split_string)
132
- end
133
-
134
- #---
135
-
136
- def self.hash(data, default = {})
137
- result = default
138
- if data
139
- case data
140
- when Hash
141
- result = data
142
- else
143
- result = {}
144
- end
145
- end
146
- return result
147
- end
148
-
149
- #---
150
-
151
- def hash(data, default = {})
152
- return self.class.hash(data, default)
153
- end
154
-
155
- #---
156
-
157
- def self.string(data, default = '')
158
- result = default
159
- if data
160
- case data
161
- when String
162
- result = data
163
- else
164
- result = data.to_s
165
- end
166
- end
167
- return result
168
- end
169
-
170
- #---
171
-
172
- def string(data, default = '')
173
- return self.class.string(data, default)
174
- end
175
-
176
- #---
177
-
178
- def self.symbol(data, default = :undefined)
179
- result = default
180
- if data
181
- case data
182
- when Symbol
183
- result = data
184
- when String
185
- result = data.to_sym
186
- else
187
- result = data.class.to_sym
188
- end
189
- end
190
- return result
191
- end
192
-
193
- #---
194
-
195
- def symbol(data, default = '')
196
- return self.class.symbol(data, default)
197
- end
198
-
199
- #---
200
-
201
- def self.test(data)
202
- return false if Util::Data.empty?(data)
203
- return true
204
- end
205
-
206
- #---
207
-
208
- def test(data)
209
- return self.class.test(data)
210
- end
211
- end
212
- end
@@ -1,55 +0,0 @@
1
-
2
- module Coral
3
- class RegexpEvent < Event
4
-
5
- #-----------------------------------------------------------------------------
6
- # Properties
7
-
8
- TYPE = :regexp
9
-
10
- #-----------------------------------------------------------------------------
11
- # Constructor / Destructor
12
-
13
- def initialize(options = {})
14
- options[:type] = TYPE
15
-
16
- super(options)
17
-
18
- if options.has_key?(:string)
19
- self.pattern = options[:string]
20
- end
21
- end
22
-
23
- #-----------------------------------------------------------------------------
24
- # Property accessors / modifiers
25
-
26
- def pattern
27
- return property(:pattern, '', :string)
28
- end
29
-
30
- #---
31
-
32
- def pattern=pattern
33
- set_property(:pattern, string(pattern))
34
- end
35
-
36
- #-----------------------------------------------------------------------------
37
- # Import / Export
38
-
39
- def export
40
- return "#{type}:#{pattern}"
41
- end
42
-
43
- #-----------------------------------------------------------------------------
44
- # Event handling
45
-
46
- def check(source)
47
- if source.match(/#{pattern}/)
48
- logger.debug("MATCH! -> #{pattern} matched #{source}")
49
- return true
50
- end
51
- logger.debug("nothing -> #{pattern} - #{source}")
52
- return false
53
- end
54
- end
55
- end