lorj 0.2.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -0
  3. data/.rubocop.yml +46 -0
  4. data/Gemfile +15 -15
  5. data/README.md +22 -17
  6. data/Rakefile +12 -2
  7. data/bin/cloud_test.rb +13 -65
  8. data/example/students_1/process/students.rb +39 -0
  9. data/example/students_1/students.rb +22 -5
  10. data/example/students_2/process/students.rb +48 -0
  11. data/example/students_2/students.rb +39 -16
  12. data/example/students_3/controller/yaml_students.rb +50 -43
  13. data/example/students_3/controller/yaml_students_controller.rb +100 -87
  14. data/example/students_3/process/students.rb +161 -97
  15. data/example/students_3/students.rb +85 -56
  16. data/example/yaml_students/students.rb +40 -40
  17. data/example/yaml_students/yaml_students.rb +103 -90
  18. data/lib/core/core.rb +356 -696
  19. data/lib/core/core_controller.rb +227 -0
  20. data/lib/core/core_internal.rb +339 -0
  21. data/lib/core/core_model.rb +328 -0
  22. data/lib/core/core_object_data.rb +330 -0
  23. data/lib/core/core_object_params.rb +230 -0
  24. data/lib/core/core_process.rb +391 -0
  25. data/lib/core/core_process_setup.rb +353 -0
  26. data/lib/core/core_setup_ask.rb +241 -0
  27. data/lib/core/core_setup_encrypt.rb +146 -0
  28. data/lib/core/core_setup_init.rb +229 -0
  29. data/lib/core/core_setup_list.rb +160 -0
  30. data/lib/core/definition.rb +647 -469
  31. data/lib/core/definition_internal.rb +264 -308
  32. data/lib/core/lorj_basecontroller.rb +95 -0
  33. data/lib/core/lorj_basedefinition.rb +307 -0
  34. data/lib/core/lorj_baseprocess.rb +265 -0
  35. data/lib/core/lorj_data.rb +583 -0
  36. data/lib/core/lorj_keypath.rb +119 -0
  37. data/lib/core_process/cloud/process/common.rb +63 -0
  38. data/lib/core_process/cloud/process/connection.rb +93 -0
  39. data/lib/core_process/cloud/process/external_network.rb +94 -0
  40. data/lib/core_process/cloud/process/flavor.rb +99 -0
  41. data/lib/core_process/cloud/process/images.rb +87 -0
  42. data/lib/core_process/cloud/process/internet_network.rb +34 -0
  43. data/lib/core_process/cloud/process/internet_server.rb +30 -0
  44. data/lib/core_process/cloud/process/keypairs.rb +276 -0
  45. data/lib/core_process/cloud/process/network.rb +108 -0
  46. data/lib/core_process/cloud/process/public_ip.rb +100 -0
  47. data/lib/core_process/cloud/process/router.rb +260 -0
  48. data/lib/core_process/cloud/process/rules.rb +120 -0
  49. data/lib/core_process/cloud/process/security_groups.rb +121 -0
  50. data/lib/core_process/cloud/process/server.rb +127 -0
  51. data/lib/core_process/cloud/process/server_log.rb +35 -0
  52. data/lib/core_process/cloud/process/subnetwork.rb +108 -0
  53. data/lib/core_process/cloud_process.rb +30 -0
  54. data/lib/logging.rb +298 -0
  55. data/lib/lorj/version.rb +18 -1
  56. data/lib/lorj.rb +58 -18
  57. data/lib/lorj_account.rb +556 -0
  58. data/lib/lorj_config.rb +468 -0
  59. data/lib/lorj_defaults.rb +278 -0
  60. data/lib/prc.rb +136 -104
  61. data/lib/prc_base_config.rb +285 -0
  62. data/lib/prc_core_config.rb +878 -0
  63. data/lib/prc_section_config.rb +57 -0
  64. data/lib/providers/hpcloud/compute.rb +81 -93
  65. data/lib/providers/hpcloud/hpcloud.rb +462 -0
  66. data/lib/providers/hpcloud/network.rb +96 -98
  67. data/lib/providers/hpcloud/security_groups.rb +41 -40
  68. data/lib/providers/mock/mock.rb +144 -0
  69. data/lib/providers/openstack/openstack.rb +45 -0
  70. data/lib/providers/templates/compute.rb +21 -23
  71. data/lib/providers/templates/mycloud.rb +72 -0
  72. data/lib/providers/templates/network.rb +11 -12
  73. data/lib/rh.rb +339 -0
  74. data/lorj-spec/defaults.yaml +4 -0
  75. data/lorj.gemspec +6 -0
  76. data/spec/00_lorj_log_spec.rb +53 -0
  77. data/spec/01_hash_rh_spec.rb +243 -0
  78. data/spec/02_prc_base_config_spec.rb +216 -0
  79. data/spec/04_prc_core_config_spec.rb +83 -0
  80. data/spec/11_lorj_config_spec.rb +263 -0
  81. data/spec/12_lorj_account_spec.rb +181 -0
  82. metadata +76 -28
  83. data/Gemfile.lock +0 -37
  84. data/example/students_1/process/Students.rb +0 -20
  85. data/example/students_2/process/Students.rb +0 -27
  86. data/example/students_4/controller/yaml_students.rb +0 -82
  87. data/example/students_4/controller/yaml_students_controller.rb +0 -141
  88. data/example/students_4/process/students.rb +0 -112
  89. data/example/students_4/students.rb +0 -103
  90. data/lib/core/lorj-basecontroller.rb +0 -90
  91. data/lib/core/lorj-basedefinition.rb +0 -1139
  92. data/lib/core/lorj-baseprocess.rb +0 -236
  93. data/lib/core/lorj-data.rb +0 -567
  94. data/lib/core/lorj-keypath.rb +0 -115
  95. data/lib/core_process/CloudProcess.rb +0 -337
  96. data/lib/core_process/global_process.rb +0 -502
  97. data/lib/core_process/network_process.rb +0 -605
  98. data/lib/prc-account.rb +0 -339
  99. data/lib/prc-config.rb +0 -1030
  100. data/lib/prc-logging.rb +0 -261
  101. data/lib/providers/hpcloud/Hpcloud.rb +0 -426
  102. data/lib/providers/mock/Mock.rb +0 -141
  103. data/lib/providers/openstack/Openstack.rb +0 -47
  104. data/lib/providers/templates/core.rb +0 -61
  105. data/spec/forj-account_spec.rb +0 -75
  106. data/spec/forj-config_spec.rb +0 -196
@@ -0,0 +1,468 @@
1
+ # encoding: UTF-8
2
+
3
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ require 'rubygems'
18
+ require 'yaml'
19
+
20
+ module Lorj
21
+ # Lorj::Config is a generic class for configuration management.
22
+ # It is used by lorj to get/set data
23
+ #
24
+ # lorj uses following function in different context:
25
+ #
26
+ # In your main:
27
+ # * Config.set : To set runtime depending on options given by the user
28
+ # (cli parameters for example)
29
+ # * Config.get : To get any kind of data, for example to test values.
30
+ # * Config.save_local_config : To save setting in local config. Use
31
+ # Lorj::Config::local_set to set this kind of data to
32
+ # save
33
+ # * Config.local_set : To set a local default data. If the main wanted to
34
+ # manage local config.
35
+ # * Config.meta_each : For example to display all data per section name, with
36
+ # values.
37
+ #
38
+ # In Process functions: The Config object is accessible as 'config'.
39
+ # * config.set : To set runtime data. Ex: adapt process runtime
40
+ # behavior.
41
+ # The best approach is to declare an obj_needs optional. lorj will set it in
42
+ # hParams.
43
+ # * config.get : To get data and adapt process behavior.
44
+ # The best approach is to declare an obj_needs optional and get the value
45
+ # from hParams.
46
+ #
47
+ # In Controller functions.
48
+ # Usually, the process has implemented everything.
49
+ # You should not use the config object. Thus, config object is not accessible.
50
+ class Config < PRC::CoreConfig
51
+ # Internal Object variables:
52
+ #
53
+ # * @config_name= 'config.yaml'
54
+ # * @runtime_data = data in memory.
55
+ # * @local_data = config.yaml file data hash.
56
+ # * @config_obj = Extra loaded data
57
+ # * Lorj::Default = Application defaults class
58
+
59
+ # This function return the filename of the config layer asked:
60
+ #
61
+ # * *Args* :
62
+ # - +layer_name+ : Layer name to get the config file name
63
+ def config_filename(name = 'local')
64
+ index = layer_index(name)
65
+
66
+ index = 1 if index.nil?
67
+
68
+ @config_layers[index][:config].filename
69
+ end
70
+
71
+ # Basic dump
72
+ #
73
+ # * *Args* :
74
+ #
75
+ # * *Returns* :
76
+ # - hash of config hashes.
77
+ # * *Raises* :
78
+ # nothing
79
+ def config_dump(names = %w(local default))
80
+ # Build a config hash.
81
+
82
+ res = {}
83
+
84
+ names = %w(local default) unless names.is_a?(Array)
85
+
86
+ options = _common_options_get(:names => names)
87
+ config_layers = options[0][0]
88
+ if names.length == 1
89
+ res = config_layers[0][:config].data
90
+ else
91
+ config_layers.each do | layer |
92
+ res[layer[:name]] = layer[:config].data
93
+ end
94
+ end
95
+ res
96
+ end
97
+
98
+ # Load yaml documents (defaults + config)
99
+ # If config doesn't exist, it will be created, empty with 'defaults:' only
100
+ #
101
+ #
102
+ # * *Args* :
103
+ # - +config_name+ : Config file name to use. By default, file path is
104
+ # built as PrcLib.data_path+'config.yaml'
105
+ # * *Returns* :
106
+ # -
107
+ # * *Raises* :
108
+ # - ++ ->
109
+ def initialize(config_name = nil)
110
+ config_layers = []
111
+
112
+ # Application layer
113
+ config_layers << define_default_layer
114
+
115
+ # Local Config layer
116
+ config_layers << define_local_layer
117
+
118
+ # runtime Config layer
119
+ config_layers << define_runtime_layer
120
+
121
+ Lorj.defaults.load # Loading global application defaults
122
+
123
+ if PrcLib.data_path.nil?
124
+ PrcLib.fatal(1, 'Internal PrcLib.data_path was not set.')
125
+ end
126
+
127
+ initialize_local(config_layers[1][:config], config_name)
128
+
129
+ initialize_layers(config_layers)
130
+ end
131
+
132
+ def define_default_layer
133
+ PRC::CoreConfig.define_layer(:name => 'default',
134
+ :config => Lorj.defaults,
135
+ :set => false, :load => true)
136
+ end
137
+
138
+ def define_local_layer
139
+ PRC::CoreConfig.define_layer(:name => 'local',
140
+ :config => PRC::SectionConfig.new,
141
+ :load => true, :save => true)
142
+ end
143
+
144
+ def define_runtime_layer
145
+ PRC::CoreConfig.define_layer
146
+ end
147
+
148
+ def initialize_local(config, config_name = nil)
149
+ config_name = initialize_local_filename(config_name)
150
+
151
+ PrcLib.ensure_dir_exists(File.dirname(config_name))
152
+
153
+ if File.exist?(config_name)
154
+ config.load(config_name)
155
+
156
+ if config.data.rh_key_to_symbol?(2)
157
+ config.rh_key_to_symbol(2)
158
+ config.save config_name
159
+ end
160
+
161
+ else
162
+ config.data[:default] = nil
163
+ # Write the empty file
164
+ PrcLib.info('Creating your default configuration file ...')
165
+ config.save config_name
166
+ end
167
+ end
168
+
169
+ def initialize_local_filename(config_name = nil)
170
+ config_default_name = 'config.yaml'
171
+
172
+ if config_name
173
+ if File.dirname(config_name) == '.'
174
+ config_name = File.join(PrcLib.data_path, config_name)
175
+ end
176
+ config_name = File.expand_path(config_name)
177
+ unless File.exist?(config_name)
178
+ PrcLib.warning("Config file '%s' doesn't exists. Using default one.",
179
+ config_name)
180
+ config_name = File.join(PrcLib.data_path, config_default_name)
181
+ end
182
+ else
183
+ config_name = File.join(PrcLib.data_path, config_default_name)
184
+ end
185
+ config_name
186
+ end
187
+
188
+ # Function to set a runtime key/value, but remove it if value is nil.
189
+ # To set in config.yaml, use Lorj::Config::local_set
190
+ # To set on extra data, like account information, use
191
+ # Lorj::Config::extra_set
192
+ #
193
+ # * *Args* :
194
+ # - +key+ : key name. Can be an key tree (Array of keys).
195
+ # - +value+ : Value to set
196
+ # * *Returns* :
197
+ # - value set
198
+ # * *Raises* :
199
+ # Nothing
200
+ def set(key, value)
201
+ self[key] = value # Call PRC::CoreConfig [] function
202
+ end
203
+
204
+ # Get function
205
+ # Will search over several places:
206
+ # * runtime - Call internal runtime_get -
207
+ # * local config (config>yaml) - Call internal local_get -
208
+ # * application default (defaults.yaml) - Call Lorj.defaults.get -
209
+ # * default
210
+ #
211
+ # key can be an array, a string (converted to a symbol) or a symbol.
212
+ #
213
+ # * *Args* :
214
+ # - +key+ : key name
215
+ # - +default+: Default value to set if not found.
216
+ # * *Returns* :
217
+ # value found or default
218
+ # * *Raises* :
219
+ # nothing
220
+ def get(key, default = nil)
221
+ self[key, default]
222
+ end
223
+
224
+ # Call get function
225
+ #
226
+ # * *Args* :
227
+ # - +key+ : key name
228
+ # - +default+: Default value to set if not found.
229
+ # * *Returns* :
230
+ # value found or default
231
+ # * *Raises* :
232
+ # nothing
233
+ def [](key, default = nil) # Re-define PRC::CoreConfig []= function
234
+ return _get(:keys => [key]) if exist?(key)
235
+ default
236
+ end
237
+
238
+ # get_section helps to identify in which section the data is defined by
239
+ # data model of the application.
240
+ #
241
+ # * *Args* :
242
+ # - +key+ : key name
243
+ #
244
+ # * *Returns* :
245
+ # - the section name found
246
+ # OR
247
+ # - nil
248
+ #
249
+ # * *Raises* :
250
+ # nothing
251
+ def get_section(key)
252
+ section = Lorj.defaults.get_meta_section(key)
253
+
254
+ unless section
255
+ return PrcLib.debug('%s: Unable to get account data '\
256
+ "'%s'. No section found. check defaults.yaml.",
257
+ __callee__, key)
258
+ end
259
+ section
260
+ end
261
+ end
262
+
263
+ # Add Runtime/local functions
264
+ class Config
265
+ # Check if the key exist as a runtime data.
266
+ #
267
+ # * *Args* :
268
+ # - +key+ : key name. It do not support it to be a key tree
269
+ # (Arrays of keys).
270
+ # * *Returns* :
271
+ # - true/false
272
+ # * *Raises* :
273
+ # Nothing
274
+ def runtime_exist?(key)
275
+ index = layer_index('runtime')
276
+ @config_layers[index][:config].exist?(key)
277
+ end
278
+
279
+ # Get exclusively the Runtime data.
280
+ # Internally used by get.
281
+ #
282
+ # * *Args* :
283
+ # - +key+ : key name. It do not support it to be a key tree
284
+ # (Arrays of keys).
285
+ # * *Returns* :
286
+ # - key value.
287
+ # * *Raises* :
288
+ # Nothing
289
+ def runtime_get(key)
290
+ index = layer_index('runtime')
291
+ @config_layers[index][:config][key]
292
+ end
293
+
294
+ # Get Application data
295
+ # Used to get any kind of section available in the Application default.yaml.
296
+ #
297
+ # * *Args* :
298
+ # - +section+ : section name to get the key.
299
+ # - +keys_tree+ : list of key name tree
300
+ # * *Returns* :
301
+ # value found
302
+ # * *Raises* :
303
+ # nothing
304
+ def app_default(*keys)
305
+ return Lorj.defaults.data.rh_get(*keys) if keys.length > 0
306
+ nil
307
+ end
308
+
309
+ # Save the config.yaml file.
310
+ #
311
+ # * *Args* :
312
+ # nothing
313
+ # * *Returns* :
314
+ # - true/false
315
+ # * *Raises* :
316
+ # nothing
317
+ def save_local_config
318
+ index = layer_index('local')
319
+ file = file(nil, :index => index)
320
+ begin
321
+ result = save(:index => index)
322
+ rescue => e
323
+ PrcLib.error("%s\n%s", e.message, e.backtrace.join("\n"))
324
+ return false
325
+ end
326
+
327
+ if result
328
+ PrcLib.info('Configuration file "%s" updated.', file)
329
+ return true
330
+ end
331
+ PrcLib.debug('Configuration file "%s" was NOT updated.', file)
332
+ false
333
+ end
334
+
335
+ #
336
+ # Function to check default keys existence(in section :default) from local
337
+ # config file only.
338
+ #
339
+ # * *Args* :
340
+ # - +key+ : Symbol/String(converted to symbol) key name to test.
341
+ # * *Returns* :
342
+ # -
343
+ # * *Raises* :
344
+ # nothing
345
+ def local_default_exist?(key)
346
+ local_exist?(key)
347
+ end
348
+
349
+ # Function to check key existence from local config file only.
350
+ #
351
+ # * *Args* :
352
+ # - +key+ : Symbol/String(converted to symbol) key name to test.
353
+ # - +section+ : Section name to test the key.
354
+ # * *Returns* :
355
+ # -
356
+ # * *Raises* :
357
+ # nothing
358
+ def local_exist?(key, section = :default)
359
+ index = layer_index('local')
360
+
361
+ config = @config_layers[index][:config]
362
+ config.data_options(:section => section)
363
+
364
+ config.exist?(key)
365
+ end
366
+
367
+ # Function to set a key value in local config file only.
368
+ #
369
+ # * *Args* :
370
+ # - +key+ : Symbol/String(converted to symbol) key name to test.
371
+ # - +value+ : Value to set
372
+ # - +section+ : Section name to test the key.
373
+ #
374
+ # * *Returns* :
375
+ # - Value set.
376
+ # * *Raises* :
377
+ # nothing
378
+ def local_set(key, value, section = :default)
379
+ key = key.to_sym if key.class == String
380
+ return false if !key || !value
381
+
382
+ index = layer_index('local')
383
+
384
+ config = @config_layers[index][:config]
385
+ config.data_options(:section => section)
386
+ config[key] = value
387
+ end
388
+
389
+ # Function to Get a key value from local config file only.
390
+ #
391
+ # * *Args* :
392
+ # - +key+ : Symbol/String(converted to symbol) key name to test.
393
+ # - +section+ : Section name to test the key.
394
+ # - +default+ : default value if not found.
395
+ #
396
+ # * *Returns* :
397
+ # - Value get or default.
398
+ # * *Raises* :
399
+ # nothing
400
+ def local_get(key, section = :default, default = nil)
401
+ key = key.to_sym if key.class == String
402
+
403
+ return default unless local_exist?(key, section)
404
+
405
+ index = layer_index('local')
406
+ config = @config_layers[index][:config]
407
+ config.data_options(:section => section)
408
+ config[key]
409
+ end
410
+
411
+ # Function to Delete a key value in local config file only.
412
+ #
413
+ # * *Args* :
414
+ # - +key+ : Symbol/String(converted to symbol) key name to test.
415
+ # - +section+ : Section name to test the key.
416
+ #
417
+ # * *Returns* :
418
+ # - value deleted
419
+ # OR
420
+ # - false
421
+ # * *Raises* :
422
+ # nothing
423
+ def local_del(key, section = :default)
424
+ key = key.to_sym if key.class == String
425
+
426
+ return false if key.nil?
427
+
428
+ index = layer_index('local')
429
+ config = @config_layers[index][:config]
430
+ config.data_options(:section => section)
431
+ config.del(key)
432
+ end
433
+
434
+ # Function to return in fatal error if a config data is nil. Help to control
435
+ # function requirement.
436
+ #
437
+ #
438
+ # * *Args* :
439
+ # - +key+ : Symbol/String(converted to symbol) key name to test.
440
+ # * *Returns* :
441
+ # nothing
442
+ # * *Raises* :
443
+ # - +fatal+ : Call to PrcLib.fatal to exit the application with error 1.
444
+ def fatal_if_inexistent(key)
445
+ PrcLib.fatal(1, "Internal error - %s: '%s' is missing",
446
+ caller, key) unless get(key)
447
+ end
448
+
449
+ # each loop on Application Account section/key (meta data).
450
+ # This loop will extract data from :section of the application definition
451
+ # (defaults.yaml)
452
+ # key identified as account exclusive (:account_exclusive = true) are not
453
+ # selected.
454
+ #
455
+ # * *Args* :
456
+ # - ++ ->
457
+ # * *Returns* :
458
+ # -
459
+ # * *Raises* :
460
+ # - ++ ->
461
+ def meta_each
462
+ Lorj.defaults.meta_each do |section, key, value|
463
+ next if !value.nil? && value.rh_get(:account_exclusive).is_a?(TrueClass)
464
+ yield section, key, value
465
+ end
466
+ end
467
+ end
468
+ end