Created
September 11, 2018 08:54
-
-
Save petericebear/02739f8b9b3c4dd20040b6b996d7e722 to your computer and use it in GitHub Desktop.
vspere_vm_disk_iops.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'chef/knife' | |
require 'chef/knife/base_vsphere_command' | |
# Set specific disk IOPS for an aattached disk to the VM | |
# VsphereVmdisklist extends the BaseVspherecommand | |
class Chef::Knife::VsphereVmDiskIops < Chef::Knife::BaseVsphereCommand | |
banner 'knife vsphere vm disk iops VMNAME DISKINDEX IOPS' | |
common_options | |
# The main run method for vm_disk_iops | |
# | |
def run | |
$stdout.sync = true | |
unless vmname = @name_args[0] | |
show_usage | |
fatal_exit 'You must specify a virtual machine name' | |
end | |
unless diskindex = @name_args[1] | |
show_usage | |
fatal_exit 'You must specify a virtual disk index' | |
end | |
unless iops = @name_args[2] | |
show_usage | |
fatal_exit 'You must specify an IOPS limit' | |
end | |
vim_connection | |
vm = get_vm(vmname) | |
fatal_exit "Could not find #{vmname}" unless vm | |
fatal_exit "Primary disk could not be removed" unless diskindex.to_i>0 | |
disks = vm.config.hardware.device.select do |device| | |
device.is_a? RbVmomi::VIM::VirtualDisk | |
end | |
spec = RbVmomi::VIM::VirtualMachineConfigSpec( | |
deviceChange: [ | |
RbVmomi::VIM::VirtualDeviceConfigSpec( | |
device: disks[diskindex.to_i], | |
operation: RbVmomi::VIM::VirtualDeviceConfigSpecOperation('edit') | |
) | |
] | |
) | |
spec.deviceChange[0].device.storageIOAllocation.limit = iops.to_i | |
vm.ReconfigVM_Task(spec: spec).wait_for_completion | |
puts "IOPS disk #{diskindex} set to #{iops} iops" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment