Skip to content

Commit dd0848c

Browse files
jcbhlmwjones-aws
authored andcommitted
Pass IsCommandNoop diagnostic information through PutHostCommandAcknowledgement
This patch allows us to pass diagnostic information on whether a command is a noop or not from the Agent to CodeDeploy commands service, which CodeDeploy service team will track.
1 parent 8dca5a6 commit dd0848c

File tree

2 files changed

+58
-38
lines changed

2 files changed

+58
-38
lines changed

lib/instance_agent/plugins/codedeploy/command_poller.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,9 @@ def graceful_shutdown
9090
end
9191

9292
def acknowledge_and_process_command(command)
93-
return unless acknowledge_command(command)
94-
9593
begin
9694
spec = get_deployment_specification(command)
95+
return unless acknowledge_command(command, spec)
9796
process_command(command, spec)
9897
#Commands that throw an exception will be considered to have failed
9998
rescue Exception => e
@@ -161,10 +160,18 @@ def next_command
161160
end
162161

163162
private
164-
def acknowledge_command(command)
163+
def get_ack_diagnostics(command, spec)
164+
is_command_noop = @plugin.is_command_noop?(command.command_name, spec)
165+
return {:format => "JSON", :payload => {'IsCommandNoop' => is_command_noop}.to_json()}
166+
end
167+
168+
private
169+
def acknowledge_command(command, spec)
170+
ack_diagnostics = get_ack_diagnostics(command, spec)
171+
165172
log(:debug, "Calling PutHostCommandAcknowledgement:")
166173
output = @deploy_control_client.put_host_command_acknowledgement(
167-
:diagnostics => nil,
174+
:diagnostics => ack_diagnostics,
168175
:host_command_identifier => command.host_command_identifier)
169176
status = output.command_status
170177
log(:debug, "Command Status = #{status}")

test/instance_agent/plugins/codedeploy/command_poller_test.rb

Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,18 @@ def gather_diagnostics(script_output, msg = "")
1414
{'error_code' => InstanceAgent::Plugins::CodeDeployPlugin::ScriptError::SUCCEEDED_CODE, 'script_name' => "", 'message' => "Succeeded: #{msg}", 'log' => script_output}.to_json
1515
end
1616

17+
def get_ack_diagnostics(is_command_noop)
18+
return {:format => "JSON", :payload => {'IsCommandNoop' => is_command_noop}.to_json()}
19+
end
20+
1721
context 'The command poller' do
1822

1923
setup do
2024
@host_identifier = "i-123"
2125
@aws_region = 'us-east-1'
2226
@deploy_control_endpoint = "my-deploy-control.amazon.com"
23-
@deploy_control_client = mock()
24-
@deploy_control_api = mock()
27+
@deploy_control_client = mock('deploy-control-client')
28+
@deploy_control_api = mock('deploy-control-api')
2529
@executor = stub(:execute_command => "test this is not returned",
2630
:deployment_system => "CodeDeploy")
2731

@@ -107,6 +111,7 @@ def gather_diagnostics(script_output, msg = "")
107111
starts_as('setup')
108112
@executor.stubs(:execute_command).
109113
when(@execute_command_state.is('setup'))
114+
@executor.stubs(:is_command_noop?).returns(false)
110115

111116
@put_host_command_complete_state = states('put_host_command_complete_state').
112117
starts_as('setup')
@@ -115,7 +120,7 @@ def gather_diagnostics(script_output, msg = "")
115120
@deployment_id = stub(:deployment_id => "D-1234")
116121
InstanceAgent::Config.config[:root_dir] = File.join(Dir.tmpdir(), "CodeDeploy")
117122
InstanceAgent::Config.config[:ongoing_deployment_tracking] = "ongoing-deployment"
118-
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification.stubs(:parse).returns(@deployment_id)
123+
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentSpecification.stubs(:parse).returns(@deployment_id)
119124
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentCommandTracker.stubs(:delete_deployment_command_tracking_file).returns(true)
120125
InstanceAgent::Plugins::CodeDeployPlugin::DeploymentCommandTracker.stubs(:create_ongoing_deployment_tracking_file).returns(true)
121126
end
@@ -257,7 +262,7 @@ def gather_diagnostics(script_output, msg = "")
257262

258263
should 'call PutHostCommandAcknowledgement with host_command_identifier returned by PollHostCommand' do
259264
@deploy_control_client.expects(:put_host_command_acknowledgement).
260-
with(:diagnostics => nil,
265+
with(:diagnostics => get_ack_diagnostics(false),
261266
:host_command_identifier => @command.host_command_identifier).
262267
returns(@poll_host_command_acknowledgement_output)
263268

@@ -266,13 +271,10 @@ def gather_diagnostics(script_output, msg = "")
266271

267272
should 'return when Succeeded command status is given by PutHostCommandAcknowledgement' do
268273
@deploy_control_client.expects(:put_host_command_acknowledgement).
269-
with(:diagnostics => nil,
274+
with(:diagnostics => get_ack_diagnostics(false),
270275
:host_command_identifier => @command.host_command_identifier).
271276
returns(stub(:command_status => "Succeeded"))
272277

273-
@get_deployment_specification_state.become('never')
274-
@deploy_control_client.expects(:get_deployment_specification).never.
275-
when(@get_deployment_specification_state.is('never'))
276278
@put_host_command_complete_state.become('never')
277279
@deploy_control_client.expects(:put_host_command_complete).never.
278280
when(@put_host_command_complete_state.is('never'))
@@ -281,39 +283,50 @@ def gather_diagnostics(script_output, msg = "")
281283
end
282284

283285
context 'when Failed command status is given by PutHostCommandAcknowledgement' do
284-
setup do
285-
@deploy_control_client.expects(:put_host_command_acknowledgement).
286-
with(:diagnostics => nil,
287-
:host_command_identifier => @command.host_command_identifier).
288-
returns(stub(:command_status => "Failed"))
286+
context 'when the command is not a noop' do
287+
setup do
288+
@deploy_control_client.expects(:put_host_command_acknowledgement).
289+
with(:diagnostics => get_ack_diagnostics(false),
290+
:host_command_identifier => @command.host_command_identifier).
291+
returns(stub(:command_status => "Failed"))
292+
293+
@executor.expects(:is_command_noop?).
294+
with(@command.command_name, @deployment_specification.generic_envelope).returns(false)
295+
end
289296

290-
@deploy_control_client.expects(:get_deployment_specification).
291-
with(:deployment_execution_id => @command.deployment_execution_id,
292-
:host_identifier => @host_identifier).
293-
returns(@get_deploy_specification_output)
294-
end
297+
should 'do nothing' do
298+
@put_host_command_complete_state.become('never')
299+
@deploy_control_client.expects(:put_host_command_complete).never.
300+
when(@put_host_command_complete_state.is('never'))
295301

296-
should "return when the command is not a noop" do
297-
@executor.expects(:is_command_noop?).
298-
with(@command.command_name, @deployment_specification.generic_envelope).returns(false)
302+
@poller.acknowledge_and_process_command(@command)
303+
end
304+
end
299305

300-
@put_host_command_complete_state.become('never')
301-
@deploy_control_client.expects(:put_host_command_complete).never.
302-
when(@put_host_command_complete_state.is('never'))
306+
context 'when the command is a noop' do
307+
setup do
308+
@deploy_control_client.expects(:put_host_command_acknowledgement).
309+
with(:diagnostics => get_ack_diagnostics(true),
310+
:host_command_identifier => @command.host_command_identifier).
311+
returns(stub(:command_status => "Failed"))
303312

304-
@poller.acknowledge_and_process_command(@command)
305-
end
313+
@deploy_control_client.expects(:get_deployment_specification).
314+
with(:deployment_execution_id => @command.deployment_execution_id,
315+
:host_identifier => @host_identifier).
316+
returns(@get_deploy_specification_output)
306317

307-
should "PutHostCommandComplete when the command is a noop" do
308-
@executor.expects(:is_command_noop?).
309-
with(@command.command_name, @deployment_specification.generic_envelope).returns(true)
318+
@executor.expects(:is_command_noop?).
319+
with(@command.command_name, @deployment_specification.generic_envelope).returns(true).twice
320+
end
310321

311-
@deploy_control_client.expects(:put_host_command_complete).
312-
with(:command_status => "Succeeded",
313-
:diagnostics => {:format => "JSON", :payload => gather_diagnostics("", "CompletedNoopCommand")},
314-
:host_command_identifier => @command.host_command_identifier)
322+
should 'call PutHostCommandComplete with Succeeded' do
323+
@deploy_control_client.expects(:put_host_command_complete).
324+
with(:command_status => "Succeeded",
325+
:diagnostics => {:format => "JSON", :payload => gather_diagnostics("", "CompletedNoopCommand")},
326+
:host_command_identifier => @command.host_command_identifier)
315327

316-
@poller.acknowledge_and_process_command(@command)
328+
@poller.acknowledge_and_process_command(@command)
329+
end
317330
end
318331
end
319332

0 commit comments

Comments
 (0)