Class: Bosh::Director::InstanceUpdater

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

Defined Under Namespace

Classes: InstanceState, Preparer, StateApplier

Constant Summary collapse

MAX_RECREATE_ATTEMPTS =
3

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cloud, logger, ip_provider, blobstore, vm_deleter, vm_creator, dns_manager, disk_manager, vm_recreator) ⇒ InstanceUpdater

Returns a new instance of InstanceUpdater.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/bosh/director/instance_updater.rb', line 30

def initialize(cloud, logger, ip_provider, blobstore, vm_deleter, vm_creator, dns_manager, disk_manager, vm_recreator)
  @cloud = cloud
  @logger = logger
  @blobstore = blobstore
  @vm_deleter = vm_deleter
  @vm_creator = vm_creator
  @dns_manager = dns_manager
  @disk_manager = disk_manager
  @ip_provider = ip_provider
  @vm_recreator = vm_recreator
  @current_state = {}
end

Class Method Details

.new_instance_updater(ip_provider) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/bosh/director/instance_updater.rb', line 7

def self.new_instance_updater(ip_provider)
  logger = Config.logger
  cloud = Config.cloud
  vm_deleter = VmDeleter.new(cloud, logger, false, Config.enable_virtual_delete_vms)
  disk_manager = DiskManager.new(cloud, logger)
  job_renderer = JobRenderer.create
  arp_flusher = ArpFlusher.new
  vm_creator = VmCreator.new(cloud, logger, vm_deleter, disk_manager, job_renderer, arp_flusher)
  vm_recreator = VmRecreator.new(vm_creator, vm_deleter)
  dns_manager = DnsManagerProvider.create
  new(
    cloud,
    logger,
    ip_provider,
    App.instance.blobstores.blobstore,
    vm_deleter,
    vm_creator,
    dns_manager,
    disk_manager,
    vm_recreator
  )
end

Instance Method Details

#update(instance_plan, options = {}) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/bosh/director/instance_updater.rb', line 43

def update(instance_plan, options = {})
  instance = instance_plan.instance
  action, context = get_action_and_context(instance_plan)
  parent_id = add_event(instance.deployment_model.name, action, instance.model.name, context) if instance_plan.changed?
  @logger.info("Updating instance #{instance}, changes: #{instance_plan.changes.to_a.join(', ').inspect}")

  update_procedure = lambda do
    # Optimization to only update DNS if nothing else changed.
    if dns_change_only?(instance_plan)
      @logger.debug('Only change is DNS configuration')
      update_dns(instance_plan)
      return
    end

    unless instance_plan.already_detached?
      Preparer.new(instance_plan, agent(instance), @logger).prepare

      stop(instance_plan)
      take_snapshot(instance)

      if instance.state == 'stopped'
        instance.update_state
        return
      end
    end

    if instance.state == 'detached'
      @logger.info("Detaching instance #{instance}")
      unless instance_plan.already_detached?
        @disk_manager.unmount_disk_for(instance_plan)
        instance_model = instance_plan.new? ? instance_plan.instance.model : instance_plan.existing_instance
        @vm_deleter.delete_for_instance(instance_model)
      end
      release_obsolete_ips(instance_plan)
      instance.update_state
      return
    end

    recreated = false
    if needs_recreate?(instance_plan)
      @logger.debug('Failed to update in place. Recreating VM')
      @disk_manager.unmount_disk_for(instance_plan)
      @vm_recreator.recreate_vm(instance_plan, nil)
      recreated = true
    end

    release_obsolete_ips(instance_plan)

    update_dns(instance_plan)
    @disk_manager.update_persistent_disk(instance_plan)

    unless recreated
      if instance.trusted_certs_changed?
        @logger.debug('Updating trusted certs')
        instance.update_trusted_certs
      end
    end

    cleaner = RenderedJobTemplatesCleaner.new(instance.model, @blobstore, @logger)
    state_applier = InstanceUpdater::StateApplier.new(
      instance_plan,
      agent(instance),
      cleaner,
      @logger,
      canary: options[:canary]
    )
    state_applier.apply(instance_plan.desired_instance.job.update)
  end
  InstanceUpdater::InstanceState.with_instance_update_and_event_creation(instance.model, parent_id, instance.deployment_model.name, action, &update_procedure)
end