openshift-origin-controller 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. data/COPYRIGHT +1 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +12 -0
  4. data/README.md +3 -0
  5. data/Rakefile +9 -0
  6. data/app/controllers/app_events_controller.rb +115 -0
  7. data/app/controllers/application_templates_controller.rb +19 -0
  8. data/app/controllers/applications_controller.rb +214 -0
  9. data/app/controllers/base_controller.rb +367 -0
  10. data/app/controllers/cartridges_controller.rb +48 -0
  11. data/app/controllers/descriptors_controller.rb +23 -0
  12. data/app/controllers/dns_resolvable_controller.rb +35 -0
  13. data/app/controllers/domains_controller.rb +156 -0
  14. data/app/controllers/emb_cart_controller.rb +276 -0
  15. data/app/controllers/emb_cart_events_controller.rb +52 -0
  16. data/app/controllers/environment_controller.rb +11 -0
  17. data/app/controllers/estimates_controller.rb +71 -0
  18. data/app/controllers/gear_groups_controller.rb +53 -0
  19. data/app/controllers/gears_controller.rb +70 -0
  20. data/app/controllers/keys_controller.rb +96 -0
  21. data/app/controllers/legacy_broker_controller.rb +510 -0
  22. data/app/controllers/quickstarts_controller.rb +29 -0
  23. data/app/controllers/user_controller.rb +38 -0
  24. data/app/helpers/cartridge_helper.rb +25 -0
  25. data/app/helpers/legacy_broker_helper.rb +21 -0
  26. data/app/helpers/user_action_logger.rb +38 -0
  27. data/app/models/application.rb +1718 -0
  28. data/app/models/application_template.rb +27 -0
  29. data/app/models/cartridge_cache.rb +51 -0
  30. data/app/models/cloud_user.rb +334 -0
  31. data/app/models/component_instance.rb +228 -0
  32. data/app/models/connection_endpoint.rb +10 -0
  33. data/app/models/district.rb +210 -0
  34. data/app/models/domain.rb +234 -0
  35. data/app/models/gear.rb +376 -0
  36. data/app/models/group_instance.rb +306 -0
  37. data/app/models/key.rb +20 -0
  38. data/app/models/legacy_reply.rb +15 -0
  39. data/app/models/legacy_request.rb +126 -0
  40. data/app/models/link.rb +11 -0
  41. data/app/models/message.rb +10 -0
  42. data/app/models/name_server_cache.rb +46 -0
  43. data/app/models/optional_param.rb +12 -0
  44. data/app/models/param.rb +13 -0
  45. data/app/models/remote_job.rb +57 -0
  46. data/app/models/rest_application.rb +126 -0
  47. data/app/models/rest_application10.rb +106 -0
  48. data/app/models/rest_application12.rb +124 -0
  49. data/app/models/rest_application_estimate.rb +12 -0
  50. data/app/models/rest_application_template.rb +20 -0
  51. data/app/models/rest_cartridge10.rb +41 -0
  52. data/app/models/rest_cartridge11.rb +151 -0
  53. data/app/models/rest_domain.rb +43 -0
  54. data/app/models/rest_domain10.rb +42 -0
  55. data/app/models/rest_estimates.rb +16 -0
  56. data/app/models/rest_gear.rb +14 -0
  57. data/app/models/rest_gear_group.rb +26 -0
  58. data/app/models/rest_key.rb +24 -0
  59. data/app/models/rest_reply.rb +31 -0
  60. data/app/models/rest_user.rb +43 -0
  61. data/app/models/result_io.rb +67 -0
  62. data/app/models/usage_record.rb +37 -0
  63. data/app/models/validators/app_validator.rb +30 -0
  64. data/app/models/validators/key_validator.rb +30 -0
  65. data/app/models/validators/namespace_validator.rb +18 -0
  66. data/config/routes.rb +36 -0
  67. data/lib/controller_engine.rb +7 -0
  68. data/lib/openshift-origin-controller.rb +14 -0
  69. data/lib/openshift/application_container_proxy.rb +241 -0
  70. data/lib/openshift/auth_service.rb +101 -0
  71. data/lib/openshift/data_store.rb +33 -0
  72. data/lib/openshift/dns_service.rb +41 -0
  73. data/lib/openshift/mongo_data_store.rb +671 -0
  74. data/openshift-origin-controller.gemspec +42 -0
  75. data/rubygem-openshift-origin-controller.spec +274 -0
  76. data/test/cucumber/application-estimate.feature +25 -0
  77. data/test/cucumber/cartridge-10gen-mms-agent.feature +28 -0
  78. data/test/cucumber/cartridge-cron.feature +32 -0
  79. data/test/cucumber/cartridge-haproxy.feature +31 -0
  80. data/test/cucumber/cartridge-jenkins-build.feature +12 -0
  81. data/test/cucumber/cartridge-jenkins-client.feature +10 -0
  82. data/test/cucumber/cartridge-lifecycle-diy.feature +21 -0
  83. data/test/cucumber/cartridge-lifecycle-jbossas.feature +61 -0
  84. data/test/cucumber/cartridge-lifecycle-jbosseap.feature +61 -0
  85. data/test/cucumber/cartridge-lifecycle-jbossews10.feature +61 -0
  86. data/test/cucumber/cartridge-lifecycle-jenkins.feature +41 -0
  87. data/test/cucumber/cartridge-lifecycle-nodejs.feature +59 -0
  88. data/test/cucumber/cartridge-lifecycle-perl.feature +40 -0
  89. data/test/cucumber/cartridge-lifecycle-php.feature +106 -0
  90. data/test/cucumber/cartridge-lifecycle-python.feature +40 -0
  91. data/test/cucumber/cartridge-lifecycle-ruby18.feature +49 -0
  92. data/test/cucumber/cartridge-lifecycle-ruby19.feature +41 -0
  93. data/test/cucumber/cartridge-mongodb.feature +31 -0
  94. data/test/cucumber/cartridge-mysql.feature +30 -0
  95. data/test/cucumber/cartridge-php.feature +14 -0
  96. data/test/cucumber/cartridge-phpmyadmin.feature +32 -0
  97. data/test/cucumber/cartridge-postgresql.feature +32 -0
  98. data/test/cucumber/cartridge-runtime-extended-db.feature +64 -0
  99. data/test/cucumber/cartridge-runtime-extended-jboss.feature +24 -0
  100. data/test/cucumber/cartridge-runtime-extended-nodejs.feature +21 -0
  101. data/test/cucumber/cartridge-runtime-extended-perl.feature +18 -0
  102. data/test/cucumber/cartridge-runtime-extended-php.feature +19 -0
  103. data/test/cucumber/cartridge-runtime-extended-python.feature +18 -0
  104. data/test/cucumber/cartridge-runtime-extended-ruby.feature +22 -0
  105. data/test/cucumber/cartridge-runtime-standard-diy.feature +6 -0
  106. data/test/cucumber/cartridge-runtime-standard-jbossas.feature +7 -0
  107. data/test/cucumber/cartridge-runtime-standard-jbosseap.feature +7 -0
  108. data/test/cucumber/cartridge-runtime-standard-jbossews10.feature +7 -0
  109. data/test/cucumber/cartridge-runtime-standard-jenkins.feature +8 -0
  110. data/test/cucumber/cartridge-runtime-standard-nodejs.feature +7 -0
  111. data/test/cucumber/cartridge-runtime-standard-perl.feature +6 -0
  112. data/test/cucumber/cartridge-runtime-standard-php.feature +6 -0
  113. data/test/cucumber/cartridge-runtime-standard-python.feature +6 -0
  114. data/test/cucumber/cartridge-runtime-standard-ruby.feature +19 -0
  115. data/test/cucumber/cartridge-switchyard.feature +36 -0
  116. data/test/cucumber/descriptor.feature +40 -0
  117. data/test/cucumber/embedded.feature +44 -0
  118. data/test/cucumber/idler.feature +75 -0
  119. data/test/cucumber/misc/descriptor/manifest.yml +22 -0
  120. data/test/cucumber/misc/php/db_test.php +21 -0
  121. data/test/cucumber/openshift-node.feature +21 -0
  122. data/test/cucumber/rest-application-templates.feature +31 -0
  123. data/test/cucumber/rest-applications.feature +431 -0
  124. data/test/cucumber/rest-cartridge-types.feature +16 -0
  125. data/test/cucumber/rest-domains.feature +276 -0
  126. data/test/cucumber/rest-gears.feature +38 -0
  127. data/test/cucumber/rest-keys.feature +247 -0
  128. data/test/cucumber/rest-quickstarts.feature +27 -0
  129. data/test/cucumber/rest-workflow.feature +64 -0
  130. data/test/cucumber/step_definitions/api_steps.rb +369 -0
  131. data/test/cucumber/step_definitions/application-estimate-steps.rb +51 -0
  132. data/test/cucumber/step_definitions/application_steps.rb +215 -0
  133. data/test/cucumber/step_definitions/cartridge-10gen-mms-agent_steps.rb +11 -0
  134. data/test/cucumber/step_definitions/cartridge-cron_steps.rb +51 -0
  135. data/test/cucumber/step_definitions/cartridge-haproxy_steps.rb +30 -0
  136. data/test/cucumber/step_definitions/cartridge-jenkins_steps.rb +93 -0
  137. data/test/cucumber/step_definitions/cartridge-lifecycle-nodejs_steps.rb +30 -0
  138. data/test/cucumber/step_definitions/cartridge-mongodb_steps.rb +60 -0
  139. data/test/cucumber/step_definitions/cartridge-mysql_steps.rb +56 -0
  140. data/test/cucumber/step_definitions/cartridge-php_steps.rb +72 -0
  141. data/test/cucumber/step_definitions/cartridge-postgresql_steps.rb +59 -0
  142. data/test/cucumber/step_definitions/cartridge-switchyard_steps.rb +29 -0
  143. data/test/cucumber/step_definitions/client_steps.rb +12 -0
  144. data/test/cucumber/step_definitions/descriptor_step.rb +32 -0
  145. data/test/cucumber/step_definitions/idler_steps.rb +37 -0
  146. data/test/cucumber/step_definitions/node_steps.rb +203 -0
  147. data/test/cucumber/step_definitions/runtime_steps.rb +547 -0
  148. data/test/cucumber/step_definitions/runtime_url_steps.rb +46 -0
  149. data/test/cucumber/step_definitions/trap-user-extended_steps.rb +14 -0
  150. data/test/cucumber/step_definitions/trap-user_steps.rb +58 -0
  151. data/test/cucumber/support/00_setup_helper.rb +106 -0
  152. data/test/cucumber/support/app_helper.rb +243 -0
  153. data/test/cucumber/support/assertions.rb +52 -0
  154. data/test/cucumber/support/command_helper.rb +453 -0
  155. data/test/cucumber/support/dns_helper.rb +54 -0
  156. data/test/cucumber/support/env.rb +5 -0
  157. data/test/cucumber/support/process_helper.rb +44 -0
  158. data/test/cucumber/support/runtime_support.rb +440 -0
  159. data/test/cucumber/support/unused.rb +27 -0
  160. data/test/cucumber/support/user_helper.rb +37 -0
  161. data/test/cucumber/trap-user-extended.feature +53 -0
  162. data/test/cucumber/trap-user.feature +34 -0
  163. data/test/ddns/1.168.192-rev.db.init +13 -0
  164. data/test/ddns/HOWTO.txt +207 -0
  165. data/test/ddns/Kexample.com.+157+06142.key +1 -0
  166. data/test/ddns/Kexample.com.+157+06142.private +7 -0
  167. data/test/ddns/authconfig.rb +14 -0
  168. data/test/ddns/example.com.db.init +23 -0
  169. data/test/ddns/example.com.key +4 -0
  170. data/test/ddns/named.ca +52 -0
  171. data/test/ddns/named.conf +48 -0
  172. data/test/ddns/named.empty +10 -0
  173. data/test/ddns/named.localhost +10 -0
  174. data/test/ddns/named.loopback +11 -0
  175. data/test/ddns/named.rfc1912.zones +42 -0
  176. data/test/ddns/named.root.key +5 -0
  177. data/test/ddns/named_service.rb +127 -0
  178. data/test/unit/bind_dns_service_test.rb +167 -0
  179. data/test/unit/broker_auth_test.rb +28 -0
  180. metadata +545 -0
@@ -0,0 +1,10 @@
1
+ class ConnectionEndpoint < OpenShift::Model
2
+ attr_accessor :from_comp_inst, :to_comp_inst, :from_connector, :to_connector
3
+
4
+ def initialize(from_comp, to_comp, pub, sub)
5
+ self.from_comp_inst = from_comp.name
6
+ self.to_comp_inst = to_comp.name
7
+ self.from_connector = pub
8
+ self.to_connector = sub
9
+ end
10
+ end
@@ -0,0 +1,210 @@
1
+ class District < OpenShift::Model
2
+
3
+ attr_accessor :server_identities, :active_server_identities_size, :uuid, :creation_time, :available_capacity, :available_uids, :max_uid, :max_capacity, :externally_reserved_uids_size, :node_profile, :name
4
+ primary_key :uuid
5
+
6
+ def initialize()
7
+ end
8
+
9
+ def construct(name, node_profile=nil)
10
+ self.uuid = OpenShift::Model.gen_uuid
11
+ self.creation_time = DateTime::now().strftime
12
+ self.server_identities = {}
13
+ self.available_capacity = Rails.configuration.msg_broker[:districts][:max_capacity]
14
+ self.available_uids = []
15
+ self.available_uids.fill(0, Rails.configuration.msg_broker[:districts][:max_capacity]) {|i| i+Rails.configuration.msg_broker[:districts][:first_uid]}
16
+ self.max_uid = Rails.configuration.msg_broker[:districts][:max_capacity] + Rails.configuration.msg_broker[:districts][:first_uid] - 1
17
+ self.max_capacity = Rails.configuration.msg_broker[:districts][:max_capacity]
18
+ self.externally_reserved_uids_size = 0
19
+ self.active_server_identities_size = 0
20
+ self.name = name
21
+ self.node_profile = node_profile ? node_profile : "small"
22
+ end
23
+
24
+ def self.find(uuid)
25
+ hash = OpenShift::DataStore.instance.find_district(uuid, {:read => :primary})
26
+ return nil unless hash
27
+ hash_to_district(hash)
28
+ end
29
+
30
+ def self.find_by_name(name)
31
+ hash = OpenShift::DataStore.instance.find_district_by_name(name, {:read => :primary})
32
+ return nil unless hash
33
+ hash_to_district(hash)
34
+ end
35
+
36
+ def self.find_all()
37
+ data = OpenShift::DataStore.instance.find_all_districts()
38
+ return [] unless data
39
+ districts = data.map do |hash|
40
+ hash_to_district(hash)
41
+ end
42
+ districts
43
+ end
44
+
45
+ def self.find_available(node_profile=nil)
46
+ hash = OpenShift::DataStore.instance.find_available_district(node_profile)
47
+ return nil unless hash
48
+ hash_to_district(hash)
49
+ end
50
+
51
+ def delete()
52
+ if server_identities.empty?
53
+ OpenShift::DataStore.instance.delete_district(@uuid)
54
+ else
55
+ raise OpenShift::OOException.new("Couldn't destroy district '#{uuid}' because it still contains nodes")
56
+ end
57
+ end
58
+
59
+ def save()
60
+ OpenShift::DataStore.instance.save_district(@uuid, self.attributes)
61
+ @previously_changed = changes
62
+ @changed_attributes.clear
63
+ @new_record = false
64
+ @persisted = true
65
+ @deleted = false
66
+ self
67
+ end
68
+
69
+ def add_node(server_identity)
70
+ if server_identity
71
+ hash = OpenShift::DataStore.instance.find_district_with_node(server_identity)
72
+ unless hash
73
+ unless server_identities.has_key?(server_identity)
74
+ container = OpenShift::ApplicationContainerProxy.instance(server_identity)
75
+ begin
76
+ capacity = container.get_capacity
77
+ if capacity == 0
78
+ container_node_profile = container.get_node_profile
79
+ if container_node_profile == node_profile
80
+ container.set_district(@uuid, true)
81
+ server_identities[server_identity] = {"active" => true}
82
+ OpenShift::DataStore.instance.add_district_node(@uuid, server_identity)
83
+ else
84
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} is of node profile '#{container_node_profile}' and needs to be '#{node_profile}' to add to district '#{name}'")
85
+ end
86
+ else
87
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} already has apps on it")
88
+ end
89
+ rescue OpenShift::NodeException => e
90
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} could not be found")
91
+ end
92
+ else
93
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} already belongs to district: #{@uuid}")
94
+ end
95
+ else
96
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} already belongs to another district: #{hash["uuid"]}")
97
+ end
98
+ else
99
+ raise OpenShift::UserException.new("server_identity is required")
100
+ end
101
+ end
102
+
103
+ def remove_node(server_identity)
104
+ if server_identities.has_key?(server_identity)
105
+ unless server_identities[server_identity]["active"]
106
+ container = OpenShift::ApplicationContainerProxy.instance(server_identity)
107
+ capacity = container.get_capacity
108
+ if capacity == 0
109
+ if OpenShift::DataStore.instance.remove_district_node(@uuid, server_identity)
110
+ container.set_district('NONE', false)
111
+ server_identities.delete(server_identity)
112
+ else
113
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} could not be removed from district: #{@uuid}")
114
+ end
115
+ else
116
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} could not be removed from district: #{@uuid} because it still has apps on it")
117
+ end
118
+ else
119
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} from district: #{@uuid} must be deactivated before it can be removed")
120
+ end
121
+ else
122
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} doesn't belong to district: #{@uuid}")
123
+ end
124
+ end
125
+
126
+ def deactivate_node(server_identity)
127
+ if server_identities.has_key?(server_identity)
128
+ if server_identities[server_identity]["active"]
129
+ OpenShift::DataStore.instance.deactivate_district_node(@uuid, server_identity)
130
+ container = OpenShift::ApplicationContainerProxy.instance(server_identity)
131
+ container.set_district(@uuid, false)
132
+ server_identities[server_identity] = {"active" => false}
133
+ else
134
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} is already deactivated")
135
+ end
136
+ else
137
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} doesn't belong to district: #{@uuid}")
138
+ end
139
+ end
140
+
141
+ def activate_node(server_identity)
142
+ if server_identities.has_key?(server_identity)
143
+ unless server_identities[server_identity]["active"]
144
+ OpenShift::DataStore.instance.activate_district_node(@uuid, server_identity)
145
+ container = OpenShift::ApplicationContainerProxy.instance(server_identity)
146
+ container.set_district(@uuid, true)
147
+ server_identities[server_identity] = {"active" => true}
148
+ else
149
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} is already active")
150
+ end
151
+ else
152
+ raise OpenShift::OOException.new("Node with server identity: #{server_identity} doesn't belong to district: #{@uuid}")
153
+ end
154
+ end
155
+
156
+ def add_capacity(num_uids)
157
+ if num_uids > 0
158
+ additions = []
159
+ additions.fill(0, num_uids) {|i| i+max_uid+1}
160
+ OpenShift::DataStore.instance.add_district_uids(uuid, additions)
161
+ @available_capacity += num_uids
162
+ @max_uid += num_uids
163
+ @max_capacity += num_uids
164
+ @available_uids += additions
165
+ else
166
+ raise OpenShift::OOException.new("You must supply a positive number of uids to remove")
167
+ end
168
+ end
169
+
170
+ def remove_capacity(num_uids)
171
+ if num_uids > 0
172
+ subtractions = []
173
+ subtractions.fill(0, num_uids) {|i| i+max_uid-num_uids+1}
174
+ pos = 0
175
+ found_first_pos = false
176
+ available_uids.each do |available_uid|
177
+ if !found_first_pos && available_uid == subtractions[pos]
178
+ found_first_pos = true
179
+ elsif found_first_pos
180
+ unless available_uid == subtractions[pos]
181
+ raise OpenShift::OOException.new("Uid: #{subtractions[pos]} not found in order in available_uids. Can not continue!")
182
+ end
183
+ end
184
+ pos += 1 if found_first_pos
185
+ break if pos == subtractions.length
186
+ end
187
+ if !found_first_pos
188
+ raise OpenShift::OOException.new("Missing uid: #{subtractions[0]} in existing available_uids. Can not continue!")
189
+ end
190
+ OpenShift::DataStore.instance.remove_district_uids(uuid, subtractions)
191
+ @available_capacity -= num_uids
192
+ @max_uid -= num_uids
193
+ @max_capacity -= num_uids
194
+ @available_uids -= subtractions
195
+ else
196
+ raise OpenShift::OOException.new("You must supply a positive number of uids to remove")
197
+ end
198
+ end
199
+
200
+ private
201
+
202
+ def self.hash_to_district(hash)
203
+ district = self.new
204
+ hash.each do |k,v|
205
+ district.instance_variable_set("@#{k}", v)
206
+ end
207
+ district.reset_state
208
+ district
209
+ end
210
+ end
@@ -0,0 +1,234 @@
1
+ require 'validators/namespace_validator'
2
+ class Domain < OpenShift::UserModel
3
+ include ActiveModel::Validations
4
+ attr_accessor :uuid, :namespace, :user
5
+ primary_key :uuid
6
+ exclude_attributes :user
7
+ validates :namespace, :namespace => true
8
+ def initialize(namespace=nil, user=nil)
9
+ self.user = user
10
+ self.namespace = namespace
11
+ self.uuid = OpenShift::Model.gen_uuid
12
+ end
13
+ def save
14
+ resultIO = ResultIO.new
15
+ created = false
16
+ if not persisted?
17
+ resultIO.append(create())
18
+ created = true
19
+ else
20
+ resultIO.append(update())
21
+ end
22
+
23
+ begin
24
+ super(self.user.login)
25
+ rescue
26
+ delete_dns if created
27
+ raise
28
+ end
29
+ resultIO
30
+ end
31
+
32
+ def hasAccess?(user)
33
+ #TODO
34
+ #if user.domains.include? self.uuid
35
+ return true
36
+ #end
37
+ #return false
38
+ end
39
+
40
+ def hasFullAccess?(user)
41
+ #TODO
42
+ #if self.user.login == user.login
43
+ return true
44
+ #end
45
+ #return false
46
+ end
47
+
48
+ def self.find(user, id)
49
+ domain = super(user.login, id)
50
+ domain.user = user if domain
51
+ return nil unless domain
52
+ domain
53
+ end
54
+
55
+ def self.find_all(user, namespace=nil)
56
+ domains = super(user.login)
57
+ unless namespace
58
+ user.domains = domains
59
+ return domains
60
+ else
61
+ filtered_domains = nil
62
+ domains.each do |domain|
63
+ if domain.namespace == namespace
64
+ filtered_domains.push(domain)
65
+ end
66
+ end
67
+ return filtered_domains
68
+ end
69
+ end
70
+
71
+ def self.get(user, id)
72
+ user.domains.each do |domain|
73
+ return domain if domain.namespace == id
74
+ end if user.domains
75
+ return nil
76
+ end
77
+
78
+ def delete_dns
79
+ dns_service = OpenShift::DnsService.instance
80
+ begin
81
+ dns_service.deregister_namespace(self.namespace)
82
+ dns_service.publish
83
+ ensure
84
+ dns_service.close
85
+ end
86
+ end
87
+
88
+ def delete
89
+ Rails.logger.debug "Deleting domain #{self.namespace} uuid #{self.uuid}"
90
+ resultIO = ResultIO.new
91
+ delete_dns
92
+ super(user.login)
93
+ Rails.logger.debug "notifying the domain observer of domain delete"
94
+ notify_observers(:after_domain_destroy)
95
+ Rails.logger.debug "done notifying the domain observer"
96
+ resultIO
97
+ end
98
+
99
+ def self.namespace_available?(namespace)
100
+ Rails.logger.debug "Checking to see if namesspace #{namespace} is available"
101
+ dns_service = OpenShift::DnsService.instance
102
+ return dns_service.namespace_available?(namespace)
103
+ end
104
+
105
+ def self.hash_to_obj(hash)
106
+ domain = super(hash)
107
+ domain
108
+ end
109
+
110
+ private
111
+
112
+ def update
113
+ result_io = ResultIO.new
114
+ old_domain = Domain.find(self.user, self.uuid)
115
+ old_namespace = old_domain.namespace
116
+ Rails.logger.debug "Updating namespace for domain #{self.uuid} from #{old_namespace} to #{self.namespace}"
117
+ dns_service = OpenShift::DnsService.instance
118
+
119
+ begin
120
+ raise OpenShift::UserException.new("A namespace with name '#{self.namespace}' already exists", 103) unless dns_service.namespace_available?(self.namespace)
121
+ dns_service.register_namespace(self.namespace)
122
+ dns_service.deregister_namespace(old_namespace)
123
+ cloud_user = self.user
124
+ prepare_failures = []
125
+
126
+ cloud_user.applications.each do |app|
127
+ if app.domain.uuid == self.uuid
128
+ Rails.logger.debug "Updating namespace to #{self.namespace} for app: #{app.name}"
129
+ result = app.prepare_namespace_update(dns_service, self.namespace, old_namespace)
130
+ prepare_failures.push(app.name) unless result[:success]
131
+ result_io.append result[:result_io]
132
+ end
133
+ end if cloud_user.applications
134
+
135
+ complete_exception = nil
136
+ begin
137
+ cloud_user.applications.each do |app|
138
+ if app.domain.uuid == self.uuid
139
+ app.complete_namespace_update(self.namespace, old_namespace)
140
+ end
141
+ end if cloud_user.applications and prepare_failures.empty?
142
+ rescue Exception => e
143
+ complete_exception = e
144
+ Rails.logger.debug e.message
145
+ Rails.logger.debug e.backtrace
146
+ end
147
+
148
+ dns_exception = nil
149
+ begin
150
+ dns_service.publish if prepare_failures.empty? and !complete_exception
151
+ rescue Exception => e
152
+ dns_exception = e
153
+ Rails.logger.debug e.message
154
+ Rails.logger.debug e.backtrace
155
+ end
156
+
157
+ # Rollback incase of failures
158
+ if !prepare_failures.empty? or complete_exception or dns_exception
159
+ undo_prepare_failures = []
160
+ undo_prepare_err_msg = ""
161
+ cloud_user.applications.each do |app|
162
+ if app.domain.uuid == self.uuid
163
+ Rails.logger.debug "Undo namespace update to #{old_namespace} for app: #{app.name}"
164
+ result = app.prepare_namespace_update(dns_service, old_namespace, self.namespace)
165
+ undo_prepare_failures.push(app.name) unless result[:success]
166
+ result_io.append result[:result_io]
167
+ end
168
+ end if cloud_user.applications
169
+ undo_prepare_err_msg = "Undo namespace update failed: #{undo_prepare_failures.pretty_inspect.chomp}." unless undo_prepare_failures.empty?
170
+
171
+ undo_complete_err_msg = ""
172
+ begin
173
+ cloud_user.applications.each do |app|
174
+ if app.domain.uuid == self.uuid
175
+ app.complete_namespace_update(old_namespace, self.namespace)
176
+ end
177
+ end if cloud_user.applications
178
+ rescue Exception => e
179
+ undo_complete_err_msg = "Undo namespace update failed: #{e.message}."
180
+ Rails.logger.debug e.message
181
+ Rails.logger.debug e.backtrace
182
+ end
183
+
184
+ err_msg = "Error updating namespace: "
185
+ err_code = 143
186
+ if dns_exception
187
+ err_msg += dns_exception.message
188
+ err_code = dns_exception.code if dns_exception.respond_to?('code')
189
+ elsif complete_exception
190
+ err_msg += complete_exception.message
191
+ err_code = complete_exception.code if complete_exception.respond_to?('code')
192
+ else # prepare failures
193
+ err_msg += prepare_failures.pretty_inspect.chomp
194
+ end
195
+ err_msg += undo_prepare_err_msg + undo_complete_err_msg
196
+ raise OpenShift::NodeException.new(err_msg + " If the problem persists please contact support.", err_code)
197
+ end
198
+ Rails.logger.debug "Notifying domain observer of domain update"
199
+ notify_observers(:after_domain_update)
200
+ ensure
201
+ dns_service.close
202
+ end
203
+ result_io
204
+ end
205
+
206
+ def create
207
+ Rails.logger.debug "Creating domain #{self.uuid} with namespace #{self.namespace} for user #{self.user.login}"
208
+ resultIO = ResultIO.new
209
+ dns_service = OpenShift::DnsService.instance
210
+ begin
211
+ raise OpenShift::UserException.new("A namespace with name '#{self.namespace}' already exists", 103) unless dns_service.namespace_available?(self.namespace)
212
+ begin
213
+ Rails.logger.debug "Attempting to add namespace '#{@namespace}'"
214
+ dns_service.register_namespace(@namespace)
215
+ dns_service.publish
216
+ Rails.logger.debug "notifying the domain observer of domain create"
217
+ notify_observers(:after_domain_create)
218
+ Rails.logger.debug "done notifying the domain observer"
219
+ rescue Exception => e
220
+ Rails.logger.debug e
221
+ begin
222
+ Rails.logger.debug "Attempting to remove namespace '#{@namespace}' after failure to add user '#{self.user.login}'"
223
+ dns_service.deregister_namespace(@namespace)
224
+ dns_service.publish
225
+ ensure
226
+ raise
227
+ end
228
+ end
229
+ ensure
230
+ dns_service.close
231
+ end
232
+ resultIO
233
+ end
234
+ end