1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

feature #2259: Add CreateSnapshot

This commit is contained in:
Daniel Molina 2013-08-13 18:04:40 +02:00
parent 0b68e9b59a
commit 33648033ac
6 changed files with 86 additions and 5 deletions

View File

@ -1296,6 +1296,7 @@ ECO_LIB_VIEW_FILES="src/cloud/ec2/lib/views/describe_images.erb \
src/cloud/ec2/lib/views/describe_regions.erb \
src/cloud/ec2/lib/views/describe_availability_zones.erb \
src/cloud/ec2/lib/views/create_volume.erb \
src/cloud/ec2/lib/views/create_snapshot.erb \
src/cloud/ec2/lib/views/create_image.erb \
src/cloud/ec2/lib/views/describe_volumes.erb \
src/cloud/ec2/lib/views/attach_volume.erb \

View File

@ -44,7 +44,7 @@ class ImageEC2 < Image
}
ONE_IMAGE = %q{
NAME = "ec2-<%= uuid %>"
NAME = "<%= ImageEC2.generate_uuid %>"
TYPE = <%= @image_info[:type] %>
<% if @image_info[:size] != nil %>
SIZE = "<%= @image_info[:size] %>"
@ -74,8 +74,6 @@ class ImageEC2 < Image
end
def to_one_template()
uuid = UUIDTools::UUID.random_create.to_s
one = ERB.new(ONE_IMAGE)
return one.result(binding)
end
@ -91,4 +89,8 @@ class ImageEC2 < Image
def render_create_time
Time.at(self["REGTIME"].to_i).xmlschema
end
def self.generate_uuid
"ec2-" + UUIDTools::UUID.random_create.to_s
end
end

View File

@ -237,4 +237,69 @@ module EBS
return response.result(binding), 200
end
# Creates a snapshot of an Amazon EBS volume
#
# @param [Hash] params
# @option params [String] VolumeId The ID of the Amazon EBS volume.
# @option params [Description] A description for the snapshot.
def create_snapshot(params)
image_id = params['VolumeId']
image_id = image_id.split('-')[1]
image = ImageEC2.new(Image.build_xml(image_id.to_i), @client)
rc = image.info
if OpenNebula::is_error?(rc) || image["TEMPLATE/EBS_VOLUME"] != "YES"
rc ||= OpenNebula::Error.new()
rc.ec2_code = "InvalidVolume.NotFound"
return rc
end
instance_id = image["TEMPLATE/EBS/INSTANCE_ID"]
if instance_id
# Disk snapshot
instance_id = instance_id.split('-')[1]
vm = VirtualMachine.new(
VirtualMachine.build_xml(instance_id),
@client)
rc = vm.info
if OpenNebula::is_error?(rc)
rc.ec2_code = "InvalidInstanceID.NotFound"
return rc
end
disk_id = vm["TEMPLATE/DISK[IMAGE_ID=#{image_id}]/DISK_ID"]
if !disk_id.nil?
snapshot_id = vm.disk_snapshot(disk_id.to_i,
params["Description"]||ImageEC2.generate_uuid,
OpenNebula::Image::IMAGE_TYPES[image["TYPE"].to_i],
true)
if OpenNebula::is_error?(snapshot_id)
return snapshot_id
end
end
end
if snapshot_id.nil?
# Clone
snapshot_id = image.clone(params["Description"]||ImageEC2.generate_uuid)
if OpenNebula::is_error?(snapshot_id)
return snapshot_id
end
end
snapshot = ImageEC2.new(Image.build_xml(snapshot_id.to_i), @client)
rc = snapshot.info
if OpenNebula::is_error?(rc)
return rc
end
erb_version = params['Version']
response = ERB.new(File.read(@config[:views]+"/create_snapshot.erb"))
return response.result(binding), 200
end
end

View File

@ -134,6 +134,8 @@ class EC2Application
result,rc = econe_server.describe_regions(params)
when 'DescribeAvailabilityZones'
result,rc = econe_server.describe_availability_zones(params)
when 'CreateSnapshot'
result,rc = econe_server.create_snapshot(params)
when 'CreateImage'
result,rc = econe_server.create_image(params)
when 'CreateVolume'

View File

@ -0,0 +1,11 @@
<CreateSnapshotResponse xmlns="http://ec2.amazonaws.com/doc/2013-06-15/">
<requestId><%= @request_id %></requestId>
<snapshotId>snap-<%= sprintf('%08i', snapshot["ID"]) %></snapshotId>
<volumeId><%= params["VolumeId"] %></volumeId>
<status><%= snapshot.render_state %></status>
<startTime><%= snapshot.render_create_time %></startTime>
<progress>100%</progress>
<ownerId><%= snapshot["UNAME"] %></ownerId>
<volumeSize><%= snapshot.render_size %></volumeSize>
<description><%= snapshot["NAME"] %></description>
</CreateSnapshotResponse>

View File

@ -1,10 +1,10 @@
<?xml version="1.0"?>
<CreateVolumeResponse xmlns="http://ec2.amazonaws.com/doc/<%=erb_version%>/">
<requestId><%= @request_id %></requestId>
<requestId><%= @request_id %></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>
</CreateVolumeResponse>