Skip to content

Commit 701a922

Browse files
committed
Refactor instance state checks to use new methods
1 parent 749f0d4 commit 701a922

7 files changed

Lines changed: 30 additions & 25 deletions

File tree

src/bosh-director/lib/bosh/director/deployment_plan/instance.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,18 @@ def virtual_state
314314
@virtual_state.to_s.inquiry
315315
end
316316

317+
def started?
318+
state == STATE_STARTED
319+
end
320+
321+
def stopped?
322+
state == STATE_STOPPED
323+
end
324+
325+
def detached?
326+
state == STATE_DETACHED
327+
end
328+
317329
##
318330
# Checks if the target VM already has the same set of trusted SSL certificates
319331
# as the director currently wants to install on all managed VMs. This will

src/bosh-director/lib/bosh/director/deployment_plan/instance_group.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,6 @@ class InstanceGroup
5656
# @return [DeploymentPlan::UpdateConfig] Instance group update settings
5757
attr_reader :update
5858

59-
# @return [Array<DeploymentPlan::Instance>] All instances
60-
attr_accessor :instances
61-
62-
# @return [Array<Models::Instance>] List of excess instance models that
63-
# are not needed for current deployment
64-
attr_accessor :unneeded_instances
65-
6659
# @return [String] Expected instance group state
6760
attr_reader :state
6861

@@ -159,7 +152,7 @@ def unignored_instance_plans_needing_duplicate_vm
159152
.select(&:needs_duplicate_vm?)
160153
.reject(&:new?)
161154
.reject(&:should_be_ignored?)
162-
.reject { |plan| plan.instance.state == 'detached' }
155+
.reject { |plan| plan.instance.detached? }
163156
end
164157

165158
def add_job(job_to_add)
@@ -370,7 +363,7 @@ def errand?
370363

371364
def instance_plans_with_missing_vms
372365
needed_instance_plans.reject do |instance_plan|
373-
instance_plan.instance.vm_created? || instance_plan.instance.state == 'detached'
366+
instance_plan.instance.vm_created? || instance_plan.instance.detached?
374367
end
375368
end
376369

src/bosh-director/lib/bosh/director/deployment_plan/instance_plan.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,15 @@ def network_settings_changed?
186186
end
187187

188188
def state_changed?
189-
if instance.state == Instance::STATE_DETACHED && existing_instance.state != instance.state
189+
if instance.detached? && existing_instance.state != instance.state
190190
@logger.debug("Instance '#{instance}' needs to be detached")
191191
return true
192192
end
193193

194194
return true if unresponsive_agent?
195195

196-
if (instance.state == Instance::STATE_STOPPED && instance.current_job_state.running?) ||
197-
(instance.state == Instance::STATE_STARTED && !instance.current_job_state.running?)
196+
if (instance.stopped? && instance.current_job_state.running?) ||
197+
(instance.started? && !instance.current_job_state.running?)
198198
@logger.debug("Instance state is '#{instance.state}' and agent reports '#{instance.current_job_state}'")
199199
return true
200200
end

src/bosh-director/lib/bosh/director/deployment_plan/planner.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ def instance_groups_starting_on_deploy
260260
if instance_group.service?
261261
instance_groups << instance_group
262262
elsif instance_group.errand?
263-
if instance_group.instances.any?(&:vm_created?) || instance_group.instances.any? { |i| i.model.state == 'detached' }
263+
if instance_group.instances.any?(&:vm_created?) || instance_group.instances.any? { |i| i.model.detached? }
264264
instance_groups << instance_group
265265
end
266266
end

src/bosh-director/lib/bosh/director/instance_updater/state_applier.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def apply(update_config, wait_for_running = true)
1717
@instance.update_templates(@instance_plan.templates)
1818
@rendered_job_templates_cleaner.clean
1919

20-
start(update_config, wait_for_running) if @instance.state == 'started'
20+
start(update_config, wait_for_running) if @instance.started?
2121
end
2222

2323
private

src/bosh-director/lib/bosh/director/instance_updater/update_procedure.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def perform
7878
handle_not_detached_instance_plan
7979

8080
# desired state
81-
if instance.state == 'stopped'
81+
if instance.stopped?
8282
# Command issued: `bosh stop`
8383
update_instance
8484
return
@@ -87,12 +87,12 @@ def perform
8787
handle_detached_instance_if_detached
8888
end
8989

90-
converge_vm if instance.state != 'detached'
90+
converge_vm unless instance.detached?
9191
update_instance
9292
update_dns_if_changed
9393
update_vm_disk_metadata
9494

95-
return if instance.state == 'detached'
95+
return if instance.detached?
9696

9797
@rendered_templates_persister.persist(instance_plan)
9898
apply_state
@@ -119,19 +119,19 @@ def handle_not_detached_instance_plan
119119
@rendered_templates_persister.persist(instance_plan)
120120
end
121121

122-
unless instance_plan.needs_shutting_down? || instance.state == 'detached'
122+
unless instance_plan.needs_shutting_down? || instance.detached?
123123
DeploymentPlan::Steps::PrepareInstanceStep.new(instance_plan).perform(instance_report)
124124
end
125125

126126
# current state
127-
return unless instance.model.state != 'stopped'
127+
return if instance.model.stopped?
128128

129129
stop
130130
take_snapshot
131131
end
132132

133133
def handle_detached_instance_if_detached
134-
return unless instance.state == 'detached'
134+
return unless instance.detached?
135135

136136
# Command issued: `bosh stop --hard`
137137
@logger.info("Detaching instance #{instance}")
@@ -149,7 +149,7 @@ def update_instance
149149
def update_vm_disk_metadata
150150
return unless instance_plan.changes.include?(:tags)
151151
return if instance_plan.new? || @needs_recreate
152-
return if instance.state == 'detached' # disks will get a metadata update when attaching again
152+
return if instance.detached? # disks will get a metadata update when attaching again
153153

154154
@logger.debug("Updating instance #{instance} VM and disk metadata with tags")
155155
tags = instance_plan.tags
@@ -188,7 +188,7 @@ def stop
188188
end
189189

190190
def deleting_vm?
191-
@needs_recreate || instance_plan.needs_shutting_down? || instance.state == 'detached' ||
191+
@needs_recreate || instance_plan.needs_shutting_down? || instance.detached? ||
192192
(instance_plan.should_create_swap_delete? && instance_plan.instance.model.vms.count > 1)
193193
end
194194

src/bosh-director/lib/bosh/director/jobs/attach_disk.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def validate_instance(instance)
5353
'Attaching disks to ignored instances is not allowed.'
5454
end
5555

56-
if instance.state != 'detached' && instance.state != 'stopped'
56+
unless instance.detached? || instance.stopped?
5757
raise AttachDiskInvalidInstanceState, "Instance '#{@job_name}/#{@instance_id}' in deployment '#{@deployment_name}' must be in 'bosh stopped' state"
5858
end
5959
end
@@ -66,7 +66,7 @@ def handle_previous_disk(instance)
6666
@cloud_properties = previous_persistent_disk.cloud_properties
6767
end
6868

69-
if instance.state == 'stopped'
69+
if instance.stopped?
7070
@disk_manager.detach_disk(previous_persistent_disk)
7171
end
7272

@@ -88,7 +88,7 @@ def handle_new_disk(instance)
8888
)
8989
end
9090

91-
if instance.state == 'stopped'
91+
if instance.stopped?
9292
@disk_manager.attach_disk(disk, instance.deployment.tags)
9393
end
9494
end

0 commit comments

Comments
 (0)