Class: NewRelic::Cli::Deployments
- Defined in:
- lib/new_relic/cli/commands/deployments.rb
Instance Attribute Summary collapse
-
#control ⇒ Object
readonly
Returns the value of attribute control.
Attributes inherited from Command
Class Method Summary collapse
Instance Method Summary collapse
- #api_v1? ⇒ Boolean
-
#initialize(command_line_args) ⇒ Deployments
constructor
Initialize the deployment uploader with command line args.
- #load_yaml_from_env(env) ⇒ Object
-
#run ⇒ Object
Run the Deployment upload in New Relic via Active Resource.
- #setup_logging(env) ⇒ Object
Methods inherited from Command
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
#control ⇒ Object (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
.command ⇒ Object
19 |
# File 'lib/new_relic/cli/commands/deployments.rb', line 19 def self.command; 'deployments'; end |
Instance Method Details
#api_v1? ⇒ 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? = yaml.failures.flatten.map(&:to_s).join("\n") raise NewRelic::Cli::Command::CommandFailure.new("Error in loading newrelic.yml.\n#{}") end NewRelic::Agent.config.replace_or_add_config(yaml) end |
#run ⇒ Object
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. 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 |