Class: NewRelic::Command::Deployments

Inherits:
NewRelic::Command show all
Defined in:
lib/new_relic/commands/deployments.rb

Instance Attribute Summary collapse

Attributes inherited from NewRelic::Command

#leftover

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from NewRelic::Command

#err, #info, inherited, run

Constructor Details

#initialize(command_line_args) ⇒ Deployments

Initialize the deployment uploader with command line args. Use -h to see options. When command_line_args is a hash, we are invoking directly and it’s treated as an options with optional string values for :user, :description, :appname, :revision, :environment, and :changes.

Will throw CommandFailed exception if there’s any error.



26
27
28
29
30
31
32
33
# File 'lib/new_relic/commands/deployments.rb', line 26

def initialize command_line_args
  @config = NewRelic::Control.instance
  super(command_line_args)
  @description ||= @leftover && @leftover.join(" ")
  @user ||= ENV['USER']
  config.env = @environment if @environment
  @appname ||= config.app_names[0] || config.env || 'development'
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



14
15
16
# File 'lib/new_relic/commands/deployments.rb', line 14

def config
  @config
end

Class Method Details

.commandObject



15
# File 'lib/new_relic/commands/deployments.rb', line 15

def self.command; "deployments"; end

Instance Method Details

#runObject

Run the Deployment upload in New Relic via Active Resource. Will possibly print errors and exit the VM



37
38
39
40
41
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
73
74
75
76
77
78
79
80
# File 'lib/new_relic/commands/deployments.rb', line 37

def run
  begin
    @description = nil if @description && @description.strip.empty?
    create_params = {}
    {
          :application_id => @appname,
          :host => Socket.gethostname,
          :description => @description,
          :user => @user,
          :revision => @revision,
          :changelog => @changelog
    }.each do |k, v|
      create_params["deployment[#{k}]"] = v unless v.nil? || v == ''
    end
    http = config.http_connection(config.api_server)

    uri = "/deployments.xml"

    raise "license_key was not set in newrelic.yml for #{config.env}" if config['license_key'].nil?
    request = Net::HTTP::Post.new(uri, {'x-license-key' => config['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::Command::CommandFailure, "Deployment not recorded: #{err_string}"
    end
  rescue SystemCallError, SocketError => e
    # These include Errno connection errors
    err_string = "Transient error attempting to connect to #{config.api_server} (#{e})"
    raise NewRelic::Command::CommandFailure.new(err_string)
  rescue NewRelic::Command::CommandFailure
    raise
  rescue Exception => e
    err "Unexpected error attempting to connect to #{config.api_server}"
    info "#{e}: #{e.backtrace.join("\n   ")}"
    raise NewRelic::Command::CommandFailure.new(e.to_s)
  end
end