Currently, I'm Setting up a custom button in Cloudforms to allow for the removal of a drive from a Virtual Machine that has been provisioned using Cloudforms(hooked into ansible)
I've been looking at this a little while and after some digging discovered the following https://github.com/ManageIQ/manageiq-automation_engine/blob/master/lib/miq_automation_engine/service_models/miq_ae_service_manageiq-providers-vmware-infra_manager-vm.rb
More specifically:
def remove_disk(disk_name, options = {})
sync_or_async_ems_operation(options[:sync], "remove_disk", [disk_name, options])
end
I've assumed two things, that this would probably take the vmdk name and would work the same as "add_disk" (vm.add_disk("[#{vm.storage_name}]", size * 1024, :sync => true)).
I AM AWARE that you can edit disks using the built-in functionality of CLoudforms via the provided configuration button, however, due to customer requirements we needed to edit the HAML files to remove some functionality. Redoing the HAML on every update of CloudForms is rather counterproductive. Creating our own custom dialogs provides us with the customization we needed.
# Get vm object
vm = $evm.root['vm']
raise "Missing $evm.root['vm'] object" unless vm
def log(level, message)
@method = 'Remove_Disk'
$evm.log(level, "#{@method} - #{message}")
end
$evm.create_notification(:audience => 'user', :level => :success, :message => "Lifecycle action 'Remove Disk' Initiated")
log(:info, "Started LCA to remove disk on vm: <#{vm}>")
# Remove disk from the VM
disk_choice = $evm.root['dialog_availabledisks'].to_i
if disk_choice.zero?
disk_name = "#{vm}"
$evm.create_notification(:audience => 'user', :level => :failure, :message => "Lifecycle action 'Remove Disk' Failed, OS Drive cannot be removed.")
log(:error, "C: Drive cannot be deleted")
exit MIQ_ABORT
else
disk_name = "#{vm}_#{disk_choice}"
end
log(:info, "Removing disk:<#{disk_name}> from #{vm}")
begin
vm.remove_disk(disk_name, :sync => true)
rescue => e
log(:error, "e: #{e}")
log(:error, "e.inspect: #{e.inspect}")
log(:error,"[#{e}]\n#{e.backtrace.join("\n")}")
log(:error, "e.message: #{e.message}")
end
exit MIQ_OK
My code runs through without any errors, however, does not actually do anything, the disk selected is not removed from the VM. Fairly sure I am just missing something obvious(Or my assumptions are incorrect), any ideas?
Hello I Have Successfully Remove Disk from VM in Cloudforms with Your Code
But There is one minor thing I have changed
you are using disk_name = "#{vm}_#{disk_choice}" as a disk name in order to delete Disk. But This is not like that.
You have to mention Datastore for that disk in disk_name then it will be work fine
you can try with following code
disk_name = "[your_datastore_name] #{vm}/#{vm}_#{disk_choice}.vmdk"
This works fine with me..!!
One more thing you can consider i.e Delete Backing
Delete Backing => OFF [Default] It will Just Detach your Disk from VM but not completely removed
so you have to turn on your Delete Backing so that it can completely remove disk from storage Domain as well
Delete Backing => ON
you can explore here
Thank you..!!