Skip to content

Commit 88e21e7

Browse files
neddparamprice
authored andcommitted
Refactor instance state handling to use constants
- Remove unnecessary check in already_detached? method - Fix instance state checks to use constants instead of methods - Hoist constants out to Bosh::Director - resurrect attr_accessor's in instance_group.rb - replace magic array's of strings with arrays of constants
1 parent 1d2a233 commit 88e21e7

18 files changed

Lines changed: 87 additions & 52 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
module Bosh
22
module Director
3+
INSTANCE_STATE_STARTED = 'started'.freeze
4+
INSTANCE_STATE_STOPPED = 'stopped'.freeze
5+
INSTANCE_STATE_DETACHED = 'detached'.freeze
6+
7+
INSTANCE_VIRTUAL_STATE_RESTART = 'restart'.freeze
8+
INSTANCE_VIRTUAL_STATE_RECREATE = 'recreate'.freeze
39
end
410
end
511

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def create_instance_plan(stemcell)
168168
instance = Instance.create_from_instance_group(
169169
compile_instance_group,
170170
0,
171-
'started',
171+
Bosh::Director::INSTANCE_STATE_STARTED,
172172
@deployment_plan.model,
173173
{},
174174
availability_zone,

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ module Bosh::Director
55
module DeploymentPlan
66
# Represents a single Instance Group instance.
77
class Instance
8-
RECREATE = 'recreate'.freeze
9-
RESTART = 'restart'.freeze
10-
STARTED = 'started'.freeze
11-
128
VIRTUAL_STATE_TO_STATE_MAPPING = {
13-
RECREATE => STARTED,
14-
RESTART => STARTED
9+
Bosh::Director::INSTANCE_VIRTUAL_STATE_RESTART => Bosh::Director::INSTANCE_STATE_STARTED,
10+
Bosh::Director::INSTANCE_VIRTUAL_STATE_RECREATE => Bosh::Director::INSTANCE_STATE_STARTED
1511
}
1612

1713
# @return [Integer] Instance index
@@ -311,6 +307,18 @@ def virtual_state
311307
@virtual_state.to_s.inquiry
312308
end
313309

310+
def started?
311+
state == Bosh::Director::INSTANCE_STATE_STARTED
312+
end
313+
314+
def stopped?
315+
state == Bosh::Director::INSTANCE_STATE_STOPPED
316+
end
317+
318+
def detached?
319+
state == Bosh::Director::INSTANCE_STATE_DETACHED
320+
end
321+
314322
##
315323
# Checks if the target VM already has the same set of trusted SSL certificates
316324
# as the director currently wants to install on all managed VMs. This will
@@ -385,7 +393,7 @@ def find_or_create_model
385393
}
386394

387395
Models::Instance.find_or_create(conditions) do |model|
388-
model.state = 'started'
396+
model.state = Bosh::Director::INSTANCE_STATE_STARTED
389397
model.compilation = @compilation
390398
model.uuid = SecureRandom.uuid
391399
model.variable_set_id = @deployment_model.current_variable_set.id

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,13 @@ class InstanceGroup
1414
# recreate and restart are two virtual states
1515
# (both set target instance state to "started" and set
1616
# appropriate instance spec modifiers)
17-
VALID_STATES = %w[started stopped detached recreate restart].freeze
17+
VALID_STATES = [
18+
Bosh::Director::INSTANCE_STATE_STARTED,
19+
Bosh::Director::INSTANCE_STATE_STOPPED,
20+
Bosh::Director::INSTANCE_STATE_DETACHED,
21+
Bosh::Director::INSTANCE_VIRTUAL_STATE_RESTART,
22+
Bosh::Director::INSTANCE_VIRTUAL_STATE_RECREATE,
23+
].freeze
1824

1925
# @return [String] Instance group name
2026
attr_reader :name
@@ -159,7 +165,7 @@ def unignored_instance_plans_needing_duplicate_vm
159165
.select(&:needs_duplicate_vm?)
160166
.reject(&:new?)
161167
.reject(&:should_be_ignored?)
162-
.reject { |plan| plan.instance.state == 'detached' }
168+
.reject { |plan| plan.instance.detached? }
163169
end
164170

165171
def add_job(job_to_add)
@@ -370,7 +376,7 @@ def errand?
370376

371377
def instance_plans_with_missing_vms
372378
needed_instance_plans.reject do |instance_plan|
373-
instance_plan.instance.vm_created? || instance_plan.instance.state == 'detached'
379+
instance_plan.instance.vm_created? || instance_plan.instance.detached?
374380
end
375381
end
376382

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.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.stopped? && instance.current_job_state.running? ||
197-
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/instance_repository.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def create(desired_instance, index)
120120
instance = Instance.create_from_instance_group(
121121
desired_instance.instance_group,
122122
index,
123-
'started',
123+
Bosh::Director::INSTANCE_STATE_STARTED,
124124
desired_instance.deployment.model,
125125
nil,
126126
desired_instance.az,

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_deleter.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def add_event(deployment_name, instance_name, parent_id = nil, error = nil)
7878
end
7979

8080
def stop(instance_plan)
81-
Stopper.stop(intent: @stop_intent, instance_plan: instance_plan, target_state: 'stopped', logger: @logger)
81+
Stopper.stop(intent: @stop_intent, instance_plan: instance_plan, target_state: Bosh::Director::INSTANCE_STATE_STOPPED, logger: @logger)
8282
end
8383

8484
# FIXME: why do we hate dependency injection?

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: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
module Bosh::Director
22
class InstanceUpdater
33
class UpdateProcedure
4-
STARTED = 'started'.freeze
5-
STOPPED = 'stopped'.freeze
6-
DETACHED = 'detached'.freeze
7-
84
RECREATE = 'recreate'.freeze
95
CREATE = 'create'.freeze
106
START = 'start'.freeze
117
STOP = 'stop'.freeze
128
UPDATE = 'update'.freeze
139

1410
VIRTUAL_STATE_TO_ACTION_MAPPING = {
15-
STARTED => START,
16-
STOPPED => STOP,
17-
DETACHED => STOP,
11+
Bosh::Director::INSTANCE_STATE_STARTED => START,
12+
Bosh::Director::INSTANCE_STATE_STOPPED => STOP,
13+
Bosh::Director::INSTANCE_STATE_DETACHED => STOP,
1814
}
1915

2016
attr_reader :instance, :instance_plan, :options, :instance_report, :action, :context
@@ -78,7 +74,7 @@ def perform
7874
handle_not_detached_instance_plan
7975

8076
# desired state
81-
if instance.state == 'stopped'
77+
if instance.stopped?
8278
# Command issued: `bosh stop`
8379
update_instance
8480
return
@@ -87,12 +83,12 @@ def perform
8783
handle_detached_instance_if_detached
8884
end
8985

90-
converge_vm if instance.state != 'detached'
86+
converge_vm unless instance.detached?
9187
update_instance
9288
update_dns_if_changed
9389
update_vm_disk_metadata
9490

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

9793
@rendered_templates_persister.persist(instance_plan)
9894
apply_state
@@ -119,19 +115,19 @@ def handle_not_detached_instance_plan
119115
@rendered_templates_persister.persist(instance_plan)
120116
end
121117

122-
unless instance_plan.needs_shutting_down? || instance.state == 'detached'
118+
unless instance_plan.needs_shutting_down? || instance.detached?
123119
DeploymentPlan::Steps::PrepareInstanceStep.new(instance_plan).perform(instance_report)
124120
end
125121

126122
# current state
127-
return unless instance.model.state != 'stopped'
123+
return if instance.model.stopped?
128124

129125
stop
130126
take_snapshot
131127
end
132128

133129
def handle_detached_instance_if_detached
134-
return unless instance.state == 'detached'
130+
return unless instance.detached?
135131

136132
# Command issued: `bosh stop --hard`
137133
@logger.info("Detaching instance #{instance}")
@@ -149,7 +145,7 @@ def update_instance
149145
def update_vm_disk_metadata
150146
return unless instance_plan.changes.include?(:tags)
151147
return if instance_plan.new? || @needs_recreate
152-
return if instance.state == 'detached' # disks will get a metadata update when attaching again
148+
return if instance.detached? # disks will get a metadata update when attaching again
153149

154150
@logger.debug("Updating instance #{instance} VM and disk metadata with tags")
155151
tags = instance_plan.tags
@@ -188,7 +184,7 @@ def stop
188184
end
189185

190186
def deleting_vm?
191-
@needs_recreate || instance_plan.needs_shutting_down? || instance.state == 'detached' ||
187+
@needs_recreate || instance_plan.needs_shutting_down? || instance.detached? ||
192188
(instance_plan.should_create_swap_delete? && instance_plan.instance.model.vms.count > 1)
193189
end
194190

0 commit comments

Comments
 (0)