diff --git a/lib/apache/config.rb b/lib/apache/config.rb index ecc8183..9497e95 100644 --- a/lib/apache/config.rb +++ b/lib/apache/config.rb @@ -15,6 +15,10 @@ module Apache include Apache::Performance include Apache::Rewrites + def build_if(target, *conditions, &block) + build(target, &block) if conditions.include? APACHE_ENV + end + def build(target = nil, &block) reset! @@ -22,7 +26,7 @@ module Apache if target 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 @config @@ -59,7 +63,7 @@ module Apache def apachify(name) case name 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 name.collect { |n| apachify(n) } end @@ -96,6 +100,10 @@ module Apache blockify(apachify('directory'), dir, &block) end + def location_match(regexp, &block) + blockify(apachify('location_match'), regexp.source, &block) + end + def if_environment(env, &block) self.instance_eval(&block) if APACHE_ENV == env end @@ -115,12 +123,13 @@ module Apache start = start.join(' ') - self << "" if (@line_indent == 0) + self << "" self << "<#{start}>" @line_indent += 1 self.instance_eval(&block) @line_indent -= 1 self << "" + self << "" end def apache_include(*opts) diff --git a/lib/apache/permissions.rb b/lib/apache/permissions.rb index 19e7d05..fa404ef 100644 --- a/lib/apache/permissions.rb +++ b/lib/apache/permissions.rb @@ -34,5 +34,29 @@ module Apache end 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 diff --git a/lib/apache/rake/create.rb b/lib/apache/rake/create.rb index d1ea5d4..293132a 100644 --- a/lib/apache/rake/create.rb +++ b/lib/apache/rake/create.rb @@ -8,17 +8,17 @@ namespace :apache do task :create, :environment do |t, args| 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['dest_path'] = File.expand_path(CONFIG['destination']) + CONFIG[:source_path] = File.expand_path(CONFIG[:source]) + 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'] - Dir.chdir CONFIG['dest_path'] + FileUtils.mkdir_p 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 require file end diff --git a/lib/apache/rewrites.rb b/lib/apache/rewrites.rb index 69f2953..0b3b652 100644 --- a/lib/apache/rewrites.rb +++ b/lib/apache/rewrites.rb @@ -1,6 +1,7 @@ module Apache module Rewrites def enable_rewrite_engine(options) + self << '' rewrite_engine! :on options.each do |option, value| case option @@ -8,10 +9,12 @@ module Apache rewrite_log_level! value end end + self << '' end def rewrites(&block) self + indent(RewriteManager.build(&block)) + self << '' end def r301(*opt) diff --git a/spec/config_spec.rb b/spec/config_spec.rb index b889da7..efa9e83 100644 --- a/spec/config_spec.rb +++ b/spec/config_spec.rb @@ -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("ssl_option").should == "SSLOption" Apache::Config.apachify("exec_cgi").should == "ExecCGI" + Apache::Config.apachify("authz_ldap_authoritative").should == "AuthzLDAPAuthoritative" + Apache::Config.apachify("authz_ldap_url").should == "AuthzLDAPURL" end 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 Apache::Config.blockify("Tag", [ 'part', 'part2' ]) do something "goes here" - end.should == ['', '', ' Something "goes here"', ''] + end.should == ['', '', ' Something "goes here"', '', ''] Apache::Config.reset! Apache::Config.blockify("Tag", 'part') do something "goes here" - end.should == ['', '', ' Something "goes here"', ''] + end.should == ['', '', ' Something "goes here"', '', ''] end it "should handle a build" do