Class: Bosh::Director::InstanceUpdater::VmUpdater

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

Defined Under Namespace

Classes: DiskAttacher, DiskDetacher, VmCreator, VmDeleter, VmStateApplier

Instance Method Summary collapse

Constructor Details

#initialize(instance, vm_model, agent_client, job_renderer, cloud, max_update_tries, logger) ⇒ VmUpdater

Returns a new instance of VmUpdater.



3
4
5
6
7
8
9
10
11
# File 'lib/bosh/director/instance_updater/vm_updater.rb', line 3

def initialize(instance, vm_model, agent_client, job_renderer, cloud, max_update_tries, logger)
  @instance = instance
  @vm_model = vm_model
  @agent_client = agent_client
  @job_renderer = job_renderer
  @cloud = cloud
  @max_update_tries = max_update_tries
  @logger = logger
end

Instance Method Details

#attach_missing_diskObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bosh/director/instance_updater/vm_updater.rb', line 63

def attach_missing_disk
  if !@instance.model.persistent_disk_cid || @instance.disk_currently_attached?
    @logger.info('Skipping attaching missing VM')
    return
  end

  begin
    disk_attacher = DiskAttacher.new(@instance, @vm_model, @agent_client, @cloud, @logger)
    disk_attacher.attach
  rescue Bosh::Clouds::NoDiskSpace => e
    @logger.warn("Failed attaching missing disk first time: #{e.inspect}")
    update(@instance.model.persistent_disk_cid)
  end
end

#detachObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bosh/director/instance_updater/vm_updater.rb', line 51

def detach
  @logger.info('Detaching VM')

  disk_detacher = DiskDetacher.new(@instance, @vm_model, @agent_client, @cloud, @logger)
  disk_detacher.detach

  vm_deleter = VmDeleter.new(@instance, @vm_model, @cloud, @logger)
  vm_deleter.delete

  @instance.job.resource_pool.add_idle_vm
end

#update(new_disk_cid) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/bosh/director/instance_updater/vm_updater.rb', line 13

def update(new_disk_cid)
  unless @instance.resource_pool_changed? || new_disk_cid
    @logger.info('Skipping VM update')
    return [@vm_model, @agent_client]
  end

  disk_detacher = DiskDetacher.new(@instance, @vm_model, @agent_client, @cloud, @logger)
  disk_detacher.detach

  @max_update_tries.times do |try|
    vm_deleter = VmDeleter.new(@instance, @vm_model, @cloud, @logger)
    vm_deleter.delete

    vm_creator = VmCreator.new(@instance, @cloud, @logger)
    @vm_model, @agent_client = vm_creator.create(new_disk_cid)

    begin
      # Could raise Bosh::Clouds::NoDiskSpace because some CPIs might lazily create disks
      disk_attacher = DiskAttacher.new(@instance, @vm_model, @agent_client, @cloud, @logger)
      disk_attacher.attach
      break
    rescue Bosh::Clouds::NoDiskSpace => e
      if e.ok_to_retry && try < @max_update_tries-1
        @logger.warn("Retrying attach disk operation #{try}: #{e.inspect}")
      else
        @logger.warn("Failed to attach disk to new VM: #{e.inspect}")
        raise CloudNotEnoughDiskSpace,
              "Not enough disk space to update `#{@instance}'"
      end
    end
  end

  vm_state_applier = VmStateApplier.new(@instance, @vm_model, @agent_client, @job_renderer, @logger)
  vm_state_applier.apply

  [@vm_model, @agent_client]
end