def run
begin
@description = nil if @description && @description.strip.empty?
create_params = {}
{
:application_id => @appname,
:host => NewRelic::Agent::Hostname.get,
:description => @description,
:user => @user,
:revision => @revision,
:changelog => @changelog
}.each do |k, v|
create_params["deployment[#{k}]"] = v unless v.nil? || v == ''
end
http = ::NewRelic::Agent::NewRelicService.new(nil, control.api_server).http_connection
uri = "/deployments.xml"
if @license_key.nil? || @license_key.empty?
raise "license_key was not set in newrelic.yml for #{control.env}"
end
request = Net::HTTP::Post.new(uri, {'x-license-key' => @license_key})
request.content_type = "application/octet-stream"
request.set_form_data(create_params)
response = http.request(request)
if response.is_a? Net::HTTPSuccess
info "Recorded deployment to '#{@appname}' (#{@description || Time.now })"
else
err_string = REXML::Document.new(response.body).elements['errors/error'].map(&:to_s).join("; ") rescue response.message
raise NewRelic::Cli::Command::CommandFailure, "Deployment not recorded: #{err_string}"
end
rescue SystemCallError, SocketError => e
err_string = "Transient error attempting to connect to #{control.api_server} (#{e})"
raise NewRelic::Cli::Command::CommandFailure.new(err_string)
rescue NewRelic::Cli::Command::CommandFailure
raise
rescue => e
err "Unexpected error attempting to connect to #{control.api_server}"
info "#{e}: #{e.backtrace.join("\n ")}"
raise NewRelic::Cli::Command::CommandFailure.new(e.to_s)
end
end