occi 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,310 +0,0 @@
1
- ##############################################################################
2
- # Copyright 2011 Service Computing group, TU Dortmund
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- ##############################################################################
16
-
17
- ##############################################################################
18
- # Description: EC2 Backend
19
- # Author(s): Max Guenther
20
- ##############################################################################
21
-
22
- require 'aws-sdk'
23
- require 'occi/log'
24
-
25
- module OCCI
26
- module Backend
27
- module EC2
28
-
29
- # ---------------------------------------------------------------------------------------------------------------------
30
- module Compute
31
-
32
- # ---------------------------------------------------------------------------------------------------------------------
33
- private
34
- # ---------------------------------------------------------------------------------------------------------------------
35
-
36
- def get_backend_instance(compute)
37
- # get the ec2 interface
38
- ec2 = OCCI::Backend::EC2.get_ec2_interface()
39
-
40
- # get the ec2 backend instance
41
- backend_instance = ec2.instances[compute.backend[:id]]
42
-
43
- raise "Problems refreshing compute instance: An instance with the EC2 ID #{compute.backend[:id]} could not be found." if backend_instance.nil?
44
- # return the instance
45
- return backend_instance
46
- end
47
-
48
- def has_console_link(compute)
49
- compute.links.each do |link|
50
- OCCI::Log.debug("Link: #{link}")
51
- if link.kind.term == "console"
52
- return true
53
- end
54
- end
55
- return false
56
- end
57
-
58
- def key_pair_exists(key_name)
59
- ec2 = OCCI::Backend::EC2.get_ec2_interface()
60
-
61
- ec2.key_pairs.each do |key|
62
- if key.name == "default_occi_key"
63
- return true
64
- end
65
- end
66
- return false
67
- end
68
-
69
- public
70
- # ---------------------------------------------------------------------------------------------------------------------
71
-
72
- # ---------------------------------------------------------------------------------------------------------------------
73
- def compute_deploy(compute)
74
- OCCI::Log.debug("Deploying EC2 instance.")
75
-
76
- # look what template and instance type to use
77
- image_id = "ami-973b06e3" # fallback os template
78
- instance_type = "t1.micro" # fallback resource template
79
- compute.mixins.each do |mixin|
80
- mixin.related.each do |related|
81
- if related.term == "os_tpl"
82
- image_id = mixin.term
83
- break # use the first template found
84
- elsif related.term == "resource_tpl"
85
- instance_type = mixin.term
86
- break # use the first template found
87
- end
88
- end
89
- end
90
-
91
- # get the ec2 interface
92
- ec2 = OCCI::Backend::EC2.get_ec2_interface()
93
-
94
- # create an instance
95
- if key_pair_exists("default_occi_key")
96
- backend_instance = ec2.instances.create(:image_id => image_id,
97
- :instance_type => instance_type,
98
- :key_name => "default_occi_key")
99
- else
100
- backend_instance = ec2.instances.create(:image_id => image_id,
101
- :instance_type => instance_type)
102
- end
103
-
104
- # save the id of the instance
105
- compute.backend[:id] = backend_instance.id
106
-
107
- # link it to the private ec2 network
108
- OCCI::Log.debug("Linking instance to \"/network/ec2_private_network\".")
109
- private_network = OCCI::Rendering::HTTP::LocationRegistry.get_object("/network/ec2_private_network")
110
- attributes = OCCI::Core::Attributes.new()
111
- attributes["occi.networkinterface.interface"] = ""
112
- attributes["occi.core.source"] = compute.get_location
113
- attributes["occi.core.target"] = "/network/ec2_private_network"
114
- ipnetwork = OCCI::CategoryRegistry.get_by_id("http://schemas.ogf.org/occi/infrastructure/networkinterface#ipnetworkinterface")
115
- ipnetwork.backend[:network] = "private"
116
- mixins = [ipnetwork]
117
- private_networkinterface = OCCI::Infrastructure::Networkinterface.new(attributes, mixins)
118
- # save the id of the compute backend instance in the network link for future identification
119
- private_networkinterface.backend[:backend_id] = backend_instance.id
120
- private_networkinterface.backend[:network] = "ec2_private_network"
121
- compute.links << private_networkinterface
122
- private_network.links << private_networkinterface
123
- OCCI::Rendering::HTTP::LocationRegistry.register(private_networkinterface.get_location, private_networkinterface)
124
-
125
- # link it to the public ec2 network
126
- OCCI::Log.debug("Linking instance to \"/network/ec2_public_network\".")
127
- public_network = OCCI::Rendering::HTTP::LocationRegistry.get_object("/network/ec2_public_network")
128
- attributes = OCCI::Core::Attributes.new()
129
- attributes["occi.networkinterface.interface"] = ""
130
- attributes["occi.core.source"] = compute.get_location
131
- attributes["occi.core.target"] = "/network/ec2_public_network"
132
- ipnetwork = OCCI::CategoryRegistry.get_by_id("http://schemas.ogf.org/occi/infrastructure/networkinterface#ipnetworkinterface")
133
- ipnetwork.backend[:network] = "ec2_public_network"
134
- mixins = [ipnetwork]
135
- public_networkinterface = OCCI::Infrastructure::Networkinterface.new(attributes, mixins)
136
- # save the id of the compute backend instance in the network link for future identification
137
- public_networkinterface.backend[:backend_id] = backend_instance.id
138
- public_networkinterface.backend[:network] = "public"
139
- compute.links << public_networkinterface
140
- public_network.links << public_networkinterface
141
- OCCI::Rendering::HTTP::LocationRegistry.register(public_networkinterface.get_location, public_networkinterface)
142
-
143
- OCCI::Log.debug("Deployed EC2 instance with EC2 ID: #{compute.backend[:id]}")
144
- end
145
-
146
- # ---------------------------------------------------------------------------------------------------------------------
147
- def compute_refresh(compute)
148
- OCCI::Log.debug("Refreshing EC2 compute object with backend ID: #{compute.backend[:id]}")
149
-
150
- # get the ec2 backend instance
151
- backend_instance = get_backend_instance(compute)
152
-
153
- # check if there are any problems with the backend instance
154
- if backend_instance.nil?
155
- OCCI::Log.debug("Problems refreshing compute instance: An instance with the EC2 ID #{compute.backend[:id]} could not be found.")
156
- return
157
- end
158
-
159
- # update the state
160
- compute_update_state(compute)
161
- OCCI::Log.debug("Refreshed EC2 compute object with backend ID: #{compute.backend[:id]}")
162
-
163
- # setting the architecture
164
- compute.attributes["occi.compute.architecture"] = backend_instance.architecture.to_s
165
- compute.attributes["ec2.compute.platform"] = backend_instance.platform
166
- compute.attributes["ec2.compute.kernel_id"] = backend_instance.kernel_id
167
-
168
- # update public and private ip
169
- compute.links.each do |link|
170
- if link.kind.term == "networkinterface" and link.backend[:backend_id] == backend_instance.id
171
- if link.backend[:network] == "public" and backend_instance.ip_address != nil
172
- link.mixins.each do |mixin|
173
- if mixin.backend[:network] == "ec2_public_network"
174
- link.attributes["occi.networkinterface.address"] = backend_instance.ip_address
175
- state = OCCI::Infrastructure::Networkinterface::STATE_ACTIVE
176
- link.state_machine.set_state(state)
177
- link.attributes['occi.networkinterface.state'] = "active"
178
- end
179
- end
180
- elsif link.backend[:network] == "private" and backend_instance.private_ip_address != nil
181
- link.mixins.each do |mixin|
182
- if mixin.backend[:network] == "ec2_private_network"
183
- link.attributes["occi.networkinterface.address"] = backend_instance.private_ip_address
184
- state = OCCI::Infrastructure::Networkinterface::STATE_ACTIVE
185
- link.state_machine.set_state(state)
186
- link.attributes['occi.networkinterface.state'] = "active"
187
- end
188
- end
189
- end
190
- end
191
- end
192
-
193
- # create the console link if not already existent and if in state active
194
- if compute.state_machine.current_state == OCCI::Infrastructure::Compute::STATE_ACTIVE and not has_console_link(compute) and backend_instance.key_name == "default_occi_key"
195
- # create a ConsoleLink
196
- OCCI::Log.debug("Creating a ConsoleLink to the EC2 Compute instance.")
197
- attributes = OCCI::Core::Attributes.new()
198
- attributes['occi.core.target'] = "ssh://" + backend_instance.public_dns_name
199
- attributes['occi.core.source'] = compute.get_location
200
- mixins = []
201
- console_link = OCCI::Infrastructure::ConsoleLink.new(attributes, mixins)
202
- # save the id of the compute backend instance in the network link for future identification
203
- console_link.backend[:backend_id] = backend_instance.id
204
- # link and register
205
- location = console_link.get_location
206
- compute.links << console_link
207
- OCCI::Rendering::HTTP::LocationRegistry.register(location, console_link)
208
- end
209
- end
210
-
211
- # ---------------------------------------------------------------------------------------------------------------------
212
- def compute_update_state(compute)
213
- # get the ec2 backend instance
214
- backend_instance = get_backend_instance(compute)
215
-
216
- # map the ec2 state to an OCCI state
217
- OCCI::Log.debug("Current EC2 VM state is: #{backend_instance.status}")
218
- state = case backend_instance.status
219
- when :running then
220
- OCCI::Infrastructure::Compute::STATE_ACTIVE
221
- when :pending, :shutting_down, :stopping, :terminated then
222
- OCCI::Infrastructure::Compute::STATE_INACTIVE
223
- when :stopped then
224
- OCCI::Infrastructure::Compute::STATE_SUSPENDED
225
- else
226
- OCCI::Infrastructure::Compute::STATE_INACTIVE
227
- end
228
- # set the state
229
- compute.state_machine.set_state(state)
230
- compute.attributes['occi.compute.state'] = compute.state_machine.current_state.name
231
- end
232
-
233
- # ---------------------------------------------------------------------------------------------------------------------
234
- def compute_delete(compute)
235
- OCCI::Log.debug("Deleting EC2 Compute instance with EC2 ID #{compute.backend[:id]}")
236
-
237
- # get the ec2 backend instance
238
- backend_instance = get_backend_instance(compute)
239
-
240
- if backend_instance.nil?
241
- OCCI::Log.debug("Problems refreshing compute instance: An instance with the EC2 ID #{compute.backend[:id]} could not be found.")
242
- return
243
- end
244
- # terminate the instance
245
- backend_instance.terminate()
246
-
247
- # delete networklinks to the private and public network and the ConsoleLink
248
- compute.links.each do |link|
249
- if link.backend[:backend_id] == backend_instance.id
250
- location = OCCI::Rendering::HTTP::LocationRegistry.get_location_of_object(link)
251
- OCCI::Log.debug("Deleting link to #{location}")
252
- OCCI::Rendering::HTTP::LocationRegistry.unregister(location)
253
- end
254
- end
255
- OCCI::Log.debug("Deleted EC2 Compute instance with EC2 ID #{compute.backend[:id]}")
256
- end
257
-
258
- # ---------------------------------------------------------------------------------------------------------------------
259
- # COMPUTE ACTIONS
260
- # ---------------------------------------------------------------------------------------------------------------------
261
- # COMPUTE Action start
262
- def compute_start(compute, parameters=nil)
263
- OCCI::Log.debug("Starting EC2 VM with EC2 instance ID #{compute.backend[:id]}")
264
-
265
- # get the ec2 backend instance
266
- backend_instance = get_backend_instance(compute)
267
-
268
- # suspend the instance (in EC2 lingo stop it)
269
- backend_instance.start()
270
- OCCI::Log.debug("Started EC2 VM with EC2 instance ID #{compute.backend[:id]}")
271
- end
272
-
273
- # ---------------------------------------------------------------------------------------------------------------------
274
- # Action stop
275
- def compute_stop(compute, parameters=nil)
276
- OCCI::Log.debug("Stopping EC2 VM with EC2 instance ID #{compute.backend[:id]}")
277
-
278
- # get the ec2 backend instance
279
- backend_instance = get_backend_instance(compute)
280
-
281
- # stop the instance
282
- backend_instance.stop()
283
- OCCI::Log.debug("Stopped EC2 VM with EC2 instance ID #{compute.backend[:id]}")
284
- end
285
-
286
- # ---------------------------------------------------------------------------------------------------------------------
287
- # Action restart
288
- def compute_restart(compute, parameters=nil)
289
- OCCI::Log.debug("Restarting EC2 VM with EC2 instance ID #{compute.backend[:id]}")
290
-
291
- # get the ec2 backend instance
292
- backend_instance = get_backend_instance(compute)
293
-
294
- # restart the instance
295
- backend_instance.reboot()
296
- OCCI::Log.debug("Restarted EC2 VM with EC2 instance ID #{compute.backend[:id]}")
297
- end
298
-
299
- # ---------------------------------------------------------------------------------------------------------------------
300
- # Action suspend
301
- def compute_suspend(compute, parameters=nil)
302
- OCCI::Log.warning("Stop suspend is not supported on EC2 Compute instances.")
303
- end
304
-
305
- end
306
-
307
- end
308
- end
309
- end
310
-
@@ -1,215 +0,0 @@
1
- ##############################################################################
2
- # Copyright 2011 Service Computing group, TU Dortmund
3
- #
4
- # Licensed under the Apache License, Version 2.0 (the "License");
5
- # you may not use this file except in compliance with the License.
6
- # You may obtain a copy of the License at
7
- #
8
- # http://www.apache.org/licenses/LICENSE-2.0
9
- #
10
- # Unless required by applicable law or agreed to in writing, software
11
- # distributed under the License is distributed on an "AS IS" BASIS,
12
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # See the License for the specific language governing permissions and
14
- # limitations under the License.
15
- ##############################################################################
16
-
17
- ##############################################################################
18
- # Description: EC2 Backend
19
- # Author(s): Max Guenther
20
- ##############################################################################
21
-
22
- # Amazon Webservices SDK
23
- require 'aws-sdk'
24
-
25
- # EC2 backend
26
- #require 'occi/backend/ec2/Compute'
27
-
28
- # for XML parsing (gem = "xml-simple")
29
- require 'xmlsimple'
30
-
31
- require 'occi/log'
32
-
33
-
34
- module OCCI
35
- module Backend
36
- module EC2
37
- # ---------------------------------------------------------------------------------------------------------------------
38
-
39
- #begin
40
- # ### Create networks
41
- # # Create private network
42
- # attributes = OCCI::Core::Attributes.new()
43
- # attributes['occi.core.title'] = "ec2_private_network"
44
- # attributes['occi.core.summary'] = "This network contains private links to all EC2 compute instances."
45
- # mixins = []
46
- # private_network = OCCI::Infrastructure::Network.new(attributes, mixins)
47
- # OCCI::Rendering::HTTP::LocationRegistry.register("/network/ec2_private_network", private_network)
48
- # private_network.state_machine.set_state(OCCI::Infrastructure::Network::STATE_ACTIVE)
49
- # private_network.attributes['occi.network.state'] = "active"
50
- #
51
- # # Create public network
52
- # attributes = OCCI::Core::Attributes.new()
53
- # attributes['occi.core.title'] = "ec2_public_network"
54
- # attributes['occi.core.summary'] = "This network contains public links to all EC2 compute instances."
55
- # mixins = []
56
- # public_network = OCCI::Infrastructure::Network.new(attributes, mixins)
57
- # OCCI::Rendering::HTTP::LocationRegistry.register("/network/ec2_public_network", public_network)
58
- # public_network.state_machine.set_state(OCCI::Infrastructure::Network::STATE_ACTIVE)
59
- # public_network.attributes['occi.network.state'] = "active"
60
- #end
61
-
62
- public
63
-
64
- def self.get_ec2_interface
65
- ec2 = AWS::EC2.new
66
- return ec2.regions[OCCI::Server.config["avail_zone"]]
67
- end
68
-
69
- class EC2
70
-
71
- #include Compute
72
-
73
- # ---------------------------------------------------------------------------------------------------------------------
74
- # Operation mappings
75
-
76
-
77
- OPERATIONS = {}
78
-
79
- OPERATIONS["http://schemas.ogf.org/occi/infrastructure#compute"] = {
80
-
81
- # Generic resource operations
82
- :deploy => :compute_deploy,
83
- :update_state => :compute_update_state,
84
- :refresh => :compute_refresh,
85
- :delete => :compute_delete,
86
-
87
- # Compute specific resource operations
88
- :start => :compute_start,
89
- :stop => :compute_stop,
90
- :restart => :compute_restart,
91
- :suspend => nil
92
- }
93
-
94
- OPERATIONS["http://schemas.ogf.org/occi/infrastructure#network"] = {
95
-
96
- # Generic resource operations
97
- :deploy => nil,
98
- :update_state => nil,
99
- :refresh => nil,
100
- :delete => nil,
101
-
102
- # Network specific resource operations
103
- :up => nil,
104
- :down => nil
105
- }
106
-
107
- OPERATIONS["http://schemas.ogf.org/occi/infrastructure#storage"] = {
108
-
109
- # Generic resource operations
110
- :deploy => nil,
111
- :update_state => nil,
112
- :refresh => nil,
113
- :delete => nil,
114
-
115
- # Network specific resource operations
116
- :online => nil,
117
- :offline => nil,
118
- :backup => nil,
119
- :snapshot => nil,
120
- :resize => nil
121
- }
122
-
123
- OPERATIONS["http://schemas.ogf.org/gwdg#nfsstorage"] = {
124
-
125
- # Generic resource operations
126
- :deploy => nil,
127
- :update_state => nil,
128
- :refresh => nil,
129
- :delete => nil
130
- }
131
-
132
- # ---------------------------------------------------------------------------------------------------------------------
133
-
134
- def initialize(access_key_id, secret_access_key)
135
- # EC2 access key
136
- AWS.config(
137
- :access_key_id => access_key_id,
138
- :secret_access_key => secret_access_key)
139
- end
140
-
141
- def register_templates
142
- OCCI::Log.debug("Loading EC2 templates.")
143
-
144
- # import Compute Resource Templates from etc/ec2_templates/resource_templates.xml
145
- xml = XmlSimple.xml_in("etc/ec2_templates/resource_templates.xml")
146
- xml["instance"].each do |instance|
147
- term = instance["term"][0]
148
- scheme = "http://mycloud.org/templates/compute#"
149
- title = instance["title"][0]
150
- attributes = OCCI::Core::Attributes.new()
151
- actions = []
152
- entities = []
153
- related = [OCCI::Infrastructure::ResourceTemplate::MIXIN]
154
- mixin = OCCI::Core::Mixin.new(term, scheme, title, attributes, actions, related, entities)
155
- OCCI::CategoryRegistry.register(mixin)
156
- end
157
-
158
- ## import image templates
159
- # get the ec2 interface
160
- ec2 = OCCI::Backend::EC2.get_ec2_interface()
161
- # register each image
162
- ec2.images.each do |image|
163
- term = image.id
164
- scheme = "http://mycloud.org/templates/os#"
165
- title = image.name
166
- attributes = OCCI::Core::Attributes.new()
167
- actions = []
168
- entities = []
169
- related = [OCCI::Infrastructure::OSTemplate::MIXIN]
170
- mixin = OCCI::Core::Mixin.new(term, scheme, title, attributes, actions, related, entities)
171
- OCCI::CategoryRegistry.register(mixin)
172
- end
173
- OCCI::Log.debug("Finished loading EC2 templates.")
174
- end
175
-
176
-
177
- # ---------------------------------------------------------------------------------------------------------------------
178
- def register_existing_resources
179
-
180
- end
181
-
182
- # ---------------------------------------------------------------------------------------------------------------------
183
- def resource_deploy(resource)
184
- OCCI::Log.debug("Deploying resource '#{resource.attributes['occi.core.title']}'...")
185
- end
186
-
187
- # ---------------------------------------------------------------------------------------------------------------------
188
- def resource_refresh(resource)
189
- OCCI::Log.debug("Refreshing resource '#{resource.attributes['occi.core.title']}'...")
190
- end
191
-
192
- # ---------------------------------------------------------------------------------------------------------------------
193
- def resource_update_state(resource)
194
- OCCI::Log.debug("Updating state of resource '#{resource.attributes['occi.core.title']}'...")
195
- end
196
-
197
- # ---------------------------------------------------------------------------------------------------------------------
198
- def resource_delete(resource)
199
- OCCI::Log.debug("Deleting resource '#{resource.attributes['occi.core.title']}'...")
200
- end
201
-
202
- # ---------------------------------------------------------------------------------------------------------------------
203
- # ACTIONS
204
- # ---------------------------------------------------------------------------------------------------------------------
205
-
206
- # ---------------------------------------------------------------------------------------------------------------------
207
- def action_dummy(compute, parameters)
208
- OCCI::Log.debug("Calling method for resource '#{resource.attributes['occi.core.title']}' with parameters: #{parameters.inspect}")
209
- end
210
-
211
- end
212
-
213
- end
214
- end
215
- end