Module: NginxStage::Configuration

Included in:
NginxStage
Defined in:
lib/nginx_stage/configuration.rb

Overview

An object that stores the configuration options to control NginxStage’s behavior.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#app_config_path(env:, owner:, name:) ⇒ String

Path to generated NGINX app config

Examples:

Dev app owned by Bob

app_config_path(env: :dev, owner: 'bob', name: 'rails1')
#=> "/var/lib/nginx/config/apps/dev/bob/rails1.conf"

User app owned by Dan

app_config_path(env: :usr, owner: 'dan', name: 'fillsim')
#=> "/var/lib/nginx/config/apps/usr/dan/fillsim.conf"


183
184
185
# File 'lib/nginx_stage/configuration.rb', line 183

def app_config_path(env:, owner:, name:)
  File.expand_path @app_config_path[env] % {env: env, owner: owner, name: name}
end

#app_passenger_env(env:, owner:, name:) ⇒ String

The passenger environment used for the given app environment

Examples:

Dev app owned by Bob

app_passenger_env(env: :dev, owner: 'bob', name: 'rails1')
#=> "development"

User app owned by Dan

app_passenger_env(env: :usr, owner: 'dan', name: 'fillsim')
#=> "production"


273
274
275
# File 'lib/nginx_stage/configuration.rb', line 273

def app_passenger_env(env:, owner:, name:)
  @app_passenger_env.fetch(env, "production")
end

#app_request_regexHash

Regular expression used to distinguish the environment from a request URI and from there distinguish the app owner and app name

See Also:



236
237
238
# File 'lib/nginx_stage/configuration.rb', line 236

def app_request_regex
  @app_request_regex.each_with_object({}) { |(k, v), h| h[k] = ::Regexp.new v }
end

#app_request_uri(env:, owner:, name:) ⇒ String

The URI used to access the app from the browser, not including any base-uri

Examples:

URI for dev app owned by Bob

app_request_uri(env: :dev, owner: 'bob', name: 'rails1')
#=> "/dev/rails1"

URI for user app owned by Dan

app_request_uri(env: :dev, owner: 'dan', name: 'fillsim')
#=> "/usr/dan/fillsim"

Raises:



223
224
225
226
227
# File 'lib/nginx_stage/configuration.rb', line 223

def app_request_uri(env:, owner:, name:)
  @app_request_uri.fetch(env) do
    raise InvalidRequest, "invalid request environment: #{env}"
  end % {env: env, owner: owner, name: name}
end

#app_root(env:, owner:, name:) ⇒ String

Path to the app root on the local filesystem

Examples:

App root for dev app owned by Bob

app_root(env: :dev, owner: 'bob', name: 'rails1')
#=> "~bob/ondemand/dev/rails1"

App root for user app owned by Dan

app_root(env: :usr, owner: 'dan', name: 'fillsim')
#=> "/var/www/ood/apps/usr/dan/gateway/fillsim"

Raises:



201
202
203
204
205
206
207
# File 'lib/nginx_stage/configuration.rb', line 201

def app_root(env:, owner:, name:)
  File.expand_path(
    @app_root.fetch(env) do
      raise InvalidRequest, "invalid request environment: #{env}"
    end % {env: env, owner: owner, name: name}
  )
end

#app_token(env:, owner:, name:) ⇒ String

Token used to identify a given app from the other apps

Examples:

app token for dev app owned by Bob

app_token(env: :dev, owner: 'bob', name: 'rails1')
#=> "dev/bob/rails1"

app token for user app owned by Dan

app_request_uri(env: :dev, owner: 'dan', name: 'fillsim')
#=> "usr/dan/fillsim"

Raises:



254
255
256
257
258
# File 'lib/nginx_stage/configuration.rb', line 254

def app_token(env:, owner:, name:)
  @app_token.fetch(env) do
    raise InvalidRequest, "invalid request environment: #{env}"
  end % {env: env, owner: owner, name: name}
end

#disabled_shellString

Restrict starting up per-user NGINX process as user with this shell. NB: This only affects the pun command, you are still able to

start or stop the PUN using other commands (e.g., <tt>nginx</tt>,
<tt>nginx_clean</tt>, ...)


301
302
303
# File 'lib/nginx_stage/configuration.rb', line 301

def disabled_shell
  @disabled_shell
end

#mime_types_pathString

Path to system-installed NGINX mime.types config file



33
34
35
# File 'lib/nginx_stage/configuration.rb', line 33

def mime_types_path
  @mime_types_path
end

#min_uidInteger

Minimum user id required to run the per-user NGINX as this user. This restricts running processes as special users (i.e., ‘root’)



294
295
296
# File 'lib/nginx_stage/configuration.rb', line 294

def min_uid
  @min_uid
end

#nginx_binString

Path to system-installed NGINX binary



25
26
27
# File 'lib/nginx_stage/configuration.rb', line 25

def nginx_bin
  @nginx_bin
end

#nginx_signalsArray<Symbol>

A whitelist of signals that can be sent to the NGINX process



29
30
31
# File 'lib/nginx_stage/configuration.rb', line 29

def nginx_signals
  @nginx_signals
end

#passenger_nodejsString

Path to system-installed NodeJS binary



45
46
47
# File 'lib/nginx_stage/configuration.rb', line 45

def passenger_nodejs
  @passenger_nodejs
end

#passenger_pythonString

Path to system-installed python binary



49
50
51
# File 'lib/nginx_stage/configuration.rb', line 49

def passenger_python
  @passenger_python
end

#passenger_rootString

Path to system-installed Passenger locations.ini file



37
38
39
# File 'lib/nginx_stage/configuration.rb', line 37

def passenger_root
  @passenger_root
end

#passenger_rubyString

Path to system-installed Ruby binary



41
42
43
# File 'lib/nginx_stage/configuration.rb', line 41

def passenger_ruby
  @passenger_ruby
end

#proxy_userString

The reverse proxy daemon user used to access the sockets



21
22
23
# File 'lib/nginx_stage/configuration.rb', line 21

def proxy_user
  @proxy_user
end

#pun_access_log_path(user:) ⇒ String

Path to the user’s personal access.log

Examples:

User Bob’s nginx access log

pun_access_log_path(user: 'bob')
#=> "/var/log/nginx/bob/access.log"


86
87
88
# File 'lib/nginx_stage/configuration.rb', line 86

def pun_access_log_path(user:)
  File.expand_path @pun_access_log_path % {user: user}
end

#pun_app_configs(user:) ⇒ Array<Hash>

List of hashes that help define the location of the app configs for the per-user NGINX config. These will be arguments for #app_config_path.

Examples:

User Bob’s app configs

pun_app_configs(user: 'bob')
#=> [ {env: :dev, owner: 'bob', name: '*'},
      {env: :usr, owner: '*', name: '*'} ]


157
158
159
160
161
162
163
164
# File 'lib/nginx_stage/configuration.rb', line 157

def pun_app_configs(user:)
  @pun_app_configs.map do |envmt|
    envmt.each_with_object({}) do |(k, v), h|
      h[k] = v.respond_to?(:%) ? (v % {user: user}) : v
      h[k] = v.to_sym if k == :env
    end
  end
end

#pun_config_path(user:) ⇒ String

Root location where per-user NGINX configs are generated Path to generated per-user NGINX config file

Examples:

User Bob’s nginx config

pun_config_path(user: 'bob')
#=> "/var/log/nginx/config/puns/bob.conf"


62
63
64
# File 'lib/nginx_stage/configuration.rb', line 62

def pun_config_path(user:)
  File.expand_path @pun_config_path % {user: user}
end

#pun_error_log_path(user:) ⇒ String

Path to the user’s personal error.log

Examples:

User Bob’s nginx error log

pun_error_log_path(user: 'bob')
#=> "/var/log/nginx/bob/error.log"


98
99
100
# File 'lib/nginx_stage/configuration.rb', line 98

def pun_error_log_path(user:)
  File.expand_path @pun_error_log_path % {user: user}
end

#pun_pid_path(user:) ⇒ String

Path to the user’s per-user NGINX pid file

Examples:

User Bob’s pid file

pun_pid_path(user: 'bob')
#=> "/var/run/nginx/bob/passenger.pid"


110
111
112
# File 'lib/nginx_stage/configuration.rb', line 110

def pun_pid_path(user:)
  File.expand_path @pun_pid_path % {user: user}
end

#pun_sendfile_root(user:) ⇒ String

Path to the local filesystem root where the per-user Nginx serves files from with the sendfile feature

Examples:

Filesystem root for user Bob

pun_sendfile_root(user: 'bob')
#=> "/"


135
136
137
# File 'lib/nginx_stage/configuration.rb', line 135

def pun_sendfile_root(user:)
  File.expand_path @pun_sendfile_root % {user: user}
end

#pun_sendfile_uriString

The internal URI used to access the filesystem for downloading files from the browser, not including any base-uri

Examples:

pun_sendfile_uri
#=> "/sendfile"


147
148
149
# File 'lib/nginx_stage/configuration.rb', line 147

def pun_sendfile_uri
  @pun_sendfile_uri
end

#pun_socket_path(user:) ⇒ String

Path to the user’s per-user NGINX socket file

Examples:

User Bob’s socket file

socket_path(user: 'bob')
#=> "/var/run/nginx/bob/passenger.sock"


122
123
124
# File 'lib/nginx_stage/configuration.rb', line 122

def pun_socket_path(user:)
  File.expand_path @pun_socket_path % {user: user}
end

#pun_tmp_root(user:) ⇒ String

Path to user’s personal tmp root

Examples:

User Bob’s nginx tmp root

pun_tmp_root(user: 'bob')
#=> "/var/lib/nginx/tmp/bob"


74
75
76
# File 'lib/nginx_stage/configuration.rb', line 74

def pun_tmp_root(user:)
  File.expand_path @pun_tmp_root % {user: user}
end

#template_rootString

Location of ERB templates used as NGINX configs



13
14
15
# File 'lib/nginx_stage/configuration.rb', line 13

def template_root
  @template_root
end

#user_regexRegexp

Regular expression used to validate a given user name



285
286
287
# File 'lib/nginx_stage/configuration.rb', line 285

def user_regex
  /\A#{@user_regex}\z/
end

Class Method Details

.extended(base) ⇒ Object

Sets default configuration options in any class that extends NginxStage::Configuration



326
327
328
# File 'lib/nginx_stage/configuration.rb', line 326

def self.extended(base)
  base.set_default_configuration
end

Instance Method Details

#configure {|config| ... } ⇒ void

This method returns an undefined value.

Yields the configuration object.

Yield Parameters:



321
322
323
# File 'lib/nginx_stage/configuration.rb', line 321

def configure
  yield self
end

#default_config_pathString

Default configuration file



309
310
311
312
313
314
315
316
# File 'lib/nginx_stage/configuration.rb', line 309

def default_config_path
  config = config_file
  unless File.file?(config)
    config = File.join root, 'config', 'nginx_stage.yml'
    warn "[DEPRECATION] The file '#{config}' is being deprecated. Please move this file to '#{config_file}'." if File.file?(config)
  end
  config
end

#read_configuration(file) ⇒ void

This method returns an undefined value.

Read in a configuration from a file



399
400
401
402
403
404
405
406
407
408
# File 'lib/nginx_stage/configuration.rb', line 399

def read_configuration(file)
  config_hash = symbolize(YAML.load_file(file)) || {}
  config_hash.each do |k,v|
    if instance_variable_defined? "@#{k}"
      self.send("#{k}=", v)
    else
      $stderr.puts %{Warning: invalid configuration option "#{k}"}
    end
  end
end

#set_default_configurationvoid

This method returns an undefined value.

Sets the default configuration options



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
# File 'lib/nginx_stage/configuration.rb', line 332

def set_default_configuration
  self.template_root  = "#{root}/templates"

  self.proxy_user       = 'apache'
  self.nginx_bin        = '/opt/rh/nginx16/root/usr/sbin/nginx'
  self.nginx_signals    = i(stop quit reopen reload)
  self.mime_types_path  = '/opt/rh/nginx16/root/etc/nginx/mime.types'
  self.passenger_root   = '/opt/rh/rh-passenger40/root/usr/share/passenger/phusion_passenger/locations.ini'
  self.passenger_ruby   = "#{root}/bin/ruby"
  self.passenger_nodejs = "#{root}/bin/node"
  self.passenger_python = "#{root}/bin/python"

  self.pun_config_path     = '/var/lib/nginx/config/puns/%{user}.conf'
  self.pun_tmp_root        = '/var/lib/nginx/tmp/%{user}'
  self.pun_access_log_path = '/var/log/nginx/%{user}/access.log'
  self.pun_error_log_path  = '/var/log/nginx/%{user}/error.log'
  self.pun_pid_path        = '/var/run/nginx/%{user}/passenger.pid'
  self.pun_socket_path     = '/var/run/nginx/%{user}/passenger.sock'
  self.pun_sendfile_root   = '/'
  self.pun_sendfile_uri    = '/sendfile'
  self.pun_app_configs     = [
    {env: :dev, owner: '%{user}', name: '*'},
    {env: :usr, owner: '*',       name: '*'},
    {env: :sys, owner: '',        name: '*'}
  ]

  self.app_config_path   = {
    dev: '/var/lib/nginx/config/apps/dev/%{owner}/%{name}.conf',
    usr: '/var/lib/nginx/config/apps/usr/%{owner}/%{name}.conf',
    sys: '/var/lib/nginx/config/apps/sys/%{name}.conf'
  }
  self.app_root          = {
    dev: '~%{owner}/ondemand/dev/%{name}',
    usr: '/var/www/ood/apps/usr/%{owner}/gateway/%{name}',
    sys: '/var/www/ood/apps/sys/%{name}'
  }
  self.app_request_uri   = {
    dev: '/dev/%{name}',
    usr: '/usr/%{owner}/%{name}',
    sys: '/sys/%{name}'
  }
  self.app_request_regex = {
    dev: '^/dev/(?<name>[-\w.]+)',
    usr: '^/usr/(?<owner>[\w]+)/(?<name>[-\w.]+)',
    sys: '^/sys/(?<name>[-\w.]+)'
  }
  self.app_token = {
    dev: 'dev/%{owner}/%{name}',
    usr: 'usr/%{owner}/%{name}',
    sys: 'sys/%{name}'
  }
  self.app_passenger_env = {
    dev: 'development',
    usr: 'production',
    sys: 'production'
  }

  self.user_regex     = '[\w@\.\-]+'
  self.min_uid        = 1000
  self.disabled_shell = '/access/denied'

  read_configuration(default_config_path) if File.file?(default_config_path)
end