From c116f2f47fcc64057505034bb3f14336a9af4bd5 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Tue, 27 Apr 2010 16:11:47 -0400 Subject: [PATCH] a bunch of it works --- .gitignore | 2 ++ Rakefile | 12 +++++++ lib/apache.rb | 3 ++ lib/apache/config.rb | 23 ++++++++++++ lib/apache/master.rb | 86 ++++++++++++++++++++++++++++++++++++++++++++ spec/master_spec.rb | 3 ++ test/config/httpd.rb | 27 ++++++++++++++ 7 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 Rakefile create mode 100644 lib/apache.rb create mode 100644 lib/apache/config.rb create mode 100644 lib/apache/master.rb create mode 100644 spec/master_spec.rb create mode 100644 test/config/httpd.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7b3b46b --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.loadpath +.project diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..953edfe --- /dev/null +++ b/Rakefile @@ -0,0 +1,12 @@ +$LOAD_PATH << 'lib' + +require 'apache' + +namespace :apache do + desc "Generate the configs" + task :generate, :path do |t, args| + Dir[File.join(args[:path], '**', '*.rb')].each do |file| + require file + end + end +end diff --git a/lib/apache.rb b/lib/apache.rb new file mode 100644 index 0000000..6ebbffa --- /dev/null +++ b/lib/apache.rb @@ -0,0 +1,3 @@ +Dir[File.join(File.dirname(__FILE__), '**', '*.rb')].each do |file| + require file +end diff --git a/lib/apache/config.rb b/lib/apache/config.rb new file mode 100644 index 0000000..07544ea --- /dev/null +++ b/lib/apache/config.rb @@ -0,0 +1,23 @@ +require 'apache/master' + +module Apache + class Config + class << self + include Apache::Master + include Apache::Quoteize + + def build(target, &block) + @config = [] + @indent = 0 + + self.instance_eval(&block) + + puts @config * "\n" + + #File.open(target, 'w') { |f| f.puts @config * "\n" } + end + end + + block_methods :if_module, :directory, :virtual_host + end +end diff --git a/lib/apache/master.rb b/lib/apache/master.rb new file mode 100644 index 0000000..13d9b87 --- /dev/null +++ b/lib/apache/master.rb @@ -0,0 +1,86 @@ +module Apache + module Quoteize + def quoteize(*args) + args.collect { |a| %{"#{a}"} } + end + end + + module Master + def modules(&block) + @config << Modules.build(&block) + end + + def indent(string) + " " * (@indent * 2) + string + end + + def block_methods(*methods) + methods.each do |method| + self.class.class_eval <<-EOT + def #{method}(name = nil, &block) + tag_name = apachify("#{method}") + start = [ tag_name ] + start << '"' + name + '"' if name + start = start.uniq.join(' ') + + @config << "" if (@indent == 0) + @config << indent("<" + start + ">") + @indent += 1 + self.instance_eval(&block) + @indent -= 1 + @config << indent("") + end + EOT + end + end + + def method_missing(method, *args) + @config << indent([ apachify(method), *quoteize(*args) ] * ' ') + end + + def runner(user, group = nil) + @config << indent("User #{user}") + @config << indent("Group #{group}") if group + end + + def deny_from_all + @config << indent("Order deny,allow") + @config << indent("Deny from all") + end + + def allow_from_all + @config << indent("Order allow,deny") + @config << indent("Allow from all") + end + + private + def apachify(name) + name = name.to_s + case name + when true + else + name.split("_").collect(&:capitalize).join + end + end + end + + class Modules + class << self + include Apache::Quoteize + + def build(&block) + @modules = [] + + self.instance_eval(&block) + + @modules + end + + def method_missing(method, *args) + module_name = "#{method}_module" + module_path = args[0] || "modules/mod_#{method}.so" + @modules << [ 'LoadModule', *quoteize(module_name, module_path) ] * " " + end + end + end +end diff --git a/spec/master_spec.rb b/spec/master_spec.rb new file mode 100644 index 0000000..066f5aa --- /dev/null +++ b/spec/master_spec.rb @@ -0,0 +1,3 @@ +if __FILE__ == $0 + # TODO Generated stub +end \ No newline at end of file diff --git a/test/config/httpd.rb b/test/config/httpd.rb new file mode 100644 index 0000000..d95a640 --- /dev/null +++ b/test/config/httpd.rb @@ -0,0 +1,27 @@ +Apache::Config.build('httpd.conf') do + server_root '/var/html/apache' + + modules do + expires + headers + end + + passenger_root '/var/html/ree/lib/ruby/gems/1.8/gems/passenger-2.2.11' + passenger_ruby '/var/html/ree/bin/ruby' + + if_module "!mpm_netware" do + runner 'webby', 'opoadm' + end + + directory '/' do + options 'FollowSymLinks' + allow_override 'None' + deny_from_all + end + + virtual_host '127.0.0.1:80' do + directory '/' do + allow_from_all + end + end +end