Class: Bosh::Director::InstanceUpdater

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

Defined Under Namespace

Classes: NetworkUpdater, Preparer, Stopper, VmUpdater

Constant Summary collapse

WATCH_INTERVALS =
10

Constants included from DnsHelper

DnsHelper::SOA, DnsHelper::TTL_4H, DnsHelper::TTL_5M

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DnsHelper

#add_default_dns_server, #canonical, #default_dns_server, #delete_dns_records, #delete_empty_domain, #dns_domain_name, #dns_ns_record, #dns_servers, #flush_dns_cache, #invalid_dns, #reverse_domain, #reverse_host, #update_dns_a_record, #update_dns_ptr_record

Constructor Details

#initialize(instance, event_log_task, job_renderer) ⇒ InstanceUpdater

Returns a new instance of InstanceUpdater.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/bosh/director/instance_updater.rb', line 12

def initialize(instance, event_log_task, job_renderer)
  @instance = instance
  @event_log_task = event_log_task
  @job_renderer = job_renderer

  @cloud = Config.cloud
  @logger = Config.logger
  @blobstore = App.instance.blobstores.blobstore

  @job = instance.job
  @target_state = @instance.state

  @deployment_plan = @job.deployment
  @resource_pool = @job.resource_pool
  @update_config = @job.update

  @vm = @instance.model.vm

  @current_state = {}

  @agent = AgentClient.with_defaults(@vm.agent_id)
end

Instance Attribute Details

#agentObject (readonly)

Returns the value of attribute agent.



317
318
319
# File 'lib/bosh/director/instance_updater.rb', line 317

def agent
  @agent
end

#current_stateObject (readonly)

Returns the value of attribute current_state.



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

def current_state
  @current_state
end

Instance Method Details

#apply_state(state) ⇒ Object



158
159
160
161
# File 'lib/bosh/director/instance_updater.rb', line 158

def apply_state(state)
  @vm.update(:apply_spec => state)
  agent.apply(state)
end

#canary?Boolean

Returns:

  • (Boolean)


313
314
315
# File 'lib/bosh/director/instance_updater.rb', line 313

def canary?
  @canary
end

#check_persistent_diskvoid

This method returns an undefined value.

Synchronizes persistent_disks with the agent. (Currently assumes that we only have 1 persistent disk.)



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/bosh/director/instance_updater.rb', line 234

def check_persistent_disk
  return if @instance.model.persistent_disks.empty?
  agent_disk_cid = disk_info.first

  if agent_disk_cid != @instance.model.persistent_disk_cid
    raise AgentDiskOutOfSync,
          "`#{@instance}' has invalid disks: agent reports " +
              "`#{agent_disk_cid}' while director record shows " +
              "`#{@instance.model.persistent_disk_cid}'"
  end

  @instance.model.persistent_disks.each do |disk|
    unless disk.active
      @logger.warn("`#{@instance}' has inactive disk #{disk.disk_cid}")
    end
  end
end

#delete_mounted_disk(disk) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/bosh/director/instance_updater.rb', line 181

def delete_mounted_disk(disk)
  disk_cid = disk.disk_cid
  vm_cid = @vm.cid

  # Unmount the disk only if disk is known by the agent
  if agent && disk_info.include?(disk_cid)
    agent.unmount_disk(disk_cid)
  end

  begin
    @cloud.detach_disk(vm_cid, disk_cid) if vm_cid
  rescue Bosh::Clouds::DiskNotAttached
    if disk.active
      raise CloudDiskNotAttached,
            "`#{@instance}' VM should have persistent disk attached " +
                "but it doesn't (according to CPI)"
    end
  end

  delete_snapshots(disk)

  begin
    @cloud.delete_disk(disk_cid)
  rescue Bosh::Clouds::DiskNotFound
    if disk.active
      raise CloudDiskMissing,
            "Disk `#{disk_cid}' is missing according to CPI but marked " +
                "as active in DB"
    end
  end

  disk.destroy
end

#delete_snapshots(disk) ⇒ Object



154
155
156
# File 'lib/bosh/director/instance_updater.rb', line 154

def delete_snapshots(disk)
  Api::SnapshotManager.delete_snapshots(disk.snapshots)
end

#delete_unused_disk(disk) ⇒ Object



176
177
178
179
# File 'lib/bosh/director/instance_updater.rb', line 176

def delete_unused_disk(disk)
  @cloud.delete_disk(disk.disk_cid)
  disk.destroy
end

#disk_infoArray<String>

Retrieve list of mounted disks from the agent

Returns:

  • (Array<String>)

    list of disk CIDs



165
166
167
168
169
170
171
172
173
174
# File 'lib/bosh/director/instance_updater.rb', line 165

def disk_info
  return @disk_list if @disk_list

  begin
    @disk_list = agent.list_disk
  rescue RuntimeError
    # old agents don't support list_disk rpc
    [@instance.persistent_disk_cid]
  end
end

#dns_change_only?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/bosh/director/instance_updater.rb', line 137

def dns_change_only?
  @instance.changes.include?(:dns) && @instance.changes.size == 1
end

#max_watch_timeObject



309
310
311
# File 'lib/bosh/director/instance_updater.rb', line 309

def max_watch_time
  canary? ? @update_config.max_canary_watch_time : @update_config.max_update_watch_time
end

#min_watch_timeObject



305
306
307
# File 'lib/bosh/director/instance_updater.rb', line 305

def min_watch_time
  canary? ? @update_config.min_canary_watch_time : @update_config.min_update_watch_time
end

#need_start?Boolean

Returns:

  • (Boolean)


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

def need_start?
  @target_state == 'started'
end

#recreate_vm(new_disk_cid) ⇒ Object



227
228
229
# File 'lib/bosh/director/instance_updater.rb', line 227

def recreate_vm(new_disk_cid)
  @vm, @agent = vm_updater.update(new_disk_cid)
end

#report_progress(num_steps) ⇒ Object



35
36
37
# File 'lib/bosh/director/instance_updater.rb', line 35

def report_progress(num_steps)
  @event_log_task.advance(100.0 / num_steps)
end

#start!Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bosh/director/instance_updater.rb', line 118

def start!
  agent.start
rescue RuntimeError => e
  # FIXME: this is somewhat ghetto: we don't have a good way to
  # negotiate on BOSH protocol between director and agent (yet),
  # so updating from agent version that doesn't support 'start' RPC
  # to the one that does might be hard. Right now we decided to
  # just swallow the exception.
  # This needs to be removed in one of the following cases:
  # 1. BOSH protocol handshake gets implemented
  # 2. All agents updated to support 'start' RPC
  #    and we no longer care about backward compatibility.
  @logger.warn("Agent start raised an exception: #{e.inspect}, ignoring for compatibility")
end

#stopObject



145
146
147
148
# File 'lib/bosh/director/instance_updater.rb', line 145

def stop
  stopper = Stopper.new(@instance, agent, @target_state, Config, @logger)
  stopper.stop
end

#take_snapshotObject



150
151
152
# File 'lib/bosh/director/instance_updater.rb', line 150

def take_snapshot
  Api::SnapshotManager.take_snapshot(@instance.model, clean: true)
end

#trusted_certs_change_only?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/bosh/director/instance_updater.rb', line 141

def trusted_certs_change_only?
  @instance.changes.include?(:trusted_certs) && @instance.changes.size == 1
end

#update(options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/bosh/director/instance_updater.rb', line 79

def update(options = {})
  steps = update_steps(options)

  @logger.info("Updating instance #{@instance}, changes: #{@instance.changes.to_a.join(', ')}")

  steps.each do |step|
    step.call
    report_progress(steps.length)
  end

  if @target_state == "started" && current_state["job_state"] != "running"
    raise AgentJobNotRunning, "`#{@instance}' is not running after update"
  end

  if @target_state == "stopped" && current_state["job_state"] == "running"
    raise AgentJobNotStopped, "`#{@instance}' is still running despite the stop command"
  end
end

#update_dnsObject



215
216
217
218
219
220
221
222
223
224
225
# File 'lib/bosh/director/instance_updater.rb', line 215

def update_dns
  return unless @instance.dns_changed?

  domain = @deployment_plan.dns_domain
  @instance.dns_record_info.each do |record_name, ip_address|
    @logger.info("Updating DNS for: #{record_name} to #{ip_address}")
    update_dns_a_record(domain, record_name, ip_address)
    update_dns_ptr_record(record_name, ip_address)
  end
  flush_dns_cache
end

#update_networksObject



275
276
277
278
# File 'lib/bosh/director/instance_updater.rb', line 275

def update_networks
  network_updater = NetworkUpdater.new(@instance, @vm, agent, vm_updater, @cloud, @logger)
  @vm, @agent = network_updater.update
end

#update_persistent_diskObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/bosh/director/instance_updater.rb', line 252

def update_persistent_disk
  vm_updater.attach_missing_disk
  check_persistent_disk

  disk = nil
  return unless @instance.persistent_disk_changed?

  old_disk = @instance.model.persistent_disk

  if @job.persistent_disk_pool && @job.persistent_disk_pool.disk_size > 0
    disk = create_disk
    attach_disk(disk)
    mount_and_migrate_disk(disk, old_disk)
  end

  @instance.model.db.transaction do
    old_disk.update(:active => false) if old_disk
    disk.update(:active => true) if disk
  end

  delete_mounted_disk(old_disk) if old_disk
end

#update_settingsObject



280
281
282
283
284
285
# File 'lib/bosh/director/instance_updater.rb', line 280

def update_settings
  if @instance.trusted_certs_changed?
    @agent.update_settings(Config.trusted_certs)
    @vm.update(:trusted_certs_sha1 => Digest::SHA1.hexdigest(Config.trusted_certs))
  end
end

#update_steps(options = {}) ⇒ Object



39
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
69
70
71
72
73
74
75
76
77
# File 'lib/bosh/director/instance_updater.rb', line 39

def update_steps(options = {})
  steps = []
  @canary = options.fetch(:canary, false)

  # Optimization to only update DNS if nothing else changed.
  if dns_change_only?
    steps << proc { update_dns }
    return steps
  end

  steps << proc { Preparer.new(@instance, agent, @logger).prepare }
  steps << proc { stop }
  steps << proc { take_snapshot }

  if @target_state == "detached"
    steps << proc { vm_updater.detach }
    return steps
  end

  steps << proc { recreate_vm(nil) }
  steps << proc { update_networks }
  steps << proc { update_dns }
  steps << proc { update_persistent_disk }
  steps << proc { update_settings }

  if !trusted_certs_change_only?
    steps << proc {
      VmMetadataUpdater.build.update(@vm, {})
      apply_state(@instance.spec)
      RenderedJobTemplatesCleaner.new(@instance.model, @blobstore).clean
    }
  end

  steps << proc { start! if need_start? }

  steps << proc { wait_until_running }

  steps
end

#vm_updaterObject



319
320
321
322
323
# File 'lib/bosh/director/instance_updater.rb', line 319

def vm_updater
  # Do not memoize to avoid caching same VM and agent
  # which could be replaced after updating a VM
  VmUpdater.new(@instance, @vm, agent, @job_renderer, @cloud, 3, @logger)
end

#wait_until_runningObject

Watch times don’t include the get_state roundtrip time, so effective max watch time is roughly: max_watch_time + N_WATCH_INTERVALS * avg_roundtrip_time



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/bosh/director/instance_updater.rb', line 101

def wait_until_running
  watch_schedule(min_watch_time, max_watch_time).each do |watch_time|
    sleep_time = watch_time.to_f / 1000
    @logger.info("Waiting for #{sleep_time} seconds to check #{@instance} status")
    sleep(sleep_time)
    @logger.info("Checking if #{@instance} has been updated after #{sleep_time} seconds")

    @current_state = agent.get_state

    if @target_state == "started"
      break if current_state["job_state"] == "running"
    elsif @target_state == "stopped"
      break if current_state["job_state"] != "running"
    end
  end
end

#watch_schedule(min_watch_time, max_watch_time, intervals = WATCH_INTERVALS) ⇒ Array<Numeric>

Returns an array of wait times distributed on the [min_watch_time..max_watch_time] interval.

Tries to respect intervals but doesn’t allow an interval to fall under 1 second. All times are in milliseconds.

Parameters:

  • min_watch_time (Numeric)

    minimum time to watch the jobs

  • max_watch_time (Numeric)

    maximum time to watch the jobs

  • intervals (Numeric) (defaults to: WATCH_INTERVALS)

    number of intervals between polling the state of the jobs

Returns:

  • (Array<Numeric>)

    watch schedule



298
299
300
301
302
303
# File 'lib/bosh/director/instance_updater.rb', line 298

def watch_schedule(min_watch_time, max_watch_time, intervals = WATCH_INTERVALS)
  delta = (max_watch_time - min_watch_time).to_f
  step = [1000, delta / (intervals - 1)].max

  [min_watch_time] + ([step] * (delta / step).floor)
end