Class: Apache::Config

Inherits:
Object
  • Object
show all
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 #=> 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

Class Method Summary collapse

Methods included from SSL

enable_ssl_engine

Methods included from MPM

prefork_config

Methods included from Rewrites

enable_rewrite_engine, r301, rewrite, rewrites

Methods included from Performance

activate_keepalive

Methods included from Logging

combined_log_format, common_log_format

Methods included from Directories

index_options, options

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_indentObject

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_pathObject

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



145
146
147
# File 'lib/apache/config.rb', line 145

def +(other)
  @config += other
end

.<<(string) ⇒ Object

Add the string to the current config



140
141
142
# File 'lib/apache/config.rb', line 140

def <<(string)
  @config << indent(string)
end

.blank_line!Object



225
226
227
# File 'lib/apache/config.rb', line 225

def blank_line!
  self << ""
end

.block_methods(*methods) ⇒ Object

Handle creating block methods

Methods created this way are:

  • virtual_host

  • location

  • files



174
175
176
177
178
179
180
181
182
# File 'lib/apache/config.rb', line 174

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



217
218
219
220
221
222
223
# File 'lib/apache/config.rb', line 217

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
100
101
102
# File 'lib/apache/config.rb', line 92

def build(target, &block)
  config = build_and_return(&block)

  if !disabled?
    FileUtils.mkdir_p File.split(target).first
    File.open(target, 'w') { |file| file.puts generate_config_file(config) * "\n" }
    @was_written = true
  end

  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.



193
194
195
196
# File 'lib/apache/config.rb', line 193

def directory(dir, &block)
  directory? dir
  blockify('directory'.apachify, dir, &block)
end

.disable!Object

If included in a configuration, will not generate the config file in the Rake task



105
106
107
# File 'lib/apache/config.rb', line 105

def disable!
  @is_disabled = true
end

.disabled?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/apache/config.rb', line 109

def disabled?
  @is_disabled
end

.environment_ok?(*environments) ⇒ Boolean

Returns:

  • (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$">


206
207
208
# File 'lib/apache/config.rb', line 206

def files_match(regexp, &block)
  blockify('files_match'.apachify, regexp.source, &block)
end

.generate_config_file(config) ⇒ Object



117
118
119
# File 'lib/apache/config.rb', line 117

def generate_config_file(config)
  [ "# Generated by apache-config-generator #{Time.now.to_s}", config ].flatten
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


212
213
214
# File 'lib/apache/config.rb', line 212

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>


188
189
190
# File 'lib/apache/config.rb', line 188

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



130
131
132
133
134
135
136
137
# File 'lib/apache/config.rb', line 130

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">


200
201
202
# File 'lib/apache/config.rb', line 200

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 !



157
158
159
160
161
162
163
164
165
166
# File 'lib/apache/config.rb', line 157

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



122
123
124
125
126
127
# File 'lib/apache/config.rb', line 122

def reset!
  @config = []
  @line_indent = 0
  @is_disabled = false
  @was_written = false
end

.rotatelogs(path, time) ⇒ Object

Build a string that invokes Apache’s rotatelogs command



230
231
232
233
234
235
236
237
238
# File 'lib/apache/config.rb', line 230

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_aObject

Get the config



150
151
152
# File 'lib/apache/config.rb', line 150

def to_a
  @config
end

.warn_msg(from, color = :red) ⇒ Object



240
241
242
# File 'lib/apache/config.rb', line 240

def warn_msg(from, color = :red)
  "[warn::#{from}]".foreground(color)
end

.written?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/apache/config.rb', line 113

def written?
  @was_written
end