Class: Nginxtra::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/nginxtra/config.rb

Overview

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.

Defined Under Namespace

Classes: ConfigFile, Extension, Indentation, SimpleConfig

Constant Summary collapse

FILENAME =
"nginxtra.conf.rb".freeze
NGINX_CONF_FILENAME =
"nginx.conf".freeze
NGINX_PIDFILE_FILENAME =
".nginx_pid".freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*_args) {|_self| ... } ⇒ Config

Returns a new instance of Config.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
16
17
18
19
# 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

Class Attribute Details

.last_configObject

Returns the value of attribute last_config.



155
156
157
# File 'lib/nginxtra/config.rb', line 155

def last_config
  @last_config
end

.loaded_config_pathObject (readonly)

Retrieve the path to the config file that was loaded.



183
184
185
# File 'lib/nginxtra/config.rb', line 183

def loaded_config_path
  @loaded_config_path
end

Class Method Details

.base_dirObject

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



230
231
232
# File 'lib/nginxtra/config.rb', line 230

def base_dir
  @base_dir ||= File.absolute_path File.expand_path("~/.nginxtra")
end

.base_dir=(value) ⇒ Object

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.



224
225
226
# File 'lib/nginxtra/config.rb', line 224

def base_dir=(value)
  @base_dir = value if value
end

.base_nginx_dirObject

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



236
237
238
# File 'lib/nginxtra/config.rb', line 236

def base_nginx_dir
  File.join base_dir, "nginx-#{nginx_version}"
end

.build_dirObject

Retrieve the directory where nginx is built into.



246
247
248
# File 'lib/nginxtra/config.rb', line 246

def build_dir
  File.join base_nginx_dir, "build"
end

.config_dirObject

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



263
264
265
# File 'lib/nginxtra/config.rb', line 263

def config_dir
  File.join base_dir, "conf"
end

.gem_dirObject

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



217
218
219
# File 'lib/nginxtra/config.rb', line 217

def gem_dir
  File.absolute_path File.expand_path("../../..", __FILE__)
end

.gem_template_dirObject

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



257
258
259
# File 'lib/nginxtra/config.rb', line 257

def gem_template_dir
  File.join gem_dir, "templates"
end

.nginx_configObject

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



269
270
271
# File 'lib/nginxtra/config.rb', line 269

def nginx_config
  File.join config_dir, NGINX_CONF_FILENAME
end

.nginx_executableObject

Retrieve the full path to the nginx executable.



280
281
282
# File 'lib/nginxtra/config.rb', line 280

def nginx_executable
  File.join build_dir, "sbin/nginx"
end

.nginx_pidfileObject

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



275
276
277
# File 'lib/nginxtra/config.rb', line 275

def nginx_pidfile
  File.join base_dir, NGINX_PIDFILE_FILENAME
end

.nginx_running?Boolean

Determine if nginx is running, based on the pidfile.

Returns:

  • (Boolean)


303
304
305
306
307
308
309
310
# 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_versionObject

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



211
212
213
# File 'lib/nginxtra/config.rb', line 211

def nginx_version
  @nginx_version ||= version.split(".").take(3).join(".")
end

.passenger_config_dirObject



298
299
300
# 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_specObject

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



292
293
294
295
296
# 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

.pathObject

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.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# 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) ⇒ Object

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.



189
190
191
192
193
194
195
196
197
198
199
200
201
202
# 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_pathObject

Retrieve the path to ruby.



285
286
287
# File 'lib/nginxtra/config.rb', line 285

def ruby_path
  `which ruby`.strip
end

.src_dirObject

Retrieve the directory where nginx source is located.



241
242
243
# File 'lib/nginxtra/config.rb', line 241

def src_dir
  File.join base_nginx_dir, "src"
end

.template_dirObject

Retrieve the directory where templates are loaded from.



251
252
253
# File 'lib/nginxtra/config.rb', line 251

def template_dir
  File.join base_dir, "templates"
end

.versionObject

The current version of nginxtra.



205
206
207
# File 'lib/nginxtra/config.rb', line 205

def version
  Nginxtra::Version.to_s
end

Instance Method Details

#compile_option(opt) ⇒ Object

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


128
129
130
131
132
133
134
135
# 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_optionsObject

Obtain the compile options that have been configured.



112
113
114
# File 'lib/nginxtra/config.rb', line 112

def compile_options
  @compile_options.join " "
end

#config(&block) ⇒ Object

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


74
75
76
77
# File 'lib/nginxtra/config.rb', line 74

def config(&block)
  instance_eval(&block)
  self
end

#custom_files(path) ⇒ Object

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


39
40
41
# File 'lib/nginxtra/config.rb', line 39

def custom_files(path)
  @file_paths << path
end

#custom_partials(path) ⇒ Object

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


29
30
31
# File 'lib/nginxtra/config.rb', line 29

def custom_partials(path)
  @partial_paths << path
end

#file(filename, &block) ⇒ Object

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



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

def file(filename, &block)
  @files[filename] = Nginxtra::Config::ConfigFile.new filename, self, &block
end

#file_contents(filename) ⇒ Object

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



139
140
141
# File 'lib/nginxtra/config.rb', line 139

def file_contents(filename)
  @files[filename].config_file_contents
end

#file_pathsObject

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.



61
62
63
64
65
66
67
# 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

#filesObject

Retrieve the files that have been defined.



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

def files
  @files.keys
end

#partial_pathsObject

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.



48
49
50
51
52
53
54
# 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!Object

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.



105
106
107
108
109
# 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!Object

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



91
92
93
# File 'lib/nginxtra/config.rb', line 91

def require_root!
  @requires_root = true
end

#require_root?Boolean

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

Returns:

  • (Boolean)


98
99
100
# File 'lib/nginxtra/config.rb', line 98

def require_root?
  @requires_root
end

#simple_config(options = {}, &block) ⇒ Object

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.



83
84
85
86
# File 'lib/nginxtra/config.rb', line 83

def simple_config(options = {}, &block)
  SimpleConfig.new(self, options, &block).process!
  self
end