bin/pimon
#!/usr/bin/env ruby
require 'optparse'
options = {}
option_parser = OptionParser.new do |opts|
executable_name = File.basename($PROGRAM_NAME)
opts.banner = "Usage: #{executable_name} start|stop [options]
Options:
"
opts.on('-c CONFIG',
'--config CONFIG',
'YAML configuration file for pimon') do |config|
options[:pimon_config] = config
end
opts.on('-d',
'--daemonize',
'Run Pimon daemonized in the background') do
options[:daemonize] = true
end
opts.on('-e ENVIRONMENT',
'--environment ENVIRONMENT',
'Application environment (default: "development", options: "development", "production")',
/^development|production$/) do |environment|
options[:environment] = environment
end
opts.on('-i INTERFACE',
'--interface INTERFACE',
'Hostname or IP address of the interface to listen on (default: "localhost")') do |interface|
options[:interface] = interface
end
opts.on('-p PORT',
'--port PORT',
'Port to use (default: 3000)') do |port|
options[:port] = port
end
opts.on('-P PIDFILE',
'--pid PIDFILE',
'File to store PID (default: /tmp/pimon.pid)') do |pid_file|
options[:pid_file] = pid_file
end
opts.on('-v',
'--version',
'Display Pimon version') do |_|
ARGV[0] = 'version'
end
end
begin
option_parser.parse!
if ARGV.empty?
puts 'error: you must supply an action'
puts option_parser.help
exit 1
end
pid_file = options[:pid_file] || '/tmp/pimon.pid'
case ARGV[0]
when 'start'
require 'thin'
port = options[:port] || 3000
interface = options[:interface] || 'localhost'
options[:environment] ? ENV['RACK_ENV'] = options[:environment] : ENV['RACK_ENV'] = 'development'
config = "#{File.dirname(__FILE__)}/../config/config.ru"
ENV['PIMON_CONFIG'] = options[:pimon_config] if options[:pimon_config]
puts "Pimon is starting at http://#{interface}:#{port}"
puts "Running in #{ENV['RACK_ENV']} mode."
if ENV['PIMON_CONFIG']
puts "Using configuration file #{ENV['PIMON_CONFIG']}"
else
puts 'Using default configuration'
end
server_options = {
config: config,
daemonize: options[:daemonize],
pid: pid_file,
Port: port,
server: 'thin',
Host: interface
}
server = Rack::Server.new(server_options)
server.start
when 'stop'
if File.file?(pid_file)
pid = File.read(pid_file).to_i
Process.kill('KILL', pid)
File.delete(pid_file) if File.exist?(pid_file)
puts 'Pimon stopped...'
else
puts "Pid file not found at #{pid_file}, please supply a valid pid_file using -p or --pid"
end
when 'version'
puts "Pimon version is #{Gem::Specification.load("#{File.dirname(__FILE__)}/../pimon.gemspec").version}"
else
STDERR.puts option_parser
end
rescue OptionParser::InvalidArgument => ex
STDERR.puts ex.message
STDERR.puts option_parser
end