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

Returns a new instance of ResourcePool.

Parameters:

  • deployment_plan (DeploymentPlan)

    Deployment plan

  • spec (Hash)

    Raw resource pool spec from the deployment manifest



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
68
# 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
  @errand_capacity = 0
end

Instance Attribute Details

#active_vm_countInteger (readonly)

Returns Number of active resource pool VMs.

Returns:

  • (Integer)

    Number of active resource pool VMs



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)

Returns Array<DeploymentPlan::IdleVm] List of allocated idle VMs.

Returns:



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

def allocated_vms
  @allocated_vms
end

#cloud_propertiesHash (readonly)

Returns Cloud properties.

Returns:

  • (Hash)

    Cloud properties



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

def cloud_properties
  @cloud_properties
end

#deployment_planDeploymentPlan (readonly)

Returns Deployment plan.

Returns:



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

def deployment_plan
  @deployment_plan
end

#envHash (readonly)

Returns Resource pool environment.

Returns:

  • (Hash)

    Resource pool environment



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

def env
  @env
end

#idle_vmsArray<DeploymentPlan::IdleVm> (readonly)

Returns List of idle VMs.

Returns:



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

def idle_vms
  @idle_vms
end

#nameString (readonly)

Returns Resource pool name.

Returns:

  • (String)

    Resource pool name



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

def name
  @name
end

#networkDeploymentPlan::Network (readonly)

Returns Network spec.

Returns:



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

def network
  @network
end

#sizeInteger (readonly)

Returns Expected resource pool size (in VMs).

Returns:

  • (Integer)

    Expected resource pool size (in VMs)



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

def size
  @size
end

#stemcellDeploymentPlan::Stemcell (readonly)

Returns Stemcell spec.

Returns:



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



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

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

#allocate_vmObject



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

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

#deallocate_vm(idle_vm_cid) ⇒ Object



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

def deallocate_vm(idle_vm_cid)
  deallocated_vm = @allocated_vms.find { |idle_vm| idle_vm.vm.cid == idle_vm_cid }
  @allocated_vms.delete(deallocated_vm)
  @idle_vms << deallocated_vm
  deallocated_vm
end

#mark_active_vmvoid

This method returns an undefined value.

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



133
134
135
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 133

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

Returns:

  • (Integer)


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

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.



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

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



141
142
143
144
145
146
147
148
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 141

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

Returns:

Raises:



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

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

#reserve_errand_capacity(n) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/bosh/director/deployment_plan/resource_pool.rb', line 150

def reserve_errand_capacity(n)
  needed = n - @errand_capacity

  if needed > 0
    reserve_capacity(needed)
    @errand_capacity = n
  end
end

#specHash

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

Returns:

  • (Hash)

    Resource pool spec



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

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