From 781a9928100889b09577b068f83d26763ea8d24b Mon Sep 17 00:00:00 2001 From: Jaime Melis Date: Fri, 2 Dec 2016 19:16:30 +0100 Subject: [PATCH] F 4913: Implement non-persistent images (clone and delete) --- src/tm_mad/vcenter/clone | 104 +++++++++++++++++- src/tm_mad/vcenter/delete | 24 +++- src/vmm_mad/remotes/vcenter/vcenter_driver.rb | 68 +++++++++--- 3 files changed, 181 insertions(+), 15 deletions(-) mode change 120000 => 100755 src/tm_mad/vcenter/clone diff --git a/src/tm_mad/vcenter/clone b/src/tm_mad/vcenter/clone deleted file mode 120000 index 300563f2ad..0000000000 --- a/src/tm_mad/vcenter/clone +++ /dev/null @@ -1 +0,0 @@ -../common/dummy.sh \ No newline at end of file diff --git a/src/tm_mad/vcenter/clone b/src/tm_mad/vcenter/clone new file mode 100755 index 0000000000..2ea33c32fe --- /dev/null +++ b/src/tm_mad/vcenter/clone @@ -0,0 +1,103 @@ +#!/usr/bin/env ruby + +# ---------------------------------------------------------------------------- # +# Copyright 2002-2016, 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 # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +# ---------------------------------------------------------------------------- # + +# clone fe:SOURCE host:remote_system_ds/disk.i vmid dsid +# - fe is the front-end hostname +# - SOURCE is the path of the disk image in the form DS_BASE_PATH/disk +# - host is the target host to deploy the VM +# - remote_system_ds is the path for the system datastore in the host +# - vmid is the id of the VM +# - dsid is the target datastore (0 is the system datastore) + +ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) + +if !ONE_LOCATION + RUBY_LIB_LOCATION="/usr/lib/one/ruby" if !defined?(RUBY_LIB_LOCATION) +else + RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby" if !defined?(RUBY_LIB_LOCATION) +end + +$: << RUBY_LIB_LOCATION +$: << File.dirname(__FILE__) + +require 'opennebula' +require 'vcenter_driver' +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 + +################################################################################ + +src = ARGV[0] +dst = ARGV[1] +vm_id = ARGV[2] +source_ds_id = ARGV[3] + +target_ds_id = dst.split("/")[-3] +disk_id = dst.split(".")[-1] + +src_host, src_path = src.split ":" +hostname, dst_path = dst.split ":" + +client = OpenNebula::Client.new +ds_pool = OpenNebula::DatastorePool.new(client) + +rc = ds_pool.info +if OpenNebula.is_error?(rc) + puts rc.message + exit -1 +end + +xml = "/DATASTORE_POOL/DATASTORE[ID='#{source_ds_id}']/TEMPLATE/VCENTER_NAME" +source_ds = ds_pool[xml] + +xml = "/DATASTORE_POOL/DATASTORE[ID='#{target_ds_id}']/TEMPLATE/VCENTER_NAME" +target_ds = ds_pool[xml] + +################################################################################ + +# Generate target path +target_path = "one_#{vm_id}_#{disk_id}.vmdk" + +################################################################################ + +# Check for errors +check_valid source_ds, "source_ds" +check_valid target_ds, "target_ds" +check_valid hostname, "hostname" +check_valid src_path, "src_path" + +################################################################################ + +begin + host_id = VCenterDriver::VIClient.translate_hostname(hostname) + vi_client = VCenterDriver::VIClient.new host_id + + vi_client.copy_virtual_disk(src_path, source_ds, target_path, target_ds) +rescue Exception => e + STDERR.puts "Error cloning img #{src_path} size. Reason: #{e.message}" + exit -1 +end diff --git a/src/tm_mad/vcenter/delete b/src/tm_mad/vcenter/delete index 039de76570..10dbf275ad 100755 --- a/src/tm_mad/vcenter/delete +++ b/src/tm_mad/vcenter/delete @@ -22,6 +22,15 @@ # - vmid is the id of the VM # - dsid is the target datastore (0 is the system datastore) +# Return if this has called for the whole directory, instead of for a specific +# disk. + +if !ARGV[0].match(/disk\.\d+$/) + exit(0) +end + +# ---------------------------------------------------------------------------- # + ONE_LOCATION=ENV["ONE_LOCATION"] if !defined?(ONE_LOCATION) if !ONE_LOCATION @@ -34,11 +43,24 @@ $: << RUBY_LIB_LOCATION $: << File.dirname(__FILE__) require 'vcenter_driver' +require 'opennebula' hostname, img_path = ARGV[0].split(":") vmid = ARGV[1] dsid = ARGV[2] +client = OpenNebula::Client.new +vm = OpenNebula::VirtualMachine.new_with_id(vmid, client) +vm.info + +disk_id = img_path.split(".")[-1] + +persistent = vm["TEMPLATE/DISK[DISK_ID=#{disk_id}]/PERSISTENT"] + +if persistent != "YES" + img_path = "one_#{vmid}_#{disk_id}.vmdk" +end + begin host_id = VCenterDriver::VIClient.translate_hostname(hostname) vi_client = VCenterDriver::VIClient.new host_id @@ -47,6 +69,6 @@ begin vi_client.delete_virtual_disk(img_path, ds_name) rescue Exception => e STDERR.puts "Error delete virtual disk #{img_path} in datastore #{dsid}."\ - " Reason: #{e.message}" + " Reason: #{e.message}\n#{e.backtrace}" exit -1 end diff --git a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb index c671e3f976..4279a81d70 100644 --- a/src/vmm_mad/remotes/vcenter/vcenter_driver.rb +++ b/src/vmm_mad/remotes/vcenter/vcenter_driver.rb @@ -823,7 +823,7 @@ class VIClient end def self.find_ds_name(ds_id) - ds = OpenNebula::Datastore.new_with_id(ds_id) + ds = OpenNebula::Datastore.new_with_id(ds_id, OpenNebula::Client.new) rc = ds.info raise "Could not find datastore #{ds_id}" if OpenNebula.is_error?(rc) @@ -1524,6 +1524,7 @@ class VCenterVm reconfigure_vm(vm, xml, false, hostname) vm.PowerOnVM_Task.wait_for_completion + return vm.config.uuid end end @@ -1537,13 +1538,15 @@ class VCenterVm # @param disks VM attached disks ############################################################################ def self.cancel(deploy_id, hostname, lcm_state, keep_disks, disks, to_template) + case lcm_state when "SHUTDOWN_POWEROFF", "SHUTDOWN_UNDEPLOY" shutdown(deploy_id, hostname, lcm_state, keep_disks, disks, to_template) when "CANCEL", "LCM_INIT", "CLEANUP_RESUBMIT", "SHUTDOWN", "CLEANUP_DELETE" hid = VIClient::translate_hostname(hostname) connection = VIClient.new(hid) - vm = connection.find_vm_template(deploy_id) + + vm = connection.find_vm_template(deploy_id) begin if vm.summary.runtime.powerState == "poweredOn" @@ -1551,6 +1554,7 @@ class VCenterVm end rescue end + if keep_disks detach_all_disks(vm) else @@ -1661,7 +1665,8 @@ class VCenterVm hid = VIClient::translate_hostname(hostname) connection = VIClient.new(hid) - vm = connection.find_vm_template(deploy_id) + vm = connection.find_vm_template(deploy_id) + vmid = get_vm_id case lcm_state when "SHUTDOWN" @@ -2628,8 +2633,6 @@ private end - - reconfigure_vm(vm, xml, true, hostname) # Power on the VM @@ -2860,11 +2863,11 @@ private hid = VIClient::translate_hostname(hostname) connection = VIClient.new(hid) disks.each{|disk| - ds_name = disk.elements["DATASTORE"].text - img_name = disk.elements["SOURCE"].text - type_str = disk.elements["TYPE"].text + ds_name = disk.elements["DATASTORE"].text + img_name = get_disk_img_path(disk, vmid) + type_str = disk.elements["TYPE"].text - disk_array += attach_disk("", "", ds_name, img_name, type_str, 0, vm, connection)[:deviceChange] + disk_array += attach_disk("", "", ds_name, img_name, type_str, 0, vm, connection)[:deviceChange] } device_change += disk_array @@ -2888,6 +2891,7 @@ private end spec = RbVmomi::VIM.VirtualMachineConfigSpec(spec_hash) + vm.ReconfigVM_Task(:spec => spec).wait_for_completion end @@ -2924,7 +2928,7 @@ private end } - ds = datastores.select{|ds| ds.name == ds_name}[0] + ds = datastores.select{|ds| ds.name == ds_name}[0] controller, new_number = find_free_controller(vm) @@ -3165,14 +3169,20 @@ private hid = VIClient::translate_hostname(hostname) connection = VIClient.new(hid) + vmid = vm.config.extraConfig.select do |val| + val[:key] == "opennebula.vm.id" + end.first.value + spec = { :deviceChange => [] } disks.each{ |disk| - ds_and_img_name = "[#{disk['DATASTORE']}] #{disk['SOURCE']}" - vcenter_disk = vm.config.hardware.device.select { |d| is_disk?(d) && + img_name = get_disk_img_path(disk, vmid) + ds_and_img_name = "[#{disk['DATASTORE']}] #{img_name}" + + vcenter_disk = vm.config.hardware.device.select { |d| is_disk?(d) && d.backing.respond_to?(:fileName) && d.backing.fileName == ds_and_img_name }[0] - spec[:deviceChange] << { + spec[:deviceChange] << { :operation => :remove, :device => vcenter_disk } @@ -3180,5 +3190,37 @@ private vm.ReconfigVM_Task(:spec => spec).wait_for_completion end + + + ############################################################################ + # Returns the source path of a disk. It will use the 'SOURCE' path if + # persistent and one-#{vm_id}-#{disk_id}.vmdk otherwise + # @param disks VM attached disks, either an REXML document, or a hash + # @param vmid The VM ID + ############################################################################ + def self.get_disk_img_path(disk, vmid) + if disk.respond_to? :elements + # It's a REXML::Document, probably coming from self.reconfigure_vm + persistent = disk.elements["PERSISTENT"].text == "YES" rescue false + + if persistent + disk.elements["SOURCE"].text + else + disk_id = disk.elements["DISK_ID"].text + "one_#{vmid}_#{disk_id}.vmdk" + end + else + # It's a hash, probably coming from self.detach_attached_disks + persistent = disk["PERSISTENT"] == "YES" + + if persistent + disk["SOURCE"] + else + disk_id = disk["DISK_ID"] + "one_#{vmid}_#{disk_id}.vmdk" + end + end + end + end end