a bunch more real-world work

This commit is contained in:
John Bintz 2010-05-05 12:25:07 -04:00
parent d19c5d6934
commit 2c933b7837
9 changed files with 227 additions and 12 deletions

View File

@ -1,22 +1,29 @@
require 'fileutils'
Dir[File.join(File.dirname(__FILE__), '*.rb')].each { |f| require f } Dir[File.join(File.dirname(__FILE__), '*.rb')].each { |f| require f }
module Apache module Apache
class Config class Config
class << self class << self
attr_accessor :line_indent, :config attr_accessor :line_indent, :config, :rotate_logs_path
include Apache::Master include Apache::Master
include Apache::Quoteize include Apache::Quoteize
include Apache::Permissions include Apache::Permissions
include Apache::Directories include Apache::Directories
include Apache::Logging include Apache::Logging
include Apache::Performance
include Apache::Rewrites
def build(target = nil, &block) def build(target = nil, &block)
reset! reset!
self.instance_eval(&block) self.instance_eval(&block)
File.open(target, 'w') { |f| f.puts @config * "\n" } if target if target
FileUtils.mkdir_p File.split(target).first
File.open(target, 'w') { |f| f.puts @config * "\n" }
end
@config @config
end end
@ -28,8 +35,13 @@ module Apache
end end
# Indent the string by the current @line_indent level # Indent the string by the current @line_indent level
def indent(string) def indent(string_or_array)
" " * (@line_indent * 2) + string case string_or_array
when Array
string_or_array.collect { |s| indent(s) }
else
" " * (@line_indent * 2) + string_or_array.to_s
end
end end
# Add the string to the current config # Add the string to the current config
@ -37,11 +49,20 @@ module Apache
@config << indent(string) @config << indent(string)
end end
def +(other)
@config += other
end
# Apachify a string # Apachify a string
# #
# Split the provided name on underscores and capitalize the individual parts # Split the provided name on underscores and capitalize the individual parts
def apachify(name) def apachify(name)
name.to_s.split("_").collect(&:capitalize).join.gsub('Ssl', 'SSL') case name
when String, Symbol
name.to_s.split("_").collect(&:capitalize).join.gsub('Ssl', 'SSL')
when Array
name.collect { |n| apachify(n) }
end
end end
# Handle options that aren't specially handled # Handle options that aren't specially handled
@ -75,6 +96,10 @@ module Apache
blockify(apachify('directory'), dir, &block) blockify(apachify('directory'), dir, &block)
end end
def if_environment(env, &block)
self.instance_eval(&block) if APACHE_ENV == env
end
# Handle the blockification of a provided block # Handle the blockification of a provided block
def blockify(tag_name, name, &block) def blockify(tag_name, name, &block)
start = [ tag_name ] start = [ tag_name ]
@ -102,6 +127,10 @@ module Apache
self << "Include #{opts * " "}" self << "Include #{opts * " "}"
end end
def rotatelogs(path, time)
"|#{@rotate_logs_path} #{path} #{time}"
end
private private
def writable?(path) def writable?(path)
if !File.directory? File.split(path).first if !File.directory? File.split(path).first
@ -117,6 +146,6 @@ module Apache
end end
block_methods :virtual_host, :files_match block_methods :virtual_host, :files_match, :location
end end
end end

View File

@ -1,8 +1,11 @@
module Apache module Apache
module Directories module Directories
def options(*opt) def options(*opt)
opt = opt.collect { |o| apachify(o) } self << "Options #{apachify(opt) * " "}"
self << "Options #{opt * " "}" end
def index_options(*opt)
self << "IndexOptions #{apachify(opt) * " "}"
end end
end end
end end

View File

@ -1,13 +1,23 @@
module Apache module Apache
module Logging module Logging
def error_log(*opts) def error_log(*opts)
writable? opts.first handle_log 'ErrorLog', opts.first, opts.first, opts[1..-1]
self << "ErrorLog #{opts * " "}"
end end
def custom_log(*opts) def custom_log(*opts)
writable? opts.first handle_log 'CustomLog', opts.first, opts.first, opts[1..-1]
self << "CustomLog #{opts * " "}" end
def rotate_custom_log(*opts)
handle_log 'CustomLog', opts.first, quoteize(rotatelogs(*opts[0..1])), opts[2..-1]
end
def rotate_error_log(*opts)
handle_log 'ErrorLog', opts.first, quoteize(rotatelogs(*opts[0..1])), opts[2..-1]
end
def rotate_script_log(*opts)
handle_log 'ScriptLog', opts.first, quoteize(rotatelogs(*opts[0..1])), opts[2..-1]
end end
def combined_log_format(name = 'combined') def combined_log_format(name = 'combined')
@ -17,5 +27,11 @@ module Apache
def common_log_format(name = 'common') def common_log_format(name = 'common')
log_format '%h %l %u %t \"%r\" %>s %b', name.to_sym log_format '%h %l %u %t \"%r\" %>s %b', name.to_sym
end end
private
def handle_log(tag, path, real_path, *opts)
writable? path
self << "#{tag} #{[real_path, opts].flatten * " "}"
end
end end
end end

View File

@ -23,5 +23,28 @@ module Apache
browser_match! '\bMSIE', '!no-gzip', '!gzip-only-text/html' browser_match! '\bMSIE', '!no-gzip', '!gzip-only-text/html'
end end
end end
def timeout(t)
self << "Timeout #{t}"
end
def comment(c)
out = [ '' ]
case c
when String
out += c.split("\n")
when Array
out = c
end
out << ''
self + out.collect { |line| "# #{line.strip}" }
end
def script_alias(uri, path)
directory? path
self << %{ScriptAlias "#{uri}" "#{path}"}
end
alias :script_alias! :script_alias
end end
end end

34
lib/apache/mpm_prefork.rb Normal file
View File

@ -0,0 +1,34 @@
module Apache
module MPM
class Prefork
class << self
def build(&block)
@config = ['', '# Prefork config', '']
self.instance_eval(&block)
@config
end
def method_missing(method, *opts)
if which = {
:start => 'StartServers',
:spares => [ 'MinSpareServers', 'MaxSpareServers' ],
:limit => 'ServerLimit',
:clients => 'MaxClients',
:max_requests => 'MaxRequestsPerChild'
}[method]
case which
when String
@config << "#{which} #{opts * " "}"
when Array
which.each do |w|
@config << "#{w} #{opts.shift}"
end
end
end
end
end
end
end
end

19
lib/apache/performance.rb Normal file
View File

@ -0,0 +1,19 @@
module Apache
module Performance
def activate_keepalive(options)
self << "KeepAlive On"
options.each do |option, value|
case option
when :requests
self << "MaxKeepAliveRequests #{value}"
when :timeout
self << "KeepAliveTimeout #{value}"
end
end
end
def prefork_config(&block)
self + Apache::MPM::Prefork.build(&block)
end
end
end

View File

@ -10,6 +10,10 @@ module Apache
allow :from_all allow :from_all
end end
def allow_from(where)
allow "from_#{where}".to_sym
end
def order(*args) def order(*args)
self << "Order #{args * ','}" self << "Order #{args * ','}"
end end

View File

@ -13,6 +13,8 @@ namespace :apache do
CONFIG['source_path'] = File.expand_path(CONFIG['source']) CONFIG['source_path'] = File.expand_path(CONFIG['source'])
CONFIG['dest_path'] = File.expand_path(CONFIG['destination']) CONFIG['dest_path'] = File.expand_path(CONFIG['destination'])
Apache::Config.rotate_logs_path = CONFIG['rotate_logs_path']
FileUtils.mkdir_p CONFIG['dest_path'] FileUtils.mkdir_p CONFIG['dest_path']
Dir.chdir CONFIG['dest_path'] Dir.chdir CONFIG['dest_path']

85
lib/apache/rewrites.rb Normal file
View File

@ -0,0 +1,85 @@
module Apache
module Rewrites
def enable_rewrite_engine(options)
rewrite_engine! :on
options.each do |option, value|
case option
when :log_level
rewrite_log_level! value
end
end
end
def rewrites(&block)
self + indent(RewriteManager.build(&block))
end
end
class RewriteManager
class << self
attr_accessor :rewrites
def build(&block)
@rewrites = []
self.instance_eval(&block)
@rewrites
end
def rewrite(*opts)
@rewrite = RewriteRule.new
@rewrite.rule(*opts)
@rewrites << @rewrite
end
def rewrite_test(from, to, opts = {})
orig_from = from.dup
@rewrites.each do |r|
from = r.test(from, opts)
end
if from != to
puts "[warn] #{orig_from} >> #{to} failed!"
puts "[warn] Result: #{from}"
end
end
end
end
class RewriteRule
def initialize
@from = nil
@to = nil
@options = nil
@conditions = []
end
def rule(from, to, *opts)
@from = from
@to = to
@options = opts
end
def test(from, opts)
from = from.gsub(@from, @to.gsub(/\$([0-9])/) { |m| '\\' + $1 })
opts.each do |opt, value|
from = from.gsub('%{' + opt.to_s.upcase + '}', value)
end
from
end
def to_s
output = []
@conditions.each do |condition|
end
output << "RewriteRule #{[@from.source, @to, @options].flatten * " "}"
output * "\n"
end
end
end