Class: Bosh::Director::ResourcePoolUpdater

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

Instance Method Summary collapse

Constructor Details

#initialize(resource_pool) ⇒ ResourcePoolUpdater

Returns a new instance of ResourcePoolUpdater.



3
4
5
6
7
8
# File 'lib/bosh/director/resource_pool_updater.rb', line 3

def initialize(resource_pool)
  @resource_pool = resource_pool
  @cloud = Config.cloud
  @logger = Config.logger
  @event_log = Config.event_log
end

Instance Method Details

#bound_missing_vm_countObject



163
164
165
166
167
168
169
170
171
# File 'lib/bosh/director/resource_pool_updater.rb', line 163

def bound_missing_vm_count
  counter = 0
  @resource_pool.vms.each do |vm|
    next if vm.model
    next if vm.bound_instance.nil?
    counter += 1
  end
  counter
end

#create_bound_missing_vms(thread_pool) ⇒ Object

Creates missing VMs that have bound instances (as opposed to missing resource pool VMs)



43
44
45
# File 'lib/bosh/director/resource_pool_updater.rb', line 43

def create_bound_missing_vms(thread_pool)
  create_missing_vms(thread_pool) { |vm| !vm.bound_instance.nil? }
end

#create_missing_vm(vm) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bosh/director/resource_pool_updater.rb', line 47

def create_missing_vm(vm)
  deployment = @resource_pool.deployment_plan.model
  stemcell = @resource_pool.stemcell.model

  vm_model = VmCreator.new.create(deployment, stemcell, @resource_pool.cloud_properties,
                            vm.network_settings, nil, @resource_pool.env)

  agent = AgentClient.with_defaults(vm_model.agent_id)
  agent.wait_until_ready

  update_state(agent, vm_model, vm)

  vm.model = vm_model
  vm.current_state = agent.get_state
rescue Exception => e
  @logger.info("Cleaning up the created VM due to an error: #{e}")
  begin
    @cloud.delete_vm(vm_model.cid) if vm_model && vm_model.cid
    vm_model.destroy if vm_model && vm_model.id
  rescue Exception
    @logger.info("Could not cleanup VM: #{vm_model.cid}") if vm_model
  end
  raise e
end

#create_missing_vms(thread_pool) {|VirtualMachine| ... } ⇒ Object

Creates VMs that are considered missing from the deployment

Parameters:

  • thread_pool (ThreadPool)

    Thread pool that will be used to parallelize the operation

Yields:

  • (VirtualMachine)

    filter for which missing VMs to create



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/bosh/director/resource_pool_updater.rb', line 16

def create_missing_vms(thread_pool)
  counter = 0
  vms_to_process = []

  @resource_pool.vms.each do |vm|
    next if vm.model
    if !block_given? || yield(vm)
      counter += 1
      vms_to_process << vm
    end
  end

  @logger.info("Creating #{counter} missing VMs")
  vms_to_process.each_with_index do |vm, index|
    thread_pool.process do
      @event_log.track("#{@resource_pool.name}/#{index}") do
        with_thread_name("create_missing_vm(#{@resource_pool.name}, #{index}/#{counter})") do
          @logger.info("Creating missing VM")
          create_missing_vm(vm)
        end
      end
    end
  end
end

#delete_extra_vms(thread_pool) ⇒ Object

Deletes extra VMs in a resource pool

Parameters:

  • thread_pool

    Thread pool used to parallelize delete operations



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/bosh/director/resource_pool_updater.rb', line 85

def delete_extra_vms(thread_pool)
  count = @resource_pool.extra_vm_count
  @logger.info("Deleting #{count} extra VMs")

  count.times do
    vm = @resource_pool.idle_vms.shift
    vm_cid = vm.model.cid

    thread_pool.process do
      @event_log.track("#{@resource_pool.name}/#{vm_cid}") do
        @logger.info("Deleting extra VM: #{vm_cid}")
        @cloud.delete_vm(vm_cid)
        vm.model.destroy
      end
    end
  end
end

#delete_outdated_idle_vms(thread_pool) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/bosh/director/resource_pool_updater.rb', line 103

def delete_outdated_idle_vms(thread_pool)
  count = outdated_idle_vm_count
  index = 0
  index_lock = Mutex.new

  @logger.info("Deleting #{count} outdated idle VMs")

  @resource_pool.idle_vms.each do |vm|
    next unless vm.model && vm.changed?
    vm_cid = vm.model.cid

    thread_pool.process do
      @event_log.track("#{@resource_pool.name}/#{vm_cid}") do
        index_lock.synchronize { index += 1 }

        with_thread_name("delete_outdated_vm(#{@resource_pool.name}, #{index - 1}/#{count})") do
          @logger.info("Deleting outdated VM: #{vm_cid}")
          @cloud.delete_vm(vm_cid)
          vm_model = vm.model
          vm.clean_vm
          vm_model.destroy
        end
      end
    end
  end
end

#extra_vm_countObject



142
143
144
# File 'lib/bosh/director/resource_pool_updater.rb', line 142

def extra_vm_count
  @resource_pool.extra_vm_count
end

#generate_agent_idObject



138
139
140
# File 'lib/bosh/director/resource_pool_updater.rb', line 138

def generate_agent_id
  SecureRandom.uuid
end

#missing_vm_countObject



154
155
156
157
158
159
160
161
# File 'lib/bosh/director/resource_pool_updater.rb', line 154

def missing_vm_count
  counter = 0
  @resource_pool.vms.each do |vm|
    next if vm.model
    counter += 1
  end
  counter
end

#outdated_idle_vm_countObject



146
147
148
149
150
151
152
# File 'lib/bosh/director/resource_pool_updater.rb', line 146

def outdated_idle_vm_count
  counter = 0
  @resource_pool.idle_vms.each do |vm|
    counter += 1 if vm.model && vm.changed?
  end
  counter
end

#reserve_networksObject

Attempts to allocate a dynamic IP address for all idle VMs (unless they already have one). This allows us to fail earlier in case any of resource pools is not big enough to accommodate those VMs.



134
135
136
# File 'lib/bosh/director/resource_pool_updater.rb', line 134

def reserve_networks
  @resource_pool.reserve_dynamic_networks
end

#update_state(agent, vm_model, vm) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/bosh/director/resource_pool_updater.rb', line 72

def update_state(agent, vm_model, vm)
  state = {
      "deployment" => @resource_pool.deployment_plan.name,
      "resource_pool" => @resource_pool.spec,
      "networks" => vm.network_settings
  }

  vm_model.update(:apply_spec => state)
  agent.apply(state)
end