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

UPDATE_STEPS =
7
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.



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

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.



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

def agent
  @agent
end

#current_stateObject (readonly)

Returns the value of attribute current_state.



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

def current_state
  @current_state
end

Instance Method Details

#apply_state(state) ⇒ Object



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

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

#canary?Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/bosh/director/instance_updater.rb', line 297

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



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/bosh/director/instance_updater.rb', line 225

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



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/bosh/director/instance_updater.rb', line 172

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



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

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

#delete_unused_disk(disk) ⇒ Object



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

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



156
157
158
159
160
161
162
163
164
165
# File 'lib/bosh/director/instance_updater.rb', line 156

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)


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

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

#max_watch_timeObject



293
294
295
# File 'lib/bosh/director/instance_updater.rb', line 293

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

#min_watch_timeObject



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

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

#need_start?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/bosh/director/instance_updater.rb', line 128

def need_start?
  @target_state == 'started'
end

#recreate_vm(new_disk_cid) ⇒ Object



218
219
220
# File 'lib/bosh/director/instance_updater.rb', line 218

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

#report_progressObject



41
42
43
# File 'lib/bosh/director/instance_updater.rb', line 41

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

#start!Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/bosh/director/instance_updater.rb', line 113

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

#stepObject



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

def step
  yield
  report_progress
end

#stopObject



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

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

#take_snapshotObject



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

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

#update(options = {}) ⇒ Object



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
# File 'lib/bosh/director/instance_updater.rb', line 49

def update(options = {})
  @canary = options.fetch(:canary, false)

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

  # Optimization to only update DNS if nothing else changed.
  if dns_change_only?
    update_dns
    return
  end

  step { Preparer.new(@instance, agent, @logger).prepare }
  step { stop }
  step { take_snapshot }

  if @target_state == "detached"
    vm_updater.detach
    return
  end

  step { recreate_vm(nil) }
  step { update_networks }
  step { update_dns }
  step { update_persistent_disk }

  VmMetadataUpdater.build.update(@vm, {})

  step { apply_state(@instance.spec) }

  RenderedJobTemplatesCleaner.new(@instance.model, @blobstore).clean

  start! if need_start?

  step { wait_until_running }

  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



206
207
208
209
210
211
212
213
214
215
216
# File 'lib/bosh/director/instance_updater.rb', line 206

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



266
267
268
269
# File 'lib/bosh/director/instance_updater.rb', line 266

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

#update_persistent_diskObject



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

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_stepsObject



45
46
47
# File 'lib/bosh/director/instance_updater.rb', line 45

def update_steps
  @instance.job_changed? || @instance.packages_changed? ? UPDATE_STEPS + 1 : UPDATE_STEPS
end

#vm_updaterObject



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

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



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bosh/director/instance_updater.rb', line 96

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



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

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