From bbe67ab8a3c846cb6d0289758e9bbf2b7dfdb947 Mon Sep 17 00:00:00 2001 From: Scott Schneider Date: Tue, 4 Feb 2014 09:42:28 -0800 Subject: [PATCH] Rework vCenter VM retrieval method Previous, the propertyCollector method was used to find VMs within vCenter. This method was ineffecient, as it would retrieve a list of the entire vCenter inventory and then parse for the specified VM. This has been replaced with a fetch via searchIndex.FindByDnsName, which returns only the requested VM object. --- lib/vsphere_helper.rb | 83 ++++++++++++++++--------------------------- vmware-host-pooler | 8 ++--- 2 files changed, 34 insertions(+), 57 deletions(-) diff --git a/lib/vsphere_helper.rb b/lib/vsphere_helper.rb index 3e88019..85d765b 100755 --- a/lib/vsphere_helper.rb +++ b/lib/vsphere_helper.rb @@ -22,60 +22,13 @@ class VsphereHelper - # an easier wrapper around the horrid PropertyCollector interface, - # necessary for searching VMs in all Datacenters that may be nested - # within folders of arbitrary depth - # returns a hash array of => - def find_vms names, connection = @connection - names = names.is_a?(Array) ? names : [ names ] - containerView = get_base_vm_container_from connection - propertyCollector = connection.propertyCollector - - objectSet = [{ - :obj => containerView, - :skip => true, - :selectSet => [ RbVmomi::VIM::TraversalSpec.new({ - :name => 'gettingTheVMs', - :path => 'view', - :skip => false, - :type => 'ContainerView' - }) ] - }] - - propSet = [{ - :pathSet => [ 'name' ], - :type => 'VirtualMachine' - }] - - results = propertyCollector.RetrievePropertiesEx({ - :specSet => [{ - :objectSet => objectSet, - :propSet => propSet - }], - :options => { :maxObjects => nil } - }) - - vms = {} - results.objects.each do |result| - name = result.propSet.first.val - next unless names.include? name - vms[name] = result.obj - end - - while results.token do - results = propertyCollector.ContinueRetrievePropertiesEx({:token => results.token}) - results.objects.each do |result| - name = result.propSet.first.val - next unless names.include? name - vms[name] = result.obj - end - end - vms - end - - - def find_datastore datastorename + begin + @connection.serviceInstance.CurrentTime + rescue + initialize() + end + datacenter = @connection.serviceInstance.find_datacenter datacenter.find_datastore(datastorename) end @@ -83,6 +36,12 @@ class VsphereHelper def find_folder foldername + begin + @connection.serviceInstance.CurrentTime + rescue + initialize() + end + datacenter = @connection.serviceInstance.find_datacenter base = datacenter.vmFolder folders = foldername.split('/') @@ -129,7 +88,25 @@ class VsphereHelper + def find_vm vmname + begin + @connection.serviceInstance.CurrentTime + rescue + initialize() + end + + @connection.searchIndex.FindByDnsName(:vmSearch => true, :dnsName => vmname) + end + + + def get_base_vm_container_from connection + begin + connection.serviceInstance.CurrentTime + rescue + initialize() + end + viewManager = connection.serviceContent.viewManager viewManager.CreateContainerView({ :container => connection.serviceContent.rootFolder, diff --git a/vmware-host-pooler b/vmware-host-pooler index bb281f7..d411b4b 100755 --- a/vmware-host-pooler +++ b/vmware-host-pooler @@ -45,7 +45,7 @@ $threads = {} # Check the state of a VM def check_pending_vm vm, pool, timeout Thread.new { - host = $vsphere[pool].find_vms(vm)[vm] + host = $vsphere[pool].find_vm(vm) if (host) if ( @@ -79,7 +79,7 @@ end def check_ready_vm vm, pool, ttl Thread.new { - host = $vsphere[pool].find_vms(vm)[vm] + host = $vsphere[pool].find_vm(vm) if (host) if ( @@ -107,7 +107,7 @@ end def check_running_vm vm, pool, ttl Thread.new { - host = $vsphere[pool].find_vms(vm)[vm] + host = $vsphere[pool].find_vm(vm) if (host) if ( @@ -214,7 +214,7 @@ def destroy_vm vm, pool $redis.srem('vmware_host_pool__completed__'+pool, vm) $redis.hdel('vmware_host_pool__active__'+pool, vm) - host = $vsphere[pool].find_vms(vm)[vm] + host = $vsphere[pool].find_vm(vm) if (host) start = Time.now