Class: Apache::Config
- Inherits:
-
Object
- Object
- Apache::Config
- Extended by:
- Directories, Logging, MPM, Master, Performance, Permissions, Rewrites, SSL
- Defined in:
- lib/apache/config.rb
Overview
The core class of Apache Config Generator.
Configuration is built by calling either build or build_if:
Ex: Build a config file regardless of current environment:
Apache::Config.build('my-config.conf') do
document_root '/my/document/root'
...
end
Ex: Build a config file only if you’re in the development environment:
Apache::Config.build_if('my-config.conf', :development) do
document_root '/my/document/root'
...
end
By default, methods called within the block are NerdCapsed to match the Apache config directive format:
document_root #=> DocumentRoot
#=> Options
allow_override #=> AllowOverride
Parameters passed into the methods are quoted if they’re Strings or Integers, and not quoted if they’re Symbols:
document_root '/my/document/root' #=> DocumentRoot "/my/document/root"
document_root '/my/document/root'.to_sym #=> DocumentRoot /my/document/root
accept_path_info :off #=> AcceptPathInfo off
Suffixing the method name with an exclamation point turns off quoting for all parameters:
document_root! '/my/document/root' #=> DocumentRoot /my/document/root
Block-level directives work the same as the top-level build method:
directory '/my/site' do
allow_from_all
satisfy :any
end
Directives that require a regular expression take a Regexp:
location_match %r{^/my/site} do
set_env 'this_is_my_site', 'yes'
end
Class Attribute Summary collapse
-
.line_indent ⇒ Object
Returns the value of attribute line_indent.
-
.rotate_logs_path ⇒ Object
Returns the value of attribute rotate_logs_path.
Class Method Summary collapse
-
.+(other) ⇒ Object
Append the array to the current config.
-
.<<(string) ⇒ Object
Add the string to the current config.
- .blank_line! ⇒ Object
-
.block_methods(*methods) ⇒ Object
Handle creating block methods.
-
.blockify(tag_name, name, &block) ⇒ Object
Handle the blockification of a provided block.
- .build(target, &block) ⇒ Object
-
.build_and_return(&block) ⇒ Object
Build the provided configuration.
- .build_and_return_if(*conditions, &block) ⇒ Object
-
.build_if(target, *conditions, &block) ⇒ Object
Build the provided configuration only if the current environment matches one of the conditions.
-
.directory(dir, &block) ⇒ Object
Create a directory block, checking to see if the source directory exists.
- .environment_ok?(*environments) ⇒ Boolean
-
.files_match(regexp, &block) ⇒ Object
Create a FilesMatch block with the provied Regexp: files_match %rhtml$ do #=> FilesMatch “.html$”>.
-
.if_environment(*env, &block) ⇒ Object
Only execute the provided block if APACHE_ENV matches one of the provided enviroment symbols: if_environment(:production) do.
-
.if_module(mod, &block) ⇒ Object
If the given module is loaded, process the directives within.
-
.indent(string_or_array) ⇒ Object
Indent the string by the current @line_indent level.
-
.location_match(regexp, &block) ⇒ Object
Create a LocationMatch block with the provided Regexp: location_match %rhref="a-z0-9">my/location/.html do #=> <LocationMatch “^/my/location/.html”>.
-
.method_missing(method, *args) ⇒ Object
Handle options that aren’t specially handled.
-
.reset! ⇒ Object
Reset the current settings.
-
.rotatelogs(path, time) ⇒ Object
Build a string that invokes Apache’s rotatelogs command.
-
.to_a ⇒ Object
Get the config.
- .warn_msg(from, color = :red) ⇒ Object
Methods included from SSL
Methods included from MPM
Methods included from Rewrites
enable_rewrite_engine, r301, rewrite, rewrites
Methods included from Performance
Methods included from Logging
combined_log_format, common_log_format
Methods included from Directories
Methods included from Permissions
allow_from, allow_from_all, apache_require, basic_authentication, default_restrictive!, deny_from_all, ldap_authentication, no_htfiles!, order
Methods included from Master
add_type!, apache_alias, apache_include, comment, document_root, enable_gzip!, listen, modules, passenger, runner, script_alias, server_name, set_header, timeout
Class Attribute Details
.line_indent ⇒ Object
Returns the value of attribute line_indent.
58 59 60 |
# File 'lib/apache/config.rb', line 58 def line_indent @line_indent end |
.rotate_logs_path ⇒ Object
Returns the value of attribute rotate_logs_path.
58 59 60 |
# File 'lib/apache/config.rb', line 58 def rotate_logs_path @rotate_logs_path end |
Class Method Details
.+(other) ⇒ Object
Append the array to the current config
123 124 125 |
# File 'lib/apache/config.rb', line 123 def +(other) @config += other end |
.<<(string) ⇒ Object
Add the string to the current config
118 119 120 |
# File 'lib/apache/config.rb', line 118 def <<(string) @config << indent(string) end |
.blank_line! ⇒ Object
203 204 205 |
# File 'lib/apache/config.rb', line 203 def blank_line! self << "" end |
.block_methods(*methods) ⇒ Object
Handle creating block methods
Methods created this way are:
-
virtual_host
-
location
-
files
152 153 154 155 156 157 158 159 160 |
# File 'lib/apache/config.rb', line 152 def block_methods(*methods) methods.each do |method| self.class.class_eval <<-EOT def #{method}(*name, &block) blockify("#{method}".apachify, name, &block) end EOT end end |
.blockify(tag_name, name, &block) ⇒ Object
Handle the blockification of a provided block
195 196 197 198 199 200 201 |
# File 'lib/apache/config.rb', line 195 def blockify(tag_name, name, &block) self + [ '', "<#{[ tag_name, name.blockify ].compact * ' '}>" ] @line_indent += 1 self.instance_eval(&block) @line_indent -= 1 self + [ "</#{tag_name}>", '' ] end |
.build(target, &block) ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/apache/config.rb', line 92 def build(target, &block) config = build_and_return(&block) FileUtils.mkdir_p File.split(target).first File.open(target, 'w') { |file| file.puts [ "# Generated by apache-config-generator #{Time.now.to_s}", config ].flatten * "\n" } config end |
.build_and_return(&block) ⇒ Object
Build the provided configuration
75 76 77 78 79 80 81 |
# File 'lib/apache/config.rb', line 75 def build_and_return(&block) reset! self.instance_eval(&block) @config end |
.build_and_return_if(*conditions, &block) ⇒ Object
83 84 85 |
# File 'lib/apache/config.rb', line 83 def build_and_return_if(*conditions, &block) build_and_return(&block) if environment_ok?(*conditions) end |
.build_if(target, *conditions, &block) ⇒ Object
Build the provided configuration only if the current environment matches one of the conditions
70 71 72 |
# File 'lib/apache/config.rb', line 70 def build_if(target, *conditions, &block) build(target, &block) if environment_ok?(*conditions) end |
.directory(dir, &block) ⇒ Object
Create a directory block, checking to see if the source directory exists.
171 172 173 174 |
# File 'lib/apache/config.rb', line 171 def directory(dir, &block) directory? dir blockify('directory'.apachify, dir, &block) end |
.environment_ok?(*environments) ⇒ Boolean
87 88 89 90 |
# File 'lib/apache/config.rb', line 87 def environment_ok?(*environments) return true if APACHE_ENV == true environments.include?(APACHE_ENV) end |
.files_match(regexp, &block) ⇒ Object
Create a FilesMatch block with the provied Regexp:
files_match %r{\.html$} do #=> FilesMatch "\.html$">
184 185 186 |
# File 'lib/apache/config.rb', line 184 def files_match(regexp, &block) blockify('files_match'.apachify, regexp.source, &block) end |
.if_environment(*env, &block) ⇒ Object
Only execute the provided block if APACHE_ENV matches one of the provided enviroment symbols:
if_environment(:production) do
190 191 192 |
# File 'lib/apache/config.rb', line 190 def if_environment(*env, &block) self.instance_eval(&block) if env.include?(APACHE_ENV) end |
.if_module(mod, &block) ⇒ Object
If the given module is loaded, process the directives within.
The provided module name is converted into Apache module name format:
if_module(:php5) do #=> <IfModule mod_php5>
166 167 168 |
# File 'lib/apache/config.rb', line 166 def if_module(mod, &block) blockify('if_module'.apachify, "#{mod}_module".to_sym, &block) end |
.indent(string_or_array) ⇒ Object
Indent the string by the current @line_indent level
108 109 110 111 112 113 114 115 |
# File 'lib/apache/config.rb', line 108 def indent(string_or_array) case string_or_array when Array string_or_array.collect { |line| indent(line) } else " " * (@line_indent * 2) + string_or_array.to_s end end |
.location_match(regexp, &block) ⇒ Object
Create a LocationMatch block with the provided Regexp:
location_match %r{^/my/location/[a-z0-9]+\.html} do #=> <LocationMatch "^/my/location/[a-z0-9]+\.html">
178 179 180 |
# File 'lib/apache/config.rb', line 178 def location_match(regexp, &block) blockify('location_match'.apachify, regexp.source, &block) end |
.method_missing(method, *args) ⇒ Object
Handle options that aren’t specially handled
Method names are NerdCapsed and paramters are quoted, unless the method ends with !
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/apache/config.rb', line 135 def method_missing(method, *args) method_name = method.to_s if method_name[-1..-1] == "!" method = method_name[0..-2].to_sym else args.quoteize! end self << [ method.apachify, *args ].compact * ' ' end |
.reset! ⇒ Object
Reset the current settings
102 103 104 105 |
# File 'lib/apache/config.rb', line 102 def reset! @config = [] @line_indent = 0 end |
.rotatelogs(path, time) ⇒ Object
Build a string that invokes Apache’s rotatelogs command
208 209 210 211 212 213 214 215 216 |
# File 'lib/apache/config.rb', line 208 def rotatelogs(path, time) begin time = time.to_i rescue raise "Time should be an integer: #{path} #{time}" end "|#{@rotate_logs_path} #{path} #{time}" end |
.to_a ⇒ Object
Get the config
128 129 130 |
# File 'lib/apache/config.rb', line 128 def to_a @config end |
.warn_msg(from, color = :red) ⇒ Object
218 219 220 |
# File 'lib/apache/config.rb', line 218 def warn_msg(from, color = :red) "[warn::#{from}]".foreground(color) end |