Class: Bosh::Director::VmReuser

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/vm_reuser.rb

Overview

A class for maintaining VmData objects, making reusing VMs easier.

Instance Method Summary collapse

Constructor Details

#initializeVmReuser

Returns a new instance of VmReuser.



4
5
6
# File 'lib/bosh/director/vm_reuser.rb', line 4

def initialize
  @stemcells_to_vms = {}
end

Instance Method Details

#add_vm(reservation, vm, stemcell, network_settings) ⇒ VmData

Adds a VM’s information to the pool of VMs that can be reused.

Parameters:

  • reservation (NetworkReservation)

    The network reservation for this VM.

  • vm (Models::Vm)

    The VM to be reused.

  • stemcell (Models::Stemcell)

    The Stemcell to make the VM on.

  • network_settings (Hash)

    A hash containing the network reservation.

Returns:

  • (VmData)

    The VmData instance for the new VM.



14
15
16
17
18
19
20
# File 'lib/bosh/director/vm_reuser.rb', line 14

def add_vm(reservation, vm, stemcell, network_settings)
  vm_d = VmData.new(reservation, vm, stemcell, network_settings)
  @stemcells_to_vms[stemcell] ||= []
  @stemcells_to_vms[stemcell] << vm_d
  vm_d.mark_in_use
  vm_d
end

#each {|VmData| ... } ⇒ Object

An iterator for all compilation VMs on all stemcells.

Yields:

  • (VmData)

    Yields each VM in VmReuser.



53
54
55
56
57
58
59
# File 'lib/bosh/director/vm_reuser.rb', line 53

def each
  @stemcells_to_vms.each do |stemcell, vms|
    vms.each do |vm|
      yield vm
    end
  end
end

#get_num_vms(stemcell) ⇒ Integer

Gets the total number of compilation VMs created with a given stemcell.

Parameters:

Returns:

  • (Integer)

    The number of VMs running a given stemcell.



47
48
49
# File 'lib/bosh/director/vm_reuser.rb', line 47

def get_num_vms(stemcell)
  @stemcells_to_vms[stemcell].nil? ? 0 : @stemcells_to_vms[stemcell].size
end

#get_vm(stemcell) ⇒ VmData?

Returns the VmData instance of a VM that is not in use and can be reused.

Parameters:

Returns:

  • (VmData?)

    The VmData instance for an existing unused VM, if one exists. Otherwise, nil.



25
26
27
28
29
30
31
32
33
34
# File 'lib/bosh/director/vm_reuser.rb', line 25

def get_vm(stemcell)
  unless @stemcells_to_vms[stemcell].nil?
    @stemcells_to_vms[stemcell].each do |vm_data|
      if vm_data.mark_in_use
        return vm_data
      end
    end
  end
  nil
end

#remove_vm(vm) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/bosh/director/vm_reuser.rb', line 36

def remove_vm(vm)
  @stemcells_to_vms.each_value do |vms|
    vms.each do |vm_data|
      vms.delete(vm_data) if vm_data.vm == vm
    end
  end
end