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,278 @@
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
+ require 'rubygems'
19
+ require 'yaml'
20
+
21
+ # Lorj module implements Lorj::Config
22
+ # Lorj exposes defaults, as attribute to access the Lorj::Defaults instance.
23
+ module Lorj
24
+ # This class is the Application configuration class used by Lorj::Config
25
+ #
26
+ # It load a defaults.yaml file (path defined by PrcLib::app_defaults)
27
+ #
28
+ # defaults.yaml is divided in 3 sections:
29
+ #
30
+ # * :default: Contains a list of key = value
31
+ # * :setup: Contains :ask_step array
32
+ # - :ask_step: Array of group of keys/values to setup. Each group will be
33
+ # internally identified by a index starting at 0. parameters
34
+ # are as follow:
35
+ # - :desc: string to print out before group setup
36
+ # - :explanation: longer string to display after :desc:
37
+ # - :add: array of keys to add manually in the group.
38
+ #
39
+ # By default, thanks to data model dependency, the group is
40
+ # automatically populated.
41
+ #
42
+ # * :section: Contains a list of sections contains several key and attributes
43
+ # and eventually :default:
44
+ # This list of sections and keys will be used to build the account files
45
+ # with the lorj Lorj::Core::Setup function.
46
+ #
47
+ # - :default: This section define updatable data available from config.yaml.
48
+ # But will never be added in an account file.
49
+ # It contains a list of key and options.
50
+ #
51
+ # - :<aKey>: Possible options
52
+ # - :desc: default description for that <aKey>
53
+ #
54
+ # - :<aSectionName>: Name of the section which should contains a list
55
+ # - :<aKeyName>: Name of the key to setup.
56
+ # - :desc: Description of that key, printed out at setup
57
+ # time.
58
+ # - :readonly: true if this key is not modifiable by a simple
59
+ # Lorj::Account::set function. false otherwise.
60
+ # - :account_exclusive: true if the key cannot be set as default from
61
+ # config.yaml or defaults.yaml.
62
+ # - :account: true to ask setup to ask this key to the user.
63
+ # - :validate: Ruby Regex to validate the end user input.
64
+ # Ex: !ruby/regexp /^\w?\w*$/
65
+ # - :default_value: default value proposed to the user.
66
+ # - :ask_step: Define the group number to attach the key to be
67
+ # asked. ex: 2
68
+ # - :list_values: Provide capabililities to get a list and choose
69
+ # from.
70
+ # - :query_type: Can be:
71
+ #
72
+ # ':query_call' to execute a query on flavor, query_params is empty
73
+ # for all.
74
+ #
75
+ # ':process_call' to execute a process function to get the values.
76
+ #
77
+ # ':controller_call' to execute a controller query.
78
+ #
79
+ # - :object:
80
+ #
81
+ # Used with :query_type=:query_call. object type symbol to query.
82
+ #
83
+ # - :query
84
+ #
85
+ # Used with :query_type=:process_call. process function name to call
86
+ #
87
+ # - :query_call:
88
+ #
89
+ # Used with :query_type=:controller_call. Handler function to use.
90
+ # (query_e, create_e, ...)
91
+ #
92
+ # Used with :query_type=:process_call. Function name to call
93
+ #
94
+ # - :query_params:
95
+ #
96
+ # Used with :query_type=:query_call. Query hash defining filtering
97
+ # capabilities.
98
+ #
99
+ # Used with :query_type=:process_call. hParams data passed to the
100
+ # process function.
101
+ #
102
+ # - :value: fields to extract for the list of objects
103
+ # displayed.
104
+ # - :validate: if :list_strict, the value is limited to the
105
+ # possible values from the list
106
+ class Defaults < PRC::SectionConfig
107
+ # Remove inherited method []=
108
+ def []=(*_keys, _value)
109
+ end
110
+
111
+ # Load yaml documents (defaults)
112
+ # If config doesn't exist, it will be created, empty with 'defaults:' only
113
+
114
+ # Loop on Config metadata
115
+ #
116
+ #
117
+ # * *Args* :
118
+ # - ++ ->
119
+ # * *Returns* :
120
+ # -
121
+ # * *Raises* :
122
+ # - ++ ->
123
+ def meta_each
124
+ return nil if @data.rh_get(:sections).nil?
125
+
126
+ @data.rh_get(:sections).each do | section, hValue |
127
+ hValue.each do | key, value |
128
+ yield section, key, value
129
+ end
130
+ end
131
+ end
132
+
133
+ #
134
+ #
135
+ # * *Args* :
136
+ # - ++ ->
137
+ # * *Returns* :
138
+ # -
139
+ # * *Raises* :
140
+ # - ++ ->
141
+ def meta_exist?(key)
142
+ return nil unless key
143
+
144
+ key = key.to_sym if key.class == String
145
+
146
+ section = @account_section_mapping.rh_get(key)
147
+ @data.rh_exist?(:sections, section, key)
148
+ end
149
+
150
+ # Get model data options. Section name is determined by the associated
151
+ # data name
152
+ #
153
+ # * *Args* :
154
+ # - +data+ : data name
155
+ # - options+ : options tree.
156
+ # * *Returns* :
157
+ # - data options values
158
+ # OR
159
+ # - nil if:
160
+ # - missing data name as parameter.
161
+ # - data was not found. defined in /:sections/<section>/<data
162
+ # * *Raises* :
163
+ # - ++ ->
164
+ def get_meta_auto(*keys)
165
+ return nil unless keys.length > 0
166
+ section = @account_section_mapping.rh_get(keys[0])
167
+ return nil if section.nil?
168
+ @data.rh_get(:sections, section, keys)
169
+ end
170
+
171
+ # Get model section/data options.
172
+ #
173
+ # * *Args* :
174
+ # - +section+ : section name
175
+ # - +data+ : data name
176
+ # - +options+ : options tree.
177
+ #
178
+ # * *Returns* :
179
+ # - data options values
180
+ # OR
181
+ # - nil if:
182
+ # - missing section and data name as parameter.
183
+ # - data was not found. defined in /:sections/<section>/<data
184
+ # * *Raises* :
185
+ # - ++ ->
186
+ def get_meta(*keys)
187
+ return nil unless keys.length > 1
188
+ @data.rh_get(:sections, keys)
189
+ end
190
+
191
+ #
192
+ #
193
+ # * *Args* :
194
+ # - ++ ->
195
+ # * *Returns* :
196
+ # -
197
+ # * *Raises* :
198
+ # - ++ ->
199
+ def build_section_mapping
200
+ if @data.rh_get(:sections).nil?
201
+ PrcLib.warning('defaults.yaml do not defines :sections')
202
+ return nil
203
+ end
204
+
205
+ # TODO: Support multiple identical key name on distinct sections
206
+ # The primary data key should change from key to section & key.
207
+ @data.rh_get(:sections).each do | section, hValue |
208
+ next if section == :default
209
+ hValue.each_key do | map_key |
210
+ if @account_section_mapping.rh_exist?(map_key)
211
+ PrcLib.fatal(1, 'defaults.yaml: Duplicate entry between sections. '\
212
+ "'%s' defined in section '%s' already exists in"\
213
+ " section '%s'", map_key, section,
214
+ @account_section_mapping.rh_get(map_key))
215
+ end
216
+ @account_section_mapping.rh_set(section, map_key)
217
+ end
218
+ end
219
+ end
220
+
221
+ #
222
+ #
223
+ # * *Args* :
224
+ # - ++ ->
225
+ # * *Returns* :
226
+ # -
227
+ # * *Raises* :
228
+ # - ++ ->
229
+ def get_meta_section(key)
230
+ key = key.to_sym if key.class == String
231
+ @account_section_mapping.rh_get(key)
232
+ end
233
+
234
+ #
235
+ #
236
+ # * *Args* :
237
+ # - ++ ->
238
+ # * *Returns* :
239
+ # -
240
+ # * *Raises* :
241
+ # - ++ ->
242
+ def load
243
+ @account_section_mapping = {}
244
+
245
+ if !PrcLib.app_defaults
246
+ PrcLib.warning('PrcLib.app_defaults is not set. Application defaults'\
247
+ " won't be loaded.")
248
+ else
249
+ @filename = File.join(PrcLib.app_defaults, 'defaults.yaml')
250
+
251
+ PrcLib.info("Reading default configuration '%s'...", @filename)
252
+
253
+ if File.exist?(@filename)
254
+ _load(@filename)
255
+
256
+ build_section_mapping
257
+ else
258
+ PrcLib.warning("PrcLib.app_defaults is set to '%s'. Trying to load"\
259
+ " '%s' but not found. Application defaults won't "\
260
+ 'be loaded.', PrcLib.app_defaults, @filename)
261
+ end
262
+
263
+ end
264
+ end
265
+ end
266
+
267
+ module_function
268
+
269
+ # Lorj::defaults exposes the application defaults and Config Lorj metadata.
270
+ def defaults
271
+ return @defaults unless @defaults.nil?
272
+ @defaults = Defaults.new
273
+
274
+ @defaults.load
275
+
276
+ @defaults
277
+ end
278
+ end
data/lib/prc.rb CHANGED
@@ -15,114 +15,146 @@
15
15
  require 'fileutils'
16
16
  require 'logger'
17
17
 
18
-
18
+ # General lorj library. Used as library data configuration
19
+ # List of possible library settings:
20
+ # PrcLib.log : PrcLib::Logging object. Used internally by PrcLib
21
+ # logging system.
22
+ # This object is automatically created as soon as
23
+ # a message is printed out
24
+ # PrcLib.core_level : lorj debug level. from 0 to 5.
25
+ # PrcLib.pdata_path : Define the private data local directory. Usually used
26
+ # for any private keys, passwords, etc...
27
+ # By default: ~/.config/<app_name>
28
+ # PrcLib.data_path : Define the data local directory.
29
+ # By default: ~/.<app_name>
30
+ # PrcLib.app_name : Define the application name. By default 'lorj'
31
+ # PrcLib.app_defaults : Used by Lorj::Config to load application default data.
32
+ # By default nil.
33
+ # PrcLib.log_file : Define the log file name used.
34
+ # By default, defined as ~/.<app_name>/<app_name>.log
35
+ # PrcLib.level : logger level used.
36
+ # Can be set at runtime, with PrcLib.set_level
37
+ # PrcLib.model : Model loaded.
19
38
  module PrcLib
39
+ # Check if dir exists and is fully accessible (rwx)
40
+ def self.dir_exists?(path)
41
+ return false unless File.exist?(path)
42
+
43
+ unless File.directory?(path)
44
+ msg = format("'%s' is not a directory. Please fix it.", path)
45
+
46
+ fatal_error(1, msg)
47
+ end
48
+ unless File.readable?(path) &&
49
+ File.writable?(path) &&
50
+ File.executable?(path)
51
+ msg = format("'%s is not a valid directory. "\
52
+ 'Check permissions and fix it.', path)
53
+
54
+ fatal_error(1, msg)
55
+ end
56
+ true
57
+ end
20
58
 
21
- def PrcLib.dir_exists?(path)
22
- if File.exists?(path)
23
- if not File.directory?(path)
24
- msg = "'%s' is not a directory. Please fix it." % path
25
- unless log_object.nil?
26
- log_object.fatal(1, msg)
27
- else
28
- raise msg
29
- end
30
- end
31
- if not File.readable?(path) or not File.writable?(path) or not File.executable?(path)
32
- msg = "%s is not a valid directory. Check permissions and fix it." % path
33
- unless log_object.nil?
34
- log_object.fatal(1, msg)
35
- else
36
- raise msg
37
- end
38
- end
39
- return true
40
- end
41
- false
42
- end
43
-
44
-
45
- def PrcLib.ensure_dir_exists(path)
46
- if not dir_exists?(path)
47
- FileUtils.mkpath(path) if not File.directory?(path)
48
- end
49
- end
50
-
51
-
52
- class << self
53
- attr_accessor :log, :core_level
54
- end
55
-
56
- module_function
57
-
58
- def data_path= v
59
- @data_path = File.expand_path(v) if not @data_path
60
- PrcLib.ensure_dir_exists(@data_path)
61
- end
62
-
63
- def data_path
64
- @data_path
65
- end
66
-
67
- def app_name= v
68
- @app_name = v if not @app_name
69
- end
70
-
71
- def app_name
72
- @app_name
73
- end
74
-
75
- def app_defaults= v
76
- @app_defaults = File.expand_path(v) if not @app_defaults
77
- end
78
-
79
- def app_defaults
80
- @app_defaults
81
- end
82
-
83
- def log_file= v
84
- sFile = File.basename(v)
85
- sDir = File.dirname(File.expand_path(v))
86
- if not File.exists?(sDir)
87
- raise "'%s' doesn't exist. Unable to create file '%s'" % [sDir, sFile]
88
- end
89
- @log_file = File.join(sDir, sFile)
90
- end
91
-
92
- def log_file
93
- @log_file
94
- end
95
-
96
- def level= v
97
-
98
- @level = v
99
- unless PrcLib.log.nil?
100
- PrcLib.set_level(v)
101
- end
102
- end
103
-
104
- def level
105
- @level
106
- end
107
-
108
- def lib_path=(v)
109
- @lib_path = v if @lib_path.nil?
110
- end
111
-
112
- def lib_path()
113
- @lib_path
114
- end
115
-
116
- def controller_path()
117
- File.expand_path(File.join(@lib_path, "providers"))
118
- end
119
-
120
- def process_path()
121
- File.join(@lib_path, "core_process")
122
- end
123
- end
59
+ def self.fatal_error(rc, msg)
60
+ fail msg if log.nil?
61
+ log.fatal(rc, msg)
62
+ end
63
+
64
+ # ensure dir exists and is fully accessible (rwx)
65
+ def self.ensure_dir_exists(path)
66
+ FileUtils.mkpath(path) unless dir_exists?(path)
67
+ rescue => e
68
+ fatal_error(1, e.message)
69
+ end
70
+
71
+ # Define module data for lorj library configuration
72
+ class << self
73
+ attr_accessor :log, :core_level
74
+ attr_reader :pdata_path, :data_path, :app_defaults, :log_file, :level,
75
+ :model
76
+ end
77
+
78
+ module_function
79
+
80
+ def pdata_path
81
+ return @pdata_path unless @pdata_path.nil?
82
+ @pdata_path = File.expand_path(File.join('~', '.config', app_name))
83
+ end
84
+
85
+ def app_name
86
+ @app_name = 'Lorj' unless @app_name
87
+ @app_name
88
+ end
89
+
90
+ def pdata_path=(v)
91
+ @pdata_path = File.expand_path(v) unless @pdata_path
92
+ PrcLib.ensure_dir_exists(@pdata_path)
93
+ begin
94
+ FileUtils.chmod(0700, @pdata_path) # no-op on windows
95
+ rescue => e
96
+ fatal_error(1, e.message)
97
+ end
98
+ end
99
+
100
+ def data_path
101
+ return @data_path unless @data_path.nil?
102
+
103
+ default_path = File.join('~', '.' + app_name)
104
+ @data_path = File.expand_path(default_path)
105
+ end
106
+
107
+ def data_path=(v)
108
+ @data_path = File.expand_path(v) unless @data_path
109
+ PrcLib.ensure_dir_exists(@data_path)
110
+ end
111
+
112
+ # TODO: Low. Be able to support multiple model.
113
+ def app_name=(v)
114
+ @app_name = v unless @app_name
115
+ @model = Lorj::Model.new
116
+ end
117
+
118
+ # TODO: Support for several defaults, depending on controllers loaded.
119
+ def app_defaults=(v)
120
+ return if @app_defaults
121
+
122
+ v = File.join(File.dirname(__FILE__), v) unless v[0] == '/'
123
+
124
+ @app_defaults = File.expand_path(v)
125
+ end
126
+
127
+ def log_file=(v)
128
+ file = File.basename(v)
129
+ dir = File.dirname(File.expand_path(v))
130
+ unless File.exist?(dir)
131
+ fail format("'%s' doesn't exist. Unable to create file '%s'", dir, file)
132
+ end
133
+ @log_file = File.join(dir, file)
134
+ end
135
+
136
+ def level=(v)
137
+ @level = v
124
138
 
139
+ PrcLib.level = v unless PrcLib.log.nil?
140
+ end
141
+
142
+ def lib_path=(v)
143
+ @lib_path = v if @lib_path.nil?
144
+ end
145
+
146
+ attr_reader :lib_path
147
+
148
+ def controller_path
149
+ File.expand_path(File.join(@lib_path, 'providers'))
150
+ end
151
+
152
+ def process_path
153
+ File.join(@lib_path, 'core_process')
154
+ end
155
+ end
125
156
 
157
+ # Redefine Object to add a boolean? function.
126
158
  class Object
127
159
  # Simplify boolean test on objects
128
160
  def boolean?