class ForemanAP::Allocator

Adds guests to correct hypervisor.

Public Class Methods

new() click to toggle source
# File lib/foreman_vm/allocator.rb, line 5
def initialize
  @host = []
end

Public Instance Methods

add_guest(name,memory) click to toggle source

Find the best hypervisor that meets the allocation policy

name

the name of the guest

memory

the amount of memory the guest needs, in bytes

Returns the name of the most suitable hypervisor. If no hypervisor is suitable, it returns nil.

# File lib/foreman_vm/allocator.rb, line 30
def add_guest(name,memory)
  # Sort by most free memory
  host_tmp = @host.sort_by { |x| -x[:free_memory] }
  # Delete from list if not enough memory for guest
  host_tmp.delete_if { |x| x[:free_memory] < memory.to_i }
  # Check if guest already exists and returns nil if so
  @host.each { |x| return nil if x[:guests].include?(name) }
  # Delete from list if vm type exists, unless it deletes all then return best host
  pre = name.gsub(/[0-9]/, '')
  suitable = host_tmp.dup.delete_if { |x| x[:guests].grep(/^#{pre}/).any? }
  if suitable.any? 
    return suitable[0][:name]
  elsif host_tmp.any? 
    return host_tmp[0][:name]
  else
    return nil 
  end
end
add_host(name,free_memory,guests) click to toggle source

Add information about a hypervisor

name

the name of the host

free_memory

how much free memory, in bytes

guests

a list of the names of each VM on the host

# File lib/foreman_vm/allocator.rb, line 15
def add_host(name,free_memory,guests)
  @host.push({
    :name => name,
    :free_memory => free_memory,
    :guests => guests
  })
end