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
data/lib/prc-config.rb DELETED
@@ -1,1030 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
-
18
-
19
- require 'rubygems'
20
- require 'yaml'
21
-
22
- module Lorj
23
-
24
- # Recursive Hash existence
25
- # This function will returns the level of recursive hash was found.
26
- # * *Args* :
27
- # - +yVal+ : Hash of hashes (or recursive hash).
28
- # - +p+ : Array of string or symbols. keys tree to follow and check existence in yVal.
29
- #
30
- # * *Returns* :
31
- # - +integer+ : Represents how many keys was found in the recursive hash
32
- #
33
- # * *Raises* :
34
- # No exceptions
35
- #
36
- # Example:
37
- #
38
- # yVal = { :test => {:test2 => 'value1', :test3 => 'value2'}, :test4 => 'value3'}
39
- #
40
- # yVal can be represented like:
41
- #
42
- # yVal:
43
- # test:
44
- # test2 = 'value1'
45
- # test3 = 'value2'
46
- # test4 = 'value3'
47
- #
48
- # so:
49
- # rhExist?(yVal, :test) => 1 # test is found
50
- # rhExist?(yVal, :test5) => 0 # no test5
51
- # rhExist?(yVal, :test, :test2) => 2 # :test/:test2 tree is found
52
- # rhExist?(yVal, :test, :test2, :test5) => 2 # :test/:test2 is found (value = 2), but :test5 was not found in this tree
53
- # rhExist?(yVal, :test, :test5 ) => 1 # :test was found. but :test/:test5 tree was not found. so level 1, ok.
54
- # rhExist?(yVal) => 0 # it is like searching for nothing...
55
-
56
- def Lorj::rhExist?(yVal, *p)
57
-
58
- if p.length() == 0
59
- return 0
60
- end
61
- return 0 if yVal.class != Hash
62
- p=p.flatten
63
- if p.length() == 1
64
- return 1 if yVal.key?(p[0])
65
- return 0
66
- end
67
- return 0 if yVal.nil? or not yVal.key?(p[0])
68
- ret = 0
69
- ret = Lorj::rhExist?(yVal[p[0]], p.drop(1)) if yVal[p[0]].class == Hash
70
- return 1 + ret
71
- end
72
-
73
- # Recursive Hash Get
74
- # This function will returns the level of recursive hash was found.
75
- # * *Args* :
76
- # - +yVal+ : Hash of hashes (or recursive hash).
77
- # - +p+ : Array of string or symbols. keys tree to follow and check existence in yVal.
78
- #
79
- # * *Returns* :
80
- # - +value+ : Represents the data found in the tree. Can be of any type.
81
- #
82
- # * *Raises* :
83
- # No exceptions
84
- #
85
- # Example:
86
- #
87
- # yVal = { :test => {:test2 => 'value1', :test3 => 'value2'}, :test4 => 'value3'}
88
- #
89
- # yVal can be represented like:
90
- #
91
- # yVal:
92
- # test:
93
- # test2 = 'value1'
94
- # test3 = 'value2'
95
- # test4 = 'value3'
96
- #
97
- # so:
98
- # rhGet(yVal, :test) => {:test2 => 'value1', :test3 => 'value2'}
99
- # rhGet(yVal, :test5) => nil
100
- # rhGet(yVal, :test, :test2) => 'value1'
101
- # rhGet(yVal, :test, :test2, :test5) => nil
102
- # rhGet(yVal, :test, :test5 ) => nil
103
- # rhGet(yVal) => nil
104
- def Lorj::rhGet(yVal, *p)
105
-
106
- return nil if yVal.class != Hash
107
- p=p.flatten
108
- if p.length() == 0 or not yVal
109
- return yVal
110
- end
111
- if p.length() == 1
112
- return yVal[p[0]] if yVal.key?(p[0])
113
- return nil
114
- end
115
- return nil if not yVal
116
- return Lorj::rhGet(yVal[p[0]], p.drop(1)) if yVal.key?(p[0])
117
- nil
118
- end
119
-
120
- # Recursive Hash Set
121
- # This function will build a recursive hash according to the '*p' key tree.
122
- # if yVal is not nil, it will be updated.
123
- #
124
- # * *Args* :
125
- # - +yVal+ : Hash of hashes (or recursive hash).
126
- # - +p+ : Array of string or symbols. keys tree to follow and check existence in yVal.
127
- #
128
- # * *Returns* :
129
- # - +value+ : the value set.
130
- #
131
- # * *Raises* :
132
- # No exceptions
133
- #
134
- # Example:
135
- #
136
- # yVal = {}
137
- #
138
- # rhSet(yVal, :test) => nil
139
- # # yVal = {}
140
- #
141
- # rhSet(yVal, :test5) => nil
142
- # # yVal = {}
143
- #
144
- # rhSet(yVal, :test, :test2) => :test
145
- # # yVal = {:test2 => :test}
146
- #
147
- # rhSet(yVal, :test, :test2, :test5) => :test
148
- # # yVal = {:test2 => {:test5 => :test} }
149
- #
150
- # rhSet(yVal, :test, :test5 ) => :test
151
- # # yVal = {:test2 => {:test5 => :test}, :test5 => :test }
152
- #
153
- # rhSet(yVal, 'blabla', :test2, 'text') => :test
154
- # # yVal = {:test2 => {:test5 => :test, 'text' => 'blabla'}, :test5 => :test }
155
- def self::rhSet(yVal, value, *p)
156
- if p.length() == 0
157
- return yVal
158
- end
159
- p=p.flatten
160
- if p.length() == 1
161
- if not yVal.nil?
162
- if not value.nil?
163
- yVal[p[0]] = value
164
- else
165
- yVal.delete(p[0])
166
- end
167
- return yVal
168
- end
169
- #~ if value
170
- ret = { p[0] => value }
171
- #~ else
172
- #~ ret = {}
173
- #~ end
174
- return ret
175
- end
176
- if not yVal.nil?
177
- yVal[p[0]] = {} if not yVal[p[0]] or yVal[p[0]].class != Hash
178
- ret=Lorj::rhSet(yVal[p[0]], value, p.drop(1))
179
- return yVal
180
- else
181
- ret = Lorj::rhSet(nil, value, p.drop(1))
182
- return { p[0] => ret }
183
- end
184
- end
185
-
186
- # Move levels (default level 1) of tree keys to become symbol.
187
- #
188
- # * *Args* :
189
- # - +yVal+ : Hash of hashes (or recursive hash).
190
- # - +levels+: level of key tree to update.
191
- # * *Returns* :
192
- # - hash of hashes updated.
193
- # * *Raises* :
194
- # Nothing
195
- def self.rhKeyToSymbol(yVal, levels = 1)
196
- return nil if yVal.nil? or yVal.class != Hash
197
- yRes = {}
198
- yVal.each { | key, value |
199
- if key.class == String
200
- if levels <= 1
201
- yRes[key.to_sym] = value
202
- else
203
- yRes[key.to_sym] = rhKeyToSymbol(value, levels - 1)
204
- end
205
- else
206
- if levels <= 1
207
- yRes[key] = value
208
- else
209
- yRes[key] = rhKeyToSymbol(value, levels - 1)
210
- end
211
- end
212
- }
213
- yRes
214
- end
215
-
216
- # Check if levels of tree keys are all symbols.
217
- #
218
- # * *Args* :
219
- # - +yVal+ : Hash of hashes (or recursive hash).
220
- # - +levels+: level of key tree to update.
221
- # * *Returns* :
222
- # - true : one key path is not symbol.
223
- # - false : all key path are symbols.
224
- # * *Raises* :
225
- # Nothing
226
- def self.rhKeyToSymbol?(yVal, levels = 1)
227
- return false if yVal.nil? or yVal.class != Hash
228
- yVal.each { | key, value |
229
- if key.class == String
230
- return true
231
- end
232
- if levels >1
233
- res = rhKeyToSymbol?(value, levels - 1)
234
- return true if res
235
- end
236
- }
237
- false
238
- end
239
-
240
- # This class is the Application configuration class used by Lorj::Config
241
- #
242
- # It load a defaults.yaml file (path defined by PrcLib::app_defaults)
243
- #
244
- # defaults.yaml is divided in 3 sections:
245
- #
246
- # * :default: Contains a list of key = value
247
- # * :setup: Contains :ask_step array
248
- # - :ask_step: Array of group of keys/values to setup. Each group will be internally identified by a index starting at 0. parameters are as follow:
249
- # - :desc: string to print out before group setup
250
- # - :explanation: longer string to display after :desc:
251
- # - :add: array of keys to add manually in the group.
252
- #
253
- # By default, thanks to data model dependency, the group is automatically populated.
254
- #
255
- # * :section: Contains a list of sections contains several key and attributes and eventually :default:
256
- # This list of sections and keys will be used to build the account files with the lorj Lorj::Core::Setup function.
257
- #
258
- # - :default: This section define updatable data available from config.yaml. But will never be added in an account file.
259
- # It contains a list of key and options.
260
- #
261
- # - :<aKey>: Possible options
262
- # - :desc: default description for that <aKey>
263
- #
264
- # - :<aSectionName>: Name of the section which should contains a lis
265
- # - :<aKeyName>: Name of the key to setup.
266
- # - :desc: Description of that key, printed out at setup time.
267
- # - :readonly: true if this key is not modifiable by a simple Lorj::Account::set function. false otherwise.
268
- # - :account_exclusive: true if the key cannot be set as default from config.yaml or defaults.yaml.
269
- # - :account: true to ask setup to ask this key to the user.
270
- # - :validate: Ruby Regex to validate the end user input. Ex: !ruby/regexp /^\w?\w*$/
271
- # - :default_value: default value proposed to the user.
272
- # - :ask_step: Define the group number to attach the key to be asked. ex: 2
273
- # - :list_values: Provide capabililities to get a list and choose from.
274
- # - :query_type: Can be:
275
- #
276
- # ':query_call' to execute a query on flavor, query_params is empty for all.
277
- #
278
- # ':process_call' to execute a process function to get the values.
279
- #
280
- # ':controller_call' to execute a controller query.
281
- #
282
- # - :object:
283
- #
284
- # Used with :query_type=:query_call. object type symbol to query.
285
- #
286
- # - :query
287
- #
288
- # Used with :query_type=:process_call. process function name to call.
289
- #
290
- # - :query_call:
291
- #
292
- # Used with :query_type=:controller_call. Handler function to use. (query_e, create_e, ...)
293
- #
294
- # Used with :query_type=:process_call. Function name to call
295
- #
296
- # - :query_params:
297
- #
298
- # Used with :query_type=:query_call. Query hash defining filtering capabilities.
299
- #
300
- # Used with :query_type=:process_call. hParams data passed to the process function.
301
- #
302
- # - :value: fields to extract for the list of objects displayed.
303
- # - :validate: if :list_strict, the value is limited to the possible values from the list
304
-
305
- class Default
306
-
307
- # @sDefaultsName='defaults.yaml'
308
- # @yDefaults = defaults.yaml file data hash
309
-
310
- # Load yaml documents (defaults)
311
- # If config doesn't exist, it will be created, empty with 'defaults:' only
312
-
313
- # class.exist?
314
- #
315
- #
316
- # * *Args* :
317
- # - ++ ->
318
- # * *Returns* :
319
- # -
320
- # * *Raises* :
321
- # - ++ ->
322
- def self.exist?(key, section = :default)
323
- key = key.to_sym if key.class == String
324
- (Lorj::rhExist?(@@yDefaults, section, key) == 2)
325
- end
326
-
327
- #
328
- #
329
- # * *Args* :
330
- # - ++ ->
331
- # * *Returns* :
332
- # -
333
- # * *Raises* :
334
- # - ++ ->
335
- def self.get(key, section = :default)
336
- key = key.to_sym if key.class == String
337
- return(Lorj::rhGet(@@yDefaults, section, key)) if key
338
- Lorj::rhGet(@@yDefaults, section) if not key
339
- end
340
-
341
- #
342
- #
343
- # * *Args* :
344
- # - ++ ->
345
- # * *Returns* :
346
- # -
347
- # * *Raises* :
348
- # - ++ ->
349
- def self.dump()
350
- @@yDefaults
351
- end
352
-
353
- # Loop on Config metadata
354
- #
355
- #
356
- # * *Args* :
357
- # - ++ ->
358
- # * *Returns* :
359
- # -
360
- # * *Raises* :
361
- # - ++ ->
362
- def self.meta_each
363
- return nil if Lorj::rhGet(@@yDefaults, :sections).nil?
364
-
365
- Lorj::rhGet(@@yDefaults, :sections).each { | section, hValue |
366
- hValue.each { | key, value |
367
- yield section, key, value
368
- }
369
- }
370
- end
371
-
372
- #
373
- #
374
- # * *Args* :
375
- # - ++ ->
376
- # * *Returns* :
377
- # -
378
- # * *Raises* :
379
- # - ++ ->
380
- def self.meta_exist?(key)
381
- return nil if not key
382
-
383
- key = key.to_sym if key.class == String
384
-
385
- section = Lorj::rhGet(@@account_section_mapping, key)
386
- Lorj::rhExist?(@@yDefaults, :sections, section, key) == 3
387
- end
388
-
389
- #
390
- #
391
- # * *Args* :
392
- # - ++ ->
393
- # * *Returns* :
394
- # -
395
- # * *Raises* :
396
- # - ++ ->
397
- def self.get_meta(key)
398
- return nil if not key
399
-
400
- key = key.to_sym if key.class == String
401
- section = Lorj::rhGet(@@account_section_mapping, key)
402
- Lorj::rhGet(@@yDefaults, :sections, section, key)
403
- end
404
-
405
- #
406
- #
407
- # * *Args* :
408
- # - ++ ->
409
- # * *Returns* :
410
- # -
411
- # * *Raises* :
412
- # - ++ ->
413
- def self.build_section_mapping
414
-
415
- if Lorj::rhGet(@@yDefaults, :sections).nil?
416
- PrcLib.warning("defaults.yaml do not defines :sections")
417
- return nil
418
- end
419
-
420
- Lorj::rhGet(@@yDefaults, :sections).each { | section, hValue |
421
- next if section == :default
422
- hValue.each_key { | map_key |
423
- PrcLib.fatal(1, "defaults.yaml: Duplicate entry between sections. '%s' defined in section '%s' already exists in section '%s'" % [map_key, section, Lorj::rhGet(@account_section_mapping, map_key) ])if Lorj::rhExist?(@account_section_mapping, map_key) != 0
424
- Lorj::rhSet(@@account_section_mapping, section, map_key)
425
- }
426
- }
427
- end
428
-
429
- #
430
- #
431
- # * *Args* :
432
- # - ++ ->
433
- # * *Returns* :
434
- # -
435
- # * *Raises* :
436
- # - ++ ->
437
- def self.get_meta_section(key)
438
- key = key.to_sym if key.class == String
439
- Lorj::rhGet(@@account_section_mapping, key)
440
- end
441
-
442
- #
443
- #
444
- # * *Args* :
445
- # - ++ ->
446
- # * *Returns* :
447
- # -
448
- # * *Raises* :
449
- # - ++ ->
450
- def self.load()
451
-
452
- @@account_section_mapping = {}
453
- @@yDefaults = {}
454
- @@sDefaultsName = nil
455
-
456
- if not PrcLib.app_defaults
457
- PrcLib.warning("PrcLib.app_defaults is not set. Application defaults won't be loaded.")
458
- else
459
- @@sDefaultsName = File.join(PrcLib.app_defaults,'defaults.yaml')
460
-
461
- PrcLib.info("Reading default configuration '%s'..." % @@sDefaultsName)
462
-
463
- if File.exists?(@@sDefaultsName)
464
- @@yDefaults = YAML.load_file(@@sDefaultsName)
465
-
466
- self.build_section_mapping
467
- else
468
- PrcLib.warning("PrcLib.app_defaults is set to '%s'. Trying to load '%s' but not found. Application defaults won't be loaded." % [PrcLib.app_defaults, @@sDefaultsName])
469
- end
470
-
471
- end
472
- end
473
-
474
- end
475
-
476
-
477
- # Lorj::Config is a generic class for configuration management.
478
- # It is used by lorj to get/set data
479
- #
480
- # lorj uses following function in different context:
481
- #
482
- # In your main:
483
- # * Config.set : To set runtime depending on options given by the user (cli parameters for example)
484
- # * Config.get : To get any kind of data, for example to test values.
485
- # * Config.saveConfig : To save setting in local config. Use Lorj::Config::localSet to set this kind of data to save
486
- # * Config.localSet : To set a local default data. If the main wanted to manage local config.
487
- # * Config.meta_each : For example to display all data per section name, with values.
488
- #
489
- # In Process functions: The Config object is accessible as 'config'.
490
- # * config.set : To set runtime data. Ex: adapt process runtime behavior.
491
- # The best approach is to declare an obj_needs optional. lorj will set it in hParams.
492
- # * config.get : To get data and adapt process behavior.
493
- # The best approach is to declare an obj_needs optional and get the value from hParams.
494
- #
495
- # In Controller functions.
496
- # Usually, the process has implemented everything.
497
- # You should not use the config object. Thus, config object is not accessible.
498
-
499
- class Config
500
-
501
- # Internal Object variables:
502
- #
503
- # * @sConfigName= 'config.yaml'
504
- # * @yRuntime = data in memory.
505
- # * @yLocal = config.yaml file data hash.
506
- # * @yObjConfig = Extra loaded data
507
- # * Lorj::Default = Application defaults class
508
-
509
- attr_reader :sConfigName
510
-
511
- # Basic dump
512
- #
513
- # * *Args* :
514
- # - +interms+ : Will be obsoleted shortly.
515
- # * *Returns* :
516
- # - hash of hashes.
517
- # * *Raises* :
518
- # nothing
519
- def default_dump(interms = nil)
520
- # Build a config hash.
521
-
522
- res = {}
523
- Lorj::Default.dump[:default].each_key { |key|
524
- dump_key = exist?(key)
525
- Lorj::rhSet(res, get(key), dump_key, key)
526
- }
527
- if Lorj::rhExist?(@yLocal, :default) == 1
528
- @yLocal[:default].each_key { |key|
529
- dump_key = exist?(key)
530
- Lorj::rhSet(res, get(key), dump_key, key) if Lorj::rhExist?(res, dump_key, key) != 2
531
- }
532
- end
533
- if interms
534
- if interms.instance_of? Hash
535
- @interms.each_key { | key|
536
- dump_key = exist?(key)
537
- Lorj::rhSet(res, get(key), dump_key, key) if Lorj::rhExist?(res, dump_key, key) != 2
538
- }
539
- elsif interms.instance_of? Array # Array of hash of hash
540
- interms.each { | elem |
541
- elem.each_key { | key|
542
- dump_key = exist?(key)
543
- Lorj::rhSet(res, get(key), dump_key, key) if Lorj::rhExist?(res, dump_key, key) != 2
544
- }
545
- }
546
- end
547
- end
548
- @yRuntime.each_key { |key|
549
- dump_key = exist?(key)
550
- Lorj::rhSet(res, get(key), dump_key, key) if Lorj::rhExist?(res, dump_key, key) != 2
551
- }
552
-
553
- res
554
- end
555
-
556
- # Load yaml documents (defaults + config)
557
- # If config doesn't exist, it will be created, empty with 'defaults:' only
558
- #
559
- #
560
- # * *Args* :
561
- # - +sConfigName+ : Config file name to use. By default, file path is built as PrcLib.data_path+'config.yaml'
562
- # * *Returns* :
563
- # -
564
- # * *Raises* :
565
- # - ++ ->
566
- def initialize(sConfigName=nil)
567
-
568
- Default.load() # Loading global application defaults
569
-
570
- if PrcLib.data_path.nil?
571
- PrcLib.fatal(1, 'Internal PrcLib.data_path was not set.')
572
- end
573
-
574
- sConfigDefaultName='config.yaml'
575
-
576
- if sConfigName
577
- if File.dirname(sConfigName) == '.'
578
- sConfigName = File.join(PrcLib.data_path,sConfigName)
579
- end
580
- sConfigName = File.expand_path(sConfigName)
581
- if not File.exists?(sConfigName)
582
- PrcLib.warning("Config file '%s' doesn't exists. Using default one." % [sConfigName] )
583
- @sConfigName = File.join(PrcLib.data_path,sConfigDefaultName)
584
- else
585
- @sConfigName = sConfigName
586
- end
587
- else
588
- @sConfigName = File.join(PrcLib.data_path,sConfigDefaultName)
589
- end
590
-
591
- PrcLib.ensure_dir_exists(File.dirname(@sConfigName))
592
-
593
- if File.exists?(@sConfigName)
594
- @yLocal = YAML.load_file(@sConfigName)
595
- if Lorj::rhKeyToSymbol?(@yLocal, 2)
596
- @yLocal = Lorj::rhKeyToSymbol(@yLocal, 2)
597
- self.saveConfig()
598
- end
599
-
600
- else
601
- @yLocal = { :default => nil }
602
- # Write the empty file
603
- PrcLib.info('Creating your default configuration file ...')
604
- self.saveConfig()
605
- end
606
-
607
- @yRuntime = {}
608
- @yObjConfig = {}
609
- end
610
-
611
- # Save the config.yaml file.
612
- #
613
- # * *Args* :
614
- # nothing
615
- # * *Returns* :
616
- # - true/false
617
- # * *Raises* :
618
- # nothing
619
- def saveConfig()
620
- begin
621
- File.open(@sConfigName, 'w') do |out|
622
- YAML.dump(@yLocal, out)
623
- end
624
- rescue => e
625
- Lorj.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
626
- return false
627
- end
628
- PrcLib.info('Configuration file "%s" updated.' % @sConfigName)
629
- return true
630
- end
631
-
632
- # Save extra data to a file. Will be obsoleted.
633
- #
634
- # * *Args* :
635
- # - +sFile+ : File name to use for saving data.
636
- # - +section+ : Section name where to find the key structure to save.
637
- # - +name+ : key structure name found in section to save.
638
- # * *Returns* :
639
- # - true/false
640
- # * *Raises* :
641
- # nothing
642
- def extraSave(sFile, section, name)
643
- hVal = Lorj::rhGet(@yObjConfig, section, name)
644
- if hVal
645
- begin
646
- File.open(sFile, 'w') do |out|
647
- YAML.dump(hVal, out)
648
- end
649
- rescue => e
650
- Lorj.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
651
- return false
652
- end
653
- PrcLib.info('Configuration file "%s" updated.' % sFile)
654
- return true
655
- end
656
- end
657
-
658
- # Load extra data from a file. Will be obsoleted.
659
- #
660
- # * *Args* :
661
- # - +sFile+ : File name to use for saving data.
662
- # - +section+ : Section name where to find the key structure to save.
663
- # - +name+ : key structure name found in section to save.
664
- # * *Returns* :
665
- # - key tree loaded.
666
- # * *Raises* :
667
- # nothing
668
- def extraLoad(sFile, section, name)
669
- if File.exists?(sFile)
670
- hVal = YAML.load_file(sFile)
671
- Lorj::rhSet(@yObjConfig, hVal, section, name)
672
- hVal
673
- end
674
- end
675
-
676
- # Check from Extra data existence of keys tree. Will be obsoleted.
677
- #
678
- # * *Args* :
679
- # - +section+ -> Section Name
680
- # - +name+ -> Key Name
681
- # - +key+ -> key tree
682
- # * *Returns* :
683
- # - true or false
684
- # * *Raises* :
685
- # Nothing
686
-
687
- def extraExist?(section, name, key = nil)
688
- return nil if not section or not name
689
-
690
- key = key.to_sym if key.class == String
691
-
692
- return(Lorj::rhExist?(@yObjConfig, section, name) == 2) if not key
693
- return(Lorj::rhExist?(@yObjConfig, section, name, key) == 3)
694
- end
695
-
696
- # Get from Extra data existence of keys tree. Will be obsoleted.
697
- #
698
- # * *Args* :
699
- # - +section+ -> Section Name
700
- # - +name+ -> Key Name
701
- # - +key+ -> key tree
702
- # - +default+ -> default value
703
- # * *Returns* :
704
- # - value found
705
- # * *Raises* :
706
- # Nothing
707
-
708
- def extraGet(section, name, key = nil, default = nil)
709
- return nil if not section or not name
710
-
711
- key = key.to_sym if key.class == String
712
- return default unless ExtraExist?(section, name, key)
713
- return Lorj::rhGet(@yObjConfig, section, name, key) if key
714
- Lorj::rhGet(@yObjConfig, section, name)
715
- end
716
-
717
- # Set to Extra data existence of keys tree. Will be obsoleted.
718
- #
719
- # * *Args* :
720
- # - +section+ -> Section Name
721
- # - +name+ -> Key Name
722
- # - +key+ -> key tree
723
- # - +value+ -> Value to set
724
- # * *Returns* :
725
- # - value set
726
- # * *Raises* :
727
- # Nothing
728
- def extraSet(section, name, key, value)
729
- key = key.to_sym if key.class == String
730
- if key
731
- Lorj::rhSet(@yObjConfig, value, section, name, key)
732
- else
733
- Lorj::rhSet(@yObjConfig, value, section, name)
734
- end
735
- end
736
-
737
- # Function to set a runtime key/value, but remove it if value is nil.
738
- # To set in config.yaml, use Lorj::Config::localSet
739
- # To set on extra data, like account information, use Lorj::Config::ExtraSet
740
- #
741
- # * *Args* :
742
- # - +key+ : key name. Can be an key tree (Array of keys).
743
- # - +value+ : Value to set
744
- # * *Returns* :
745
- # - value set
746
- # * *Raises* :
747
- # Nothing
748
- def set(key, value)
749
-
750
- key = key.to_sym if key.class == String
751
-
752
- return false if not([Symbol, Array].include?(key.class))
753
-
754
- Lorj::rhSet(@yRuntime, value, key)
755
- true
756
- end
757
-
758
- # Call set function
759
- #
760
- # * *Args* :
761
- # - +key+ : key name. Can be an key tree (Array of keys).
762
- # - +value+ : Value to set
763
- # * *Returns* :
764
- # - value set
765
- # * *Raises* :
766
- # Nothing
767
- def []=(key, value)
768
- set(key, value)
769
- end
770
-
771
- # Check if the key exist as a runtime data.
772
- #
773
- # * *Args* :
774
- # - +key+ : key name. It do not support it to be a key tree (Arrays of keys).
775
- # * *Returns* :
776
- # - true/false
777
- # * *Raises* :
778
- # Nothing
779
-
780
- def runtimeExist?(key)
781
- (Lorj::rhExist?(@yRuntime, key) == 1)
782
- end
783
-
784
- # Get exclusively the Runtime data.
785
- # Internally used by get.
786
- #
787
- # * *Args* :
788
- # - +key+ : key name. It do not support it to be a key tree (Arrays of keys).
789
- # * *Returns* :
790
- # - key value.
791
- # * *Raises* :
792
- # Nothing
793
-
794
- def runtimeGet(key)
795
- Lorj::rhGet(@yRuntime, key) if runtimeExist?(key)
796
- end
797
-
798
- # Get function
799
- # Will search over several places:
800
- # * runtime - Call internal runtimeGet -
801
- # * local config (config>yaml) - Call internal LocalGet -
802
- # * application default (defaults.yaml) - Call Lorj::Default.get -
803
- # * default
804
- #
805
- # key can be an array, a string (converted to a symbol) or a symbol.
806
- #
807
- # * *Args* :
808
- # - +key+ : key name
809
- # - +default+: Default value to set if not found.
810
- # * *Returns* :
811
- # value found or default
812
- # * *Raises* :
813
- # nothing
814
-
815
- def get(key, default = nil)
816
- key = key.to_sym if key.class == String
817
- return nil if not([Symbol, Array].include?(key.class))
818
- # If key is in runtime
819
- return runtimeGet(key) if runtimeExist?(key)
820
- # else key in local default config of default section.
821
- return localGet(key) if localDefaultExist?(key)
822
- # else key in application defaults
823
- return Lorj::Default.get(key) if Lorj::Default.exist?(key)
824
- # else default
825
- default
826
- end
827
-
828
- # Call get function
829
- #
830
- # * *Args* :
831
- # - +key+ : key name
832
- # - +default+: Default value to set if not found.
833
- # * *Returns* :
834
- # value found or default
835
- # * *Raises* :
836
- # nothing
837
-
838
- def [](key, default = nil)
839
- get(key, default)
840
- end
841
-
842
- # Get Application data
843
- # Used to get any kind of section available in the Application default.yaml.
844
- #
845
- # * *Args* :
846
- # - +section+: section name to get the key.
847
- # - +key+ : key name
848
- # * *Returns* :
849
- # value found
850
- # * *Raises* :
851
- # nothing
852
- def getAppDefault(section, key = nil)
853
-
854
- key = key.to_sym if key.class == String
855
-
856
- Lorj::Default.get(key, section)
857
- end
858
-
859
- # Check where the get or [] is going to get the data
860
- #
861
- # * *Args* :
862
- # - +key+ : Symbol/String(converted to symbol) key name to test.
863
- # - +interms+ : <b>Will be removed shortly!!!</b> Add intermediate hash to check
864
- # * *Returns* :
865
- # - false if no value found.
866
- # - 'runtime' if found in runtime.
867
- # - '%s' if found in intermediate. <b>Will be removed shortly!!!</b>
868
- # - 'hash[%s]' if found in intermediate. <b>Will be removed shortly!!!</b>
869
- # - 'hash' if found in intermediate. <b>Will be removed shortly!!!</b>
870
- # - 'local' if get from local config (config.yaml)
871
- # - 'default' if get from application default (defaults.yaml)
872
- # -
873
- # * *Raises* :
874
- # nothing
875
- def exist?(key, interms = nil)
876
- key = key.to_sym if key.class == String
877
-
878
- # Check data in intermediate hashes or array of hash. (like account data - key have to be identical)
879
- return "runtime" if Lorj::rhExist?(@yRuntime, key) == 1
880
- if interms
881
- if interms.instance_of? Hash
882
- return 'hash' if Lorj::rhExist?(interms, key) == 1
883
- elsif interms.instance_of? Array # Array of hash
884
- iCount = 0
885
- interms.each { | elem |
886
- if elem.class == Hash
887
- elem.each { | hashkey, value |
888
- return ("%s" % hashkey) if value.class == Hash and Lorj::rhExist?(elem, hashkey, key) == 2
889
- return ("hash[%s]" % iCount) if value.class != Hash and Lorj::rhExist?(elem, hashkey) == 1
890
- }
891
- end
892
- iCount += 1
893
- }
894
- end
895
- end
896
- return 'local' if localDefaultExist?(key)
897
- # else key in application defaults
898
- return 'default' if Lorj::Default.exist?(key)
899
- false
900
- end
901
-
902
- #
903
- # Function to check default keys existence(in section :default) from local config file only.
904
- #
905
- # * *Args* :
906
- # - +key+ : Symbol/String(converted to symbol) key name to test.
907
- # * *Returns* :
908
- # -
909
- # * *Raises* :
910
- # nothing
911
- def localDefaultExist?(key)
912
- localExist?(key)
913
- end
914
-
915
- # Function to check key existence from local config file only.
916
- #
917
- # * *Args* :
918
- # - +key+ : Symbol/String(converted to symbol) key name to test.
919
- # - +section+ : Section name to test the key.
920
- # * *Returns* :
921
- # -
922
- # * *Raises* :
923
- # nothing
924
- def localExist?(key, section = :default)
925
-
926
- key = key.to_sym if key.class == String
927
- return true if Lorj::rhExist?(@yLocal, section, key) == 2
928
- false
929
- end
930
-
931
- # Function to set a key value in local config file only.
932
- #
933
- # * *Args* :
934
- # - +key+ : Symbol/String(converted to symbol) key name to test.
935
- # - +value+ : Value to set
936
- # - +section+ : Section name to test the key.
937
- #
938
- # * *Returns* :
939
- # - Value set.
940
- # * *Raises* :
941
- # nothing
942
- def localSet(key, value, section = :default)
943
- key = key.to_sym if key.class == String
944
- if not key or not value
945
- return false
946
- end
947
- if @yLocal[section] == nil
948
- @yLocal[section]={}
949
- end
950
- if @yLocal.has_key?(section)
951
- @yLocal[section].merge!({key => value})
952
- else
953
- @yLocal.merge!(section => {key => value})
954
- end
955
- return true
956
- end
957
-
958
- # Function to Get a key value from local config file only.
959
- #
960
- # * *Args* :
961
- # - +key+ : Symbol/String(converted to symbol) key name to test.
962
- # - +section+ : Section name to test the key.
963
- # - +default+ : default value if not found.
964
- #
965
- # * *Returns* :
966
- # - Value get or default.
967
- # * *Raises* :
968
- # nothing
969
- def localGet(key, section = :default, default = nil)
970
- key = key.to_sym if key.class == String
971
-
972
- return default if Lorj::rhExist?(@yLocal, section, key) != 2
973
- Lorj::rhGet(@yLocal, section, key)
974
- end
975
-
976
- # Function to Delete a key value in local config file only.
977
- #
978
- # * *Args* :
979
- # - +key+ : Symbol/String(converted to symbol) key name to test.
980
- # - +section+ : Section name to test the key.
981
- #
982
- # * *Returns* :
983
- # - true/false
984
- # * *Raises* :
985
- # nothing
986
- def localDel(key, section = :default)
987
- key = key.to_sym if key.class == String
988
- if not key
989
- return false
990
- end
991
- if not @yLocal.has_key?(section)
992
- return false
993
- end
994
- @yLocal[section].delete(key)
995
- return true
996
- end
997
-
998
- # Function to return in fatal error if a config data is nil. Help to control function requirement.
999
- #
1000
- #
1001
- # * *Args* :
1002
- # - +key+ : Symbol/String(converted to symbol) key name to test.
1003
- # * *Returns* :
1004
- # nothing
1005
- # * *Raises* :
1006
- # - +fatal+ : Call to PrcLib.fatal to exit the application with error 1.
1007
- def fatal_if_inexistent(key)
1008
- PrcLib.fatal(1, "Internal error - %s: '%s' is missing" % [caller(), key]) if not self.get(key)
1009
- end
1010
-
1011
- # each loop on Application Account section/key (meta data).
1012
- # This loop will extract data from :section of the application definition (defaults.yaml)
1013
- # key identified as account exclusive (:account_exclusive = true) are not selected.
1014
- #
1015
- # * *Args* :
1016
- # - ++ ->
1017
- # * *Returns* :
1018
- # -
1019
- # * *Raises* :
1020
- # - ++ ->
1021
- def meta_each
1022
- Lorj::Default.meta_each { |section, key, value|
1023
- next if Lorj::rhGet(value, :account_exclusive)
1024
- yield section, key, value
1025
- }
1026
- end
1027
-
1028
- end
1029
-
1030
- end