diff --git a/install.sh b/install.sh
index 11b440c206..857fa71b3e 100755
--- a/install.sh
+++ b/install.sh
@@ -1071,12 +1071,18 @@ ECO_LIB_FILES="src/cloud/ec2/lib/EC2QueryClient.rb \
src/cloud/ec2/lib/EC2QueryServer.rb \
src/cloud/ec2/lib/ImageEC2.rb \
src/cloud/ec2/lib/elastic_ip.rb \
+ src/cloud/ec2/lib/ebs.rb \
src/cloud/ec2/lib/econe-server.rb"
ECO_LIB_CLIENT_FILES="src/cloud/ec2/lib/EC2QueryClient.rb"
ECO_LIB_VIEW_FILES="src/cloud/ec2/lib/views/describe_images.erb \
src/cloud/ec2/lib/views/describe_instances.erb \
+ src/cloud/ec2/lib/views/create_volume.erb \
+ src/cloud/ec2/lib/views/describe_volumes.erb \
+ src/cloud/ec2/lib/views/attach_volume.erb \
+ src/cloud/ec2/lib/views/detach_volume.erb \
+ src/cloud/ec2/lib/views/delete_volume.erb \
src/cloud/ec2/lib/views/register_image.erb \
src/cloud/ec2/lib/views/run_instances.erb \
src/cloud/ec2/lib/views/allocate_address.erb \
diff --git a/src/cloud/ec2/etc/econe.conf b/src/cloud/ec2/etc/econe.conf
index d8329b9f52..527953d480 100644
--- a/src/cloud/ec2/etc/econe.conf
+++ b/src/cloud/ec2/etc/econe.conf
@@ -60,7 +60,7 @@
# Cluster associated with the EC2 resources, by default no Cluster is defined
#:cluster_id:
-# Datastore in which the Images uploaded through EC2 will be allocated, by
+# Datastore in which the Images uploaded through EC2 will be allocated, by
# default 1
#:datastore_id:
@@ -76,7 +76,7 @@
# VirtualNetwork containing the elastic ips to be used with EC2. If no defined
# the Elastic IP functionality is disabled
-#:elasticips_vnet_id:
+#:elasticips_vnet_id:
# Script to associate a public IP with a private IP
# - arguments: elastic_ip private_ip vnet_template(base64_encoded)
@@ -85,3 +85,10 @@
# Script to disassociate a public IP
# - arguments: elastic_ip
:disassociate_script: /usr/bin/false
+
+#############################################################
+# EBS
+#############################################################
+
+# FSTYPE that will be used when creating new volumes (DATABLOCKs)
+:ebs_fstype: ext3
diff --git a/src/cloud/ec2/lib/EC2QueryServer.rb b/src/cloud/ec2/lib/EC2QueryServer.rb
index 9e0a7bfde6..b0de206b6b 100644
--- a/src/cloud/ec2/lib/EC2QueryServer.rb
+++ b/src/cloud/ec2/lib/EC2QueryServer.rb
@@ -59,18 +59,6 @@ class EC2QueryServer < CloudServer
'unkn' => :terminated
}
- EC2_IMAGE_STATES={
- "INIT" => "pending",
- "READY" => "available",
- "USED" => "available",
- "DISABLED" => nil,
- "LOCKED" => "pending",
- "ERROR" => "failed",
- "CLONE" => "available",
- "DELETE" => nil,
- "USED_PERS" => "available"
- }
-
###########################################################################
def initialize(client, oneadmin_client, config, logger)
@@ -85,6 +73,9 @@ class EC2QueryServer < CloudServer
require 'elastic_ip'
extend ElasticIP
end
+
+ require 'ebs'
+ extend EBS
end
###########################################################################
@@ -92,7 +83,10 @@ class EC2QueryServer < CloudServer
###########################################################################
def upload_image(params)
- image = ImageEC2.new(Image.build_xml, @client, params['file'])
+ image = ImageEC2.new(Image.build_xml,
+ @client,
+ params['file'],
+ {:type => "OS"})
template = image.to_one_template
if OpenNebula.is_error?(template)
@@ -132,7 +126,7 @@ class EC2QueryServer < CloudServer
def describe_images(params)
user_flag = OpenNebula::Pool::INFO_ALL
- impool = ImagePool.new(@client, user_flag)
+ impool = ImageEC2Pool.new(@client, user_flag)
impool.info
erb_version = params['Version']
@@ -263,9 +257,7 @@ class EC2QueryServer < CloudServer
#{ec2_state[:name]}"
end
- def render_image_state(image)
- EC2_IMAGE_STATES[image.state_str]
- end
+
def render_launch_time(vm)
return "#{Time.at(vm["STIME"].to_i).xmlschema}"
diff --git a/src/cloud/ec2/lib/ImageEC2.rb b/src/cloud/ec2/lib/ImageEC2.rb
index 7ec262bca9..214089a970 100644
--- a/src/cloud/ec2/lib/ImageEC2.rb
+++ b/src/cloud/ec2/lib/ImageEC2.rb
@@ -19,19 +19,50 @@ require 'OpenNebula'
include OpenNebula
+class ImageEC2Pool < ImagePool
+ def initialize(client, user_id=-1)
+ super(client, user_id)
+ end
+
+ def factory(element_xml)
+ ImageEC2.new(element_xml,@client)
+ end
+end
+
class ImageEC2 < Image
+ EC2_IMAGE_STATES={
+ "INIT" => "pending",
+ "READY" => "available",
+ "USED" => "available",
+ "DISABLED" => nil,
+ "LOCKED" => "pending",
+ "ERROR" => "failed",
+ "CLONE" => "available",
+ "DELETE" => nil,
+ "USED_PERS" => "in-use"
+ }
+
ONE_IMAGE = %q{
NAME = "ec2-<%= uuid %>"
- TYPE = OS
+ TYPE = <%= @image_info[:type] %>
+ <% if @image_info[:size] != nil %>
+ SIZE = "<%= @image_info[:size] %>"
+ <% end %>
+ <% if @image_info[:fstype] != nil %>
+ FSTYPE = "<%= @image_info[:fstype] %>"
+ <% end %>
+ <% if @image_info[:persistent] != nil %>
+ PERSISTENT = "YES"
+ <% end %>
<% if @image_file != nil %>
PATH = "<%= @image_file %>"
<% end %>
}.gsub(/^ /, '')
- def initialize(xml, client, file=nil)
+ def initialize(xml, client, file=nil, opts={})
super(xml, client)
- @image_info = nil
+ @image_info = opts
@image_file = file
if file && file[:tempfile]
@@ -45,4 +76,16 @@ class ImageEC2 < Image
one = ERB.new(ONE_IMAGE)
return one.result(binding)
end
+
+ def render_state
+ EC2_IMAGE_STATES[self.state_str]
+ end
+
+ def render_size
+ self['SIZE'].to_i/1024
+ end
+
+ def render_create_time
+ Time.at(self["REGTIME"].to_i).xmlschema
+ end
end
diff --git a/src/cloud/ec2/lib/ebs.rb b/src/cloud/ec2/lib/ebs.rb
new file mode 100644
index 0000000000..ba9dee4b18
--- /dev/null
+++ b/src/cloud/ec2/lib/ebs.rb
@@ -0,0 +1,152 @@
+module EBS
+ # Default FSTYPE when creating new volumes
+ DEFAULT_FSTYPE = "ext3"
+
+ # Detaches a DATABLOCK from a VM
+ #
+ # @param [Hash] params
+ # @option params [String] VolumeId The ID of the DATABLOCK
+ # @option params [String] InstanceId The ID of the VM
+ # @option params [String] Device The TARGET (unsupported)
+ # @option params [String] Force The TARGET (unsupported)
+ def detach_volume(params)
+ image_id = params['VolumeId']
+ image_id = image_id.split('-')[1] if image_id[0]==?v
+
+ vm_id = params['InstanceId']
+ vm_id = vm_id.split('-')[1] if vm_id[0]==?i
+
+ target = params['Device']
+
+ vm = VirtualMachine.new(VirtualMachine.build_xml(vm_id), @client)
+ rc = vm.info
+
+ return OpenNebula::Error.new('Unsupported'),400 if OpenNebula::is_error?(rc)
+
+ disk_id = vm["TEMPLATE/DISK[IMAGE_ID=#{image_id.to_i}]/DISK_ID"]
+
+ logger.debug { "Detaching DISK: #{disk_id} VM: #{vm_id} IMAGE: #{image_id}" }
+
+ return OpenNebula::Error.new('Unsupported'),400 if disk_id.nil?
+
+ rc = vm.detachdisk(disk_id.to_i)
+
+ return OpenNebula::Error.new('Unsupported'),400 if OpenNebula::is_error?(rc)
+
+ erb_version = params['Version']
+
+ response = ERB.new(File.read(@config[:views]+"/detach_volume.erb"))
+ return response.result(binding), 200
+ end
+
+ # Attaches a DATABLOCK to a running VM and exposes it as the specified
+ # device.
+ #
+ # @param [Hash] params
+ # @option params [String] VolumeId The ID of the DATABLOCK
+ # @option params [String] InstanceId The ID of the VM to which the
+ # volume attaches
+ # @option params [String] Device How the device is exposed to the
+ # instance (e.g., /dev/sdh, or xvdh)
+ def attach_volume(params)
+ image_id = params['VolumeId']
+ image_id = image_id.split('-')[1] if image_id[0]==?v
+
+ vm_id = params['InstanceId']
+ vm_id = vm_id.split('-')[1] if vm_id[0]==?i
+
+ target = params['Device']
+ if m = target.match(/^\/dev\/(\w+)$/)
+ target = m[1]
+ end
+
+ vm = VirtualMachine.new(VirtualMachine.build_xml(vm_id), @client)
+ rc = vm.info
+
+ return OpenNebula::Error.new('Unsupported'),400 if OpenNebula::is_error?(rc)
+
+ template = "DISK = [ IMAGE_ID = #{image_id}, TARGET = #{target} ]"
+ vm.attachdisk(template)
+
+ return OpenNebula::Error.new('Unsupported'),400 if OpenNebula::is_error?(rc)
+
+ erb_version = params['Version']
+
+ vm.info
+
+ response = ERB.new(File.read(@config[:views]+"/attach_volume.erb"))
+ return response.result(binding), 200
+ end
+
+ # Creates a new DATABLOCK that any VM can attach to
+ #
+ # @param [Hash] params
+ # @option params [String] Size The size of the volume, in GiBs.
+ # @option params [String] SnapshotId The snapshot from which to create
+ # the new volume (unsupported).
+ # @option params [String] AvailabilityZone The Availability Zone in which
+ # to create the new volume (unsupported)
+ def create_volume(params)
+ size = params['Size'].to_i # in GiBs
+ size *= 1024
+
+ opts = {
+ :type => "DATABLOCK",
+ :size => size,
+ :fstype => @config[:ebs_fstype]||DEFAULT_FSTYPE,
+ :persistent => "YES"
+ }
+
+ image = ImageEC2.new(Image.build_xml, @client, nil, opts)
+
+ template = image.to_one_template
+ if OpenNebula.is_error?(template)
+ return OpenNebula::Error.new('Unsupported'), 400
+ end
+
+ rc = image.allocate(template, @config[:datastore_id]||1)
+ if OpenNebula.is_error?(rc)
+ return OpenNebula::Error.new('Unsupported'), 400
+ end
+
+ erb_version = params['Version']
+
+ image.info
+
+ response = ERB.new(File.read(@config[:views]+"/create_volume.erb"))
+ return response.result(binding), 200
+ end
+
+ # Deletes a DATABLOCK
+ #
+ # @param [Hash] params
+ # @option params [String] VolumeId The ID of the DATABLOCK
+ def delete_volume(params)
+ image_id = params['VolumeId']
+ image_id = image_id.split('-')[1] if image_id[0]==?v
+
+ image = ImageEC2.new(Image.build_xml(image_id), @client)
+ rc = image.delete
+
+ return OpenNebula::Error.new('Unsupported'),400 if OpenNebula::is_error?(rc)
+
+ erb_version = params['Version']
+
+ response = ERB.new(File.read(@config[:views]+"/delete_volume.erb"))
+ return response.result(binding), 200
+ end
+
+
+ # Describes your Amazon EBS volumes
+ def describe_volumes(params)
+ user_flag = OpenNebula::Pool::INFO_ALL
+ impool = ImageEC2Pool.new(@client, user_flag)
+ impool.info
+
+ erb_version = params['Version']
+
+ response = ERB.new(File.read(@config[:views]+"/describe_volumes.erb"))
+ return response.result(binding), 200
+ end
+
+end
\ No newline at end of file
diff --git a/src/cloud/ec2/lib/econe-server.rb b/src/cloud/ec2/lib/econe-server.rb
index 953a11ffa0..cf57486939 100644
--- a/src/cloud/ec2/lib/econe-server.rb
+++ b/src/cloud/ec2/lib/econe-server.rb
@@ -207,6 +207,16 @@ def do_http_request(params)
result,rc = @econe_server.release_address(params)
when 'DescribeAddresses'
result,rc = @econe_server.describe_addresses(params)
+ when 'CreateVolume'
+ result,rc = @econe_server.create_volume(params)
+ when 'DescribeVolumes'
+ result,rc = @econe_server.describe_volumes(params)
+ when 'AttachVolume'
+ result,rc = @econe_server.attach_volume(params)
+ when 'DetachVolume'
+ result,rc = @econe_server.detach_volume(params)
+ when 'DeleteVolume'
+ result,rc = @econe_server.delete_volume(params)
end
if OpenNebula::is_error?(result)
diff --git a/src/cloud/ec2/lib/views/attach_volume.erb b/src/cloud/ec2/lib/views/attach_volume.erb
new file mode 100644
index 0000000000..869202f933
--- /dev/null
+++ b/src/cloud/ec2/lib/views/attach_volume.erb
@@ -0,0 +1,9 @@
+
+
+
+ vol-<%= sprintf('%08i', image_id) %>
+ i-<%= sprintf('%08i', vm_id) %>
+ /dev/<%= vm["TEMPLATE/DISK[IMAGE_ID=#{image_id.to_i}]/TARGET"] %>
+ attaching
+
+
diff --git a/src/cloud/ec2/lib/views/create_volume.erb b/src/cloud/ec2/lib/views/create_volume.erb
new file mode 100644
index 0000000000..abe6895aae
--- /dev/null
+++ b/src/cloud/ec2/lib/views/create_volume.erb
@@ -0,0 +1,10 @@
+
+
+
+ vol-<%= sprintf('%08i', image.id) %>
+ <%= image.render_size %>
+
+
+ <%= image.render_state %>
+ <%= image.render_create_time %>
+
\ No newline at end of file
diff --git a/src/cloud/ec2/lib/views/delete_volume.erb b/src/cloud/ec2/lib/views/delete_volume.erb
new file mode 100644
index 0000000000..52a6681cc5
--- /dev/null
+++ b/src/cloud/ec2/lib/views/delete_volume.erb
@@ -0,0 +1,5 @@
+
+
+
+ true
+
\ No newline at end of file
diff --git a/src/cloud/ec2/lib/views/describe_images.erb b/src/cloud/ec2/lib/views/describe_images.erb
index a38c99861d..bb590af948 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 = render_image_state(im) %>
+ <% if state_image = im.render_state %>
-
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
new file mode 100644
index 0000000000..539059d3b5
--- /dev/null
+++ b/src/cloud/ec2/lib/views/describe_volumes.erb
@@ -0,0 +1,22 @@
+
+
+
+
+ <% impool.each do |im| %>
+ <% if state_image = im.render_state %>
+
-
+ vol-<%= sprintf('%08i', im.id) %>
+ <%= im.render_size %>
+
+
+ <%= state_image %>
+ <%= im.render_create_time %>
+
+
+
+ <% else
+ next
+ end
+ end %>
+
+
\ No newline at end of file
diff --git a/src/cloud/ec2/lib/views/detach_volume.erb b/src/cloud/ec2/lib/views/detach_volume.erb
new file mode 100644
index 0000000000..0d8306a486
--- /dev/null
+++ b/src/cloud/ec2/lib/views/detach_volume.erb
@@ -0,0 +1,9 @@
+
+
+
+ vol-<%= sprintf('%08i', image_id) %>
+ i-<%= sprintf('%08i', vm_id) %>
+ <%= target %>
+ detaching
+
+