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,339 @@
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
+
20
+ module Lorj
21
+
22
+ class Accounts
23
+ # Class to query FORJ Accounts list.
24
+ def initialize()
25
+ end
26
+
27
+ def dump()
28
+ aAccounts=[]
29
+ Dir.foreach($FORJ_ACCOUNTS_PATH) { |x| aAccounts << x if not x.match(/^\..?$/) }
30
+ aAccounts
31
+ end
32
+ end
33
+
34
+ # Lorj::Account manage a list of key/value grouped by section.
35
+ # The intent of Lorj::Account is to attach some keys/values to
36
+ # an account to help end users to switch between each of them.
37
+ #
38
+ # Lorj::Account based on ForjConfig (see forj-config.rb)
39
+ # ensure ForjConfig and Lorj::Account defines following common functions
40
+ # - set (key, value)
41
+ # - get (key)
42
+ #
43
+ # This means that key HAVE to be unique across sections
44
+ # By default, keys maps with the same key name in ForjConfig.
45
+ # But we can redefine the ForjConfig mapping of any key on need.
46
+ #
47
+ # ForjConfig, loads Account meta structure from defaults.yaml, sections
48
+ #
49
+ # defaults.yaml structure is:
50
+ # sections:
51
+ # default: => defines key/values recognized by Lorj::Account to be only managed by ForjConfig.
52
+ # <key> :
53
+ # :desc : <value> => defines the ForjConfig key description.
54
+ # <section>: Define a section name. For each keys on this section, the account file will kept those data under this section.
55
+ # <key>:
56
+ # :desc: defines the key description.
57
+ # :readonly: true if this key cannot be updated by Lorj::Account.set
58
+ # :account_exclusive: true if this key cannot be predefined on ForjConfig keys list
59
+ # :default: <ForjConfig real key name> Used to map the Lorj::Account key to a different ForjConfig key name.
60
+
61
+ class Account
62
+
63
+ attr_reader :sAccountName
64
+ attr_reader :hAccountData
65
+ attr_reader :oConfig
66
+
67
+ # This object manage data located in oConfig[:hpc_accounts/AccountName]
68
+
69
+ def initialize(oConfig = nil)
70
+ # Initialize object
71
+ if oConfig.nil?
72
+ @oConfig = Lorj::Config.new()
73
+ elsif oConfig.is_a?(String)
74
+ @oConfig = Lorj::Config.new()
75
+ @oConfig[:account_name] = oConfig
76
+ else
77
+ @oConfig = oConfig
78
+ end
79
+
80
+ if @oConfig.exist?(:account_name)
81
+ @sAccountName = @oConfig[:account_name]
82
+ else
83
+ @sAccountName = 'lorj'
84
+ end
85
+ @sAccountFile = File.join(PrcLib.data_path, 'accounts', @sAccountName)
86
+
87
+ sProvider = 'lorj'
88
+ sProvider = @oConfig.get(:provider) if @oConfig.get(:provider)
89
+
90
+ @hAccountData = {}
91
+ _set(:account, :name, @sAccountName) if exist?(:name) != 'hash'
92
+ _set(:account, :provider, sProvider) if exist?(:provider) != 'hash'
93
+
94
+ PrcLib.ensure_dir_exists(File.join(PrcLib.data_path, 'accounts'))
95
+ end
96
+
97
+ # oForjAccount data get at several levels:
98
+ # - runtime : get the data from runtime (runtimeSet/runtimeGet)
99
+ # - Account : otherwise, get data from account file under section described in defaults.yaml (:account_section_mapping), as soon as this mapping exists.
100
+ # - local : otherwise, get the data from the local configuration file. Usually ~/.forj/config.yaml
101
+ # - application: otherwise, get the data from defaults.yaml (class Default)
102
+ # - default : otherwise, use the get default parameter as value. Default is nil.
103
+ #
104
+ # * *Args* :
105
+ # - +key+ : key name. It do not support it to be a key tree (Arrays of keys).
106
+ # - +default+ : default value, if not found.
107
+ # * *Returns* :
108
+ # - key value.
109
+ # * *Raises* :
110
+ # Nothing
111
+ def get(key, default = nil)
112
+ return nil if not key
113
+
114
+ key = key.to_sym if key.class == String
115
+
116
+ return @oConfig.runtimeGet(key) if @oConfig.runtimeExist?(key)
117
+
118
+ section = Lorj::Default.get_meta_section(key)
119
+ default_key = key
120
+
121
+ if not section
122
+ PrcLib.debug("Lorj::Account.get: Unable to get account data '%s'. No section found. check defaults.yaml." % [key])
123
+ else
124
+ return Lorj::rhGet(@hAccountData, section, key) if Lorj::rhExist?(@hAccountData, section, key) == 2
125
+
126
+ hMeta = @oConfig.getAppDefault(:sections)
127
+ if Lorj::rhExist?(hMeta, section, key, :default) == 3
128
+ default_key = Lorj::rhGet(hMeta, section, key, :default)
129
+ PrcLib.debug("Lorj::Account.get: Reading default key '%s' instead of '%s'" % [default_key, key])
130
+ end
131
+ return default if Lorj::rhExist?(hMeta, section, key, :account_exclusive) == 3
132
+ end
133
+
134
+ @oConfig.get(default_key , default )
135
+ end
136
+
137
+ def [](key, default = nil)
138
+ get(key, default)
139
+ end
140
+
141
+ # check key/value existence in the following order:
142
+ # - runtime : get the data from runtime (runtimeSet/runtimeGet)
143
+ # - Account : otherwise, get data from account file under section described in defaults.yaml (:account_section_mapping), as soon as this mapping exists.
144
+ # - local : otherwise, get the data from the local configuration file. Usually ~/.forj/config.yaml
145
+ # - application: otherwise, get the data from defaults.yaml (class Default)
146
+ #
147
+ # * *Args* :
148
+ # - +key+ : key name. It do not support it to be a key tree (Arrays of keys).
149
+ # - +default+ : default value, if not found.
150
+ # * *Returns* :
151
+ # - 'runtime' : if found in runtime.
152
+ # - '<AccountName>' : if found in the Account data structure.
153
+ # - 'local' : if found in the local configuration file. Usually ~/.forj/config.yaml
154
+ # - 'default' : if found in the Application default (File 'defaults.yaml') (class Default)
155
+ # * *Raises* :
156
+ # Nothing
157
+
158
+ def exist?(key)
159
+ return nil if not key
160
+
161
+ key = key.to_sym if key.class == String
162
+ section = Lorj::Default.get_meta_section(key)
163
+ if not section
164
+ PrcLib.debug("Lorj::Account.exist?: No section found for key '%s'." % [key])
165
+ return nil
166
+ end
167
+
168
+ return 'runtime' if @oConfig.runtimeExist?(key)
169
+
170
+ return @sAccountName if Lorj::rhExist?(@hAccountData, section, key) == 2
171
+
172
+ hMeta = @oConfig.getAppDefault(:sections)
173
+ if Lorj::rhExist?(hMeta, section, key, :default) == 3
174
+ default_key = Lorj::rhGet(hMeta, section, key, :default)
175
+ PrcLib.debug("Lorj::Account.exist?: Reading default key '%s' instead of '%s'" % [default_key, key])
176
+ else
177
+ default_key = key
178
+ end
179
+ return nil if Lorj::rhExist?(hMeta, section, key, :account_exclusive) == 3
180
+
181
+ @oConfig.exist?(default_key)
182
+
183
+ end
184
+
185
+ # Return true if readonly. set won't be able to update this value.
186
+ # Only _set (private function) is able.
187
+ #
188
+ # * *Args* :
189
+ # - +key+ : key name. It can support it to be a key tree (Arrays of keys).
190
+ # * *Returns* :
191
+ def readonly?(key)
192
+ return nil if not key
193
+
194
+ key = key.to_sym if key.class == String
195
+ section = Lorj::Default.get_meta_section(key)
196
+
197
+ Lorj::rhGet(@oConfig.getAppDefault(:sections, section), key, :readonly)
198
+
199
+ end
200
+
201
+ def meta_set(key, hMeta)
202
+ key = key.to_sym if key.class == String
203
+ section = Lorj::Default.get_meta_section(key)
204
+ hCurMeta = Lorj::rhGet(@oConfig.getAppDefault(:sections, section), key)
205
+ hMeta.each { | mykey, myvalue |
206
+ Lorj::rhSet(hCurMeta, myvalue, mykey)
207
+ }
208
+ end
209
+
210
+ def meta_exist?(key)
211
+ return nil if not key
212
+
213
+ key = key.to_sym if key.class == String
214
+ section = Lorj::Default.get_meta_section(key)
215
+ Lorj::rhExist?(@oConfig.getAppDefault(:sections, section), key) == 1
216
+ end
217
+
218
+ def get_meta_section(key)
219
+ key = key.to_sym if key.class == String
220
+ Lorj::rhGet(@account_section_mapping, key)
221
+ end
222
+
223
+ def meta_type?(key)
224
+ return nil if not key
225
+
226
+ section = Lorj::Default.get_meta_section(key)
227
+
228
+ return section if section == :default
229
+ @sAccountName
230
+ end
231
+
232
+ # Loop on account metadata
233
+ def metadata_each
234
+ Lorj::rhGet(Lorj::Default.dump(), :sections).each { | section, hValue |
235
+ next if section == :default
236
+ hValue.each { | key, value |
237
+ yield section, key, value
238
+ }
239
+ }
240
+ end
241
+
242
+ # Return true if exclusive
243
+ def exclusive?(key)
244
+ return nil if not key
245
+
246
+ key = key.to_sym if key.class == String
247
+ section = Lorj::Default.get_meta_section(key)
248
+
249
+ Lorj::rhGet(@oConfig.getAppDefault(:sections, section), key, :account_exclusive)
250
+ end
251
+
252
+ # This function update a section/key=value if the account structure is defined.
253
+ # If no section is defined, set it in runtime config.
254
+ def set(key, value)
255
+ return nil if not key
256
+
257
+ key = key.to_sym if key.class == String
258
+ section = Lorj::Default.get_meta_section(key)
259
+
260
+ return @oConfig.set(key, value) if not section
261
+ return nil if readonly?(key)
262
+ _set(section, key, value)
263
+ end
264
+
265
+ def []=(key, value)
266
+ set(key, value)
267
+ end
268
+
269
+ def del(key)
270
+ return nil if not key
271
+
272
+ key = key.to_sym if key.class == String
273
+ section = Lorj::Default.get_meta_section(key)
274
+ return nil if not section
275
+ Lorj::rhSet(@hAccountData, nil, section, key)
276
+ end
277
+
278
+ def getAccountData(section, key, default=nil)
279
+ return Lorj::rhGet(@hAccountData, section, key) if Lorj::rhExist?(@hAccountData, section, key) == 2
280
+ default
281
+ end
282
+
283
+ def ac_new(sAccountName)
284
+ return nil if sAccountName.nil?
285
+ @sAccountName = sAccountName
286
+ @sAccountFile = File.join($FORJ_ACCOUNTS_PATH, @sAccountName)
287
+
288
+ @hAccountData = {:account => {:name => sAccountName, :provider => @oConfig.get(:provider_name)}}
289
+ end
290
+
291
+ # Load Account Information
292
+ def ac_load(sAccountName = @sAccountName)
293
+
294
+ if sAccountName != @sAccountName
295
+ ac_new(sAccountName)
296
+ end
297
+
298
+ if File.exists?(@sAccountFile)
299
+ @hAccountData = @oConfig.extraLoad(@sAccountFile, :forj_accounts, @sAccountName)
300
+ # Check if hAccountData are using symbol or needs to be updated.
301
+ sProvider = @oConfig.get(:provider, 'hpcloud')
302
+ Lorj::rhSet(@hAccountData, @sAccountName, :account, :name) if Lorj::rhExist?(@hAccountData, :account, :name) != 2
303
+ Lorj::rhSet(@hAccountData, sProvider, :account, :provider) if Lorj::rhExist?(@hAccountData, :account, :provider) != 2
304
+
305
+ if Lorj::rhKeyToSymbol?(@hAccountData, 2)
306
+ @hAccountData = Lorj::rhKeyToSymbol(@hAccountData, 2)
307
+ self.ac_save()
308
+ end
309
+ return @hAccountData
310
+ end
311
+ nil
312
+ end
313
+
314
+ def dump()
315
+ { :forj_account => @hAccountData }
316
+ end
317
+
318
+ # Account save function.
319
+ # Use set/get to manage those data that you will be able to save in an account file.
320
+ def ac_save()
321
+ @oConfig.extraSet(:forj_accounts, @sAccountName, nil, @hAccountData)
322
+ @oConfig.extraSave(@sAccountFile, :forj_accounts, @sAccountName)
323
+
324
+ if not @oConfig.localDefaultExist?('account_name')
325
+ @oConfig.localSet('account_name',@sAccountName)
326
+ @oConfig.saveConfig
327
+ end
328
+ end
329
+
330
+ # private functions
331
+ private
332
+ def _set(section, key, value)
333
+ return nil if not key or not section
334
+
335
+ Lorj::rhSet(@hAccountData, value, section, key)
336
+ end
337
+
338
+ end
339
+ end