Skip to content

Commit

Permalink
Merge pull request #220 from puppetlabs/refactor-get_cluster_host_uti…
Browse files Browse the repository at this point in the history
…lization

Refactor get_cluster_host_utilization method
  • Loading branch information
Rob Braden authored Jun 13, 2017
2 parents 1fcb19b + 87056a7 commit ee3f5e2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
17 changes: 10 additions & 7 deletions lib/vmpooler/providers/vsphere.rb
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,11 @@ def find_folder(foldername, connection)
# Params:
# +model+:: CPU arch version to match on
# +limit+:: Hard limit for CPU or memory utilization beyond which a host is excluded for deployments
# returns nil if one on these conditions is true:
# the model param is defined and cannot be found
# the host is in maintenance mode
# the host status is not 'green'
# the cpu or memory utilization is bigger than the limit param
def get_host_utilization(host, model = nil, limit = 90)
if model
return nil unless host_has_cpu_model?(host, model)
Expand Down Expand Up @@ -555,6 +560,7 @@ def memory_utilization_for(host)
def find_least_used_host(cluster, connection)
cluster_object = find_cluster(cluster, connection)
target_hosts = get_cluster_host_utilization(cluster_object)
raise("There is no host candidate in vcenter that meets all the required conditions, check that the cluster has available hosts in a 'green' status, not in maintenance mode and not overloaded CPU and memory'") if target_hosts.empty?
least_used_host = target_hosts.sort[0][1]
least_used_host
end
Expand All @@ -564,10 +570,10 @@ def find_cluster(cluster, connection)
datacenter.hostFolder.children.find { |cluster_object| cluster_object.name == cluster }
end

def get_cluster_host_utilization(cluster)
def get_cluster_host_utilization(cluster, model=nil)
cluster_hosts = []
cluster.host.each do |host|
host_usage = get_host_utilization(host)
host_usage = get_host_utilization(host, model)
cluster_hosts << host_usage if host_usage
end
cluster_hosts
Expand All @@ -577,11 +583,8 @@ def find_least_used_vpshere_compatible_host(vm)
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 = get_cluster_host_utilization(cluster, model)
raise("There is no host candidate in vcenter that meets all the required conditions, check that the cluster has available hosts in a 'green' status, not in maintenance mode and not overloaded CPU and memory'") if target_hosts.empty?
target_host = target_hosts.sort[0][1]
[target_host, target_host.name]
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/providers/vsphere_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2198,7 +2198,7 @@
end

it 'should raise error' do
expect{subject.find_least_used_vpshere_compatible_host(vm)}.to raise_error(NoMethodError,/undefined method/)
expect{subject.find_least_used_vpshere_compatible_host(vm)}.to raise_error(/There is no host candidate in vcenter that meets all the required conditions/)
end
end

Expand Down Expand Up @@ -2238,7 +2238,7 @@
end

it 'should raise error' do
expect{subject.find_least_used_vpshere_compatible_host(vm)}.to raise_error(NoMethodError,/undefined method/)
expect{subject.find_least_used_vpshere_compatible_host(vm)}.to raise_error(/There is no host candidate in vcenter that meets all the required conditions/)
end
end

Expand Down

0 comments on commit ee3f5e2

Please sign in to comment.