more cleanups

This commit is contained in:
John Bintz 2010-05-06 10:40:45 -04:00
parent 209942c5a7
commit 53e750853d
5 changed files with 50 additions and 12 deletions

View File

@ -15,6 +15,10 @@ module Apache
include Apache::Performance include Apache::Performance
include Apache::Rewrites include Apache::Rewrites
def build_if(target, *conditions, &block)
build(target, &block) if conditions.include? APACHE_ENV
end
def build(target = nil, &block) def build(target = nil, &block)
reset! reset!
@ -22,7 +26,7 @@ module Apache
if target if target
FileUtils.mkdir_p File.split(target).first FileUtils.mkdir_p File.split(target).first
File.open(target, 'w') { |f| f.puts @config * "\n" } File.open(target, 'w') { |f| f.puts [ "# Generated by apache-config-generator #{Time.now.to_s}", @config ].flatten * "\n" }
end end
@config @config
@ -59,7 +63,7 @@ module Apache
def apachify(name) def apachify(name)
case name case name
when String, Symbol when String, Symbol
name.to_s.split("_").collect(&:capitalize).join.gsub('Ssl', 'SSL').gsub('Cgi', 'CGI') name.to_s.split("_").collect(&:capitalize).join.gsub('Ssl', 'SSL').gsub('Cgi', 'CGI').gsub('Ldap', 'LDAP').gsub('Url', 'URL')
when Array when Array
name.collect { |n| apachify(n) } name.collect { |n| apachify(n) }
end end
@ -96,6 +100,10 @@ module Apache
blockify(apachify('directory'), dir, &block) blockify(apachify('directory'), dir, &block)
end end
def location_match(regexp, &block)
blockify(apachify('location_match'), regexp.source, &block)
end
def if_environment(env, &block) def if_environment(env, &block)
self.instance_eval(&block) if APACHE_ENV == env self.instance_eval(&block) if APACHE_ENV == env
end end
@ -115,12 +123,13 @@ module Apache
start = start.join(' ') start = start.join(' ')
self << "" if (@line_indent == 0) self << ""
self << "<#{start}>" self << "<#{start}>"
@line_indent += 1 @line_indent += 1
self.instance_eval(&block) self.instance_eval(&block)
@line_indent -= 1 @line_indent -= 1
self << "</#{tag_name}>" self << "</#{tag_name}>"
self << ""
end end
def apache_include(*opts) def apache_include(*opts)

View File

@ -34,5 +34,29 @@ module Apache
end end
alias :order! :order alias :order! :order
def basic_authentication(zone, users_file, requires)
auth_type :basic
auth_name zone
auth_user_file users_file
requires.each do |type, values|
apache_require type, *values
end
end
def ldap_authentication(zone, url, requires)
auth_type :basic
auth_name zone
auth_basic_provider :ldap
authz_ldap_authoritative :on
auth_ldap_url url
requires.each do |type, values|
apache_require type, *values
end
end
def apache_require(*opts)
self << "Require #{opts * " "}"
end
end end
end end

View File

@ -8,17 +8,17 @@ namespace :apache do
task :create, :environment do |t, args| task :create, :environment do |t, args|
APACHE_ENV = (args[:environment] || 'production').to_sym APACHE_ENV = (args[:environment] || 'production').to_sym
CONFIG = YAML.load_file('config.yml') CONFIG = Hash[YAML.load_file('config.yml').collect { |k,v| [ k.to_sym, v ] }]
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'] 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]
Dir[File.join(CONFIG['source_path'], '**', '*.rb')].each do |file| Dir[File.join(CONFIG[:source_path], '**', '*.rb')].each do |file|
puts file puts file
require file require file
end end

View File

@ -1,6 +1,7 @@
module Apache module Apache
module Rewrites module Rewrites
def enable_rewrite_engine(options) def enable_rewrite_engine(options)
self << ''
rewrite_engine! :on rewrite_engine! :on
options.each do |option, value| options.each do |option, value|
case option case option
@ -8,10 +9,12 @@ module Apache
rewrite_log_level! value rewrite_log_level! value
end end
end end
self << ''
end end
def rewrites(&block) def rewrites(&block)
self + indent(RewriteManager.build(&block)) self + indent(RewriteManager.build(&block))
self << ''
end end
def r301(*opt) def r301(*opt)

View File

@ -31,6 +31,8 @@ describe Apache::Config, "should handle the basics of Apache config" do
Apache::Config.apachify("test_full_name").should == "TestFullName" Apache::Config.apachify("test_full_name").should == "TestFullName"
Apache::Config.apachify("ssl_option").should == "SSLOption" Apache::Config.apachify("ssl_option").should == "SSLOption"
Apache::Config.apachify("exec_cgi").should == "ExecCGI" Apache::Config.apachify("exec_cgi").should == "ExecCGI"
Apache::Config.apachify("authz_ldap_authoritative").should == "AuthzLDAPAuthoritative"
Apache::Config.apachify("authz_ldap_url").should == "AuthzLDAPURL"
end end
it "should quoteize properly" do it "should quoteize properly" do
@ -41,13 +43,13 @@ describe Apache::Config, "should handle the basics of Apache config" do
it "should blockify a block" do it "should blockify a block" do
Apache::Config.blockify("Tag", [ 'part', 'part2' ]) do Apache::Config.blockify("Tag", [ 'part', 'part2' ]) do
something "goes here" 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.reset!
Apache::Config.blockify("Tag", 'part') do Apache::Config.blockify("Tag", 'part') do
something "goes here" something "goes here"
end.should == ['', '<Tag "part">', ' Something "goes here"', '</Tag>'] end.should == ['', '<Tag "part">', ' Something "goes here"', '</Tag>', '']
end end
it "should handle a build" do it "should handle a build" do