Class: Bosh::Director::InstanceUpdater
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
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
Returns the value of attribute agent.
314
315
316
|
# File 'lib/bosh/director/instance_updater.rb', line 314
def agent
@agent
end
|
#current_state ⇒ Object
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
155
156
157
158
|
# File 'lib/bosh/director/instance_updater.rb', line 155
def apply_state(state)
@vm.update(:apply_spec => state)
agent.apply(state)
end
|
#canary? ⇒ Boolean
310
311
312
|
# File 'lib/bosh/director/instance_updater.rb', line 310
def canary?
@canary
end
|
#check_persistent_disk ⇒ void
This method returns an undefined value.
Synchronizes persistent_disks with the agent. (Currently assumes that we only have 1 persistent disk.)
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/bosh/director/instance_updater.rb', line 231
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
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
205
206
207
208
209
210
|
# File 'lib/bosh/director/instance_updater.rb', line 178
def delete_mounted_disk(disk)
disk_cid = disk.disk_cid
vm_cid = @vm.cid
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
151
152
153
|
# File 'lib/bosh/director/instance_updater.rb', line 151
def delete_snapshots(disk)
Api::SnapshotManager.delete_snapshots(disk.snapshots)
end
|
#delete_unused_disk(disk) ⇒ Object
173
174
175
176
|
# File 'lib/bosh/director/instance_updater.rb', line 173
def delete_unused_disk(disk)
@cloud.delete_disk(disk.disk_cid)
disk.destroy
end
|
#disk_info ⇒ Array<String>
Retrieve list of mounted disks from the agent
162
163
164
165
166
167
168
169
170
171
|
# File 'lib/bosh/director/instance_updater.rb', line 162
def disk_info
return @disk_list if @disk_list
begin
@disk_list = agent.list_disk
rescue RuntimeError
[@instance.persistent_disk_cid]
end
end
|
#dns_change_only? ⇒ Boolean
133
134
135
|
# File 'lib/bosh/director/instance_updater.rb', line 133
def dns_change_only?
@instance.changes.include?(:dns) && @instance.changes.size == 1
end
|
#max_watch_time ⇒ Object
306
307
308
|
# File 'lib/bosh/director/instance_updater.rb', line 306
def max_watch_time
canary? ? @update_config.max_canary_watch_time : @update_config.max_update_watch_time
end
|
#min_watch_time ⇒ Object
302
303
304
|
# File 'lib/bosh/director/instance_updater.rb', line 302
def min_watch_time
canary? ? @update_config.min_canary_watch_time : @update_config.min_update_watch_time
end
|
#need_start? ⇒ Boolean
129
130
131
|
# File 'lib/bosh/director/instance_updater.rb', line 129
def need_start?
@target_state == 'started'
end
|
#recreate_vm(new_disk_cid) ⇒ Object
224
225
226
|
# File 'lib/bosh/director/instance_updater.rb', line 224
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_scripts ⇒ Object
121
122
123
|
# File 'lib/bosh/director/instance_updater.rb', line 121
def run_pre_start_scripts
@agent.run_script("pre-start", {})
end
|
125
126
127
|
# File 'lib/bosh/director/instance_updater.rb', line 125
def start!
agent.start
end
|
141
142
143
144
145
|
# File 'lib/bosh/director/instance_updater.rb', line 141
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_snapshot ⇒ Object
147
148
149
|
# File 'lib/bosh/director/instance_updater.rb', line 147
def take_snapshot
Api::SnapshotManager.take_snapshot(@instance.model, clean: true)
end
|
#trusted_certs_change_only? ⇒ Boolean
137
138
139
|
# File 'lib/bosh/director/instance_updater.rb', line 137
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_dns ⇒ Object
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/bosh/director/instance_updater.rb', line 212
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_networks ⇒ Object
272
273
274
275
|
# File 'lib/bosh/director/instance_updater.rb', line 272
def update_networks
network_updater = NetworkUpdater.new(@instance, @vm, agent, vm_updater, @cloud, @logger)
@vm, @agent = network_updater.update
end
|
#update_persistent_disk ⇒ Object
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# File 'lib/bosh/director/instance_updater.rb', line 249
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_settings ⇒ Object
277
278
279
280
281
282
|
# File 'lib/bosh/director/instance_updater.rb', line 277
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)
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, @logger).clean
}
end
if need_start?
steps << proc { run_pre_start_scripts }
steps << proc { start! }
end
steps << proc { wait_until_running }
steps
end
|
#vm_updater ⇒ Object
316
317
318
319
320
|
# File 'lib/bosh/director/instance_updater.rb', line 316
def vm_updater
VmUpdater.new(@instance, @vm, agent, @job_renderer, @cloud, 3, @logger)
end
|
#wait_until_running ⇒ Object
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.
295
296
297
298
299
300
|
# File 'lib/bosh/director/instance_updater.rb', line 295
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
|