Class: Bosh::Director::DeploymentPlan::ResourcePool

Inherits:
Object
  • Object
show all
Includes:
ValidationHelper
Defined in:
lib/bosh/director/deployment_plan/resource_pool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValidationHelper

#invalid_type, #safe_property

Constructor Details

#initialize(deployment_plan, spec) ⇒ ResourcePool



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 40

def initialize(deployment_plan, spec)
  @deployment_plan = deployment_plan

  @name = safe_property(spec, "name", :class => String)
  @size = safe_property(spec, "size", :class => Integer)

  @cloud_properties =
    safe_property(spec, "cloud_properties", :class => Hash)

  stemcell_spec = safe_property(spec, "stemcell", :class => Hash)
  @stemcell = Stemcell.new(self, stemcell_spec)

  network_name = safe_property(spec, "network", :class => String)
  @network = @deployment_plan.network(network_name)

  if @network.nil?
    raise ResourcePoolUnknownNetwork,
          "Resource pool `#{@name}' references " +
          "an unknown network `#{network_name}'"
  end

  @env = safe_property(spec, "env", :class => Hash, :default => {})

  @idle_vms = []
  @allocated_vms = []
  @active_vm_count = 0
  @required_capacity = 0
end

Instance Attribute Details

#active_vm_countInteger (readonly)



36
37
38
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 36

def active_vm_count
  @active_vm_count
end

#allocated_vmsArray<DeploymentPlan::IdleVm] List of allocated idle VMs (readonly)



33
34
35
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 33

def allocated_vms
  @allocated_vms
end

#cloud_propertiesHash (readonly)



24
25
26
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 24

def cloud_properties
  @cloud_properties
end

#deployment_planDeploymentPlan (readonly)



15
16
17
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 15

def deployment_plan
  @deployment_plan
end

#envHash (readonly)



27
28
29
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 27

def env
  @env
end

#idle_vmsArray<DeploymentPlan::IdleVm> (readonly)



30
31
32
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 30

def idle_vms
  @idle_vms
end

#nameString (readonly)



9
10
11
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 9

def name
  @name
end

#networkDeploymentPlan::Network (readonly)



21
22
23
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 21

def network
  @network
end

#sizeInteger (readonly)



12
13
14
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 12

def size
  @size
end

#stemcellDeploymentPlan::Stemcell (readonly)



18
19
20
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 18

def stemcell
  @stemcell
end

Instance Method Details

#add_idle_vmObject

Adds a new VM to a list of managed idle VMs



111
112
113
114
115
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 111

def add_idle_vm
  idle_vm = IdleVm.new(self)
  @idle_vms << idle_vm
  idle_vm
end

#allocate_vmObject



117
118
119
120
121
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 117

def allocate_vm
  allocated_vm = @idle_vms.pop
  @allocated_vms << allocated_vm
  allocated_vm
end

#mark_active_vmvoid

This method returns an undefined value.

“Active” VM is a VM that is currently running a job



125
126
127
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 125

def mark_active_vm
  @active_vm_count += 1
end

#missing_vm_countInteger

Returns a number of VMs that need to be created in order to bring this resource pool to a desired size



106
107
108
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 106

def missing_vm_count
  @size - @active_vm_count - @idle_vms.size
end

#process_idle_vmsvoid

This method returns an undefined value.

Creates IdleVm objects for any missing resource pool VMs and reserves dynamic networks for all idle VMs.



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 82

def process_idle_vms
  # First, see if we need any data structures to balance the pool size
  missing_vm_count.times { add_idle_vm }

  # Second, see if some of idle VMs still need network reservations
  idle_vms.each do |idle_vm|
    unless idle_vm.has_network_reservation?
      idle_vm.use_reservation(reserve_dynamic_network)
    end
  end
end

#reserve_capacity(n) ⇒ void

This method returns an undefined value.

Checks if there is enough capacity to run extra N VMs, raise error if not enough capacity



133
134
135
136
137
138
139
140
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 133

def reserve_capacity(n)
  @required_capacity += n
  if @required_capacity > @size
    raise ResourcePoolNotEnoughCapacity,
          "Resource pool `#{@name}' is not big enough: " +
          "#{@required_capacity} VMs needed, capacity is #{@size}"
  end
end

#reserve_dynamic_networkNetworkReservation

Tries to obtain one dynamic reservation in its own network



97
98
99
100
101
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 97

def reserve_dynamic_network
  reservation = NetworkReservation.new_dynamic
  @network.reserve!(reservation, "Resource pool `#{@name}'")
  reservation
end

#specHash

Returns resource pools spec as Hash (usually for agent to serialize)



71
72
73
74
75
76
77
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 71

def spec
  {
    "name" => @name,
    "cloud_properties" => @cloud_properties,
    "stemcell" => @stemcell.spec
  }
end