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.5.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, #ondemand_portal, #ondemand_title, #ondemand_version_path, #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



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

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



177
178
179
# File 'lib/nginx_stage.rb', line 177

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



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

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

.nginx_env(user:) ⇒ Hash{String=>String}

Environment used during execution of nginx binary

Examples:

Start the per-user NGINX for user Bob

nginx_env(user: 'bob')
#=> { "USER" => "bob", ... }

Parameters:

  • user (String)

    the owner of the nginx process

Returns:

  • (Hash{String=>String})

    the environment used to execute the nginx process



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/nginx_stage.rb', line 112

def self.nginx_env(user:)
  {
    "USER" => user,
    "ONDEMAND_VERSION" => ondemand_version,
    "ONDEMAND_PORTAL" => portal,
    "ONDEMAND_TITLE" => title,
    # backwards compatibility
    "OOD_PORTAL" => ondemand_portal,
    "OOD_DASHBOARD_TITLE" => ondemand_title,
  }
end

.ondemand_versionString?

The current version of OnDemand installed

Examples:

Get version of OnDemand for version file that exists

ondemand_version #=> "1.3.0"

No version file exists

ondemand_version #=> nil

Returns:

  • (String, nil)

    current version of installed OnDemand if exists



45
46
47
48
49
50
# File 'lib/nginx_stage.rb', line 45

def self.ondemand_version
  version = File.read(ondemand_version_path).strip
  version.empty? ? nil : version
rescue
  nil
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:



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/nginx_stage.rb', line 93

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

.portalString

The unique name of the hosted OnDemand portal used to namespace apps, their data, and logging information

Examples:

No OnDemand Portal name specified

portal #=> "ondemand"

The AweSim portal is specified

portal #=> "awesim"

Returns:

  • (String)

    unique portal name



59
60
61
62
63
64
65
66
# File 'lib/nginx_stage.rb', line 59

def self.portal
  portal = ondemand_portal.to_s.strip
  if portal.empty?
    "ondemand"
  else
    portal.downcase.gsub(/\s+/, "_")
  end
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



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/nginx_stage.rb', line 159

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

.titleString

The title of the hosted OnDemand portal

Examples:

No title supplied

title #=> "Open OnDemand"

The OSC OnDemand portal

title #=> "OSC OnDemand"

Returns:

  • (String)

    portal title



74
75
76
77
78
79
80
81
# File 'lib/nginx_stage.rb', line 74

def self.title
  title = ondemand_title.to_s.strip
  if title.empty?
    "Open OnDemand"
  else
    title
  end
end