lorj 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +14 -0
  3. data/.gitreview +4 -0
  4. data/Gemfile +25 -0
  5. data/Gemfile.lock +34 -0
  6. data/LICENSE.txt +14 -0
  7. data/README.md +652 -0
  8. data/Rakefile +24 -0
  9. data/bin/cloud_test.rb +81 -0
  10. data/example/students_1/process/Students.rb +20 -0
  11. data/example/students_1/students.rb +16 -0
  12. data/example/students_2/process/Students.rb +27 -0
  13. data/example/students_2/students.rb +36 -0
  14. data/example/students_3/controller/yaml_students.rb +94 -0
  15. data/example/students_3/controller/yaml_students_controller.rb +123 -0
  16. data/example/students_3/process/students.rb +118 -0
  17. data/example/students_3/students.rb +93 -0
  18. data/example/students_4/controller/yaml_students.rb +82 -0
  19. data/example/students_4/controller/yaml_students_controller.rb +141 -0
  20. data/example/students_4/process/students.rb +112 -0
  21. data/example/students_4/students.rb +103 -0
  22. data/example/yaml_students/students.rb +78 -0
  23. data/example/yaml_students/yaml_students.rb +115 -0
  24. data/lib/concept.md +111 -0
  25. data/lib/core/core.rb +723 -0
  26. data/lib/core/definition.rb +505 -0
  27. data/lib/core/definition_internal.rb +338 -0
  28. data/lib/core/lorj-basecontroller.rb +90 -0
  29. data/lib/core/lorj-basedefinition.rb +1079 -0
  30. data/lib/core/lorj-baseprocess.rb +231 -0
  31. data/lib/core/lorj-data.rb +567 -0
  32. data/lib/core/lorj-keypath.rb +115 -0
  33. data/lib/core_process/CloudProcess.rb +334 -0
  34. data/lib/core_process/global_process.rb +406 -0
  35. data/lib/core_process/network_process.rb +603 -0
  36. data/lib/img/.directory +4 -0
  37. data/lib/img/account_data_access.png +0 -0
  38. data/lib/img/config_data_access.png +0 -0
  39. data/lib/img/forj-lib-concept.png +0 -0
  40. data/lib/lorj/version.rb +3 -0
  41. data/lib/lorj.rb +51 -0
  42. data/lib/prc-account.rb +339 -0
  43. data/lib/prc-config.rb +1023 -0
  44. data/lib/prc-logging.rb +183 -0
  45. data/lib/prc.rb +108 -0
  46. data/lib/providers/hpcloud/Hpcloud.rb +419 -0
  47. data/lib/providers/hpcloud/compute.rb +108 -0
  48. data/lib/providers/hpcloud/network.rb +117 -0
  49. data/lib/providers/hpcloud/security_groups.rb +67 -0
  50. data/lib/providers/mock/Mock.rb +141 -0
  51. data/lib/providers/openstack/Openstack.rb +47 -0
  52. data/lib/providers/templates/compute.rb +42 -0
  53. data/lib/providers/templates/core.rb +61 -0
  54. data/lib/providers/templates/network.rb +33 -0
  55. data/lorj-spec/defaults.yaml +26 -0
  56. data/lorj.gemspec +39 -0
  57. data/spec/forj-account_spec.rb +75 -0
  58. data/spec/forj-config_spec.rb +196 -0
  59. metadata +164 -0
@@ -0,0 +1,338 @@
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
+ module Lorj
19
+ class BaseDefinition
20
+
21
+ private
22
+ # ------------------------------------------------------
23
+ # Class Definition internal function.
24
+ # ------------------------------------------------------
25
+
26
+ def _ask_encrypted(sDesc, sDefault)
27
+ # Checking key file used to encrypt/decrypt passwords
28
+ key_file = File.join($FORJ_CREDS_PATH, '.key')
29
+ if not File.exists?(key_file)
30
+ # Need to create a random key.
31
+ entr = {
32
+ :key => rand(36**10).to_s(36),
33
+ :salt => Time.now.to_i.to_s,
34
+ :iv => Base64::strict_encode64(OpenSSL::Cipher::Cipher.new('aes-256-cbc').random_iv)
35
+ }
36
+
37
+ Lorj.debug(2, "Writing '%s' key file" % key_file)
38
+ File.open(key_file, 'w') do |out|
39
+ out.write(Base64::encode64(entr.to_yaml))
40
+ end
41
+ else
42
+ Lorj.debug(2, "Loading '%s' key file" % key_file)
43
+ encoded_key = IO.read(key_file)
44
+ entr = YAML.load(Base64::decode64(encoded_key))
45
+ end
46
+
47
+ enc_value = sDefault
48
+
49
+ if not enc_value.nil?
50
+ begin
51
+ value_hidden = '*' * Encryptor.decrypt(
52
+ :value => Base64::strict_decode64(enc_value),
53
+ :key => entr[:key],
54
+ :iv => Base64::strict_decode64(entr[:iv]),
55
+ :salt => entr[:salt]
56
+ ).length
57
+ rescue => e
58
+ Lorj.error("Unable to decrypt your %s. You will need to re-enter it." % sDesc)
59
+ enc_value = ""
60
+ else
61
+ value_hidden="[%s]" % value_hidden
62
+ PrcLib.message("%s is already set. If you want to keep it, just press Enter" % [sDesc])
63
+ end
64
+ else
65
+ value_hidden = ""
66
+ end
67
+
68
+ value_free = ""
69
+ while value_free == ""
70
+ # ask for encrypted data.
71
+ value_free = ask("Enter %s: [%s]" % [sDesc, value_hidden]) do |q|
72
+ q.echo = '*'
73
+ end
74
+ if value_free == "" and enc_value
75
+ value_free = Encryptor.decrypt(
76
+ :value => Base64::strict_decode64(enc_value),
77
+ :key => entr[:key],
78
+ :iv => Base64::strict_decode64(entr[:iv]),
79
+ :salt => entr[:salt]
80
+ )
81
+ else
82
+ PrcLib.message("%s cannot be empty." % sDesc) if value_free == ""
83
+ end
84
+ end
85
+ enc_value = Base64::strict_encode64(
86
+ Encryptor.encrypt(
87
+ :value => value_free,
88
+ :key => entr[:key],
89
+ :iv => Base64::strict_decode64(entr[:iv]),
90
+ :salt => entr[:salt]
91
+ )
92
+ )
93
+ end
94
+
95
+ def _ask(sDesc, sDefault, rValidate, bEncrypted, bRequired)
96
+ if bEncrypted
97
+ value = _ask_encrypted(sDesc, sDefault)
98
+ if bRequired and value == ""
99
+ say "%sThis information is required!%s" % [ANSI.bold, ANSI.clear]
100
+ while value == ""
101
+ value = _ask_encrypted(sDesc, sDefault)
102
+ end
103
+ end
104
+ else
105
+ value = ask('Enter %s:' % [sDesc]) do |q|
106
+ q.default = sDefault unless sDefault.nil?
107
+ q.validate = rValidate unless rValidate.nil?
108
+ end
109
+ if bRequired and value == ""
110
+ say "%sThis information is required!%s" % [ANSI.bold, ANSI.clear]
111
+ while value == ""
112
+ value = ask('Enter %s:[%s]' % [sDesc, sDefault]) do |q|
113
+ q.default = sDefault unless sDefault.nil?
114
+ q.validate = rValidate unless rValidate.nil?
115
+ end
116
+ end
117
+ end
118
+ end
119
+ value.to_s
120
+ end
121
+
122
+ # return Object data meta data.
123
+ def _get_meta_data(sKey)
124
+
125
+ hMetaDefault = Lorj::Default.get_meta(sKey)
126
+ return nil if hMetaDefault.nil?
127
+ hMetaDefault = hMetaDefault.clone
128
+
129
+ sSection = Lorj::Default.get_meta_section(sKey)
130
+ return hMetaDefault if sSection.nil?
131
+ hMeta = Lorj::rhGet(@@meta_data, sSection, sKey)
132
+ return hMetaDefault if hMeta.nil?
133
+
134
+ hMetaDefault.merge!(hMeta)
135
+ end
136
+
137
+ def _return_map(sCloudObj, oControlerObject)
138
+ return nil if oControlerObject.nil?
139
+
140
+ attr_value = {}
141
+
142
+ pProc = Lorj::rhGet(@@meta_obj, sCloudObj, :lambdas, :get_attr_e)
143
+ bController = Lorj::rhGet(@@meta_obj, sCloudObj, :options, :controller)
144
+ return nil if not pProc and not bController
145
+
146
+ hMap = Lorj::rhGet(@@meta_obj, sCloudObj, :returns)
147
+ hMap.each { |key, map|
148
+ oKeyPath = KeyPath.new(key)
149
+ oMapPath = KeyPath.new(map)
150
+ next if not map
151
+ if pProc
152
+ Lorj::debug(4, "Calling process function '%s' to retrieve/map Controller object '%s' data " % [pProc, sCloudObj])
153
+ controller_attr_value = @oForjProcess.method(pProc).call(sCloudObj, oControlerObject)
154
+ else
155
+ Lorj::debug(4, "Calling controller function 'get_attr' to retrieve/map Controller object '%s' data " % [sCloudObj])
156
+ controller_attr_value = @oProvider.get_attr(oControlerObject, oMapPath.aTree) if bController
157
+ end
158
+
159
+ hValueMapping = Lorj::rhGet(@@meta_obj, sCloudObj, :value_mapping, oKeyPath.sFullPath)
160
+ if hValueMapping and not controller_attr_value.nil?
161
+ hValueMapping.each { | map_key, map_value |
162
+ if controller_attr_value == map_value
163
+ Lorj::rhSet(attr_value, map_key ,oKeyPath.aTree)
164
+ Lorj::debug(5, "Object '%s' value mapped '%s': '%s' => '%s'" % [sCloudObj, oKeyPath.aTree, controller_attr_value, map_value])
165
+ break
166
+ end
167
+ }
168
+ raise Lorj::PrcError.new(), "'%s.%s': No controller value mapping for '%s'." % [sCloudObj, oKeyPath.sKey, controller_attr_value] if attr_value.nil?
169
+ else
170
+ Lorj::debug(5, "Object '%s' value '%s' extracted: '%s'" % [sCloudObj, oKeyPath.aTree, controller_attr_value])
171
+ Lorj::rhSet(attr_value, controller_attr_value ,oKeyPath.aTree)
172
+ end
173
+ }
174
+ attr_value
175
+ end
176
+
177
+ def _build_data(sCloudObj, oParam, oKeyPath, hParams, bController = false)
178
+
179
+ sKey = oKeyPath.sKey
180
+ sDefault = Lorj::rhGet(hParams, :default_value)
181
+ if Lorj::rhExist?(hParams, :extract_from) == 1
182
+ value = oParam[hParams[:extract_from]]
183
+ end
184
+ value = @oForjConfig.get(sKey, sDefault) if not value
185
+
186
+ if bController
187
+ hValueMapping = Lorj::rhGet(@@meta_obj, sCloudObj, :value_mapping, oKeyPath.sFullPath)
188
+
189
+ # Mapping from Object/data definition
190
+ if hValueMapping
191
+ raise Lorj::PrcError.new(), "'%s.%s': No value mapping for '%s'" % [sCloudObj, sKey, value] if Lorj::rhExist?(hValueMapping, value) != 1
192
+ value = hValueMapping[value]
193
+ # Will be moved to the setup section or while setting it for a controller attached account.
194
+ #~ else
195
+ #~ # Or mapping from Config/data definition
196
+ #~ section = Lorj::Default.get_meta_section(sKey)
197
+ #~ section = :runtime if section.nil?
198
+ #~ hValueMapping = Lorj::rhGet(@@meta_data, section, sKey, :value_mapping)
199
+ #~ if hValueMapping
200
+ #~ raise PrcError.new(), "'%s.%s': No Config value mapping for '%s'" % [section, sKey, value] if Lorj::rhExist?(hValueMapping, value) != 1
201
+ #~ value = hValueMapping[value]
202
+ #~ end
203
+ end
204
+ if Lorj::rhExist?(hParams, :mapping) == 1
205
+ # NOTE: if mapping is set, the definition subtree
206
+ # is ignored.
207
+ # if key map to mykey
208
+ # [:section1][subsect][key] = value
209
+ # oParam => [:hdata][mykey] = value
210
+ # not oParam => [:hdata][:section1][subsect][mykey] = value
211
+ Lorj::rhSet(oParam[:hdata], value, Lorj::rhGet(hParams, :mapping))
212
+ end
213
+ end
214
+ oParam[oKeyPath.aTree] = value
215
+ end
216
+
217
+ def _get_object_params(sCloudObj, sEventType, fname, bController = false)
218
+
219
+ oParams = ObjectData.new(not(bController)) # hdata is built for controller. ie, ObjectData is NOT internal.
220
+
221
+ hTopParams= Lorj::rhGet(@@meta_obj,sCloudObj, :params)
222
+ hkeyPaths = Lorj::rhGet(hTopParams, :keys)
223
+ raise Lorj::PrcError.new(), "'%s' Object data needs not set. Forgot obj_needs?" % [sCloudObj] if hkeyPaths.nil?
224
+
225
+ if sEventType == :delete_e
226
+ if @ObjectData.exist?(sCloudObj)
227
+ oParams.add(@ObjectData[sCloudObj, :ObjectData])
228
+ end
229
+ end
230
+
231
+ hkeyPaths.each { | sKeypath, hParams|
232
+ next if not hParams[:for].include?(sEventType)
233
+ oKeyPath = KeyPath.new(sKeypath)
234
+ sKey = oKeyPath.sKey
235
+ case hParams[:type]
236
+ when :data
237
+ _build_data(sCloudObj, oParams, oKeyPath, hParams, bController)
238
+ when :CloudObject
239
+ #~ if hParams[:required] and Lorj::rhExist?(@CloudData, sKey, :object) != 2
240
+ if hParams[:required] and @ObjectData.type?(sKey) != :DataObject
241
+ raise Lorj::PrcError.new(), "Object '%s/%s' is not defined. '%s' requirement failed." % [ self.class, sKey, fname]
242
+ end
243
+ if @ObjectData.exist?(sKey)
244
+ oParams.add(@ObjectData[sKey, :ObjectData])
245
+ else
246
+ Lorj::debug(2, "The optional '%s' was not loaded" % sKey)
247
+ end
248
+ else
249
+ raise Lorj::PrcError.new(), "Undefined ObjectData '%s'." % [ hParams[:type]]
250
+ end
251
+ }
252
+ oParams
253
+ end
254
+
255
+ def _get_controller_map_value(keypath, sProcessValue)
256
+ section = Lorj::Default.get_meta_section(sData)
257
+ section = :runtime if section.nil?
258
+ oKeypath = KeyPath.new(keypath)
259
+ sKeyPath = oKeypath.sKeyPath
260
+ return nil if Lorj::rhExist?(@@meta_data, section, sKeyPath, :controller, sProcessValue) != 4
261
+ Lorj::rhGet(@@meta_data, section, sKeyPath, :controller, sProcessValue)
262
+ end
263
+
264
+ def _get_process_map_value(keypath, sControllerValue)
265
+ section = Lorj::Default.get_meta_section(sData)
266
+ section = :runtime if section.nil?
267
+ oKeypath = KeyPath.new(keypath)
268
+ sKeyPath = oKeypath.sKeyPath
269
+ return nil if Lorj::rhExist?(@@meta_data, section, sKeyPath, :process, sControllerValue) != 4
270
+ Lorj::rhGet(@@meta_data, section, sKeyPath, :process, sControllerValue)
271
+ end
272
+
273
+ def _check_required(sCloudObj, sEventType, fname)
274
+ aCaller = caller
275
+ aCaller.pop
276
+
277
+ oObjMissing=[]
278
+
279
+ hTopParams= Lorj::rhGet(@@meta_obj,sCloudObj, :params)
280
+ hkeyPaths = Lorj::rhGet(hTopParams, :keys)
281
+ raise Lorj::PrcError.new(), "'%s' Object data needs not set. Forgot obj_needs?" % [sCloudObj] if hkeyPaths.nil?
282
+
283
+ if sEventType == :delete_e
284
+ if @ObjectData.type?(sCloudObj) != :DataObject
285
+ oObjMissing << sCloudObj
286
+ end
287
+ end
288
+
289
+ hkeyPaths.each { | sKeypath, hParams|
290
+ next if not hParams[:for].include?(sEventType)
291
+ oKeyPath = KeyPath.new(sKeypath)
292
+
293
+ sKey = oKeyPath.sKey
294
+ case hParams[:type]
295
+ when :data
296
+ sDefault = Lorj::rhGet(hParams, :default_value)
297
+ if hParams[:required]
298
+ if hParams.key?(:extract_from)
299
+ if not @ObjectData.exist?(hParams[:extract_from])
300
+ raise Lorj::PrcError.new(), "key '%s' was not extracted from '%s'. '%s' requirement failed." % [ sKey, hParams[:extract_from], fname], aCaller
301
+ end
302
+ elsif @oForjConfig.get(sKey, sDefault).nil?
303
+ sSection = Lorj::Default.get_meta_section(sKey)
304
+ sSection = 'runtime' if not sSection
305
+ raise Lorj::PrcError.new(), "key '%s/%s' is not set. '%s' requirement failed." % [ sSection, sKey, fname], aCaller
306
+ end
307
+ end
308
+ when :CloudObject
309
+ #~ if hParams[:required] and Lorj::rhExist?(@CloudData, sKey, :object) != 2
310
+ if hParams[:required] and @ObjectData.type?(sKey) != :DataObject
311
+ oObjMissing << sKey
312
+ end
313
+ end
314
+ }
315
+ return oObjMissing
316
+ end
317
+
318
+ def _identify_data(sCloudObj, sEventType, sObjectType = :data)
319
+ aCaller = caller
320
+ aCaller.pop
321
+
322
+ aData = []
323
+
324
+ hTopParams= Lorj::rhGet(@@meta_obj,sCloudObj, :params)
325
+ hkeyPaths = Lorj::rhGet(hTopParams, :keys)
326
+ raise Lorj::PrcError.new(), "'%s' Object data needs not set. Forgot obj_needs?" % [sCloudObj] if hkeyPaths.nil?
327
+
328
+ hkeyPaths.each { | sKeypath, hParams|
329
+ next if not hParams[:for].include?(sEventType)
330
+ oKeyPath = KeyPath.new(sKeypath)
331
+
332
+ aData << oKeyPath.aTree if hParams[:type] == sObjectType
333
+ }
334
+ return aData
335
+ end
336
+
337
+ end
338
+ end
@@ -0,0 +1,90 @@
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
+ # Module Lorj which contains several classes.
19
+ #
20
+ # Those classes describes :
21
+ # - processes (BaseProcess) : How to create/delete/edit/query object.
22
+ # - controler (BaseControler) : If a provider is defined, define how will do object creation/etc...
23
+ # - definition(BaseDefinition): Functions to declare objects, query/data mapping and setup
24
+ # this task to make it to work.
25
+
26
+ module Lorj
27
+
28
+ class BaseController
29
+ # Default handlers which needs to be defined by the controller,
30
+ # called by BaseDefinition Create functions.
31
+ def connect(sObjectType, hParams)
32
+ raise Lorj::PrcError.new(), "connect has not been redefined by the controller '%s'" % self.class
33
+ end
34
+
35
+ # Default handlers which needs to be defined by the controller,
36
+ # called by BaseDefinition Create functions.
37
+ def create(sObjectType, hParams)
38
+ raise Lorj::PrcError.new(), "create_object has not been redefined by the controller '%s'" % self.class
39
+ end
40
+
41
+ # Default handlers which needs to be defined by the controller,
42
+ # called by BaseDefinition Delete functions.
43
+ def delete(sObjectType, hParams)
44
+ raise Lorj::PrcError.new(), "delete_object has not been redefined by the controller '%s'" % self.class
45
+ end
46
+
47
+ # Default handlers which needs to be defined by the controller,
48
+ # called by BaseDefinition Get functions.
49
+ def get(sObjectType, sUniqId, hParams)
50
+ raise Lorj::PrcError.new(), "get_object has not been redefined by the controller '%s'" % self.class
51
+ end
52
+
53
+ # Default handlers which needs to be defined by the controller,
54
+ # called by BaseDefinition Query functions.
55
+ def query(sObjectType, sQuery, hParams)
56
+ raise Lorj::PrcError.new(), "query_object has not been redefined by the controller '%s'" % self.class
57
+ end
58
+
59
+ # Default handlers which needs to be defined by the controller,
60
+ # called by BaseDefinition Update functions.
61
+ def update(sObjectType, oObject, hParams)
62
+ raise Lorj::PrcError.new(), "update_object has not been redefined by the controller '%s'" % self.class
63
+ end
64
+
65
+ # Simply raise an error
66
+ #
67
+ # * *Args* :
68
+ # - +Msg+ : Error message to print out.
69
+ # * *Returns* :
70
+ # - nil
71
+ # * *Raises* :
72
+ # - Lorj::PrcError
73
+ def Error(msg)
74
+ raise Lorj::PrcError.new(), "%s: %s" % [self.class, msg]
75
+ end
76
+
77
+ # check if required data is loaded. raise an error if not
78
+ #
79
+ # * *Args* :
80
+ # - +Params+ : Lorj::ObjectData object for controller.
81
+ # - +key+ : Key to check.
82
+ # * *Returns* :
83
+ # - nil
84
+ # * *Raises* :
85
+ # - +Error+ if the key do not exist.
86
+ def required?(oParams, *key)
87
+ raise Lorj::PrcError.new(), "%s: %s is not set." % [self.class, key] if not oParams.exist?(key)
88
+ end
89
+ end
90
+ end