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
@@ -1,6 +1,27 @@
1
1
  #!/usr/bin/env ruby
2
-
3
- $APP_PATH = File.dirname(__FILE__)
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
+ app_path = File.dirname(__FILE__)
19
+
20
+ if ENV['LORJ_DEV']
21
+ require 'byebug'
22
+ lib_path = File.expand_path(File.join(app_path, '..', '..', 'lib'))
23
+ $LOAD_PATH << lib_path
24
+ end
4
25
  require 'lorj'
5
26
  require 'ansi'
6
27
 
@@ -9,85 +30,93 @@ require 'ansi'
9
30
  # This object is used to provide configuration data to lorj
10
31
 
11
32
  # The config search is:
12
- # 1- Application defaults (defaults.yaml) - Not defined by default. update the following line and create defaults.yaml
33
+ # 1- Application defaults (defaults.yaml) - Not defined by default. update the
34
+ # following line and create defaults.yaml
13
35
  # PrcLib.app_defaults = $APP_PATH
14
- # 2- Local defaults (~/.<Application Name>/config.yaml) - <Application Name> is 'Lorj' by default. Can be updated with following line.
36
+ # 2- Local defaults (~/.<Application Name>/config.yaml) - <Application Name> is
37
+ # 'Lorj' by default. Can be updated with following line.
15
38
  # PrcLib.app_name = 'myapp'
16
- # 3 - runtime. Those variables are set, with oConfig[key] = value
39
+ # 3 - runtime. Those variables are set, with config[key] = value
17
40
 
18
- oConfig = Lorj::Config.new() # Use Simple Config Object
41
+ config = Lorj::Config.new # Use Simple Config Object
19
42
 
20
43
  # You can use an account object, which add an extra account level
21
44
  # between runtime and config.yaml/app default
22
- # oConfig = Lorj::Account.new('MyAccount') #
23
-
45
+ # config = Lorj::Account.new('MyAccount') #
24
46
 
25
- # If you want to see what is happening in the framework, uncomment debug settings.
26
- # PrcLib.level = Logger::DEBUG # Printed out to your console.
27
- # PrcLib.core_level = 3 # framework debug levels.
47
+ # If you want to see what is happening in the framework, uncomment
48
+ # debug settings.
49
+ PrcLib.level = Logger::DEBUG # Printed out to your console.
50
+ PrcLib.core_level = 5 # framework debug levels.
28
51
 
29
52
  # Initialize the framework
30
- hProcesses = [ File.join($APP_PATH, 'process', 'students.rb')]
53
+ processes = [File.join(app_path, 'process', 'students.rb')]
31
54
 
32
- #~ oStudentCore = Lorj::Core.new( oConfig, hProcesses, :mock)
33
- oStudentCore = Lorj::Core.new( oConfig, hProcesses, File.join($APP_PATH, 'controller', 'yaml_students.rb'))
55
+ # ~ student_core = Lorj::Core.new( config, processes, :mock)
56
+ student_core = Lorj::Core.new(config,
57
+ processes,
58
+ File.join(app_path,
59
+ 'controller',
60
+ 'yaml_students.rb'))
34
61
 
35
- oStudentCore.Create(:connection, :connection_string => "/tmp/students.yaml")
62
+ student_core.create(:connection, :connection_string => '/tmp/students.yaml')
36
63
 
37
- puts ANSI.bold("Create 1st student:")
64
+ puts ANSI.bold('Create 1st student:')
38
65
 
39
66
  # Set the student name to use
40
- # There is different way to set them... Those lines do the same using config object. Choose what you want.
41
- oConfig.set(:first_name, 'Robert')
42
- oConfig[:last_name] = "Redford"
43
- oConfig[:student_name] = "Robert Redford"
44
- oConfig[:course] = 'Art Comedy'
67
+ # There is different way to set them...
68
+ # Those lines do the same using config object. Choose what you want.
69
+ config.set(:first_name, 'Robert')
70
+ config[:last_name] = 'Redford'
71
+ config[:student_name] = 'Robert Redford'
72
+ config[:course] = 'Art Comedy'
45
73
 
46
74
  # Ask the framework to create the object student 'Robert Redford'
47
- oStudentCore.Create(:student)
75
+ student_core.create(:student)
48
76
 
49
- puts ANSI.bold("Create 2nd student:")
77
+ puts ANSI.bold('Create 2nd student:')
50
78
  # We can set runtime configuration instantly from the Create call
51
79
  # The following line :
52
- oStudentCore.Create(:student, {
53
- student_name: 'Anthony Hopkins',
54
- first_name: 'Anthony',
55
- last_name: 'Hopkins',
56
- course: 'Art Drama'
57
- })
58
- # oConfig[:student_name] = "Anthony Hopkins"
59
- # oConfig[:course] = "Art Drama"
60
- # oStudentCore.Create(:student)
61
-
62
- puts ANSI.bold("Create 3rd student:")
63
- oStudentCore.Create(:student, {
64
- student_name: "Marilyn Monroe",
65
- first_name: 'Marilyn',
66
- last_name: 'Monroe',
67
- course: 'Art Drama'
68
- })
80
+ student_core.create(:student,
81
+ :student_name => 'Anthony Hopkins',
82
+ :first_name => 'Anthony',
83
+ :last_name => 'Hopkins',
84
+ :course => 'Art Drama'
85
+ )
86
+ # config[:student_name] = "Anthony Hopkins"
87
+ # config[:course] = "Art Drama"
88
+ # student_core.Create(:student)
89
+
90
+ puts ANSI.bold('Create 3rd student:')
91
+ student_core.create(:student,
92
+ :student_name => 'Marilyn Monroe',
93
+ :first_name => 'Marilyn',
94
+ :last_name => 'Monroe',
95
+ :course => 'Art Drama'
96
+ )
69
97
  # replaced the following :
70
- # oConfig[:student_name] = "Anthony Hopkins"
71
- # oStudentCore.Create(:student)
98
+ # config[:student_name] = "Anthony Hopkins"
99
+ # student_core.Create(:student)
72
100
 
73
- puts ANSI.bold("Create mistake")
74
- oStudent = oStudentCore.Create(:student, {
75
- :student_name => "Anthony Mistake",
76
- :first_name => 'Anthony',
77
- :last_name => 'Mistake',
78
- :course => 'what ever you want!!!'
79
- })
101
+ puts ANSI.bold('Create mistake')
102
+ student = student_core.create(:student,
103
+ :student_name => 'Anthony Mistake',
104
+ :first_name => 'Anthony',
105
+ :last_name => 'Mistake',
106
+ :course => 'what ever you want!!!'
107
+ )
80
108
 
81
- puts "Student created '%s'" % oStudent[:attrs]
109
+ puts format("Student created '%s'", student[:attrs])
82
110
 
83
111
  # Because the last student was the mistake one, we can directly delete it.
84
112
  # Usually, we use get instead.
85
- puts ANSI.bold("Remove mistake")
86
- oStudentCore.Delete(:student)
87
- puts "Wrong student to remove: %s = %s" % [oStudent[:id], oStudent[:student_name]]
113
+ puts ANSI.bold('Remove mistake')
114
+ student_core.delete(:student)
115
+ puts format('Wrong student to remove: %s = %s',
116
+ student[:id], student[:student_name])
88
117
 
89
118
  puts ANSI.bold("List of students for 'Art Drama':")
90
- puts oStudentCore.Query(:student, { :course => "Art Drama"}).to_a
119
+ puts student_core.query(:student, :course => 'Art Drama').to_a
91
120
 
92
- puts ANSI.bold("Deleted students:")
93
- puts oStudentCore.Query(:student,{ :status => :removed}).to_a
121
+ puts ANSI.bold('Deleted students:')
122
+ puts student_core.query(:student, :status => :removed).to_a
@@ -19,60 +19,60 @@
19
19
  require 'rubygems'
20
20
  require 'ansi'
21
21
 
22
- $APP_PATH = File.dirname(__FILE__)
22
+ app_path = File.expand_path(File.dirname(__FILE__))
23
23
 
24
- require File.join($APP_PATH, 'yaml_students.rb')
24
+ require File.join(app_path, 'yaml_students.rb')
25
25
 
26
26
  school = YamlSchool.new('/tmp/students.yaml')
27
27
 
28
- puts ANSI.bold("Create 1st student:")
29
- if school.query_student({:name => "Robert Redford"}).length == 0
30
- school.create_student("Robert Redford", {
31
- first_name: 'Robert',
32
- last_name: 'Redford',
33
- training: 'Art Comedy'
34
- })
28
+ puts ANSI.bold('Create 1st student:')
29
+ if school.query_student(:name => 'Robert Redford').length == 0
30
+ school.create_student('Robert Redford',
31
+ :first_name => 'Robert',
32
+ :last_name => 'Redford',
33
+ :training => 'Art Comedy'
34
+ )
35
35
  end
36
36
 
37
- puts ANSI.bold("Create 2nd student:")
38
- if school.query_student({:name => "Anthony Hopkins"}).length == 0
39
- school.create_student("Anthony Hopkins", {
40
- first_name: 'Anthony',
41
- last_name: 'Hopkins',
42
- training: 'Art Drama'
43
- })
37
+ puts ANSI.bold('Create 2nd student:')
38
+ if school.query_student(:name => 'Anthony Hopkins').length == 0
39
+ school.create_student('Anthony Hopkins',
40
+ :first_name => 'Anthony',
41
+ :last_name => 'Hopkins',
42
+ :training => 'Art Drama'
43
+ )
44
44
  end
45
45
 
46
- puts ANSI.bold("Create 3rd student:")
47
- if school.query_student({:name => "Marilyn Monroe"}).length == 0
48
- school.create_student("Marilyn Monroe", {
49
- first_name: 'Marilyn',
50
- last_name: 'Mistake',
51
- training: 'Art Drama'
52
- })
46
+ puts ANSI.bold('Create 3rd student:')
47
+ if school.query_student(:name => 'Marilyn Monroe').length == 0
48
+ school.create_student('Marilyn Monroe',
49
+ :first_name => 'Marilyn',
50
+ :last_name => 'Mistake',
51
+ :training => 'Art Drama'
52
+ )
53
53
  end
54
54
 
55
- puts ANSI.bold("Create mistake")
56
- oStudent = school.create_student("Anthony Mistake", {
57
- first_name: 'Anthony',
58
- last_name: 'Mistake',
59
- training: 'what ever you want!!!'
60
- })
55
+ puts ANSI.bold('Create mistake')
56
+ student = school.create_student('Anthony Mistake',
57
+ :first_name => 'Anthony',
58
+ :last_name => 'Mistake',
59
+ :training => 'what ever you want!!!'
60
+ )
61
61
 
62
- puts "Student created: '%s'" % oStudent
62
+ puts format("Student created: '%s'", student)
63
63
 
64
- puts ANSI.bold("Remove mistake")
65
- result = school.query_student({:name => "Anthony Mistake"})
64
+ puts ANSI.bold('Remove mistake')
65
+ result = school.query_student(:name => 'Anthony Mistake')
66
66
  if result.length > 0
67
- result.each { | student |
68
- puts "Wrong student to remove: %s = %s" % [student[:id], student[:name]]
69
- school.delete_student(student[:id])
70
- }
67
+ result.each do | a_student |
68
+ puts format('Wrong student to remove: %s = %s',
69
+ a_student[:id], a_student[:name])
70
+ school.delete_student(a_student[:id])
71
+ end
71
72
  end
72
73
 
73
-
74
74
  puts ANSI.bold("List of students for 'Art Drama':")
75
- puts school.query_student({ :training => "Art Drama"})
75
+ puts school.query_student(:training => 'Art Drama')
76
76
 
77
- puts ANSI.bold("Deleted students:")
78
- puts school.query_student({ :status => :removed})
77
+ puts ANSI.bold('Deleted students:')
78
+ puts school.query_student(:status => :removed)
@@ -14,102 +14,115 @@
14
14
  # See the License for the specific language governing permissions and
15
15
  # limitations under the License.
16
16
 
17
-
18
17
  # This class describes how to process some actions, and will do everything prior
19
18
  # this task to make it to work.
20
19
 
21
20
  require 'yaml'
22
21
 
22
+ # Simple School class saving data in a yaml file.
23
23
  class YamlSchool
24
- attr_accessor :sFile
25
- attr_accessor :hData
26
-
27
- def initialize(sFile)
28
- @hData = { :students => []}
29
- @sFile = sFile
30
- load_data()
31
- end
32
-
33
- def load_data()
34
- if File.exists?(@sFile)
35
- @hData = YAML.load_file(@sFile)
36
- @hData = { :students => []} if not @hData
37
- @hData[:students] = [] unless @hData.key?(:students)
38
- end
39
- end
40
-
41
- def save_data()
42
- begin
43
- File.open(@sFile, 'w') do |out|
44
- YAML.dump(@hData, out)
45
- end
46
- rescue => e
47
- Lorj.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
48
- return false
49
- end
50
- true
51
- end
52
-
53
- def create_student(name, fields)
54
-
55
- if fields[ :first_name].nil? or fields[ :last_name].nil?
56
- puts "YAML API: Unable to create a student. :first_name and :last_name required."
57
- return nil
24
+ attr_accessor :file
25
+ attr_accessor :data
26
+
27
+ def initialize(file)
28
+ @data = { :students => [] }
29
+ @file = file
30
+ load_data
31
+ end
32
+
33
+ def load_data
34
+ return false unless File.exist?(@file)
35
+
36
+ @data = YAML.load_file(@file)
37
+ @data = { :students => [] } unless @data
38
+ @data[:students] = [] unless @data.key?(:students)
39
+ end
40
+
41
+ def save_data
42
+ begin
43
+ File.open(@file, 'w') do |out|
44
+ YAML.dump(@data, out)
58
45
  end
59
-
60
- result = fields.clone
61
- result[:name] = name
62
- result[:name] = "%s %s" % [result[:first_name], result[:first_name]] if name.nil?
63
- result[:status] = :active
64
-
65
- @hData[:students] << result
66
-
67
- result[:id] = @hData[:students].length()-1
68
-
69
- save_data()
70
- result
71
- end
72
-
73
- def delete_student(sId)
74
- return false unless File.exists?(sFile)
75
-
76
- @hData[:students].each { | value |
77
- hElem = value
78
- if value[:id] == sId
79
- @hData[:students][sId][:status] = :removed
80
- save_data()
81
- return 1
82
- end
83
- }
84
- 0
85
- end
86
-
87
- def query_student(sQuery)
88
-
89
- result = []
90
-
91
- @hData[:students].each { | value |
92
- hElem = value
93
- sQuery.each { | query_key, query_value |
94
- hElem = nil if (
95
- value.key?(:status) and
96
- value[:status] != :active and
97
- not sQuery.key?(:status)) or
98
- not value.key?(query_key) or
99
- value[query_key] != query_value
100
- }
101
- result << hElem if hElem
102
- }
103
- result
104
-
105
- end
106
-
107
- def update_student(sId, fields)
108
- aList = query_student({ :id => Id})
109
- if aList.length == 1
110
- aList[0].merge(fields)
46
+ rescue => e
47
+ Lorj.error(format("%s\n%s", e.message, e.backtrace.join("\n")))
48
+ return false
49
+ end
50
+ true
51
+ end
52
+
53
+ def create_student(name, fields)
54
+ if fields[:first_name].nil? || fields[:last_name].nil?
55
+ puts 'YAML API: Unable to create a student. '\
56
+ ':first_name and :last_name required.'
57
+ return nil
58
+ end
59
+
60
+ result = create_data(name, fields)
61
+
62
+ save_data
63
+ result
64
+ end
65
+
66
+ def create_data(name, fields)
67
+ result = fields.clone
68
+ result[:name] = name
69
+ if name.nil?
70
+ result[:name] = format('%s %s', result[:first_name], result[:first_name])
71
+ end
72
+ result[:status] = :active
73
+
74
+ @data[:students] << result
75
+
76
+ result[:id] = @data[:students].length - 1
77
+ result
78
+ end
79
+
80
+ def delete_student(sId)
81
+ return false unless File.exist?(file)
82
+
83
+ @data[:students].each do | value |
84
+ next unless value[:id] == sId
85
+
86
+ @data[:students][sId][:status] = :removed
87
+ save_data
88
+ return 1
89
+ end
90
+ 0
91
+ end
92
+
93
+ def query_student(hQuery)
94
+ result = []
95
+
96
+ @data[:students].each do | hValue |
97
+ elem = hValue
98
+ hQuery.each do | query_key, query_value |
99
+ elem = nil if not_in_query?(hQuery, hValue, query_key, query_value)
111
100
  end
112
- save_data()
113
- end
114
-
101
+ result << elem if elem
102
+ end
103
+ result
104
+ end
105
+
106
+ # check query field and data value.
107
+ # Return true if the query do not match the current data
108
+ def not_in_query?(hQuery, hValue, query_key, query_value)
109
+ return true unless valid_status?(hQuery, hValue)
110
+ return true unless hValue.key?(query_key)
111
+ return true if hValue[query_key] != query_value
112
+ false
113
+ end
114
+
115
+ # check if status field is queried. if not consider status != :active.
116
+ # return true if status
117
+ def valid_status?(hQuery, hValue)
118
+ return false if hValue.key?(:status) && hValue[:status] != :active &&
119
+ !hQuery.key?(:status)
120
+ true
121
+ end
122
+
123
+ def update_student(_sId, fields)
124
+ list = query_student(:id => Id)
125
+ list[0].merge(fields) if list.length == 1
126
+ save_data
127
+ end
115
128
  end