Class: ForemanAP::Allocator

Inherits:
Object
  • Object
show all
Defined in:
lib/foreman_vm/allocator.rb

Overview

Adds guests to correct hypervisor.

Instance Method Summary collapse

Constructor Details

#initializeAllocator



5
6
7
# File 'lib/foreman_vm/allocator.rb', line 5

def initialize
  @host = []
end

Instance Method Details

#add_guest(name, memory) ⇒ Object

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.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 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) ⇒ Object

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



15
16
17
18
19
20
21
# 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