a bunch more real-world changes

This commit is contained in:
John Bintz 2010-05-05 10:44:20 -04:00
parent c28918d905
commit d19c5d6934
11 changed files with 136 additions and 13 deletions

View File

@ -8,11 +8,17 @@ module Apache
include Apache::Master
include Apache::Quoteize
include Apache::Permissions
include Apache::Directories
include Apache::Logging
def build(target = nil, &block)
reset!
self.instance_eval(&block)
File.open(target, 'w') { |f| f.puts @config * "\n" } if target
@config
end
# Reset the current settings
@ -35,7 +41,7 @@ module Apache
#
# Split the provided name on underscores and capitalize the individual parts
def apachify(name)
name.to_s.split("_").collect(&:capitalize).join
name.to_s.split("_").collect(&:capitalize).join.gsub('Ssl', 'SSL')
end
# Handle options that aren't specially handled
@ -60,28 +66,57 @@ module Apache
end
end
def if_module(mod, &block)
blockify(apachify('if_module'), "#{mod}_module".to_sym, &block)
end
def directory(dir, &block)
directory? dir
blockify(apachify('directory'), dir, &block)
end
# Handle the blockification of a provided block
def blockify(tag_name, name, &block)
start = [ tag_name ]
case name
when String
start << quoteize(name).first if name
start << quoteize(name).first
when Array
start << (quoteize(*name) * " ") if name
start << (quoteize(*name) * " ")
when Symbol
start << name.to_s
end
start = start.join(' ')
self << "" if (@indent == 0)
self << "" if (@line_indent == 0)
self << "<#{start}>"
@line_indent += 1
self.instance_eval(&block)
@line_indent -= 1
self << "</#{tag_name}>"
end
def apache_include(*opts)
self << "Include #{opts * " "}"
end
block_methods :if_module, :directory, :virtual_host
private
def writable?(path)
if !File.directory? File.split(path).first
puts "[warn] #{path} may not be writable!"
end
end
def directory?(path)
if !File.directory? path
puts "[warn] #{path} does not exist!"
end
end
end
block_methods :virtual_host, :files_match
end
end

8
lib/apache/directory.rb Normal file
View File

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

21
lib/apache/logging.rb Normal file
View File

@ -0,0 +1,21 @@
module Apache
module Logging
def error_log(*opts)
writable? opts.first
self << "ErrorLog #{opts * " "}"
end
def custom_log(*opts)
writable? opts.first
self << "CustomLog #{opts * " "}"
end
def combined_log_format(name = 'combined')
log_format '%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"', name.to_sym
end
def common_log_format(name = 'common')
log_format '%h %l %u %t \"%r\" %>s %b', name.to_sym
end
end
end

View File

@ -15,10 +15,13 @@ module Apache
passenger_ruby "#{ruby_root}/bin/ruby"
end
def order(*args)
self << "Order #{args * ','}"
def enable_gzip!
directory '/' do
add_output_filter_by_type! :DEFLATE, 'text/html', 'text/plain', 'text/css', 'text/javascript', 'application/javascript'
browser_match! '^Mozilla/4', "gzip-only-text/html"
browser_match! '^Mozilla/4\.0[678]', "no-gzip"
browser_match! '\bMSIE', '!no-gzip', '!gzip-only-text/html'
end
end
alias :order! :order
end
end

View File

@ -17,7 +17,7 @@ module Apache
modules.each { |m| self.send(m) }
self.instance_eval(&block) if block
@modules
[ '' ] + @modules + [ '' ]
end
def method_missing(method, *args)

View File

@ -9,5 +9,26 @@ module Apache
order :allow, :deny
allow :from_all
end
def order(*args)
self << "Order #{args * ','}"
end
def default_restrictive!
directory '/' do
options :follow_sym_links
allow_override :none
deny_from_all
end
end
def no_htfiles!
files_match '^\.ht' do
deny_from_all
satisfy :all
end
end
alias :order! :order
end
end

24
lib/apache/rake/create.rb Normal file
View File

@ -0,0 +1,24 @@
require 'fileutils'
require 'apache/config'
require 'apache/rake/create'
require 'yaml'
namespace :apache do
desc "Create all defined configs for the specified environment"
task :create, :environment do |t, args|
APACHE_ENV = (args[:environment] || 'production').to_sym
CONFIG = YAML.load_file('config.yml')
CONFIG['source_path'] = File.expand_path(CONFIG['source'])
CONFIG['dest_path'] = File.expand_path(CONFIG['destination'])
FileUtils.mkdir_p CONFIG['dest_path']
Dir.chdir CONFIG['dest_path']
Dir[File.join(CONFIG['source_path'], '**', '*.rb')].each do |file|
puts file
require file
end
end
end

0
lib/apache/ssl.rb Normal file
View File

View File

@ -29,6 +29,7 @@ describe Apache::Config, "should handle the basics of Apache config" do
it "should Apachify the name" do
Apache::Config.apachify("test").should == "Test"
Apache::Config.apachify("test_full_name").should == "TestFullName"
Apache::Config.apachify("ssl_option").should == "SSLOption"
end
it "should quoteize properly" do
@ -39,13 +40,13 @@ describe Apache::Config, "should handle the basics of Apache config" do
it "should blockify a block" do
Apache::Config.blockify("Tag", [ 'part', 'part2' ]) do
something "goes here"
end.should == ['<Tag "part" "part2">', ' Something "goes here"', '</Tag>']
end.should == ['', '<Tag "part" "part2">', ' Something "goes here"', '</Tag>']
Apache::Config.reset!
Apache::Config.blockify("Tag", 'part') do
something "goes here"
end.should == ['<Tag "part">', ' Something "goes here"', '</Tag>']
end.should == ['', '<Tag "part">', ' Something "goes here"', '</Tag>']
end
it "should handle a build" do

View File

@ -11,9 +11,17 @@ describe Apache::Master, "should provide basic helpers for configuration" do
end
Apache::Config.config.should == [
'',
'LoadModule "this_module" "modules/mod_this.so"',
'LoadModule "that_module" "modules/mod_that.so"',
'LoadModule "my_module" "is here"',
''
]
end
it "should set up the runner" do
Apache::Config.runner('test', 'test2')
Apache::Config.config.should == [ 'User test', 'Group test2' ]
end
end

View File

@ -15,9 +15,11 @@ describe Apache::Modules, "should build a list of modules" do
Apache::Modules.build(:this, :that) do
mine "my_path"
end.should == [
'',
'LoadModule "this_module" "modules/mod_this.so"',
'LoadModule "that_module" "modules/mod_that.so"',
'LoadModule "mine_module" "my_path"'
'LoadModule "mine_module" "my_path"',
''
]
end
end