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.
This commit is contained in:
Scott Schneider 2014-02-04 09:42:28 -08:00
parent ed22ed6e08
commit 1003afaf00
2 changed files with 34 additions and 57 deletions

View file

@ -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 <name> => <VirtualMachine ManagedObjects>
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 def find_datastore datastorename
begin
@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
@ -83,6 +36,12 @@ class VsphereHelper
def find_folder foldername def find_folder foldername
begin
@connection.serviceInstance.CurrentTime
rescue
initialize()
end
datacenter = @connection.serviceInstance.find_datacenter datacenter = @connection.serviceInstance.find_datacenter
base = datacenter.vmFolder base = datacenter.vmFolder
folders = foldername.split('/') 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 def get_base_vm_container_from connection
begin
connection.serviceInstance.CurrentTime
rescue
initialize()
end
viewManager = connection.serviceContent.viewManager viewManager = connection.serviceContent.viewManager
viewManager.CreateContainerView({ viewManager.CreateContainerView({
:container => connection.serviceContent.rootFolder, :container => connection.serviceContent.rootFolder,

View file

@ -45,7 +45,7 @@ $threads = {}
# Check the state of a VM # Check the state of a VM
def check_pending_vm vm, pool, timeout def check_pending_vm vm, pool, timeout
Thread.new { Thread.new {
host = $vsphere[pool].find_vms(vm)[vm] host = $vsphere[pool].find_vm(vm)
if (host) if (host)
if ( if (
@ -79,7 +79,7 @@ end
def check_ready_vm vm, pool, ttl def check_ready_vm vm, pool, ttl
Thread.new { Thread.new {
host = $vsphere[pool].find_vms(vm)[vm] host = $vsphere[pool].find_vm(vm)
if (host) if (host)
if ( if (
@ -107,7 +107,7 @@ end
def check_running_vm vm, pool, ttl def check_running_vm vm, pool, ttl
Thread.new { Thread.new {
host = $vsphere[pool].find_vms(vm)[vm] host = $vsphere[pool].find_vm(vm)
if (host) if (host)
if ( if (
@ -214,7 +214,7 @@ def destroy_vm vm, pool
$redis.srem('vmware_host_pool__completed__'+pool, vm) $redis.srem('vmware_host_pool__completed__'+pool, vm)
$redis.hdel('vmware_host_pool__active__'+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) if (host)
start = Time.now start = Time.now