Permalink
Browse files

Clean up commands, rename newrelic_cmd to newrelic, add install command

  • Loading branch information...
1 parent c5c0d14 commit 96d972f8e25710b950970f405cd6d6b7eb3dae8f @bkayser bkayser committed May 18, 2010
Showing with 183 additions and 31 deletions.
  1. +2 −0 CHANGELOG
  2. +13 −0 bin/newrelic
  3. +2 −1 bin/newrelic_cmd
  4. +85 −0 lib/new_relic/command.rb
  5. +81 −0 lib/new_relic/commands/install.rb
  6. +0 −30 lib/new_relic/commands/new_relic_commands.rb
View
@@ -1,3 +1,5 @@
+ * renamed executable 'newrelic_cmd' to 'newrelic'; old name still supported
+ for backward compatibility
v2.12.0
* support basic instrumentation for ActsAsSolr and Sunspot
View
@@ -0,0 +1,13 @@
+#!/usr/bin/env ruby
+# executes one of the commands in the new_relic/commands directory
+# pass the name of the command as an argument
+
+$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), '..','lib'))
+require 'new_relic/command'
+begin
+ NewRelic::Command.run
+rescue NewRelic::Command::CommandFailure => failure
+ STDERR.puts failure.message
+ STDERR.puts failure.options if failure.options
+ exit 1
+end
View
@@ -1,4 +1,5 @@
#!/usr/bin/env ruby
+# This command has been renamed "newrelic"
# 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'
+load File.dirname(__FILE__) + '/newrelic'
View
@@ -0,0 +1,85 @@
+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.
+$LOAD_PATH << "#{File.dirname(__FILE__)}/.."
+module NewRelic
+ class Command
+ attr_accessor :leftover
+ # Capture a failure to execute the command.
+ class CommandFailure < StandardError
+ attr_reader :options
+ def initialize message, opt_parser=nil
+ super message
+ @options = opt_parser
+ end
+ end
+
+ def info(message)
+ STDOUT.puts message
+ end
+
+ def err(message)
+ STDERR.puts message
+ end
+
+ def initialize(command_line_args)
+ if Hash === command_line_args
+ # command line args is an options hash
+ command_line_args.each do | key, value |
+ instance_variable_set "@#{key}", value.to_s if value
+ end
+ else
+ # parse command line args. Throw an exception on a bad arg.
+ @options = options do | opts |
+ opts.on("-h", "Show this help") { raise CommandFailure, opts.to_s }
+ end
+ @leftover = @options.parse(command_line_args)
+ end
+ rescue OptionParser::ParseError => e
+ raise CommandFailure.new e.message, @options
+ end
+
+ @commands = []
+ def self.inherited(subclass)
+ @commands << subclass
+ end
+
+ cmds = File.expand_path(File.join(File.dirname(__FILE__), 'commands', '*.rb'))
+ Dir[cmds].each{|command| require command }
+
+ def self.run
+
+ @command_names = @commands.map(&:command)
+
+ extra = []
+ options = ARGV.options do |opts|
+ script_name = File.basename($0)
+ if script_name =~ /newrelic_cmd$/
+ $stdout.puts "warning: the 'newrelic_cmd' script has been renamed 'newrelic'"
+ script_name = 'newrelic'
+ end
+ opts.banner = "Usage: #{script_name} [ #{ @command_names.join(" | ")} ] [options]"
+ opts.separator "use '#{script_name} <command> -h' to see detailed command options"
+ opts
+ end
+ extra = options.order!
+ command = extra.shift
+ # just make it a little easier on them
+ command = 'deployments' if command =~ /deploy/
+ if command.nil?
+ STDERR.puts options
+ elsif !@command_names.include?(command)
+ STDERR.puts "Unrecognized command: #{command}"
+ STDERR.puts options
+ else
+ command_class = @commands.find{ |c| c.command == command}
+ command_class.new(extra).run
+ end
+ rescue OptionParser::InvalidOption => e
+ raise NewRelic::Command::CommandFailure, e.message
+ end
+ end
+
+end
@@ -0,0 +1,81 @@
+require 'fileutils'
+require 'new_relic/version'
+require 'erb'
+
+class NewRelic::Command::Install < NewRelic::Command
+
+ NO_LICENSE_KEY = "<PASTE LICENSE KEY HERE>"
+
+ def self.command; "install"; end
+
+ # 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,
+ # and :changes.
+ #
+ # Will throw CommandFailed exception if there's any error.
+ #
+ attr_reader :dest_dir, :license_key, :generated_for_user, :quiet, :src_file, :app_name
+ def initialize command_line_args={}
+ super command_line_args
+ if !@dest_dir
+ # Install a newrelic.yml file into the local config directory.
+ if File.directory? "config"
+ @dest_dir = "config"
+ else
+ @dest_dir = "."
+ end
+ end
+ @license_key ||= NO_LICENSE_KEY
+ @app_name ||= @leftover
+ raise CommandFailure.new("Application name required.", @options) unless @app_name && @app_name.size > 0
+ @generated_for_user ||= @user_string || ""
+ end
+
+ def run
+ dest_file = File.expand_path(@dest_dir + "/newrelic.yml")
+ if File.exist?(dest_file)
+ raise NewRelic::Command::CommandFailure, "newrelic.yml file already exists. Move it out of the way."
+ end
+ File.open(dest_file, 'w') { | out | out.puts(content) }
+
+ puts <<-EOF unless quiet
+
+Installed a default configuration file at
+#{dest_file}.
+ EOF
+ puts <<-EOF unless quiet || @license_key != NO_LICENSE_KEY
+
+To monitor your application in production mode, sign up for an account
+at www.newrelic.com, and replace the newrelic.yml file with the one
+you receive upon registration.
+ EOF
+ puts <<-EOF unless quiet
+
+E-mail support@newrelic.com with any problems or questions.
+
+ EOF
+
+ end
+
+ def content
+ @src_file ||= File.expand_path(File.join(File.dirname(__FILE__),"..","..","..","newrelic.yml"))
+ template = File.read(@src_file)
+ ERB.new(template).result(binding)
+ end
+
+ private
+
+ def options
+ OptionParser.new "Usage: #{$0} #{self.class.command} [ OPTIONS] 'application name'", 40 do |o|
+ o.on("-l", "--license_key=NAME", String,
+ "Use the given license key") { | e | @license_key = e }
+ o.on("-d", "--destdir=name", String,
+ "Write the newrelic.yml to the given directory, default is '.'") { | e | @dest_dir = e }
+ yield o if block_given?
+ end
+ end
+
+
+end
@@ -1,30 +0,0 @@
-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)
- begin
- command_class.new(extra).run
- rescue NewRelic::Commands::CommandFailure => failure
- STDERR.puts failure.message
- exit failure.exit_code
- end
-end

0 comments on commit 96d972f

Please sign in to comment.