From b9c486238ba8f4bb06621cbe3e342dfb5c07d334 Mon Sep 17 00:00:00 2001 From: Daniel Molina Date: Thu, 30 Aug 2012 19:07:01 +0200 Subject: [PATCH] feature #378: Improve EBS functionality * Add LICENSE headers * Check if a volume is already attached * Add EBS_VOLUME parameter when an image is created using CreateVolume --- src/cloud/ec2/lib/ImageEC2.rb | 3 ++ src/cloud/ec2/lib/ebs.rb | 37 +++++++++++++++----- src/cloud/ec2/lib/elastic_ip.rb | 16 +++++++++ src/cloud/ec2/lib/instance.rb | 16 +++++++++ src/cloud/ec2/lib/views/describe_images.erb | 2 +- src/cloud/ec2/lib/views/describe_volumes.erb | 2 +- 6 files changed, 66 insertions(+), 10 deletions(-) diff --git a/src/cloud/ec2/lib/ImageEC2.rb b/src/cloud/ec2/lib/ImageEC2.rb index 214089a970..585fedd81f 100644 --- a/src/cloud/ec2/lib/ImageEC2.rb +++ b/src/cloud/ec2/lib/ImageEC2.rb @@ -55,6 +55,9 @@ class ImageEC2 < Image <% if @image_info[:persistent] != nil %> PERSISTENT = "YES" <% end %> + <% if @image_info[:ebs] != "nil" %> + EBS_VOLUME = "YES" + <% end %> <% if @image_file != nil %> PATH = "<%= @image_file %>" <% end %> diff --git a/src/cloud/ec2/lib/ebs.rb b/src/cloud/ec2/lib/ebs.rb index 707edfef33..bf47296bde 100644 --- a/src/cloud/ec2/lib/ebs.rb +++ b/src/cloud/ec2/lib/ebs.rb @@ -1,3 +1,19 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + module EBS # Default FSTYPE when creating new volumes DEFAULT_FSTYPE = "ext3" @@ -93,9 +109,19 @@ module EBS target = m[1] end + # Check if the volume is already attached to another instance + image = Image.new(Image.build_xml(image_id), @client) + rc = image.info + + return rc if OpenNebula::is_error?(rc) + + if image['TEMPLATE/EBS/INSTANCE_ID'] + return OpenNebula::Error.new("Volume #{params['VolumeId']} \ + already attached to another instance \ + (#{image['TEMPLATE/EBS/INSTANCE_ID']})") + end # Attach - vm = VirtualMachine.new(VirtualMachine.build_xml(vm_id), @client) rc = vm.info @@ -108,12 +134,6 @@ module EBS # Update IMAGE metadata - - image = Image.new(Image.build_xml(image_id), @client) - rc = image.info - - return rc if OpenNebula::is_error?(rc) - attach_time = Time.now.to_i xml_hash = {'EBS' => { @@ -156,7 +176,8 @@ module EBS :type => "DATABLOCK", :size => size, :fstype => @config[:ebs_fstype]||DEFAULT_FSTYPE, - :persistent => "YES" + :persistent => "YES", + :ebs => "YES" } image = ImageEC2.new(Image.build_xml, @client, nil, opts) diff --git a/src/cloud/ec2/lib/elastic_ip.rb b/src/cloud/ec2/lib/elastic_ip.rb index a495a8c9a6..7ee2afb7a1 100644 --- a/src/cloud/ec2/lib/elastic_ip.rb +++ b/src/cloud/ec2/lib/elastic_ip.rb @@ -1,3 +1,19 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + module ElasticIP def allocate_address(params) # Get public IP diff --git a/src/cloud/ec2/lib/instance.rb b/src/cloud/ec2/lib/instance.rb index 8447ee2867..91054d3f01 100644 --- a/src/cloud/ec2/lib/instance.rb +++ b/src/cloud/ec2/lib/instance.rb @@ -1,3 +1,19 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2012, OpenNebula Project Leads (OpenNebula.org) # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + module Instance ########################################################################### # Class Constants. Defined the EC2 and OpenNebula State mapping diff --git a/src/cloud/ec2/lib/views/describe_images.erb b/src/cloud/ec2/lib/views/describe_images.erb index bb590af948..bae66fafef 100644 --- a/src/cloud/ec2/lib/views/describe_images.erb +++ b/src/cloud/ec2/lib/views/describe_images.erb @@ -2,7 +2,7 @@ <% impool.each do |im| %> - <% if state_image = im.render_state %> + <% if (state_image = im.render_state) && (im['TEMPLATE/EBS'] != 'YES') %> ami-<%= sprintf('%08i', im.id) %> <%= im['SOURCE'].split('/').last %> diff --git a/src/cloud/ec2/lib/views/describe_volumes.erb b/src/cloud/ec2/lib/views/describe_volumes.erb index de855dd404..8ad1dad271 100644 --- a/src/cloud/ec2/lib/views/describe_volumes.erb +++ b/src/cloud/ec2/lib/views/describe_volumes.erb @@ -3,7 +3,7 @@ <% impool.each do |im| %> - <% if state_image = im.render_state %> + <% if (state_image = im.render_state) && (im['TEMPLATE/EBS'] == 'YES') %> vol-<%= sprintf('%08i', im.id) %> <%= im.render_size %>