F #4913: Refactor DS clone

This commit is contained in:
mcabrerizo committed 2017-02-17 18:08:06 +01:00
1 parent 50ba69df99
commit 013979a620
1 file changed
+36 -46
+36 -46
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# ---------------------------------------------------------------------------- #
# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems #
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems #
# #
# 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 #
@@ -16,10 +16,6 @@
# limitations under the License. #
# ---------------------------------------------------------------------------- #
###############################################################################
# This script is used retrieve the file size of a disk
###############################################################################
ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION)
if !ONE_LOCATION
@@ -31,61 +27,55 @@ end
$: << RUBY_LIB_LOCATION
$: << File.dirname(__FILE__)
require 'opennebula'
require 'vcenter_driver'
require 'vcenter_driver2'
require 'digest'
################################################################################
def check_valid(parameter, label)
if parameter.nil? || parameter.empty?
STDERR.puts "Not enough information to clone the image. " +
"Missing '#{label}'."
exit -1
end
end
################################################################################
drv_action_enc = ARGV[0]
id = ARGV[1]
drv_action = OpenNebula::XMLElement.new
drv_action.initialize_xml(Base64.decode64(drv_action_enc), 'DS_DRIVER_ACTION_DATA')
target_ds_name = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/NAME"]
hostname = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_CLUSTER"]
img_path = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/PATH"]
src_img_id = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/CLONING_ID"]
target_ds_ref = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_DS_REF"]
host_id = drv_action["/DS_DRIVER_ACTION_DATA/DATASTORE/TEMPLATE/VCENTER_CLUSTER"]
src_path = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/PATH"]
src_img_id = drv_action["/DS_DRIVER_ACTION_DATA/IMAGE/CLONING_ID"]
if target_ds_name.nil? || hostname.nil? || img_path.nil?
STDERR.puts "Not enough information to clone the image, missing datastore"\
" name or vcenter cluster name or image path."
exit -1
end
check_valid src_img_id, "cloning id"
check_valid host_id, "vcenter cluster"
check_valid src_path, "image path"
check_valid target_ds_ref, "target ds ref"
# Get source image
client = OpenNebula::Client.new
src_img = OpenNebula::Image.new_with_id(src_img_id, client)
rc = src_img.info
if OpenNebula.is_error?(rc)
STDERR.puts rc.message
exit -1
end
src_ds_id = src_img['DATASTORE_ID']
# Get the source datastore
src_ds = OpenNebula::Datastore.new_with_id(src_ds_id, client)
rc = src_ds.info
if OpenNebula.is_error?(rc)
STDERR.puts rc.message
exit -1
end
src_ds_name = src_ds["TEMPLATE/VCENTER_NAME"]
# Get source ds_ref
source_img = VCenterDriver::VIHelper.one_item(OpenNebula::Image, src_img_id)
source_ds_id = source_img['DATASTORE_ID']
source_ds = VCenterDriver::VIHelper.one_item(OpenNebula::Datastore, source_ds_id)
source_ds_ref = source_ds['TEMPLATE/VCENTER_DS_REF']
# Generate target path
str_for_target_path = Time.now.to_s + id.to_s
target_path = Digest::MD5.hexdigest(str_for_target_path) + ".vmdk"
target_path = Digest::MD5.hexdigest(Time.now.to_s + id.to_s) + ".vmdk"
begin
host_id = VCenterDriver::VIClient.translate_hostname(hostname)
vi_client = VCenterDriver::VIClient.new host_id
vi_client = VCenterDriver::VIClient.new_from_host(host_id)
ds = VCenterDriver::Datastore.new_from_ref(source_ds_ref, vi_client)
puts vi_client.copy_virtual_disk(img_path, src_ds_name, target_path, target_ds_name)
target_ds = VCenterDriver::Datastore.new_from_ref(target_ds_ref, vi_client)
target_ds_name = target_ds['name']
puts ds.copy_virtual_disk(src_path, target_ds_name, target_path)
rescue Exception => e
STDERR.puts "Error cloning img #{img_path} size. Reason: #{e.message}"
STDERR.puts "Error cloning img #{src_path}. Reason: #{e.message}"
exit -1
end
end