1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00

Late night hackery. Moving code for checking users and groups to common place, letting people who set allow_concurrency shoot themselves in the foot.

git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@225 19e92222-5c0b-0410-8929-a290d50e31e9
This commit is contained in:
zedshaw 2006-06-05 08:54:06 +00:00
parent 05a11e01ba
commit fbf042877d
4 changed files with 55 additions and 49 deletions

View file

@ -56,31 +56,14 @@ class Start < GemPlugin::Plugin "/commands"
valid_dir? File.dirname(@generate), "Problem accessing directory to #@generate" if @generate valid_dir? File.dirname(@generate), "Problem accessing directory to #@generate" if @generate
valid_user? @user if @user valid_user? @user if @user
valid_group? @group if @group valid_group? @group if @group
if ActionController::Base.allow_concurrency
STDERR.puts "[RAILS] allow_concurrency is true. Wow, you're very brave."
end
return @valid return @valid
end end
def valid_user?(user)
valid?(Process.uid == 0, "You must be root to change the user.")
valid?(@group, "You must also specify a group.")
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.")
valid?(@user, "You must also specify a user.")
begin
Etc.getgrnam(group)
rescue
failure "Group does not exist: #{group}"
@valid = false
end
end
def run def run

View file

@ -6,4 +6,4 @@ directoryName: Apache
h1. Apache Best Practice Deployment h1. Apache Best Practice Deployment
Coming soon...

View file

@ -29,12 +29,12 @@ module Mongrel
module Command module Command
BANNER = "Usage: mongrel_rails <command> [options]" BANNER = "Usage: mongrel_rails <command> [options]"
# A Command pattern implementation used to create the set of command available to the user # A Command pattern implementation used to create the set of command available to the user
# from Mongrel. The script uses objects which implement this interface to do the # from Mongrel. The script uses objects which implement this interface to do the
# user's bidding. # user's bidding.
module Base module Base
attr_reader :valid, :done_validating, :original_args attr_reader :valid, :done_validating, :original_args
# Called by the implemented command to set the options for that command. # Called by the implemented command to set the options for that command.
@ -68,7 +68,7 @@ module Mongrel
@done_validating = true @done_validating = true
puts @opt puts @opt
end end
# I need to add my own -v definition to prevent the -v from exiting by default as well. # I need to add my own -v definition to prevent the -v from exiting by default as well.
@opt.on_tail("--version", "Show version") do @opt.on_tail("--version", "Show version") do
@done_validating = true @done_validating = true
@ -76,7 +76,7 @@ module Mongrel
puts "Version #{Mongrel::Const::MONGREL_VERSION}" puts "Version #{Mongrel::Const::MONGREL_VERSION}"
end end
end end
@opt.parse! argv @opt.parse! argv
end end
@ -88,19 +88,19 @@ module Mongrel
def validate def validate
return @valid return @valid
end end
# Returns a help message. Defaults to OptionParser#help which should be good. # Returns a help message. Defaults to OptionParser#help which should be good.
def help def help
@opt.help @opt.help
end end
# Runs the command doing it's job. You should implement this otherwise it will # Runs the command doing it's job. You should implement this otherwise it will
# throw a NotImplementedError as a reminder. # throw a NotImplementedError as a reminder.
def run def run
raise NotImplementedError raise NotImplementedError
end end
# Validates the given expression is true and prints the message if not, exiting. # Validates the given expression is true and prints the message if not, exiting.
def valid?(exp, message) def valid?(exp, message)
if not @done_validating and (not exp) if not @done_validating and (not exp)
@ -114,29 +114,51 @@ module Mongrel
def valid_exists?(file, message) def valid_exists?(file, message)
valid?(file != nil && File.exist?(file), message) valid?(file != nil && File.exist?(file), message)
end end
# Validates that the file is a file and not a directory or something else. # Validates that the file is a file and not a directory or something else.
def valid_file?(file, message) def valid_file?(file, message)
valid?(file != nil && File.file?(file), message) valid?(file != nil && File.file?(file), message)
end end
# Validates that the given directory exists # Validates that the given directory exists
def valid_dir?(file, message) def valid_dir?(file, message)
valid?(file != nil && File.directory?(file), message) valid?(file != nil && File.directory?(file), message)
end end
def valid_user?(user)
valid?(Process.uid == 0, "You must be root to change the user.")
valid?(@group, "You must also specify a group.")
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.")
valid?(@user, "You must also specify a user.")
begin
Etc.getgrnam(group)
rescue
failure "Group does not exist: #{group}"
@valid = false
end
end
# Just a simple method to display failure until something better is developed. # Just a simple method to display failure until something better is developed.
def failure(message) def failure(message)
STDERR.puts "!!! #{message}" STDERR.puts "!!! #{message}"
end end
end end
# A Singleton class that manages all of the available commands # A Singleton class that manages all of the available commands
# and handles running them. # and handles running them.
class Registry class Registry
include Singleton include Singleton
# Builds a list of possible commands from the Command derivates list # Builds a list of possible commands from the Command derivates list
def commands def commands
pmgr = GemPlugin::Manager.instance pmgr = GemPlugin::Manager.instance
@ -147,27 +169,27 @@ module Mongrel
# Prints a list of available commands. # Prints a list of available commands.
def print_command_list def print_command_list
puts "#{Mongrel::Command::BANNER}\nAvailable commands are:\n\n" puts "#{Mongrel::Command::BANNER}\nAvailable commands are:\n\n"
self.commands.each do |name| self.commands.each do |name|
puts " - #{name[1 .. -1]}\n" puts " - #{name[1 .. -1]}\n"
end end
puts "\nEach command takes -h as an option to get help." puts "\nEach command takes -h as an option to get help."
end end
# Runs the args against the first argument as the command name. # Runs the args against the first argument as the command name.
# If it has any errors it returns a false, otherwise it return true. # If it has any errors it returns a false, otherwise it return true.
def run(args) def run(args)
# find the command # find the command
cmd_name = args.shift cmd_name = args.shift
if !cmd_name or cmd_name == "?" or cmd_name == "help" if !cmd_name or cmd_name == "?" or cmd_name == "help"
print_command_list print_command_list
return true return true
end end
# command exists, set it up and validate it # command exists, set it up and validate it
begin begin
command = GemPlugin::Manager.instance.create("/commands/#{cmd_name}", :argv => args) command = GemPlugin::Manager.instance.create("/commands/#{cmd_name}", :argv => args)
@ -176,7 +198,7 @@ module Mongrel
print_command_list print_command_list
return return
end end
# Normally the command is NOT valid right after being created # Normally the command is NOT valid right after being created
# but sometimes (like with -h or -v) there's no further processing # but sometimes (like with -h or -v) there's no further processing
# needed so the command is already valid so we can skip it. # needed so the command is already valid so we can skip it.
@ -190,7 +212,7 @@ module Mongrel
end end
return true return true
end end
end end
end end
end end

View file

@ -77,10 +77,9 @@ module Mongrel
cgi = Mongrel::CGIWrapper.new(request, response) cgi = Mongrel::CGIWrapper.new(request, response)
cgi.handler = self cgi.handler = self
@guard.synchronize do @guard.lock unless ActionController::Base.allow_concurrency
# Rails is not thread safe so must be run entirely within synchronize # Rails is not thread safe so must be run entirely within synchronize
Dispatcher.dispatch(cgi, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, response.body) Dispatcher.dispatch(cgi, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, response.body)
end
# This finalizes the output using the proper HttpResponse way # This finalizes the output using the proper HttpResponse way
cgi.out {""} cgi.out {""}
@ -89,6 +88,8 @@ module Mongrel
rescue Object => rails_error rescue Object => rails_error
STDERR.puts "Error calling Dispatcher.dispatch #{rails_error.inspect}" STDERR.puts "Error calling Dispatcher.dispatch #{rails_error.inspect}"
STDERR.puts rails_error.backtrace.join("\n") STDERR.puts rails_error.backtrace.join("\n")
ensure
@guard.unlock
end end
end end
end end