Module: NginxStage

Extended by:
Configuration
Defined in:
lib/nginx_stage.rb,
lib/nginx_stage/user.rb,
lib/nginx_stage/errors.rb,
lib/nginx_stage/version.rb,
lib/nginx_stage/pid_file.rb,
lib/nginx_stage/generator.rb,
lib/nginx_stage/application.rb,
lib/nginx_stage/socket_file.rb,
lib/nginx_stage/configuration.rb,
lib/nginx_stage/generator_helpers.rb,
lib/nginx_stage/views/app_config_view.rb,
lib/nginx_stage/views/pun_config_view.rb,
lib/nginx_stage/generators/app_list_generator.rb,
lib/nginx_stage/generators/app_clean_generator.rb,
lib/nginx_stage/generators/app_reset_generator.rb,
lib/nginx_stage/generators/app_config_generator.rb,
lib/nginx_stage/generators/nginx_list_generator.rb,
lib/nginx_stage/generators/nginx_show_generator.rb,
lib/nginx_stage/generators/pun_config_generator.rb,
lib/nginx_stage/generators/nginx_clean_generator.rb,
lib/nginx_stage/generators/nginx_process_generator.rb

Overview

The main namespace for NginxStage. Provides a global configuration.

Defined Under Namespace

Modules: AppConfigView, Application, Configuration, GeneratorHelpers, PunConfigView Classes: AppCleanGenerator, AppConfigGenerator, AppListGenerator, AppResetGenerator, Error, Generator, InvalidAppInitUrl, InvalidCommand, InvalidConfigOption, InvalidPidFile, InvalidRequest, InvalidSocket, InvalidSocketFile, InvalidSubUri, InvalidUser, MissingCommand, MissingOption, MissingPidFile, MissingSocketFile, NginxCleanGenerator, NginxListGenerator, NginxProcessGenerator, NginxShowGenerator, PidFile, PunConfigGenerator, SocketFile, StalePidFile, User

Constant Summary collapse

VERSION =

The current version of NginxStage

"0.4.0"

Instance Attribute Summary

Attributes included from Configuration

#app_config_path, #app_passenger_env, #app_request_regex, #app_request_uri, #app_root, #app_token, #disabled_shell, #mime_types_path, #min_uid, #nginx_bin, #nginx_signals, #passenger_nodejs, #passenger_python, #passenger_root, #passenger_ruby, #proxy_user, #pun_access_log_path, #pun_app_configs, #pun_config_path, #pun_error_log_path, #pun_pid_path, #pun_sendfile_root, #pun_sendfile_uri, #pun_socket_path, #pun_tmp_root, #template_root, #user_regex

Class Method Summary collapse

Methods included from Configuration

configure, default_config_path, extended, read_configuration, set_default_configuration

Class Method Details

.active_usersArray<User>

List of users with nginx processes running

Returns:

  • (Array<User>)

    the list of users with running nginx processes



80
81
82
# File 'lib/nginx_stage.rb', line 80

def self.active_users
  Dir[pun_pid_path(user: '*')].map{|v| User.new v[/#{pun_pid_path(user: '(.+)')}/, 1]}
end

.as_user(user) { ... } ⇒ Object

Run Ruby block as a different user if possible NB: Will forego user switching if current process is not root-owned

Parameters:

  • user (String, Integer, nil)

    the user or user id to switch to

Yields:

  • Block to run as given user



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

def self.as_user(user, &block)
  (Process.uid == 0) && user ? sudo(user, &block) : block.call
end

.config_fileString

Path to the configuration file

Returns:

  • (String)

    path to config file



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

def self.config_file
  ENV["NGINX_STAGE_CONFIG_FILE"] || '/etc/ood/config/nginx_stage.yml'
end

.nginx_args(user:, signal: nil) ⇒ Array<String>

Arguments used during execution of nginx binary

Examples:

Start the per-user NGINX for user Bob

nginx_args(user: 'bob')
#=> ['-c', '/var/lib/nginx/config/puns/bob.conf']

Stop the per-user NGINX for user Bob

nginx_args(user: 'bob', signal: :stop)
#=> ['-c', '/var/lib/nginx/config/puns/bob.conf', '-s', 'stop']

Parameters:

  • user (String)

    the owner of the nginx process

  • signal (Symbol) (defaults to: nil)

    the signal sent to the nginx process

Returns:

  • (Array<String>)

    the shell arguments used to execute the nginx process



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

def self.nginx_args(user:, signal: nil)
  args = ['-c', pun_config_path(user: user)]
  args.push('-s', signal.to_s) if signal
  args
end

.parse_app_request(request:) ⇒ Hash

Regex used to parse an app request

Examples:

Dev app request

parse_app_request(request: '/dev/rails1/structure/1')
#=> {env: :dev, name: 'rails1'}

User app request with owner Bob

parse_app_request(request: '/usr/bob/fillsim/containers')
#=> {env: :usr, owner: 'bob', name: 'fillsim'}

Parameters:

  • request (String)

    the URI request used to access app

Returns:

  • (Hash)

    hash containing parsed information

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/nginx_stage.rb', line 49

def self.parse_app_request(request:)
  app_info = {}
  app_request_regex.each do |env, regex|
    if matches = regex.match(request)
      app_info[:env] = env
      matches.names.each { |k| app_info[k.to_sym] = matches[k] }
      break
    end
  end
  raise InvalidRequest, "invalid request: #{request}" if app_info.empty?
  app_info
end

.rootString

Root path of this library

Returns:

  • (String)

    root path of library



27
28
29
# File 'lib/nginx_stage.rb', line 27

def self.root
  File.dirname __dir__
end

.staged_appsHash

Get a hash of all the staged app configs

Examples:

List of all staged app configs

staged_apps
#=> {
      dev: [
        {owner: 'bob', name: 'rails1'},
        {owner: 'dan', name: 'fillsim'}
      ],
      usr: [
        {owner: 'bob', name: 'airsim'}
      ]
    }

Returns:

  • (Hash)

    the hash of app environments with list of corresponding apps



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/nginx_stage.rb', line 97

def self.staged_apps
  staged_apps = {}
  @app_config_path.each do |env, path|
    staged_apps[env] = Dir[app_config_path(env: env, owner: '*', name: '*')].map do |v|
      matches = /#{app_config_path(env: env, owner: '(?<owner>.+)', name: '(?<name>.+)')}/.match(v)
      {
        owner: matches.names.include?('owner') ? matches[:owner] : nil,
        name:  matches.names.include?('name')  ? matches[:name]  : nil
      }
    end
  end
  staged_apps
end