mirror of
https://github.com/OpenNebula/one.git
synced 2026-11-23 13:15:14 +09:00
F #4913: Code improvements TM clone
This commit is contained in:
1 parent
c1a67c2339
commit
7ae1436cbb
1 file changed
+30
-37
+30
-37
@@ -47,18 +47,6 @@ def check_valid(parameter, label)
|
||||
end
|
||||
end
|
||||
|
||||
def get_ds(ds_id, client)
|
||||
ds = OpenNebula::Datastore.new_with_id(ds_id, client)
|
||||
|
||||
rc = ds.info
|
||||
if OpenNebula::is_error?(rc)
|
||||
STDERR.puts(rc.message)
|
||||
exit -1
|
||||
end
|
||||
|
||||
ds
|
||||
end
|
||||
|
||||
################################################################################
|
||||
|
||||
src = ARGV[0]
|
||||
@@ -66,47 +54,52 @@ dst = ARGV[1]
|
||||
vm_id = ARGV[2]
|
||||
source_ds_id = ARGV[3]
|
||||
|
||||
check_valid src, "src"
|
||||
check_valid dst, "dst"
|
||||
check_valid vm_id, "vm_id"
|
||||
check_valid source_ds_id, "source_ds_id"
|
||||
|
||||
target_ds_id = dst.split("/")[-3]
|
||||
disk_id = dst.split(".")[-1]
|
||||
|
||||
src_host, src_path = src.split ":"
|
||||
hostname, dst_path = dst.split ":"
|
||||
|
||||
################################################################################
|
||||
|
||||
host_pool = VCenterDriver::VIHelper.one_pool(OpenNebula::HostPool)
|
||||
|
||||
# host_id
|
||||
host = host_pool.select{|host| host['NAME'] == hostname}[0] rescue nil
|
||||
|
||||
if host.nil?
|
||||
STDERR.puts "No host found (#{hostname})."
|
||||
exit -1
|
||||
end
|
||||
|
||||
# Get host ID
|
||||
host = VCenterDriver::VIHelper.find_by_name(OpenNebula::HostPool, hostname)
|
||||
host_id = host['ID']
|
||||
|
||||
# get ds_ref (source image)
|
||||
# Get datastores refs
|
||||
source_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, source_ds_id)
|
||||
source_ds_ref = source_ds['TEMPLATE/VCENTER_DS_REF']
|
||||
|
||||
target_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, target_ds_id)
|
||||
target_ds_ref = target_ds['TEMPLATE/VCENTER_DS_REF']
|
||||
|
||||
check_valid source_ds_ref, "source_ds"
|
||||
check_valid target_ds_ref, "target_ds"
|
||||
|
||||
# calculate target path
|
||||
target_path = src_path.split(".").first + "-#{vm_id}-#{disk_id}.vmdk"
|
||||
target_path = VCenterDriver::FileHelper.get_img_name_from_path(src_path,
|
||||
vm_id,
|
||||
disk_id)
|
||||
begin
|
||||
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
|
||||
|
||||
# vi_client
|
||||
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
|
||||
source_ds_vc = VCenterDriver::Datastore.new_from_ref(source_ds_ref, vi_client)
|
||||
|
||||
source_ds_vc = VCenterDriver::Datastore.new_from_ref(source_ds_ref, vi_client)
|
||||
if source_ds_ref == target_ds_ref
|
||||
target_ds_vc = source_ds_vc
|
||||
else
|
||||
target_ds_vc = VCenterDriver::Datastore.new_from_ref(target_ds_ref, vi_client)
|
||||
end
|
||||
|
||||
if source_ds_ref == target_ds_ref
|
||||
target_ds_vc = source_ds_vc
|
||||
else
|
||||
target_ds_vc = VCenterDriver::Datastore.new_from_ref(target_ds_ref, vi_client)
|
||||
target_ds_name_vc = target_ds_vc['name']
|
||||
|
||||
source_ds_vc.copy_virtual_disk(src_path, target_ds_name_vc, target_path)
|
||||
rescue Exception => e
|
||||
STDERR.puts "Error clone virtual disk #{src_path} in "\
|
||||
"datastore #{target_ds_ref}. "\
|
||||
"Reason: #{e.message}\n#{e.backtrace}"
|
||||
exit -1
|
||||
end
|
||||
|
||||
target_ds_name_vc = target_ds_vc['name']
|
||||
|
||||
source_ds_vc.copy_virtual_disk(src_path, target_ds_name_vc, target_path)
|
||||
Reference in new issue
Block a user