2010-05-05 16:25:07 +00:00
|
|
|
require 'fileutils'
|
|
|
|
|
2010-05-04 15:48:59 +00:00
|
|
|
Dir[File.join(File.dirname(__FILE__), '*.rb')].each { |f| require f }
|
2010-04-27 20:11:47 +00:00
|
|
|
|
|
|
|
module Apache
|
|
|
|
class Config
|
|
|
|
class << self
|
2010-05-07 15:26:40 +00:00
|
|
|
attr_accessor :line_indent, :rotate_logs_path
|
2010-05-04 15:48:59 +00:00
|
|
|
|
2010-04-27 20:11:47 +00:00
|
|
|
include Apache::Master
|
|
|
|
include Apache::Quoteize
|
2010-04-27 21:00:43 +00:00
|
|
|
include Apache::Permissions
|
2010-05-05 14:44:20 +00:00
|
|
|
include Apache::Directories
|
|
|
|
include Apache::Logging
|
2010-05-05 16:25:07 +00:00
|
|
|
include Apache::Performance
|
|
|
|
include Apache::Rewrites
|
2010-05-07 20:04:06 +00:00
|
|
|
include Apache::MPM
|
2010-04-27 20:11:47 +00:00
|
|
|
|
2010-05-07 15:26:40 +00:00
|
|
|
# Build the provided configuration only if the current environment matches one of the conditions
|
2010-05-06 14:40:45 +00:00
|
|
|
def build_if(target, *conditions, &block)
|
|
|
|
build(target, &block) if conditions.include? APACHE_ENV
|
|
|
|
end
|
|
|
|
|
2010-05-07 15:26:40 +00:00
|
|
|
# Build the provided configuration
|
2010-05-04 21:04:44 +00:00
|
|
|
def build(target = nil, &block)
|
2010-05-04 15:48:59 +00:00
|
|
|
reset!
|
2010-04-27 20:11:47 +00:00
|
|
|
|
|
|
|
self.instance_eval(&block)
|
2010-05-05 14:44:20 +00:00
|
|
|
|
2010-05-05 16:25:07 +00:00
|
|
|
if target
|
|
|
|
FileUtils.mkdir_p File.split(target).first
|
2010-05-06 14:40:45 +00:00
|
|
|
File.open(target, 'w') { |f| f.puts [ "# Generated by apache-config-generator #{Time.now.to_s}", @config ].flatten * "\n" }
|
2010-05-05 16:25:07 +00:00
|
|
|
end
|
2010-05-05 14:44:20 +00:00
|
|
|
|
|
|
|
@config
|
2010-05-04 15:48:59 +00:00
|
|
|
end
|
2010-04-27 20:11:47 +00:00
|
|
|
|
2010-05-04 15:48:59 +00:00
|
|
|
# Reset the current settings
|
|
|
|
def reset!
|
|
|
|
@config = []
|
|
|
|
@line_indent = 0
|
2010-04-27 20:11:47 +00:00
|
|
|
end
|
2010-04-28 20:19:03 +00:00
|
|
|
|
2010-05-04 15:48:59 +00:00
|
|
|
# Indent the string by the current @line_indent level
|
2010-05-05 16:25:07 +00:00
|
|
|
def indent(string_or_array)
|
|
|
|
case string_or_array
|
|
|
|
when Array
|
|
|
|
string_or_array.collect { |s| indent(s) }
|
|
|
|
else
|
|
|
|
" " * (@line_indent * 2) + string_or_array.to_s
|
|
|
|
end
|
2010-04-28 20:19:03 +00:00
|
|
|
end
|
|
|
|
|
2010-05-04 15:48:59 +00:00
|
|
|
# Add the string to the current config
|
2010-04-28 20:19:03 +00:00
|
|
|
def <<(string)
|
|
|
|
@config << indent(string)
|
|
|
|
end
|
2010-05-04 15:48:59 +00:00
|
|
|
|
2010-05-07 15:26:40 +00:00
|
|
|
# Append the array to the current config
|
2010-05-05 16:25:07 +00:00
|
|
|
def +(other)
|
|
|
|
@config += other
|
|
|
|
end
|
|
|
|
|
2010-05-07 15:26:40 +00:00
|
|
|
# Get the config
|
|
|
|
def to_a
|
|
|
|
@config
|
|
|
|
end
|
|
|
|
|
2010-05-04 15:48:59 +00:00
|
|
|
# Apachify a string
|
|
|
|
#
|
|
|
|
# Split the provided name on underscores and capitalize the individual parts
|
|
|
|
def apachify(name)
|
2010-05-05 16:25:07 +00:00
|
|
|
case name
|
|
|
|
when String, Symbol
|
2010-05-06 14:40:45 +00:00
|
|
|
name.to_s.split("_").collect(&:capitalize).join.gsub('Ssl', 'SSL').gsub('Cgi', 'CGI').gsub('Ldap', 'LDAP').gsub('Url', 'URL')
|
2010-05-05 16:25:07 +00:00
|
|
|
when Array
|
|
|
|
name.collect { |n| apachify(n) }
|
|
|
|
end
|
2010-05-04 15:48:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Handle options that aren't specially handled
|
|
|
|
def method_missing(method, *args)
|
|
|
|
if method.to_s[-1..-1] == "!"
|
|
|
|
method = method.to_s[0..-2].to_sym
|
|
|
|
else
|
|
|
|
args = *quoteize(*args)
|
|
|
|
end
|
|
|
|
|
2010-05-07 15:26:40 +00:00
|
|
|
self << [ apachify(method), *args ].compact * ' '
|
2010-05-04 15:48:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Handle creating block methods
|
|
|
|
def block_methods(*methods)
|
|
|
|
methods.each do |method|
|
|
|
|
self.class.class_eval <<-EOT
|
|
|
|
def #{method}(*name, &block)
|
|
|
|
blockify(apachify("#{method}"), name, &block)
|
|
|
|
end
|
|
|
|
EOT
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-05-05 14:44:20 +00:00
|
|
|
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
|
|
|
|
|
2010-05-06 14:40:45 +00:00
|
|
|
def location_match(regexp, &block)
|
|
|
|
blockify(apachify('location_match'), regexp.source, &block)
|
|
|
|
end
|
|
|
|
|
2010-05-05 16:25:07 +00:00
|
|
|
def if_environment(env, &block)
|
|
|
|
self.instance_eval(&block) if APACHE_ENV == env
|
|
|
|
end
|
|
|
|
|
2010-05-07 15:26:40 +00:00
|
|
|
def blockify_name(name)
|
2010-05-04 15:48:59 +00:00
|
|
|
case name
|
|
|
|
when String
|
2010-05-07 15:26:40 +00:00
|
|
|
quoteize(name).first
|
2010-05-04 15:48:59 +00:00
|
|
|
when Array
|
2010-05-07 15:26:40 +00:00
|
|
|
(quoteize(*name) * " ")
|
2010-05-05 14:44:20 +00:00
|
|
|
when Symbol
|
2010-05-07 15:26:40 +00:00
|
|
|
name.to_s
|
2010-05-04 15:48:59 +00:00
|
|
|
end
|
2010-05-07 15:26:40 +00:00
|
|
|
end
|
2010-05-04 15:48:59 +00:00
|
|
|
|
2010-05-07 15:26:40 +00:00
|
|
|
# Handle the blockification of a provided block
|
|
|
|
def blockify(tag_name, name, &block)
|
2010-05-06 14:40:45 +00:00
|
|
|
self << ""
|
2010-05-07 15:26:40 +00:00
|
|
|
self << "<#{[ tag_name, blockify_name(name) ].compact * ' '}>"
|
2010-05-04 15:48:59 +00:00
|
|
|
@line_indent += 1
|
|
|
|
self.instance_eval(&block)
|
|
|
|
@line_indent -= 1
|
|
|
|
self << "</#{tag_name}>"
|
2010-05-06 14:40:45 +00:00
|
|
|
self << ""
|
2010-05-04 15:48:59 +00:00
|
|
|
end
|
2010-05-05 14:44:20 +00:00
|
|
|
|
2010-05-05 16:25:07 +00:00
|
|
|
def rotatelogs(path, time)
|
|
|
|
"|#{@rotate_logs_path} #{path} #{time}"
|
|
|
|
end
|
|
|
|
|
2010-05-05 14:44:20 +00:00
|
|
|
private
|
|
|
|
def writable?(path)
|
2010-05-07 20:04:06 +00:00
|
|
|
puts "[warn] #{path} may not be writable!" if !File.directory? File.split(path).first
|
2010-05-05 14:44:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def directory?(path)
|
2010-05-07 20:04:06 +00:00
|
|
|
puts "[warn] #{path} does not exist!" if !File.directory? path
|
2010-05-05 14:44:20 +00:00
|
|
|
end
|
|
|
|
|
2010-05-07 20:04:06 +00:00
|
|
|
def exist?(path)
|
|
|
|
puts "[warn] #{path} does not exist!" if !File.exist?(path)
|
|
|
|
end
|
2010-04-27 20:11:47 +00:00
|
|
|
end
|
|
|
|
|
2010-05-07 02:26:12 +00:00
|
|
|
block_methods :virtual_host, :files_match, :location, :files
|
2010-04-27 20:11:47 +00:00
|
|
|
end
|
|
|
|
end
|