bin/instrument_server in instrumental_tools-1.0.0.rc2 vs bin/instrument_server in instrumental_tools-1.0.0
- old
+ new
@@ -6,39 +6,52 @@
rescue Gem::LoadError
puts "Requires the Instrumental Agent gem:\n"
puts ' gem install instrumental_agent'
exit 1
end
+
require 'etc'
require 'instrumental_agent'
require 'fileutils'
require 'optparse'
require 'socket'
+require 'tmpdir'
+
$: << File.join(File.dirname(__FILE__), "..", "lib")
require 'instrumental_tools/version'
require 'instrumental_tools/server_controller'
def require_api_key(options, parser)
- if options[:api_key].to_s.strip.empty?
+ if options[:api_key].to_s.strip.empty? && !File.exists?(options[:config_file])
print parser.help
exit 1
end
end
-default_script_directory = File.join(Dir.home, '.instrumental_scripts')
+
+cur_directory = Dir.pwd
+home_directory = Dir.home rescue nil
+script_location = File.expand_path(File.dirname(__FILE__))
+tmp_dir = Dir.tmpdir
+script_data_directory = [cur_directory, home_directory, script_location, tmp_dir].compact.detect { |dir| File.writable?(dir) }
+
+default_script_directory = File.join(script_data_directory, '.instrumental_scripts')
default_command = :foreground
options = {
:collector => 'collector.instrumentalapp.com',
:port => '8000',
:hostname => Socket.gethostname,
- :pid_location => File.join(Dir.home, 'instrument_server.pid'),
- :log_location => File.join(Dir.home, 'instrument_server.log'),
+ :pid_location => File.join(script_data_directory, 'instrument_server.pid'),
+ :log_location => File.join(script_data_directory, 'instrument_server.log'),
+ :tmp_location => Dir.tmpdir,
:enable_scripts => false,
:script_location => default_script_directory,
:report_interval => 30,
- :debug => false
+ :debug => false,
+ :config_file => '/etc/instrumental.yml',
+ :user => nil
}
option_parser = OptionParser.new do |opts|
opts.banner = <<-EOBANNER
Usage: instrument_server -k API_KEY [options] [#{ServerController::COMMANDS.join('|')}]"
@@ -47,10 +60,14 @@
opts.on('-k', '--api_key API_KEY', 'API key of your project') do |api_key|
options[:api_key] = api_key
end
+ opts.on('-f', '--config-file PATH', "Config file with location of your API key (default #{options[:config_file]})") do |path|
+ options[:config_file] = path
+ end
+
opts.on('-c', '--collector COLLECTOR[:PORT]', "Collector (default #{options[:collector]}:#{options[:port]})") do |collector|
address, port = collector.split(':')
options[:collector] = address
options[:port] = port if port
end
@@ -77,10 +94,18 @@
opts.on('-s', '--script-location PATH_TO_DIRECTORY', "Directory where local scripts for custom metrics are located (default #{options[:script_location]})") do |path|
options[:script_location] = path
end
+ opts.on('-u', '--user USER_TO_RUN_AS', "User to run instrument_server as. You must have permissions to drop privileges to this user.") do |u|
+ options[:user] = u
+ end
+
+ opts.on('-t', '--temp-dir TEMP_DIRECTORY', "Where to store temporary files (default #{options[:tmp_location]})") do |t|
+ options[:tmp_location] = t
+ end
+
opts.on('--debug', "Print all sent metrics to the log") do
options[:debug] = true
end
opts.on('-h', '--help', 'Display this screen') do
@@ -89,13 +114,29 @@
end
end
option_parser.parse!
+if options[:user]
+ desired_uid = Etc.getpwnam(options[:user]).uid
+ Process::Sys.setuid(desired_uid)
+ if desired_uid && desired_uid != 0
+ begin
+ Process::Sys.setuid(0)
+ rescue Errno::EPERM
+ nil
+ else
+ puts "Cannot drop privileges to #{options[:user]}"
+ exit 1
+ end
+ end
+end
+
command = ARGV.first && ARGV.first.to_sym
command ||= default_command
+
options[:api_key] ||= ENV["INSTRUMENTAL_TOKEN"]
if options[:pid_location].to_s.strip.empty?
raise "You must provide a valid path for the PID file (-p PID_PATH)"
end
@@ -146,11 +187,11 @@
running_as_daemon = [:start, :restart].include?(command)
controller = ServerController.spawn(
:name => File.basename(__FILE__),
- :path => Dir.pwd,
+ :path => options[:tmp_location],
:pid_file => options[:pid_location],
:verbose => true,
:log_file => options[:log_location],
:run_options => options.merge(:daemon => running_as_daemon)
)
@@ -163,10 +204,10 @@
if running_as_daemon
begin
- Timeout.timeout(5) do
+ Timeout.timeout(1) do
Process.waitpid(controller.pid)
end
rescue Timeout::Error
end
if !controller.running?