#!/usr/bin/env ruby $:.unshift(File.join(File.dirname(__FILE__), *%w[.. lib])) ERNIE_ROOT = File.join(File.dirname(__FILE__), *%w[..]) DEFAULT_ERLANG_CODEPATHS = %w[ebin] DEFAULT_PORT = 8000 def rel(path) File.join(ERNIE_ROOT, path) end def code_paths DEFAULT_ERLANG_CODEPATHS.map {|n| "-pz #{rel(n)}" }.join(" ") + " \\" end def version yml = YAML.load(File.read(File.join(File.dirname(__FILE__), *%w[.. VERSION.yml]))) "#{yml[:major]}.#{yml[:minor]}.#{yml[:patch]}" end require 'optparse' require 'pp' require 'yaml' help = < Start an Ernie server. reload-handlers Gracefully reload all of the the ruby handlers and use the new code for all subsequent requests. stats Print usage statistics. halt Gracefully halt the server. Options: HELP options = {} OptionParser.new do |opts| opts.banner = help opts.version = version opts.on("-c CONFIG", "--config CONFIG", "Config file") do |x| options[:config] = x end opts.on("-p PORT", "--port PORT", "Port") do |x| options[:port] = x end opts.on("-l LOGLEVEL", "--log-level LOGLEVEL", "Log level (0-4)") do |x| options[:log_level] = x end opts.on("-a LOGFILE", "--access-log LOGFILE", "Access log file") do |x| options[:access_log] = x end opts.on("-d", "--detached", "Run as a daemon") do options[:detached] = true end opts.on("-P", "--pidfile PIDFILE", "Location to write pid file.") do |x| options[:pidfile] = x end end.parse! if command = ARGV[0] if !%w{reload-handlers stats halt}.include?(command) puts "Invlalid command. Valid commands are:" puts " reload-handlers" puts " stats" puts " halt" exit(1) end require 'rubygems' require 'bertrpc' port = options[:port] || DEFAULT_PORT svc = BERTRPC::Service.new('localhost', port) puts svc.call.__admin__.send(command.gsub(/-/, '_')) else if !options[:config] puts "A config file must be specified: ernie -c /path/to/config.yml" exit(1) end config = options[:config] port = options[:port] || DEFAULT_PORT log_level = options[:log_level] || 2 pidfile = options[:pidfile] ? "-ernie_server_app pidfile \"'#{options[:pidfile]}'\"" : '' detached = options[:detached] ? '-detached' : '' access_log = options[:access_log] ? "-ernie_server_app access_log '\"#{options[:access_log]}\"'" : '' cmd = %Q{erl -boot start_sasl \ #{detached} \ +Bc \ +K true \ -smp enable \ #{code_paths} #{pidfile} \ #{access_log} \ -ernie_server_app port #{port} \ -ernie_server_app config '"#{config}"' \ -ernie_server_app log_level #{log_level} \ -run ernie_server_app boot}.squeeze(' ') puts cmd exec(cmd) end