Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix destroy multiple disks won't remove the entire folder
When attaching multiple disks with new VM, it won't clean the
entire VM folder after destroying, this patch fix that.
  • Loading branch information
Ethan Lynn committed Dec 26, 2016
commit 322b6f2d59ceed46312f28b92c519e3d28b0d271
24 changes: 13 additions & 11 deletions lib/vSphere/action/clone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ def call(env)
fail Errors::VSphereError, :'invalid_configuration_linked_clone_with_sdrs' if config.linked_clone && ds.is_a?(RbVmomi::VIM::StoragePod)

location = get_location ds, dc, machine, template
spec = RbVmomi::VIM.VirtualMachineCloneSpec location: location, powerOn: true, template: false
spec = RbVmomi::VIM.VirtualMachineCloneSpec location: location, powerOn: false, template: false
spec[:config] = RbVmomi::VIM.VirtualMachineConfigSpec
customization_info = get_customization_spec_info_by_name connection, machine

spec[:customization] = get_customization_spec(machine, customization_info) unless customization_info.nil?

env[:ui].info "Setting custom disks: #{config.disks}" unless config.disks.empty?
add_custom_disks(template, spec, config.disks) unless config.disks.empty?

env[:ui].info "Setting custom address: #{config.addressType}" unless config.addressType.nil? || !config.networks.empty?
add_custom_address_type(template, spec, config.addressType) unless config.addressType.nil? || !config.networks.empty?

Expand Down Expand Up @@ -108,6 +105,14 @@ def call(env)
new_vm.setCustomValue(key: k, value: v)
end
end
machine.id = new_vm.config.uuid

env[:ui].info "Setting custom disks: #{config.disks}" unless config.disks.empty?
add_custom_disks(new_vm, config.disks) unless config.disks.empty?

env[:ui].info I18n.t('vsphere.power_on_vm')
new_vm.PowerOnVM_Task.wait_for_completion

rescue Errors::VSphereError
raise
rescue StandardError => e
Expand All @@ -116,8 +121,6 @@ def call(env)

# TODO: handle interrupted status in the environment, should the vm be destroyed?

machine.id = new_vm.config.uuid

wait_for_sysprep(env, new_vm, connection, 600, 10) if machine.config.vm.guest.eql?(:windows)

env[:ui].info I18n.t('vsphere.vm_clone_success')
Expand Down Expand Up @@ -237,13 +240,12 @@ def get_vm_base_folder(dc, template, config)
end
end

def add_custom_disks(template, spec, disks)
spec[:config][:deviceChange] ||= []
def add_custom_disks(vm, disks)
# Get necessary variables
unit_number = 0
controllerkey = 1000
datastore = ""
old_disks = template.config.hardware.device.grep(RbVmomi::VIM::VirtualDisk)
old_disks = vm.config.hardware.device.grep(RbVmomi::VIM::VirtualDisk)
old_disks.each do |d|
unit_number = d.unitNumber if d.unitNumber > unit_number
controllerkey = d.controllerKey
Expand Down Expand Up @@ -278,9 +280,9 @@ def add_custom_disks(template, spec, disks)
unitNumber: unit_number,
controllerKey: controllerkey,
)

dev_spec = RbVmomi::VIM.VirtualDeviceConfigSpec(device: disk_device, operation: 'add', fileOperation: 'create')
spec[:config][:deviceChange].push dev_spec
disk_spec = RbVmomi::VIM::VirtualMachineConfigSpec(deviceChange: [dev_spec])
vm.ReconfigVM_Task(spec: disk_spec).wait_for_completion
end
end

Expand Down