-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
merged 6119, 6118, 6097: change deployment script not to use rails
- Loading branch information
Bill Kayser
committed
Jan 21, 2009
1 parent
0de3ce7
commit 35ae910
Showing
11 changed files
with
211 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/usr/bin/env ruby | ||
# executes one of the commands in the new_relic/commands directory | ||
# pass the name of the command as an argument | ||
require File.dirname(__FILE__) + '/../lib/new_relic/commands/new_relic_commands' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# This is a class for executing commands related to deployment | ||
# events. It runs without loading the rails environment | ||
|
||
$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__),"..","..")) | ||
require 'yaml' | ||
require 'net/http' | ||
|
||
# We need to use the Config object but we don't want to load | ||
# the rails/merb environment. The defined? clause is so that | ||
# it won't load it twice, something it does when run inside a test | ||
require 'new_relic/config' unless defined? NewRelic::Config | ||
|
||
module NewRelic | ||
module Commands | ||
class Deployments | ||
|
||
attr_reader :config | ||
def self.command; "deployments"; end | ||
|
||
# Initialize the deployment uploader with command line args. | ||
# Use -h to see options. Will possibly exit the VM | ||
def initialize command_line_args | ||
@config = NewRelic::Config.instance | ||
@user = ENV['USER'] | ||
@description = options.parse(command_line_args).join " " | ||
@application_id ||= config.app_name || config.env || 'development' | ||
end | ||
|
||
# Run the Deployment upload in RPM via Active Resource. | ||
# Will possibly print errors and exit the VM | ||
def run | ||
begin | ||
@description = nil if @description.empty? | ||
create_params = {} | ||
{ | ||
:application_id => @application_id, | ||
: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, 'HTTP_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 NewRelic RPM (#{@description || Time.now })" | ||
else | ||
err "Unexpected response from server: #{response.code}: #{response.message}" | ||
# TODO look for errors in xml response | ||
just_exit -1 | ||
end | ||
rescue SystemCallError, SocketError => e | ||
# These include Errno connection errors | ||
err "Transient error attempting to connect to #{config.api_server} (#{e})" | ||
just_exit -2 | ||
rescue Exception => e | ||
err "Unexpected error attempting to connect to #{config.api_server} (#{e})" | ||
info e.backtrace.join("\n") | ||
just_exit 1 | ||
end | ||
end | ||
|
||
private | ||
|
||
def options | ||
OptionParser.new "Usage: #{self.class.command} [OPTIONS] [description] ", 40 do |o| | ||
o.separator "OPTIONS:" | ||
o.on("-a", "--appname=DIR", String, | ||
"Set the application name.", | ||
"Default is app_name setting in newrelic.yml") { |@application_id| } | ||
o.on("-e ENV", String, | ||
"Override the (RAILS|MERB|RUBY)_ENV setting", | ||
"currently: #{config.env}") { |env| config.env=env } | ||
o.on("-u", "--user=USER", String, | ||
"Specify the user deploying.", | ||
"Default: #{@user}") { |@user| } | ||
o.on("-r", "--revision=REV", String, | ||
"Specify the revision being deployed") { |@revision | } | ||
o.on("-c", "--changes", | ||
"Read in a change log from the standard input") { @changelog = STDIN.read } | ||
o.on("-?", "Print this help") { info o.help; just_exit } | ||
o.separator "" | ||
o.separator 'description = "short text"' | ||
end | ||
end | ||
|
||
def info message | ||
STDOUT.puts message | ||
end | ||
def err message | ||
STDERR.puts message | ||
end | ||
def just_exit status=0 | ||
exit status | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'optparse' | ||
|
||
# Run the command given by the first argument. Right | ||
# now all we have is deployments. We hope to have other | ||
# kinds of events here later. | ||
|
||
libdir = File.expand_path(File.join(File.dirname(__FILE__), '..','..')) | ||
command_list = Dir[File.join(libdir,'new_relic','commands','*.rb')].map{|command| command =~ /.*\/(.*)\.rb/ && $1} | ||
command_list.delete 'new_relic_commands' | ||
extra = [] | ||
options = ARGV.options do |opts| | ||
script_name = File.basename($0) | ||
opts.banner = "Usage: #{__FILE__} #{ command_list.join(" | ")} [options]" | ||
opts.separator "use -h to see detailed command options" | ||
opts | ||
end | ||
extra = options.order! | ||
command = extra.shift | ||
if !command_list.include?(command) | ||
STDERR.puts options | ||
else | ||
require File.join(libdir, 'new_relic','commands', command + ".rb") | ||
command_class = NewRelic::Commands.const_get(command.capitalize) | ||
command_class.new(extra).run | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,22 @@ | ||
class NewRelic::Config::Ruby < NewRelic::Config | ||
def app; :ruby; end | ||
def env | ||
ENV['RUBY_ENV'] || 'development' | ||
@env ||= ENV['RUBY_ENV'] || ENV['RAILS_ENV'] || 'development' | ||
end | ||
def root | ||
Dir['.'] | ||
end | ||
# Check a sequence of file locations for newrelic.yml | ||
def config_file | ||
files = [] | ||
files << File.join(root,"config","newrelic.yml") | ||
files << File.join(root,"newrelic.yml") | ||
files << File.join(ENV["HOME"], ".newrelic", "newrelic.yml") | ||
files << File.join(ENV["HOME"], "newrelic.yml") | ||
files.each do | file | | ||
return File.expand_path(file) if File.exists? file | ||
end | ||
return File.expand_path(files.first) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters