2006-03-26 15:01:50 -05:00
|
|
|
|
2006-02-09 21:38:18 -05:00
|
|
|
require 'rubygems'
|
2006-03-05 02:58:18 -05:00
|
|
|
require 'yaml'
|
2006-04-05 08:29:23 -04:00
|
|
|
require 'mongrel'
|
2006-03-26 15:01:50 -05:00
|
|
|
require 'mongrel/rails'
|
2006-05-20 19:14:08 -04:00
|
|
|
require 'etc'
|
2006-03-26 15:01:50 -05:00
|
|
|
|
|
|
|
|
2006-05-13 17:54:47 -04:00
|
|
|
class Version < GemPlugin::Plugin "/commands"
|
|
|
|
include Mongrel::Command::Base
|
|
|
|
|
|
|
|
def run
|
|
|
|
puts "Version #{Mongrel::Const::MONGREL_VERSION}"
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2006-02-09 20:06:55 -05:00
|
|
|
|
2006-03-06 00:31:39 -05:00
|
|
|
class Start < GemPlugin::Plugin "/commands"
|
2006-03-01 22:54:32 -05:00
|
|
|
include Mongrel::Command::Base
|
2006-02-09 20:06:55 -05:00
|
|
|
|
2006-02-11 20:30:33 -05:00
|
|
|
def configure
|
|
|
|
options [
|
2006-03-01 22:54:32 -05:00
|
|
|
["-e", "--environment ENV", "Rails environment to run as", :@environment, ENV['RAILS_ENV'] || "development"],
|
|
|
|
["-d", "--daemonize", "Whether to run in the background or not", :@daemon, false],
|
|
|
|
['-p', '--port PORT', "Which port to bind to", :@port, 3000],
|
|
|
|
['-a', '--address ADDR', "Address to bind to", :@address, "0.0.0.0"],
|
|
|
|
['-l', '--log FILE', "Where to write log messages", :@log_file, "log/mongrel.log"],
|
|
|
|
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"],
|
2006-05-11 15:10:34 -04:00
|
|
|
['-n', '--num-procs INT', "Number of processors active before clients denied", :@num_procs, 1024],
|
|
|
|
['-t', '--timeout TIME', "Timeout all requests after 100th seconds time", :@timeout, 0],
|
2006-03-01 22:54:32 -05:00
|
|
|
['-m', '--mime PATH', "A YAML file that lists additional MIME types", :@mime_map, nil],
|
|
|
|
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
|
|
|
|
['-r', '--root PATH', "Set the document root (default 'public')", :@docroot, "public"],
|
2006-03-26 15:01:50 -05:00
|
|
|
['-B', '--debug', "Enable debugging mode", :@debug, false],
|
2006-04-01 03:43:30 -05:00
|
|
|
['-C', '--config PATH', "Use a config file", :@config_file, nil],
|
2006-05-12 15:39:42 -04:00
|
|
|
['-S', '--script PATH', "Load the given file as an extra config script.", :@config_script, nil],
|
2006-05-20 19:14:08 -04:00
|
|
|
['-G', '--generate CONFIG', "Generate a config file for -C", :@generate, nil],
|
2006-05-21 11:05:38 -04:00
|
|
|
['', '--user USER', "User to run as", :@user, nil],
|
|
|
|
['', '--group GROUP', "Group to run as", :@group, nil]
|
2006-02-11 20:30:33 -05:00
|
|
|
]
|
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-02-11 20:30:33 -05:00
|
|
|
def validate
|
2006-02-14 07:19:02 -05:00
|
|
|
@cwd = File.expand_path(@cwd)
|
|
|
|
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
|
|
|
|
|
|
|
|
# change there to start, then we'll have to come back after daemonize
|
|
|
|
Dir.chdir(@cwd)
|
|
|
|
|
2006-02-11 20:30:33 -05:00
|
|
|
valid_dir? File.dirname(@log_file), "Path to log file not valid: #@log_file"
|
|
|
|
valid_dir? File.dirname(@pid_file), "Path to pid file not valid: #@pid_file"
|
2006-02-14 07:19:02 -05:00
|
|
|
valid_dir? @docroot, "Path to docroot not valid: #@docroot"
|
|
|
|
valid_exists? @mime_map, "MIME mapping file does not exist: #@mime_map" if @mime_map
|
2006-03-26 18:57:11 -05:00
|
|
|
valid_exists? @config_file, "Config file not there: #@config_file" if @config_file
|
2006-05-12 15:39:42 -04:00
|
|
|
valid_dir? File.dirname(@generate), "Problem accessing directory to #@generate" if @generate
|
2006-05-20 19:14:08 -04:00
|
|
|
valid_user? @user if @user
|
|
|
|
valid_group? @group if @group
|
|
|
|
|
2006-02-11 20:30:33 -05:00
|
|
|
return @valid
|
|
|
|
end
|
2006-05-20 19:14:08 -04:00
|
|
|
|
|
|
|
def valid_user?(user)
|
|
|
|
valid?(Process.uid == 0, "You must be root to change the user.")
|
2006-05-21 11:05:38 -04:00
|
|
|
valid?(@group, "You must also specify a group.")
|
2006-05-20 19:14:08 -04:00
|
|
|
begin
|
|
|
|
Etc.getpwnam(user)
|
|
|
|
rescue
|
|
|
|
failure "User does not exist: #{user}"
|
|
|
|
@valid = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid_group?(group)
|
|
|
|
valid?(Process.uid == 0, "You must be root to change the group.")
|
2006-05-21 11:05:38 -04:00
|
|
|
valid?(@user, "You must also specify a user.")
|
2006-05-20 19:14:08 -04:00
|
|
|
begin
|
|
|
|
Etc.getgrnam(group)
|
|
|
|
rescue
|
|
|
|
failure "Group does not exist: #{group}"
|
|
|
|
@valid = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-03-26 15:01:50 -05:00
|
|
|
def run
|
2006-03-01 22:54:32 -05:00
|
|
|
|
2006-03-26 18:57:11 -05:00
|
|
|
# command line setting override config file settings
|
2006-03-26 15:01:50 -05:00
|
|
|
settings = { :host => @address, :port => @port, :cwd => @cwd,
|
|
|
|
:log_file => @log_file, :pid_file => @pid_file, :environment => @environment,
|
|
|
|
:docroot => @docroot, :mime_map => @mime_map, :daemon => @daemon,
|
2006-05-12 14:58:32 -04:00
|
|
|
:debug => @debug, :includes => ["mongrel"], :config_script => @config_script,
|
2006-05-20 19:14:08 -04:00
|
|
|
:num_processors => @num_procs, :timeout => @timeout,
|
|
|
|
:user => @user, :group => @group
|
2006-03-26 15:01:50 -05:00
|
|
|
}
|
2006-02-14 07:19:02 -05:00
|
|
|
|
2006-05-12 15:39:42 -04:00
|
|
|
if @generate
|
|
|
|
STDERR.puts "** Writing config to #@generate"
|
|
|
|
open(@generate, "w") {|f| f.write(settings.to_yaml) }
|
|
|
|
end
|
|
|
|
|
2006-03-26 18:57:11 -05:00
|
|
|
if @config_file
|
|
|
|
STDERR.puts "** Loading settings from #{@config_file} (command line options override)."
|
|
|
|
conf = YAML.load_file(@config_file)
|
|
|
|
settings = conf.merge(settings)
|
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-04-03 22:09:00 -04:00
|
|
|
config = Mongrel::Rails::RailsConfigurator.new(settings) do
|
2006-04-03 22:07:30 -04:00
|
|
|
log "Starting Mongrel in #{defaults[:environment]} mode at #{defaults[:host]}:#{defaults[:port]}"
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-03-26 15:01:50 -05:00
|
|
|
if defaults[:daemon]
|
2006-04-03 22:07:30 -04:00
|
|
|
log "Daemonizing, any open files are closed. Look at #{defaults[:pid_file]} and #{defaults[:log_file]} for info."
|
2006-03-26 15:01:50 -05:00
|
|
|
daemonize
|
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-03-26 15:01:50 -05:00
|
|
|
listener do
|
|
|
|
mime = {}
|
|
|
|
if defaults[:mime_map]
|
2006-04-03 22:07:30 -04:00
|
|
|
log "Loading additional MIME types from #{defaults[:mime_map]}"
|
2006-03-26 15:01:50 -05:00
|
|
|
mime = load_mime_map(defaults[:mime_map], mime)
|
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-03-26 15:01:50 -05:00
|
|
|
if defaults[:debug]
|
|
|
|
log "Installing debugging prefixed filters. Look in log/mongrel_debug for the files."
|
|
|
|
debug "/"
|
|
|
|
end
|
|
|
|
|
2006-04-03 22:07:30 -04:00
|
|
|
log "Starting Rails in environment #{defaults[:environment]} ..."
|
2006-05-18 11:59:05 -04:00
|
|
|
uri "/", :handler => rails(:mime => mime)
|
2006-03-26 15:01:50 -05:00
|
|
|
log "Rails loaded."
|
|
|
|
|
|
|
|
log "Loading any Rails specific GemPlugins"
|
|
|
|
load_plugins
|
|
|
|
|
2006-04-03 22:07:30 -04:00
|
|
|
if defaults[:config_script]
|
|
|
|
log "Loading #{defaults[:config_script]} external config script"
|
|
|
|
run_config(defaults[:config_script])
|
2006-04-01 03:43:30 -05:00
|
|
|
end
|
|
|
|
|
2006-03-26 15:01:50 -05:00
|
|
|
setup_rails_signals
|
|
|
|
end
|
2006-02-26 19:56:59 -05:00
|
|
|
end
|
2006-03-05 02:58:18 -05:00
|
|
|
|
2006-03-26 15:01:50 -05:00
|
|
|
config.run
|
|
|
|
config.log "Mongrel available at #{settings[:host]}:#{settings[:port]}"
|
2006-05-22 23:58:37 -04:00
|
|
|
|
|
|
|
if not @daemon
|
|
|
|
config.log "Use CTRL-C to stop."
|
|
|
|
else
|
|
|
|
config.write_pid_file
|
|
|
|
end
|
|
|
|
|
2006-03-26 15:01:50 -05:00
|
|
|
config.join
|
2006-03-14 00:48:37 -05:00
|
|
|
|
2006-03-26 15:01:50 -05:00
|
|
|
if config.needs_restart
|
2006-03-14 00:48:37 -05:00
|
|
|
if RUBY_PLATFORM !~ /mswin/
|
2006-03-26 15:01:50 -05:00
|
|
|
cmd = "ruby #{__FILE__} start #{original_args.join(' ')}"
|
|
|
|
config.log "Restarting with arguments: #{cmd}"
|
|
|
|
exec cmd
|
2006-03-14 00:48:37 -05:00
|
|
|
else
|
2006-03-26 15:01:50 -05:00
|
|
|
config.log "Win32 does not support restarts. Exiting."
|
2006-03-14 00:48:37 -05:00
|
|
|
end
|
2006-02-11 22:37:38 -05:00
|
|
|
end
|
2006-02-14 07:19:02 -05:00
|
|
|
end
|
2006-02-11 22:37:38 -05:00
|
|
|
end
|
|
|
|
|
2006-02-26 16:39:40 -05:00
|
|
|
def send_signal(signal, pid_file)
|
|
|
|
pid = open(pid_file).read.to_i
|
|
|
|
print "Sending #{signal} to Mongrel at PID #{pid}..."
|
|
|
|
begin
|
|
|
|
Process.kill(signal, pid)
|
|
|
|
rescue Errno::ESRCH
|
|
|
|
puts "Process does not exist. Not running."
|
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-02-26 16:39:40 -05:00
|
|
|
puts "Done."
|
|
|
|
end
|
2006-02-11 22:37:38 -05:00
|
|
|
|
2006-02-28 02:04:41 -05:00
|
|
|
|
2006-03-06 00:31:39 -05:00
|
|
|
class Stop < GemPlugin::Plugin "/commands"
|
2006-03-01 22:54:32 -05:00
|
|
|
include Mongrel::Command::Base
|
2006-02-11 22:37:38 -05:00
|
|
|
|
|
|
|
def configure
|
2006-02-26 16:39:40 -05:00
|
|
|
options [
|
2006-05-19 22:56:30 -04:00
|
|
|
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
|
|
|
|
['-f', '--force', "Force the shutdown.", :@force, false],
|
|
|
|
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"]
|
2006-02-11 22:37:38 -05:00
|
|
|
]
|
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-02-11 22:37:38 -05:00
|
|
|
def validate
|
2006-02-14 07:19:02 -05:00
|
|
|
@cwd = File.expand_path(@cwd)
|
|
|
|
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
|
|
|
|
|
|
|
|
@pid_file = File.join(@cwd,@pid_file)
|
2006-02-11 22:37:38 -05:00
|
|
|
valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?"
|
2006-02-14 07:19:02 -05:00
|
|
|
|
2006-02-11 22:37:38 -05:00
|
|
|
return @valid
|
|
|
|
end
|
|
|
|
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-02-11 22:37:38 -05:00
|
|
|
def run
|
2006-02-26 16:39:40 -05:00
|
|
|
if @force
|
|
|
|
send_signal("KILL", @pid_file)
|
|
|
|
else
|
|
|
|
send_signal("TERM", @pid_file)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-03-06 00:31:39 -05:00
|
|
|
class Restart < GemPlugin::Plugin "/commands"
|
2006-03-01 22:54:32 -05:00
|
|
|
include Mongrel::Command::Base
|
2006-02-26 16:39:40 -05:00
|
|
|
|
|
|
|
def configure
|
|
|
|
options [
|
2006-05-19 22:56:30 -04:00
|
|
|
['-c', '--chdir PATH', "Change to dir before starting (will be expanded)", :@cwd, Dir.pwd],
|
|
|
|
['-s', '--soft', "Do a soft restart rather than a process exit restart", :@soft, false],
|
|
|
|
['-P', '--pid FILE', "Where to write the PID", :@pid_file, "log/mongrel.pid"]
|
2006-02-26 16:39:40 -05:00
|
|
|
]
|
|
|
|
end
|
2006-05-19 22:56:30 -04:00
|
|
|
|
2006-02-26 16:39:40 -05:00
|
|
|
def validate
|
|
|
|
@cwd = File.expand_path(@cwd)
|
|
|
|
valid_dir? @cwd, "Invalid path to change to during daemon mode: #@cwd"
|
|
|
|
|
|
|
|
@pid_file = File.join(@cwd,@pid_file)
|
|
|
|
valid_exists? @pid_file, "PID file #@pid_file does not exist. Not running?"
|
|
|
|
|
|
|
|
return @valid
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def run
|
|
|
|
if @soft
|
|
|
|
send_signal("HUP", @pid_file)
|
|
|
|
else
|
|
|
|
send_signal("USR2", @pid_file)
|
2006-02-11 20:30:33 -05:00
|
|
|
end
|
|
|
|
end
|
2006-02-09 21:38:18 -05:00
|
|
|
end
|
2006-02-11 20:30:33 -05:00
|
|
|
|
2006-03-25 16:15:30 -05:00
|
|
|
|
2006-03-06 00:31:39 -05:00
|
|
|
GemPlugin::Manager.instance.load "mongrel" => GemPlugin::INCLUDE, "rails" => GemPlugin::EXCLUDE
|
|
|
|
|
2006-03-25 16:15:30 -05:00
|
|
|
|
2006-03-28 10:41:52 -05:00
|
|
|
if not Mongrel::Command::Registry.instance.run ARGV
|
2006-05-19 22:56:30 -04:00
|
|
|
exit 1
|
2006-03-28 10:41:52 -05:00
|
|
|
end
|
2006-03-25 16:15:30 -05:00
|
|
|
|
2006-03-26 15:01:50 -05:00
|
|
|
|
|
|
|
|