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
34
# File 'lib/new_relic/commands/deployments.rb', line 26

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

Instance Attribute Details

#controlObject (readonly)

Returns the value of attribute control.



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

def control
  @control
end

Class Method Details

.commandObject



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

def self.command; "deployments"; end

Instance Method Details

#load_yaml_from_env(env) ⇒ Object



36
37
38
39
# File 'lib/new_relic/commands/deployments.rb', line 36

def load_yaml_from_env(env)
  yaml = NewRelic::Agent::Configuration::YamlSource.new(NewRelic::Agent.config[:config_path], env)
  NewRelic::Agent.config.replace_or_add_config(yaml, 1)
end

#runObject

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



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
81
82
83
84
85
86
87
88
89
# File 'lib/new_relic/commands/deployments.rb', line 43

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 = ::NewRelic::Agent::NewRelicService.new(nil, control.api_server).http_connection

    uri = "/deployments.xml"

    if NewRelic::Agent.config[:license_key].nil? ||
        NewRelic::Agent.config[: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' => NewRelic::Agent.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 #{control.api_server} (#{e})"
    raise NewRelic::Command::CommandFailure.new(err_string)
  rescue NewRelic::Command::CommandFailure
    raise
  rescue => e
    err "Unexpected error attempting to connect to #{control.api_server}"
    info "#{e}: #{e.backtrace.join("\n   ")}"
    raise NewRelic::Command::CommandFailure.new(e.to_s)
  end
end