mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-19 11:22:55 +09:00
feature #378: Add EBS support
This commit is contained in:
1 parent
d5036967d4
commit
7bef5e78db
12 files changed
+288
-23
No files matched your search
@@ -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 \
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
<name>#{ec2_state[:name]}</name>"
|
||||
end
|
||||
|
||||
def render_image_state(image)
|
||||
EC2_IMAGE_STATES[image.state_str]
|
||||
end
|
||||
|
||||
|
||||
def render_launch_time(vm)
|
||||
return "<launchTime>#{Time.at(vm["STIME"].to_i).xmlschema}</launchTime>"
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0"?>
|
||||
<AttachVolumeResponse xmlns="http://ec2.amazonaws.com/doc/<%=erb_version%>/">
|
||||
<requestId/>
|
||||
<volumeId>vol-<%= sprintf('%08i', image_id) %></volumeId>
|
||||
<instanceId>i-<%= sprintf('%08i', vm_id) %></instanceId>
|
||||
<device>/dev/<%= vm["TEMPLATE/DISK[IMAGE_ID=#{image_id.to_i}]/TARGET"] %></device>
|
||||
<status>attaching</status>
|
||||
<attachTime/>
|
||||
</AttachVolumeResponse>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?xml version="1.0"?>
|
||||
<CreateVolumeResponse xmlns="http://ec2.amazonaws.com/doc/<%=erb_version%>/">
|
||||
<requestId/>
|
||||
<volumeId>vol-<%= sprintf('%08i', image.id) %></volumeId>
|
||||
<size><%= image.render_size %></size>
|
||||
<snapshotId/>
|
||||
<availabilityZone/>
|
||||
<status><%= image.render_state %></status>
|
||||
<createTime><%= image.render_create_time %></createTime>
|
||||
</CreateVolumeResponse>
|
||||
@@ -0,0 +1,5 @@
|
||||
<?xml version="1.0"?>
|
||||
<DeleteVolumeResponse xmlns="http://ec2.amazonaws.com/doc/<%=erb_version%>/">
|
||||
<requestId/>
|
||||
<return>true</return>
|
||||
</DeleteVolumeResponse>
|
||||
@@ -2,7 +2,7 @@
|
||||
<DescribeImagesResponse xmlns="http://ec2.amazonaws.com/doc/<%=erb_version%>/">
|
||||
<imagesSet>
|
||||
<% impool.each do |im| %>
|
||||
<% if state_image = render_image_state(im) %>
|
||||
<% if state_image = im.render_state %>
|
||||
<item>
|
||||
<imageId>ami-<%= sprintf('%08i', im.id) %></imageId>
|
||||
<imageLocation><%= im['SOURCE'].split('/').last %></imageLocation>
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0"?>
|
||||
<DescribeVolumesResponse xmlns="http://ec2.amazonaws.com/doc/<%=erb_version%>/">
|
||||
<requestId/>
|
||||
<volumeSet>
|
||||
<% impool.each do |im| %>
|
||||
<% if state_image = im.render_state %>
|
||||
<item>
|
||||
<volumeId>vol-<%= sprintf('%08i', im.id) %></volumeId>
|
||||
<size><%= im.render_size %></size>
|
||||
<snapshotId/>
|
||||
<availabilityZone/>
|
||||
<status><%= state_image %></status>
|
||||
<createTime><%= im.render_create_time %></createTime>
|
||||
<attachmentSet/>
|
||||
<tagSet/>
|
||||
</item>
|
||||
<% else
|
||||
next
|
||||
end
|
||||
end %>
|
||||
</volumeSet>
|
||||
</DescribeVolumesResponse>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0"?>
|
||||
<DetachVolumeResponse xmlns="http://ec2.amazonaws.com/doc/<%=erb_version%>/">
|
||||
<requestId/>
|
||||
<volumeId>vol-<%= sprintf('%08i', image_id) %></volumeId>
|
||||
<instanceId>i-<%= sprintf('%08i', vm_id) %></instanceId>
|
||||
<device><%= target %></device>
|
||||
<status>detaching</status>
|
||||
<attachTime/>
|
||||
</DetachVolumeResponse>
|
||||
Reference in new issue
Block a user