Class: NewRelic::Commands::Deployments

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 sttring values for :user, :description, :appname, :revision, :environment, and :changes.

Will throw CommandFailed exception if there’s any error.



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

def initialize command_line_args
  @config = NewRelic::Control.instance
  @user = ENV['USER']
  if Hash === command_line_args
    # command line args is an options hash
    command_line_args.each do | key, value |
      if %w[user environment description appname revision changelog].include? key.to_s
        instance_variable_set "@#{key}", value.to_s if value
      else
        raise "Unrecognized option #{key}=#{value}"
      end
    end
  else
    # parse command line args.  Throw an exception on a bad arg.
    @description = options.parse(command_line_args).join " "
  end
  config.env = @environment if @environment
  @appname ||= config.app_names[0] || config.env || 'development'
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



29
30
31
# File 'lib/new_relic/commands/deployments.rb', line 29

def config
  @config
end

Class Method Details

.commandObject



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

def self.command; "deployments"; end

Instance Method Details

#runObject

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



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

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 = config.http_connection(config.api_server)
    
    uri = "/deployments.xml"
    
    raise "license_key was not set in newrelic.yml for #{config.env}" if config['license_key'].nil?
    request = Net::HTTP::Post.new(uri, {'x-license-key' => 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 = [ "Unexpected response from server: #{response.code}: #{response.message}" ]
      begin
        doc = REXML::Document.new(response.body)
        doc.elements.each('errors/error') do |error|
          err_string << "Error: #{error.text}"
        end
      rescue
      end
      raise CommandFailure.new(err_string.join("\n"), -1)
    end 
  rescue SystemCallError, SocketError => e
    # These include Errno connection errors 
    err_string = "Transient error attempting to connect to #{config.api_server} (#{e})"
    raise CommandFailure.new(err_string, -1)
  rescue CommandFailure
    raise
  rescue Exception => e
    err "Unexpected error attempting to connect to #{config.api_server}"
    info "#{e}: #{e.backtrace.join("\n   ")}"
    raise CommandFailure.new(e.to_s, -1)
  end
end