Class: InstanceAgent::CodeDeployPlugin::CommandPoller

Inherits:
Agent::Base
  • Object
show all
Defined in:
lib/instance_agent/codedeploy_plugin/command_poller.rb

Constant Summary collapse

VERSION =
"2013-04-23"

Instance Method Summary collapse

Methods inherited from Agent::Base

#description, #log, #run, runner

Constructor Details

#initializeCommandPoller

Returns a new instance of CommandPoller.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/instance_agent/codedeploy_plugin/command_poller.rb', line 12

def initialize
  region = ENV['AWS_REGION'] || InstanceMetadata.region
  @host_identifier = ENV['AWS_HOST_IDENTIFIER'] || InstanceMetadata.host_identifier

  log(:debug, "Configuring deploy control client: Region = #{region.inspect}")
  log(:debug, "Deploy control endpoint override = " + ENV['AWS_DEPLOY_CONTROL_ENDPOINT'].inspect)

  @deploy_control_client = InstanceAgent::CodeDeployPlugin::CodeDeployControl.new(:region => region, :logger => InstanceAgent::Log, :ssl_ca_directory => ENV['AWS_SSL_CA_DIRECTORY']).get_client

  @plugin = InstanceAgent::CodeDeployPlugin::CommandExecutor.new(
    :deploy_control_client => @deploy_control_client,
    :hook_mapping => create_hook_mapping)

  log(:debug, "Initializing Host Agent: " +
              "Host Identifier = #{@host_identifier}")
end

Instance Method Details

#acknowledge_command(command) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/instance_agent/codedeploy_plugin/command_poller.rb', line 92

def acknowledge_command(command)
  log(:debug, "Calling PutHostCommandAcknowledgement:")
  output =  @deploy_control_client.put_host_command_acknowledgement(
    :diagnostics => nil,
    :host_command_identifier => command.host_command_identifier)
  status = output.command_status
  log(:debug, "Command Status = #{status}")

  if status == 'Succeeded' || status == 'Failed'
    log(:debug, "Calling PutHostCommandComplete: \"#{status}\" ")
    @deploy_control_client.put_host_command_complete(
      :command_status => status,
      :diagnostics => {:format => "JSON", :payload => gather_diagnostics_from_acknowledge(status)},
      :host_command_identifier => command.host_command_identifier)
    return false
  end

  return true
end

#create_hook_mappingObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/instance_agent/codedeploy_plugin/command_poller.rb', line 29

def create_hook_mapping
  #Map commands to lifecycle hooks
  { "BeforeELBRemove"=>["BeforeELBRemove"],
    "AfterELBRemove"=>["AfterELBRemove"],
    "ApplicationStop"=>["ApplicationStop"],
    "BeforeInstall"=>["BeforeInstall"],
    "AfterInstall"=>["AfterInstall"],
    "ApplicationStart"=>["ApplicationStart"],
    "BeforeELBAdd"=>["BeforeELBAdd"],
    "AfterELBAdd"=>["AfterELBAdd"],
    "ValidateService"=>["ValidateService"]}
end

#get_deployment_specification(command) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/instance_agent/codedeploy_plugin/command_poller.rb', line 112

def get_deployment_specification(command)
  log(:debug, "Calling GetDeploymentSpecification:")
  output =  @deploy_control_client.get_deployment_specification(
    :deployment_execution_id => command.deployment_execution_id,
    :host_identifier => @host_identifier)
  log(:debug, "GetDeploymentSpecification: " +
              "Deployment System = #{output.deployment_system}")
  raise "Deployment System mismatch: #{@plugin.deployment_system} != #{output.deployment_system}" unless @plugin.deployment_system == output.deployment_system
  raise "Deployment Specification missing" if output.deployment_specification.nil?
  output.deployment_specification.generic_envelope
end

#next_commandObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/instance_agent/codedeploy_plugin/command_poller.rb', line 74

def next_command
  log(:debug, "Calling PollHostCommand:")
  output = @deploy_control_client.poll_host_command(:host_identifier => @host_identifier)
  command = output.host_command
  if command.nil?
    log(:debug, "PollHostCommand: Host Command =  nil")
  else
    log(:debug, "PollHostCommand: "  +
                "Host Identifier = #{command.host_identifier}; "  +
                "Host Command Identifier = #{command.host_command_identifier}; "  +
                "Deployment Execution ID = #{command.deployment_execution_id}; "  +
                "Command Name = #{command.command_name}")
    raise "Host Identifier mismatch: #{@host_identifier} != #{command.host_identifier}" unless @host_identifier.include? command.host_identifier
    raise "Command Name missing" if command.command_name.nil? || command.command_name.empty?
  end
  command
end

#performObject



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
# File 'lib/instance_agent/codedeploy_plugin/command_poller.rb', line 42

def perform
  return unless command = next_command
  return unless acknowledge_command(command)

  begin
    spec = get_deployment_specification(command)
    #Successful commands will complete without raising an exception
    script_output = process_command(command, spec)
    log(:debug, 'Calling PutHostCommandComplete: "Succeeded"')
    @deploy_control_client.put_host_command_complete(
      :command_status => 'Succeeded',
      :diagnostics => {:format => "JSON", :payload => gather_diagnostics()},
      :host_command_identifier => command.host_command_identifier)

    #Commands that throw an exception will be considered to have failed
  rescue ScriptError => e
    log(:debug, 'Calling PutHostCommandComplete: "Code Error" ')
    @deploy_control_client.put_host_command_complete(
      :command_status => "Failed",
      :diagnostics => {:format => "JSON", :payload => gather_diagnostics_from_script_error(e)},
      :host_command_identifier => command.host_command_identifier)
    raise e
  rescue Exception => e
    log(:debug, 'Calling PutHostCommandComplete: "Code Error" ')
    @deploy_control_client.put_host_command_complete(
      :command_status => "Failed",
      :diagnostics => {:format => "JSON", :payload => gather_diagnostics_from_error(e)},
      :host_command_identifier => command.host_command_identifier)
    raise e
  end
end

#process_command(command, spec) ⇒ Object



124
125
126
127
# File 'lib/instance_agent/codedeploy_plugin/command_poller.rb', line 124

def process_command(command, spec)
  log(:debug, "Calling #{@plugin.to_s}.execute_command")
  @plugin.execute_command(command, spec)
end