Class: Bosh::Director::DeploymentPlan::InstanceVmBinder

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

Instance Method Summary collapse

Constructor Details

#initialize(event_log) ⇒ InstanceVmBinder

Returns a new instance of InstanceVmBinder.



3
4
5
# File 'lib/bosh/director/deployment_plan/instance_vm_binder.rb', line 3

def initialize(event_log)
  @event_log = event_log
end

Instance Method Details

#bind_instance_vm(instance) ⇒ Object

Parameters:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/bosh/director/deployment_plan/instance_vm_binder.rb', line 34

def bind_instance_vm(instance)
  @event_log.track("#{instance.job.name}/#{instance.index}") do
    idle_vm = instance.idle_vm

    # Apply the assignment to the VM
    agent = AgentClient.with_defaults(idle_vm.vm.agent_id)
    state = idle_vm.current_state
    state['job'] = instance.job.spec
    state['index'] = instance.index
    agent.apply(state)

    # Our assumption here is that director database access
    # is much less likely to fail than VM agent communication
    # so we only update database after we see a successful agent apply.
    # If database update fails subsequent deploy will try to
    # assign a new VM to this instance which is ok.
    idle_vm.vm.db.transaction do
      idle_vm.vm.update(:apply_spec => state)
      instance.model.update(:vm => idle_vm.vm)
    end

    instance.current_state = state
  end
end

#bind_instance_vms(instances) ⇒ Object

Parameters:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bosh/director/deployment_plan/instance_vm_binder.rb', line 9

def bind_instance_vms(instances)
  unbound_instances = []

  instances.each do |instance|
    # Don't allocate resource pool VMs to instances in detached state
    next if instance.state == 'detached'

    # Skip bound instances
    next if instance.model.vm

    unbound_instances << instance
  end

  return if unbound_instances.empty?

  @event_log.begin_stage('Binding instance VMs', unbound_instances.size)

  ThreadPool.new(:max_threads => Config.max_threads).wrap do |pool|
    unbound_instances.each do |instance|
      pool.process { bind_instance_vm(instance) }
    end
  end
end