Class: NewRelic::Cli::Deployments

Inherits:
Command
  • Object
show all
Defined in:
lib/new_relic/cli/deployments.rb

Instance Attribute Summary collapse

Attributes inherited from Command

#leftover

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from 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, :license_key, and :changes.

Will throw CommandFailed exception if there’s any error.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/new_relic/cli/deployments.rb', line 30

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'
  @license_key ||= NewRelic::Agent.config[:license_key]

  setup_logging(control.env)
end

Instance Attribute Details

#controlObject (readonly)

Returns the value of attribute control.



18
19
20
# File 'lib/new_relic/cli/deployments.rb', line 18

def control
  @control
end

Class Method Details

.commandObject



19
# File 'lib/new_relic/cli/deployments.rb', line 19

def self.command; "deployments"; end

Instance Method Details

#load_yaml_from_env(env) ⇒ Object



44
45
46
47
# File 'lib/new_relic/cli/deployments.rb', line 44

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



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
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/new_relic/cli/deployments.rb', line 56

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 @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
    # These include Errno connection errors
    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

#setup_logging(env) ⇒ Object



49
50
51
52
# File 'lib/new_relic/cli/deployments.rb', line 49

def setup_logging(env)
  NewRelic::Agent.logger = NewRelic::Agent::AgentLogger.new(NewRelic::Agent.config)
  NewRelic::Agent.logger.info("Running Capistrano from '#{env}' environment for application '#{@appname}'")
end