Class: NewRelic::Cli::Deployments

Inherits:
Command
  • Object
show all
Defined in:
lib/new_relic/cli/commands/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
43
44
45
46
47
# File 'lib/new_relic/cli/commands/deployments.rb', line 30

def initialize(command_line_args)
  @control = NewRelic::Control.instance
  @environment = nil
  @changelog = nil
  @user = nil
  super(command_line_args)
  # needs else branch coverage
  @description ||= @leftover && @leftover.join(' ') # rubocop:disable Style/SafeNavigation
  @user ||= ENV['USER']
  control.env = @environment if @environment

  load_yaml_from_env(control.env)
  @appname ||= NewRelic::Agent.config[:app_name][0] || control.env || 'development'
  @license_key ||= NewRelic::Agent.config[:license_key]
  @api_key ||= NewRelic::Agent.config[:api_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/commands/deployments.rb', line 18

def control
  @control
end

Class Method Details

.commandObject



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

def self.command; 'deployments'; end

Instance Method Details

#api_v1?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/new_relic/cli/commands/deployments.rb', line 112

def api_v1?
  @api_key.nil? || @api_key.empty?
end

#load_yaml_from_env(env) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/new_relic/cli/commands/deployments.rb', line 49

def load_yaml_from_env(env)
  yaml = NewRelic::Agent::Configuration::YamlSource.new(NewRelic::Agent.config[:config_path], env)
  if yaml.failed?
    messages = yaml.failures.flatten.map(&:to_s).join("\n")
    raise NewRelic::Cli::Command::CommandFailure.new("Error in loading newrelic.yml.\n#{messages}")
  end

  NewRelic::Agent.config.replace_or_add_config(yaml)
end

#runObject

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



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
102
103
104
105
106
107
108
109
110
# File 'lib/new_relic/cli/commands/deployments.rb', line 66

def run
  begin
    @description = nil if @description && @description.strip.empty?

    if @license_key.nil? || @license_key.empty?
      raise "license_key not set in newrelic.yml for #{control.env}. api_key also required to use New Relic REST API v2"
    end

    if !api_v1? && (@revision.nil? || @revision.empty?)
      raise 'revision required when using New Relic REST API v2 with api_key. Pass in revision using: -r, --revision=REV'
    end

    request = if api_v1?
      uri = '/deployments.xml'
      create_request(uri, {'x-license-key' => @license_key}, 'application/octet-stream').tap do |req|
        set_params_v1(req)
      end
    else
      uri = "/v2/applications/#{application_id}/deployments.json"
      create_request(uri, {'Api-Key' => @api_key}, 'application/json').tap do |req|
        set_params_v2(req)
      end
    end

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



59
60
61
62
# File 'lib/new_relic/cli/commands/deployments.rb', line 59

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