class Nginxtra::Config

The Nginxtra::Config class is the central class for configuring nginxtra. It provides the DSL for defining the compilation options of nginx and the config file contents.

Constants

FILENAME
NGINX_CONF_FILENAME
NGINX_PIDFILE_FILENAME

Attributes

last_config[RW]
loaded_config_path[R]

Retrieve the path to the config file that was loaded.

Public Class Methods

base_dir() click to toggle source

Retrieve the base dir of nginxtra stored files (located in ~/.nginxtra, unless overridden via the –basedir option).

# File lib/nginxtra/config.rb, line 230
def base_dir
  @base_dir ||= File.absolute_path File.expand_path("~/.nginxtra")
end
base_dir=(value) click to toggle source

Set the base directory (retrieved from base_dir). If this is not called, it will default to the ~/.nginxtra directory. This will do nothing if the value is nil.

# File lib/nginxtra/config.rb, line 224
def base_dir=(value)
  @base_dir = value if value
end
base_nginx_dir() click to toggle source

The base nginx dir versioned to the current version inside the base dir.

# File lib/nginxtra/config.rb, line 236
def base_nginx_dir
  File.join base_dir, "nginx-#{nginx_version}"
end
build_dir() click to toggle source

Retrieve the directory where nginx is built into.

# File lib/nginxtra/config.rb, line 246
def build_dir
  File.join base_nginx_dir, "build"
end
config_dir() click to toggle source

The path to the config directory where nginx config files are stored (including nginx.conf).

# File lib/nginxtra/config.rb, line 263
def config_dir
  File.join base_dir, "conf"
end
gem_dir() click to toggle source

Retrieve the base dir of nginxtra (located just above lib, probably in your gems/nginxtra-xxx directory).

# File lib/nginxtra/config.rb, line 217
def gem_dir
  File.absolute_path File.expand_path("../../..", __FILE__)
end
gem_template_dir() click to toggle source

Retrieve the directory within the gem where templates are loaded from.

# File lib/nginxtra/config.rb, line 257
def gem_template_dir
  File.join gem_dir, "templates"
end
new(*_args) { |self| ... } click to toggle source
# File lib/nginxtra/config.rb, line 10
def initialize(*_args)
  @requires_root = false
  @compile_options = []
  @partial_paths = []
  @file_paths = []
  @files = {}
  Nginxtra::Config.last_config = self

  yield self if block_given?
end
nginx_config() click to toggle source

The full path to the nginx.conf file that is fed to nginx, based on nginxtra.conf.rb.

# File lib/nginxtra/config.rb, line 269
def nginx_config
  File.join config_dir, NGINX_CONF_FILENAME
end
nginx_executable() click to toggle source

Retrieve the full path to the nginx executable.

# File lib/nginxtra/config.rb, line 280
def nginx_executable
  File.join build_dir, "sbin/nginx"
end
nginx_pidfile() click to toggle source

The full path to the nginx pidfile that is used for running nginx.

# File lib/nginxtra/config.rb, line 275
def nginx_pidfile
  File.join base_dir, NGINX_PIDFILE_FILENAME
end
nginx_running?() click to toggle source

Determine if nginx is running, based on the pidfile.

# File lib/nginxtra/config.rb, line 303
def nginx_running?
  return false unless File.exist? nginx_pidfile
  pid = File.read(nginx_pidfile).strip
  Process.getpgid pid.to_i
  true
rescue Errno::ESRCH
  false
end
nginx_version() click to toggle source

The corresponding nginx version (based on the nginxtra version).

# File lib/nginxtra/config.rb, line 211
def nginx_version
  @nginx_version ||= version.split(".").take(3).join(".")
end
passenger_config_dir() click to toggle source
# File lib/nginxtra/config.rb, line 298
def passenger_config_dir
  @passenger_config_dir ||= `#{File.join passenger_spec.bin_dir, "passenger-config"} --nginx-addon-dir`.strip
end
passenger_spec() click to toggle source

Cache and retrieve the gemspec for passenger, if it exists. An InvalidConfig exception will be raised if passenger cannot be found.

# File lib/nginxtra/config.rb, line 292
def passenger_spec
  @passenger_spec ||= Gem::Specification.find_by_name("passenger").tap do |spec|
    raise Nginxtra::Error::MissingPassengerGem if spec.nil?
  end
end
path() click to toggle source

Obtain the config file path based on the current directory. This will be the path to the first nginxtra.conf.rb found starting at the current directory and working up until it is found or the filesystem boundary is hit (so /nginxtra.conf.rb is the last possible tested file). If none is found, nil is returned.

# File lib/nginxtra/config.rb, line 163
def path
  path = File.absolute_path "."
  config = File.join path, FILENAME
  return config if File.exist? config
  config = File.join path, "config", FILENAME
  return config if File.exist? config
  path = File.dirname path

  loop do
    config = File.join path, FILENAME
    return config if File.exist? config
    path = File.dirname path
    break if path == "/"
  end

  config = File.join path, FILENAME
  config if File.exist? config
end
require!(config_path = nil) click to toggle source

Determine where the config file is and require it. Return the resulting config loaded by the path. Nginxtra::Error::MissingConfig will be raised if the config file cannot be found.

# File lib/nginxtra/config.rb, line 189
def require!(config_path = nil)
  config_path = if config_path
                  File.absolute_path config_path
                else
                  path
                end

  raise Nginxtra::Error::MissingConfig, "Cannot find #{FILENAME} to configure nginxtra!" unless config_path
  raise Nginxtra::Error::MissingConfig, "Missing file #{config_path} to configure nginxtra!" unless File.exist?(config_path)
  require config_path
  raise Nginxtra::Error::NoConfigSpecified, config_path unless last_config
  @loaded_config_path = config_path
  last_config
end
ruby_path() click to toggle source

Retrieve the path to ruby.

# File lib/nginxtra/config.rb, line 285
def ruby_path
  `which ruby`.strip
end
src_dir() click to toggle source

Retrieve the directory where nginx source is located.

# File lib/nginxtra/config.rb, line 241
def src_dir
  File.join base_nginx_dir, "src"
end
template_dir() click to toggle source

Retrieve the directory where templates are loaded from.

# File lib/nginxtra/config.rb, line 251
def template_dir
  File.join base_dir, "templates"
end
version() click to toggle source

The current version of nginxtra.

# File lib/nginxtra/config.rb, line 205
def version
  Nginxtra::Version.to_s
end

Public Instance Methods

compile_option(opt) click to toggle source

Specify a compile time option for nginx. The leading “–” is optional and will be added if missing. The following options are not allowed and will cause an exception: –prefix, –sbin-path, –conf-path and –pid-path. The order the options are specified will be the order they are used when configuring nginx.

Example usage:

nginxtra.config do
  compile_option "--with-http_gzip_static_module"
  compile_option "--with-cc-opt=-Wno-error"
end
# File lib/nginxtra/config.rb, line 128
def compile_option(opt)
  opt = "--#{opt}" unless opt =~ /^--/
  raise Nginxtra::Error::InvalidCompilationOption, "prefix" if opt =~ /--prefix=/
  raise Nginxtra::Error::InvalidCompilationOption, "sbin-path" if opt =~ /--sbin-path=/
  raise Nginxtra::Error::InvalidCompilationOption, "conf-path" if opt =~ /--conf-path=/
  raise Nginxtra::Error::InvalidCompilationOption, "pid-path" if opt =~ /--pid-path=/
  @compile_options << opt
end
compile_options() click to toggle source

Obtain the compile options that have been configured.

# File lib/nginxtra/config.rb, line 112
def compile_options
  @compile_options.join " "
end
config(&block) click to toggle source

This method is used to configure nginx via nginxtra. Inside your nginxtra.conf.rb config file, you should use it like:

nginxtra.config do
  ...
end
# File lib/nginxtra/config.rb, line 74
def config(&block)
  instance_eval(&block)
  self
end
custom_files(path) click to toggle source

Define an additional directory where file templates can be found. The passed in path should have file templates within it.

For example: custom_files “/my/path”

/my/path/nginx.conf.rb
# File lib/nginxtra/config.rb, line 39
def custom_files(path)
  @file_paths << path
end
custom_partials(path) click to toggle source

Define an additional directory where partials can be found. The passed in path should have directories listed by configuration file, then partials within those.

For example: custom_partials “/my/path”

/my/path/nginx.conf/rails.rb
/my/path/nginx.conf/static.rb
# File lib/nginxtra/config.rb, line 29
def custom_partials(path)
  @partial_paths << path
end
file(filename, &block) click to toggle source

Define a new config file with the given filename and the block to define it with.

# File lib/nginxtra/config.rb, line 145
def file(filename, &block)
  @files[filename] = Nginxtra::Config::ConfigFile.new filename, self, &block
end
file_contents(filename) click to toggle source

Obtain the config file contents that will be used for nginx.conf.

# File lib/nginxtra/config.rb, line 139
def file_contents(filename)
  @files[filename].config_file_contents
end
file_paths() click to toggle source

Retrieve an array of directories where file templates can be contained. This includes the custom file directories added with the “custom_files” method, in the order they were added, then the standard override location, and finally the standard gem file templates.

# File lib/nginxtra/config.rb, line 61
def file_paths
  [].tap do |result|
    result.push(*@file_paths)
    result.push File.join(Nginxtra::Config.template_dir, "files")
    result.push File.join(Nginxtra::Config.gem_template_dir, "files")
  end
end
files() click to toggle source

Retrieve the files that have been defined.

# File lib/nginxtra/config.rb, line 150
def files
  @files.keys
end
partial_paths() click to toggle source

Retrieve an array of directories where partials can be contained. This includes the custom partial directories added with the “custom_partials” method, in the order they were added, then the standard override location, and finally the standard gem partials.

# File lib/nginxtra/config.rb, line 48
def partial_paths
  [].tap do |result|
    result.push(*@partial_paths)
    result.push File.join(Nginxtra::Config.template_dir, "partials")
    result.push File.join(Nginxtra::Config.gem_template_dir, "partials")
  end
end
require_passenger!() click to toggle source

Require passenger. This will include http_gzip_static_module, add a Wno-error compilation option, and add the passenger module to the proper passenger path.

# File lib/nginxtra/config.rb, line 105
def require_passenger!
  compile_option %(--with-http_gzip_static_module)
  compile_option %(--with-cc-opt=-Wno-error)
  compile_option %(--add-module="#{Nginxtra::Config.passenger_config_dir}")
end
require_root!() click to toggle source

Notify nginxtra that root access is needed to run the daemon commands. Sudo will automatically be used if the current user isn't root.

# File lib/nginxtra/config.rb, line 91
def require_root!
  @requires_root = true
end
require_root?() click to toggle source

Retrieve whether root is required to run the daemon. This will return true only if require_root! was invoked from the config file.

# File lib/nginxtra/config.rb, line 98
def require_root?
  @requires_root
end
simple_config(options = {}, &block) click to toggle source

Support simple configuration in a special block. This will allow wholesale configuration like for rails. It supports the :worker_processes and :worker_connections options, which will affect the resulting configuration.

# File lib/nginxtra/config.rb, line 83
def simple_config(options = {}, &block)
  SimpleConfig.new(self, options, &block).process!
  self
end