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.



325
326
327
# File 'lib/bosh/director/instance_updater.rb', line 325

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



166
167
168
169
# File 'lib/bosh/director/instance_updater.rb', line 166

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

#canary?Boolean

Returns:

  • (Boolean)


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

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.)



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/bosh/director/instance_updater.rb', line 242

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



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
214
215
216
217
218
219
220
221
# File 'lib/bosh/director/instance_updater.rb', line 189

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



162
163
164
# File 'lib/bosh/director/instance_updater.rb', line 162

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

#delete_unused_disk(disk) ⇒ Object



184
185
186
187
# File 'lib/bosh/director/instance_updater.rb', line 184

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



173
174
175
176
177
178
179
180
181
182
# File 'lib/bosh/director/instance_updater.rb', line 173

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)


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

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

#max_watch_timeObject



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

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

#min_watch_timeObject



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

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

#need_start?Boolean

Returns:

  • (Boolean)


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

def need_start?
  @target_state == 'started'
end

#recreate_vm(new_disk_cid) ⇒ Object



235
236
237
# File 'lib/bosh/director/instance_updater.rb', line 235

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

#run_pre_start_scriptsObject



121
122
123
# File 'lib/bosh/director/instance_updater.rb', line 121

def run_pre_start_scripts
  @agent.run_script("pre-start", {})
end

#start!Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/bosh/director/instance_updater.rb', line 125

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



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

def stop
  skip_drain = @deployment_plan.skip_drain_for_job?(@job.name)
  stopper = Stopper.new(@instance, agent, @target_state, skip_drain, Config, @logger)
  stopper.stop
end

#take_snapshotObject



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

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

#trusted_certs_change_only?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/bosh/director/instance_updater.rb', line 148

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

#update(options = {}) ⇒ Object



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

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



223
224
225
226
227
228
229
230
231
232
233
# File 'lib/bosh/director/instance_updater.rb', line 223

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



283
284
285
286
# File 'lib/bosh/director/instance_updater.rb', line 283

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

#update_persistent_diskObject



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/bosh/director/instance_updater.rb', line 260

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



288
289
290
291
292
293
# File 'lib/bosh/director/instance_updater.rb', line 288

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
78
79
80
# 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

  if need_start?
    steps << proc { run_pre_start_scripts }
    steps << proc { start! }
  end

  steps << proc { wait_until_running }

  steps
end

#vm_updaterObject



327
328
329
330
331
# File 'lib/bosh/director/instance_updater.rb', line 327

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



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

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



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

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