Update find_least_used_host method to evaluate hosts based on utilization. Without this change the determination is based on VM count. Additionally, a method is added to find the least used host compatible with the provided VM in order to support migrating a VM at checkout. Lastly, a capability is added to migrate VMs to a provided host, which also supports migrating VMs at checkout.

Add method to check if vsphere connection is alive. Replace repeated usage of checking the current time in a begin/rescue block with this method.
This commit is contained in:
kirby@puppetlabs.com 2016-10-14 12:46:37 -07:00
parent 506c124578
commit 538f30af8e

View file

@ -17,11 +17,7 @@ module Vmpooler
end end
def add_disk(vm, size, datastore) def add_disk(vm, size, datastore)
begin vsphere_connection_alive? @connection
@connection.serviceInstance.CurrentTime
rescue
initialize
end
return false unless size.to_i > 0 return false unless size.to_i > 0
@ -71,22 +67,14 @@ module Vmpooler
end end
def find_datastore(datastorename) def find_datastore(datastorename)
begin vsphere_connection_alive? @connection
@connection.serviceInstance.CurrentTime
rescue
initialize
end
datacenter = @connection.serviceInstance.find_datacenter datacenter = @connection.serviceInstance.find_datacenter
datacenter.find_datastore(datastorename) datacenter.find_datastore(datastorename)
end end
def find_device(vm, deviceName) def find_device(vm, deviceName)
begin vsphere_connection_alive? @connection
@connection.serviceInstance.CurrentTime
rescue
initialize
end
vm.config.hardware.device.each do |device| vm.config.hardware.device.each do |device|
return device if device.deviceInfo.label == deviceName return device if device.deviceInfo.label == deviceName
@ -96,11 +84,7 @@ module Vmpooler
end end
def find_disk_controller(vm) def find_disk_controller(vm)
begin vsphere_connection_alive? @connection
@connection.serviceInstance.CurrentTime
rescue
initialize
end
devices = find_disk_devices(vm) devices = find_disk_devices(vm)
@ -114,11 +98,7 @@ module Vmpooler
end end
def find_disk_devices(vm) def find_disk_devices(vm)
begin vsphere_connection_alive? @connection
@connection.serviceInstance.CurrentTime
rescue
initialize
end
devices = {} devices = {}
@ -146,11 +126,7 @@ module Vmpooler
end end
def find_disk_unit_number(vm, controller) def find_disk_unit_number(vm, controller)
begin vsphere_connection_alive? @connection
@connection.serviceInstance.CurrentTime
rescue
initialize
end
used_unit_numbers = [] used_unit_numbers = []
available_unit_numbers = [] available_unit_numbers = []
@ -175,11 +151,7 @@ module Vmpooler
end end
def find_folder(foldername) def find_folder(foldername)
begin vsphere_connection_alive? @connection
@connection.serviceInstance.CurrentTime
rescue
initialize
end
datacenter = @connection.serviceInstance.find_datacenter datacenter = @connection.serviceInstance.find_datacenter
base = datacenter.vmFolder base = datacenter.vmFolder
@ -196,39 +168,96 @@ module Vmpooler
base base
end end
def find_least_used_host(cluster) # Returns an array containing cumulative CPU and memory utilization of a host, and its object reference
# Params:
# +model+:: CPU arch version to match on
# +limit+:: Hard limit for CPU or memory utilization beyond which a host is excluded for deployments
def get_host_utilization(host, model=nil, limit=90)
if model
return nil unless host_has_cpu_model? host, model
end
return nil if host.runtime.inMaintenanceMode
return nil unless host.overallStatus == 'green'
cpu_utilization = cpu_utilization_for host
memory_utilization = memory_utilization_for host
return nil if cpu_utilization > limit
return nil if memory_utilization > limit
[ cpu_utilization + memory_utilization, host ]
end
def host_has_cpu_model?(host, model)
get_host_cpu_arch_version(host) == model
end
def get_host_cpu_arch_version(host)
cpu_model = host.hardware.cpuPkg[0].description
cpu_model_parts = cpu_model.split()
arch_version = cpu_model_parts[4]
arch_version
end
def cpu_utilization_for(host)
cpu_usage = host.summary.quickStats.overallCpuUsage
cpu_size = host.summary.hardware.cpuMhz * host.summary.hardware.numCpuCores
(cpu_usage.to_f / cpu_size.to_f) * 100
end
def memory_utilization_for(host)
memory_usage = host.summary.quickStats.overallMemoryUsage
memory_size = host.summary.hardware.memorySize / 1024 / 1024
(memory_usage.to_f / memory_size.to_f) * 100
end
def vsphere_connection_alive?(connection)
begin begin
@connection.serviceInstance.CurrentTime connection.serviceInstance.CurrentTime
rescue rescue
initialize initialize
end end
end
hosts = {} def find_least_used_host(cluster)
hosts_sort = {} vsphere_connection_alive? @connection
cluster_object = find_cluster(cluster)
target_hosts = get_cluster_host_utilization(cluster_object)
least_used_host = target_hosts.sort[0][1]
least_used_host
end
def find_cluster(cluster)
datacenter = @connection.serviceInstance.find_datacenter datacenter = @connection.serviceInstance.find_datacenter
datacenter.hostFolder.children.each do |folder| datacenter.hostFolder.children.find { |cluster_object| cluster_object.name == cluster }
next unless folder.name == cluster end
folder.host.each do |host|
if
(host.overallStatus == 'green') &&
(!host.runtime.inMaintenanceMode)
hosts[host.name] = host def get_cluster_host_utilization(cluster)
hosts_sort[host.name] = host.vm.length cluster_hosts = []
end cluster.host.each do |host|
end host_usage = get_host_utilization(host)
cluster_hosts << host_usage if host_usage
end end
cluster_hosts
end
hosts[hosts_sort.sort_by { |_k, v| v }[0][0]] def find_least_used_compatible_host(vm)
vsphere_connection_alive? @connection
source_host = vm.summary.runtime.host
model = get_host_cpu_arch_version(source_host)
cluster = source_host.parent
target_hosts = []
cluster.host.each do |host|
host_usage = get_host_utilization(host, model)
target_hosts << host_usage if host_usage
end
target_hosts.sort[0][1]
end end
def find_pool(poolname) def find_pool(poolname)
begin vsphere_connection_alive? @connection
@connection.serviceInstance.CurrentTime
rescue
initialize
end
datacenter = @connection.serviceInstance.find_datacenter datacenter = @connection.serviceInstance.find_datacenter
base = datacenter.hostFolder base = datacenter.hostFolder
@ -257,21 +286,13 @@ module Vmpooler
end end
def find_vm(vmname) def find_vm(vmname)
begin vsphere_connection_alive? @connection
@connection.serviceInstance.CurrentTime
rescue
initialize
end
@connection.searchIndex.FindByDnsName(vmSearch: true, dnsName: vmname) @connection.searchIndex.FindByDnsName(vmSearch: true, dnsName: vmname)
end end
def find_vm_heavy(vmname) def find_vm_heavy(vmname)
begin vsphere_connection_alive? @connection
@connection.serviceInstance.CurrentTime
rescue
initialize
end
vmname = vmname.is_a?(Array) ? vmname : [vmname] vmname = vmname.is_a?(Array) ? vmname : [vmname]
containerView = get_base_vm_container_from @connection containerView = get_base_vm_container_from @connection
@ -321,11 +342,7 @@ module Vmpooler
end end
def find_vmdks(vmname, datastore) def find_vmdks(vmname, datastore)
begin vsphere_connection_alive? @connection
connection.serviceInstance.CurrentTime
rescue
initialize
end
disks = [] disks = []
@ -344,11 +361,7 @@ module Vmpooler
end end
def get_base_vm_container_from(connection) def get_base_vm_container_from(connection)
begin vsphere_connection_alive? @connection
connection.serviceInstance.CurrentTime
rescue
initialize
end
viewManager = connection.serviceContent.viewManager viewManager = connection.serviceContent.viewManager
viewManager.CreateContainerView( viewManager.CreateContainerView(
@ -372,6 +385,11 @@ module Vmpooler
snapshot snapshot
end end
def migrate_vm_host(vm, host)
relospec = RbVmomi::VIM.VirtualMachineRelocateSpec(host: host)
vm.RelocateVM_Task(spec: relospec).wait_for_completion
end
def close def close
@connection.close @connection.close
end end