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
@@last_config =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Config

Returns a new instance of Config.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nginxtra/config.rb', line 11

def initialize(*args)
  @requires_root = false
  @compile_options = []
  @partial_paths = []
  @file_paths = []
  @files = {}
  @@last_config = self

  if block_given?
    yield self
  end
end

Class Method Details

.base_dirObject

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



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

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.



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

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.



243
244
245
# File 'lib/nginxtra/config.rb', line 243

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

.build_dirObject

Retrieve the directory where nginx is built into.



253
254
255
# File 'lib/nginxtra/config.rb', line 253

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).



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

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).



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

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.



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

def gem_template_dir
  File.join gem_dir, "templates"
end

.last_configObject

Obtain the last Nginxtra::Config object that was created.



159
160
161
# File 'lib/nginxtra/config.rb', line 159

def last_config
  @@last_config
end

.loaded_config_pathObject

Retrieve the path to the config file that was loaded.



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

def loaded_config_path
  @loaded_config_path
end

.nginx_configObject

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



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

def nginx_config
  File.join config_dir, NGINX_CONF_FILENAME
end

.nginx_executableObject

Retrieve the full path to the nginx executable.



287
288
289
# File 'lib/nginxtra/config.rb', line 287

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.



282
283
284
# File 'lib/nginxtra/config.rb', line 282

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)


310
311
312
313
314
315
316
317
# File 'lib/nginxtra/config.rb', line 310

def nginx_running?
  return false unless File.exists? 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).



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

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

.passenger_config_dirObject



305
306
307
# File 'lib/nginxtra/config.rb', line 305

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.



299
300
301
302
303
# File 'lib/nginxtra/config.rb', line 299

def passenger_spec
  @passenger_spec ||= Gem::Specification.find_by_name("passenger").tap do |spec|
    raise InvalidConfig.new("Missing passenger gem", :header => "Missing passenger gem!", :message => "You cannot reference passenger unless the passenger gem is installed!") 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.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/nginxtra/config.rb', line 169

def path
  path = File.absolute_path "."
  config = File.join path, FILENAME
  return config if File.exists? config
  config = File.join path, "config", FILENAME
  return config if File.exists? config
  path = File.dirname path

  begin
    config = File.join path, FILENAME
    return config if File.exists? config
    path = File.dirname path
  end until path == "/"

  config = File.join path, FILENAME
  config if File.exists? 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.



196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/nginxtra/config.rb', line 196

def require!(config_path = nil)
  if config_path
    config_path = File.absolute_path config_path
  else
    config_path = path
  end

  raise Nginxtra::Error::MissingConfig.new("Cannot find #{FILENAME} to configure nginxtra!") unless config_path
  raise Nginxtra::Error::MissingConfig.new("Missing file #{config_path} to configure nginxtra!") unless File.exists?(config_path)
  require config_path
  raise Nginxtra::Error::InvalidConfig.new("No configuration is specified in #{config_path}!") unless last_config
  @loaded_config_path = config_path
  last_config
end

.ruby_pathObject

Retrieve the path to ruby.



292
293
294
# File 'lib/nginxtra/config.rb', line 292

def ruby_path
  `which ruby`.strip
end

.src_dirObject

Retrieve the directory where nginx source is located.



248
249
250
# File 'lib/nginxtra/config.rb', line 248

def src_dir
  File.join base_nginx_dir, "src"
end

.template_dirObject

Retrieve the directory where templates are loaded from.



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

def template_dir
  File.join base_dir, "templates"
end

.versionObject

The current version of nginxtra.



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

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


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

def compile_option(opt)
  opt = "--#{opt}" unless opt =~ /^--/
  raise Nginxtra::Error::InvalidConfig.new("Invalid compilation option --prefix", :header => "Invalid compilation option --prefix", :message => "The --prefix compile option is not allowed with nginxtra.  It is reserved so nginxtra can control where nginx is compiled and run from.") if opt =~ /--prefix=/
  raise Nginxtra::Error::InvalidConfig.new("Invalid compilation option --sbin-path", :header => "Invalid compilation option --sbin-path", :message => "The --sbin-path compile option is not allowed with nginxtra.  It is reserved so nginxtra can control what binary is used to run nginx.") if opt =~ /--sbin-path=/
  raise Nginxtra::Error::InvalidConfig.new("Invalid compilation option --conf-path", :header => "Invalid compilation option --conf-path", :message => "The --conf-path compile option is not allowed with nginxtra.  It is reserved so nginxtra can control the configuration entirely via #{Nginxtra::Config::FILENAME}.") if opt =~ /--conf-path=/
  raise Nginxtra::Error::InvalidConfig.new("Invalid compilation option --pid-path", :header => "Invalid compilation option --pid-path", :message => "The --pid-path compile option is not allowed with nginxtra.  It is reserved so nginxtra can control where the pid file is created.") if opt =~ /--pid-path=/
  @compile_options << opt
end

#compile_optionsObject

Obtain the compile options that have been configured.



115
116
117
# File 'lib/nginxtra/config.rb', line 115

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


77
78
79
80
# File 'lib/nginxtra/config.rb', line 77

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


42
43
44
# File 'lib/nginxtra/config.rb', line 42

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


32
33
34
# File 'lib/nginxtra/config.rb', line 32

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.



148
149
150
# File 'lib/nginxtra/config.rb', line 148

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.



142
143
144
# File 'lib/nginxtra/config.rb', line 142

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.



64
65
66
67
68
69
70
# File 'lib/nginxtra/config.rb', line 64

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.



153
154
155
# File 'lib/nginxtra/config.rb', line 153

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.



51
52
53
54
55
56
57
# File 'lib/nginxtra/config.rb', line 51

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.



108
109
110
111
112
# File 'lib/nginxtra/config.rb', line 108

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.



94
95
96
# File 'lib/nginxtra/config.rb', line 94

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)


101
102
103
# File 'lib/nginxtra/config.rb', line 101

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.



86
87
88
89
# File 'lib/nginxtra/config.rb', line 86

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