coral_core 0.2.26 → 0.2.30

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.
Files changed (46) hide show
  1. data/Gemfile +7 -2
  2. data/Gemfile.lock +84 -15
  3. data/VERSION +1 -1
  4. data/coral_core.gemspec +51 -16
  5. data/lib/{coral_core/command.rb → coral/command/shell.rb} +12 -116
  6. data/lib/coral/machine/fog.rb +215 -0
  7. data/lib/coral/network/default.rb +26 -0
  8. data/lib/coral/node/rackspace.rb +23 -0
  9. data/lib/coral_core.rb +249 -134
  10. data/lib/coral_core/config.rb +233 -275
  11. data/lib/coral_core/config/collection.rb +57 -0
  12. data/lib/coral_core/config/options.rb +70 -0
  13. data/lib/coral_core/config/project.rb +225 -0
  14. data/lib/coral_core/core.rb +19 -173
  15. data/lib/coral_core/event/puppet_event.rb +98 -0
  16. data/lib/coral_core/mixin/config_collection.rb +52 -0
  17. data/lib/coral_core/mixin/config_ops.rb +51 -0
  18. data/lib/coral_core/mixin/config_options.rb +38 -0
  19. data/lib/coral_core/mixin/lookup.rb +211 -0
  20. data/lib/coral_core/mixin/macro/object_interface.rb +292 -0
  21. data/lib/coral_core/mixin/macro/plugin_interface.rb +277 -0
  22. data/lib/coral_core/mixin/settings.rb +46 -0
  23. data/lib/coral_core/mixin/sub_config.rb +208 -0
  24. data/lib/coral_core/mod/hash.rb +29 -0
  25. data/lib/{hiera_backend.rb → coral_core/mod/hiera_backend.rb} +0 -0
  26. data/lib/coral_core/plugin.rb +261 -0
  27. data/lib/coral_core/plugin/command.rb +95 -0
  28. data/lib/coral_core/plugin/machine.rb +152 -0
  29. data/lib/coral_core/plugin/network.rb +24 -0
  30. data/lib/coral_core/plugin/node.rb +184 -0
  31. data/lib/coral_core/plugin_base.rb +147 -0
  32. data/lib/coral_core/repository.rb +471 -82
  33. data/lib/coral_core/util/cli.rb +293 -0
  34. data/lib/coral_core/util/data.rb +178 -8
  35. data/lib/coral_core/util/disk.rb +13 -0
  36. data/lib/coral_core/util/git.rb +35 -10
  37. data/lib/coral_core/{interface.rb → util/interface.rb} +31 -21
  38. data/lib/coral_core/util/process.rb +43 -0
  39. data/locales/en.yml +8 -0
  40. data/spec/coral_core/interface_spec.rb +45 -45
  41. metadata +109 -34
  42. data/.gitmodules +0 -12
  43. data/lib/coral_core/memory.rb +0 -226
  44. data/lib/coral_core/util/git/base.rb +0 -65
  45. data/lib/coral_core/util/git/lib.rb +0 -89
  46. data/lib/coral_core/util/git/remote.rb +0 -12
@@ -0,0 +1,293 @@
1
+ module Coral
2
+ module Util
3
+ module CLI
4
+
5
+ #-------------------------------------------------------------------------
6
+ # Utilities
7
+
8
+ def self.message(name, default = nil)
9
+ if default.nil?
10
+ default = :none
11
+ end
12
+ return I18n.t(name.to_s, :default_value => default.to_s)
13
+ end
14
+
15
+ #-------------------------------------------------------------------------
16
+ # Parser
17
+
18
+ class Parser
19
+
20
+ attr_accessor :parser
21
+ attr_accessor :options
22
+ attr_accessor :arguments
23
+ attr_accessor :processed
24
+
25
+ #---
26
+
27
+ def initialize(args, banner = '', help = '')
28
+
29
+ @parser = OptionParser.new
30
+
31
+ self.options = {}
32
+ self.arguments = {}
33
+ self.processed = false
34
+
35
+ @arg_settings = []
36
+
37
+ self.banner = banner
38
+ self.help = help
39
+
40
+ yield(self) if block_given?
41
+
42
+ parse_command(args)
43
+ end
44
+
45
+ #---
46
+
47
+ def self.split(args, banner, separator = '')
48
+ main_args = nil
49
+ sub_command = nil
50
+ sub_args = []
51
+
52
+ args.each_index do |index|
53
+ if !args[index].start_with?('-')
54
+ main_args = args[0, index]
55
+ sub_command = args[index]
56
+ sub_args = args[index + 1, args.length - index + 1]
57
+ break
58
+ end
59
+ end
60
+
61
+ main_args = args.dup if main_args.nil?
62
+ results = [ Parser.new(main_args, banner, separator) ]
63
+
64
+ if sub_command
65
+ results << [ sub_command, sub_args ]
66
+ end
67
+
68
+ return results.flatten
69
+ end
70
+
71
+ #---
72
+
73
+ def banner=banner
74
+ parser.banner = banner
75
+ end
76
+
77
+ #---
78
+
79
+ def help=help
80
+ if help.is_a?(Array)
81
+ help.each do |line|
82
+ parser.separator line
83
+ end
84
+ else
85
+ parser.separator help
86
+ end
87
+ end
88
+
89
+ #---
90
+
91
+ def parse_command(args)
92
+ args = args.dup
93
+ error = false
94
+
95
+ self.processed = false
96
+
97
+ parser.on_tail('-h', '--help', CLI.message('coral.core.util.cli.options.help')) do
98
+ puts parser.help.chomp
99
+ options[:help] = true
100
+ return
101
+ end
102
+
103
+ parser.parse!(args)
104
+
105
+ @arg_settings.each_with_index do |settings, index|
106
+ if index >= args.length
107
+ value = nil
108
+ else
109
+ value = Util::Data.value(args[index])
110
+ end
111
+
112
+ if !value.nil? && settings.has_key?(:allowed)
113
+ allowed = settings[:allowed]
114
+ case allowed
115
+ when Class
116
+ unless value.is_a?(allowed)
117
+ Coral.ui.error(CLI.message(settings[:message]))
118
+ error = true
119
+ end
120
+ when Array
121
+ unless allowed.include(value)
122
+ Coral.ui.error(CLI.message(settings[:message]))
123
+ error = true
124
+ end
125
+ end
126
+ end
127
+
128
+ if value.nil?
129
+ if settings.has_key?(:default)
130
+ value = settings[:default]
131
+ else
132
+ error = true
133
+ end
134
+ end
135
+
136
+ if !value.nil? && settings.has_key?(:block)
137
+ value = settings[:block].call(value)
138
+ error = true if value.nil?
139
+ end
140
+
141
+ break if error
142
+ self.arguments[settings[:name]] = value
143
+ end
144
+
145
+ if error
146
+ Coral.ui.error(CLI.message('coral.util.cli.parse.error'))
147
+ puts parser.help.chomp
148
+ else
149
+ self.processed = true
150
+ end
151
+
152
+ rescue OptionParser::InvalidOption
153
+ raise Errors::CLIInvalidOptions, :help => parser.help.chomp
154
+ end
155
+
156
+ #---
157
+
158
+ def option(name, default, option_str, allowed_values, message_id, config = {})
159
+ config = Config.ensure(config)
160
+ name = name.to_sym
161
+ options[name] = config.get(name, default)
162
+
163
+ message_name = name.to_s + '_message'
164
+ message = CLI.message(message_id, options[name])
165
+
166
+ option_str = Util::Data.array(option_str)
167
+
168
+ if allowed_values
169
+ parser.on(*option_str, allowed_values, config.get(message_name.to_sym, message)) do |value|
170
+ value = yield(value) if block_given?
171
+ options[name] = value unless value.nil?
172
+ end
173
+ else
174
+ parser.on(*option_str, config.get(message_name.to_sym, message)) do |value|
175
+ value = yield(value) if block_given?
176
+ options[name] = value unless value.nil?
177
+ end
178
+ end
179
+ end
180
+
181
+ #---
182
+
183
+ def arg(name, default, allowed_values, message_id, config = {}, &block)
184
+ config = Config.ensure(config)
185
+ name = name.to_sym
186
+
187
+ message_name = name.to_s + '_message'
188
+ message = CLI.message(message_id, arguments[name])
189
+
190
+ settings = {
191
+ :name => name,
192
+ :default => config.get(name, default),
193
+ :message => config.get(message_name.to_sym, message)
194
+ }
195
+ settings[:allowed] = allowed_values if allowed_values
196
+ settings[:block] = block if block
197
+
198
+ @arg_settings << settings
199
+ end
200
+
201
+ #---
202
+
203
+ def option_bool(name, default, option_str, message_id, config = {})
204
+ option(name, default, option_str, nil, message_id, config) do |value|
205
+ value = Util::Data.value(value)
206
+ if value == true || value == false
207
+ block_given? ? yield(value) : value
208
+ else
209
+ nil
210
+ end
211
+ end
212
+ end
213
+
214
+ #---
215
+
216
+ def arg_bool(name, default, message_id, config = {})
217
+ arg(name, default, nil, message_id, config) do |value|
218
+ value = Util::Data.value(value)
219
+ if value == true || value == false
220
+ block_given? ? yield(value) : value
221
+ else
222
+ nil
223
+ end
224
+ end
225
+ end
226
+
227
+ #---
228
+
229
+ def option_int(name, default, option_str, message_id, config = {})
230
+ option(name, default, option_str, Integer, message_id, config) do |value|
231
+ block_given? ? yield(value) : value
232
+ end
233
+ end
234
+
235
+ #---
236
+
237
+ def arg_int(name, default, message_id, config = {})
238
+ arg(name, default, Integer, message_id, config) do |value|
239
+ block_given? ? yield(value) : value
240
+ end
241
+ end
242
+
243
+ #---
244
+
245
+ def option_float(name, default, option_str, message_id, config = {})
246
+ option(name, default, option_str, Float, message_id, config) do |value|
247
+ block_given? ? yield(value) : value
248
+ end
249
+ end
250
+
251
+ #---
252
+
253
+ def arg_float(name, default, message_id, config = {})
254
+ arg(name, default, Float, message_id, config) do |value|
255
+ block_given? ? yield(value) : value
256
+ end
257
+ end
258
+
259
+ #---
260
+
261
+ def option_str(name, default, option_str, message_id, config = {})
262
+ option(name, default, option_str, nil, message_id, config) do |value|
263
+ block_given? ? yield(value) : value
264
+ end
265
+ end
266
+
267
+ #---
268
+
269
+ def arg_str(name, default, message_id, config = {})
270
+ arg(name, default, nil, message_id, config) do |value|
271
+ block_given? ? yield(value) : value
272
+ end
273
+ end
274
+
275
+ #---
276
+
277
+ def option_array(name, default, option_str, message_id, config = {})
278
+ option(name, default, option_str, Array, message_id, config) do |value|
279
+ block_given? ? yield(value) : value
280
+ end
281
+ end
282
+
283
+ #---
284
+
285
+ def arg_array(name, default, message_id, config = {})
286
+ arg(name, default, Array, message_id, config) do |value|
287
+ block_given? ? yield(value) : value
288
+ end
289
+ end
290
+ end
291
+ end
292
+ end
293
+ end
@@ -43,14 +43,72 @@ class Data
43
43
  end
44
44
  return false
45
45
  end
46
+
47
+ #---
48
+
49
+ def self.exists?(data, keys, check_empty = false)
50
+ keys = [ keys ] unless keys.is_a?(Array)
51
+ keys.each do |key|
52
+ return false unless data.is_a?(Hash) && data.has_key?(key)
53
+ return false if check_empty && empty?(data[key])
54
+ data = data[key]
55
+ end
56
+ return true
57
+ end
46
58
 
47
59
  #-----------------------------------------------------------------------------
48
60
  # Translation
61
+
62
+ def self.symbol_map(data)
63
+ results = {}
64
+ return data unless data
65
+
66
+ case data
67
+ when Hash
68
+ data.each do |key, value|
69
+ results[key.to_sym] = symbol_map(value)
70
+ end
71
+ else
72
+ results = data
73
+ end
74
+ return results
75
+ end
76
+
77
+ #---
78
+
79
+ def self.string_map(data)
80
+ results = {}
81
+ return data unless data
82
+
83
+ case data
84
+ when Hash
85
+ data.each do |key, value|
86
+ results[key.to_s] = string_map(value)
87
+ end
88
+ else
89
+ results = data
90
+ end
91
+ return results
92
+ end
93
+
94
+ #---
49
95
 
50
- def self.to_json(data)
96
+ def self.parse_json(json_text)
51
97
  output = ''
52
98
  begin
53
- output = data.to_json
99
+ output = MultiJson.load(json_text)
100
+
101
+ rescue Exception
102
+ end
103
+ return output
104
+ end
105
+
106
+ #---
107
+
108
+ def self.to_json(data, pretty = true)
109
+ output = ''
110
+ begin
111
+ output = MultiJson.dump(data, :pretty => pretty)
54
112
 
55
113
  rescue Exception
56
114
  end
@@ -59,10 +117,21 @@ class Data
59
117
 
60
118
  #---
61
119
 
120
+ def self.parse_yaml(yaml_text)
121
+ output = ''
122
+ begin
123
+ output = YAML.load(yaml_text)
124
+
125
+ rescue Exception
126
+ end
127
+ return output
128
+ end
129
+
130
+ #---
131
+
62
132
  def self.to_yaml(data)
63
133
  output = ''
64
134
  begin
65
- require 'yaml'
66
135
  output = YAML.dump(data)
67
136
 
68
137
  rescue Exception
@@ -95,7 +164,88 @@ class Data
95
164
  end
96
165
  return value
97
166
  end
98
-
167
+
168
+ #---
169
+
170
+ def self.filter(data, method = false)
171
+ if method && method.is_a?(Symbol) &&
172
+ [ :array, :hash, :string, :symbol, :test ].include?(method.to_sym)
173
+ return send(method, data)
174
+ end
175
+ return data
176
+ end
177
+
178
+ #---
179
+
180
+ def self.array(data, default = [], split_string = false)
181
+ result = default
182
+ if data
183
+ case data
184
+ when Array
185
+ result = data
186
+ when String
187
+ result = [ ( split_string ? data.split(/\s*,\s*/) : data ) ]
188
+ else
189
+ result = [ data ]
190
+ end
191
+ end
192
+ return result
193
+ end
194
+
195
+ #---
196
+
197
+ def self.hash(data, default = {})
198
+ result = default
199
+ if data
200
+ case data
201
+ when Hash
202
+ result = data
203
+ else
204
+ result = {}
205
+ end
206
+ end
207
+ return result
208
+ end
209
+
210
+ #---
211
+
212
+ def self.string(data, default = '')
213
+ result = default
214
+ if data
215
+ case data
216
+ when String
217
+ result = data
218
+ else
219
+ result = data.to_s
220
+ end
221
+ end
222
+ return result
223
+ end
224
+
225
+ #---
226
+
227
+ def self.symbol(data, default = :undefined)
228
+ result = default
229
+ if data
230
+ case data
231
+ when Symbol
232
+ result = data
233
+ when String
234
+ result = data.to_sym
235
+ else
236
+ result = data.class.to_sym
237
+ end
238
+ end
239
+ return result
240
+ end
241
+
242
+ #---
243
+
244
+ def self.test(data)
245
+ return false if Util::Data.empty?(data)
246
+ return true
247
+ end
248
+
99
249
  #-----------------------------------------------------------------------------
100
250
  # Operations
101
251
 
@@ -107,10 +257,10 @@ class Data
107
257
  force = force.is_a?(Coral::Config) ? force.get(:force, true) : force
108
258
 
109
259
  if data.is_a?(Array)
110
- value = data.shift.clone
260
+ value = undef?(data[0]) ? nil : data.shift.clone
111
261
 
112
262
  data.each do |item|
113
- item = item.clone
263
+ item = undef?(item) ? nil : item.clone
114
264
 
115
265
  case value
116
266
  when Hash
@@ -132,8 +282,8 @@ class Data
132
282
  value = item
133
283
  end
134
284
 
135
- when String, Symbol
136
- value = item if item.is_a?(String) || item.is_a?(Symbol) || force
285
+ else
286
+ value = item if force || item.is_a?(String) || item.is_a?(Symbol)
137
287
  end
138
288
  end
139
289
  end
@@ -214,6 +364,26 @@ class Data
214
364
  end
215
365
  return result
216
366
  end
367
+
368
+ #---
369
+
370
+ def self.ensure(test, success_value = nil, failure_value = nil)
371
+ success_value = (success_value ? success_value : test)
372
+ failure_value = (failure_value ? failure_value : nil)
373
+
374
+ if empty?(test)
375
+ value = failure_value
376
+ else
377
+ value = success_value
378
+ end
379
+ return value
380
+ end
381
+
382
+ #---
383
+
384
+ def self.ensure_value(value, failure_value = nil)
385
+ return self.ensure(value, nil, failure_value)
386
+ end
217
387
  end
218
388
  end
219
389
  end