Module: Vault::Config

Defined in:
lib/vault-tools/config.rb

Constant Summary collapse

@@defaults =
{}
@@shared =
{}

Class Method Summary collapse

Class Method Details

.[](name) ⇒ String

Get a Config value

This is the preferred and uniform way to access config vars because defaults and shared config are included in the lookup

Uses defaults and shared if available. Converts upper-case ENV var names to lower-case default names.

Order of precedence is: 1) app's local ENV 2) shared config vars 3) default values

Config[:foo] == nil

Config.default(:foo, 'bar') Config[:foo] == 'bar'

ENV['FOO'] = 'baz' Config[:foo] == 'baz'

Parameters:

  • key (Symbol)

    The lower-case name of the ENV value

Returns:

  • (String)

    The value of the ENV value or default.



29
30
31
32
33
# File 'lib/vault-tools/config.rb', line 29

def self.[](name)
  var_name = name.to_s.upcase
  default_name = name.to_s.downcase.to_sym
  ENV[var_name] || @@shared[var_name] || @@defaults[default_name]
end

.app_deployString

The APP_DEPLOY env var is used to identify which deploy of the codebase is running in librato. This usually matches the name of the environment such as local, production, staging, etc.

Returns:

  • (String)

    The deploy/environment of the app



123
124
125
# File 'lib/vault-tools/config.rb', line 123

def self.app_deploy
  env("APP_DEPLOY")
end

.app_nameString

The APP_NAME env var is used to identify which codebase is running in librato. This usually matches the name of the repository.

Returns:

  • (String)

    The name of the app



114
115
116
# File 'lib/vault-tools/config.rb', line 114

def self.app_name
  env("APP_NAME")
end

.array(name) ⇒ Array

Comma-separated words converted to an array.

Parameters:

  • name (String)

    The name of the environment variable to fetch an array for.

Returns:

  • (Array)

    An array of values.

Raises:

  • (RuntimeError)

    Raised if the environment variable is not defined.



167
168
169
# File 'lib/vault-tools/config.rb', line 167

def self.array(name)
  env(name).to_s.split(',')
end

.bool?(name) ⇒ bool

An environment variable converted to a bool.

Parameters:

  • name (String)

    The name of the environment variable to fetch a boolean for.

Returns:

  • (bool)

    True if the value is true, otherwise false.



176
177
178
# File 'lib/vault-tools/config.rb', line 176

def self.bool?(name)
  self[name] == 'true'
end

.database_url(kind = '') ⇒ Object

The database URL from the environment.

Parameters:

  • kind (String) (defaults to: '')

    Optionally, the leading name of *_DATABASE_URL environment variable. Defaults to DATABASE_URL.

Raises:

  • (RuntimeError)

    Raised if the environment variable is not defined.



139
140
141
142
# File 'lib/vault-tools/config.rb', line 139

def self.database_url(kind = '')
  kind = "#{kind}_".upcase unless kind.empty?
  env!("#{kind}DATABASE_URL")
end

.default(key, value) ⇒ String

Set a default Defaults are supplied when accessing via Config[:varname]

Parameters:

  • key (Symbol/String)

    The lower-case name of the default

Returns:

  • (String)

    The value of the default



74
75
76
# File 'lib/vault-tools/config.rb', line 74

def self.default(key, value)
  @@defaults[key.to_sym] = value
end

.defaultsHash

Get all the defaults

Returns:

  • (Hash)

    The current set of defaults



80
81
82
# File 'lib/vault-tools/config.rb', line 80

def self.defaults
  @@defaults
end

.enable_ssl?Bool

Enforce HTTPS connections in the web API?

Returns:

  • (Bool)

    True if SSL is enforced, otherwise false.



147
148
149
# File 'lib/vault-tools/config.rb', line 147

def self.enable_ssl?
  !bool?('VAULT_TOOLS_DISABLE_SSL')
end

.env(name) ⇒ String

An environment variable.

Parameters:

  • name (String)

    The name of the environment variable to fetch a value for.

Returns:

  • (String)

    The value of the environment variable or nil if no match is available.



65
66
67
# File 'lib/vault-tools/config.rb', line 65

def self.env(name)
  self[name]
end

.env!(name) ⇒ String

An environment variable.

Parameters:

  • name (String)

    The name of the environment variable to fetch a value for.

Returns:

  • (String)

    The value of the environment variable.

Raises:

  • (RuntimeError)

    Raised if the environment variable is not defined.



90
91
92
# File 'lib/vault-tools/config.rb', line 90

def self.env!(name)
  self[name] || raise("missing #{name}")
end

.int(name) ⇒ Fixnum

An environment variable converted to a Fixnum.

Parameters:

  • name (String)

    The name of the environment variable to fetch a Fixnum for.

Returns:

  • (Fixnum)

    The number or nil if the value couldn't be coerced to a Fixnum.



157
158
159
# File 'lib/vault-tools/config.rb', line 157

def self.int(name)
  self[name] && self[name].to_i
end

.load_shared!(app = nil) ⇒ Object

Loads config from another app.



36
37
38
39
# File 'lib/vault-tools/config.rb', line 36

def self.load_shared!(app = nil)
  heroku   = Heroku::API.new
  @@shared = heroku.get_config_vars(app).body
end

.portFixnum

The port to listen on for web requests.

Returns:

  • (Fixnum)

    The port to listen on for web requests.



130
131
132
# File 'lib/vault-tools/config.rb', line 130

def self.port
  env!("PORT").to_i
end

.production?Bool

The RACK_ENV environment variable is used to determine whether the service is in production mode or not.

Returns:

  • (Bool)

    True if the service is in production mode.



98
99
100
# File 'lib/vault-tools/config.rb', line 98

def self.production?
  self['RACK_ENV'] == 'production'
end

.remote_env(app, name) ⇒ String

An environment variable from another app.

Parameters:

  • app (String)

    The name of the app to get the value from.

  • name (String)

    The name of the environment variable to fetch a value for.

Returns:

  • (String)

    The value of an environment variable from another Heroku app or nil if no match is available.



54
55
56
57
# File 'lib/vault-tools/config.rb', line 54

def self.remote_env(app, name)
  heroku = Heroku::API.new
  heroku.get_config_vars(app).body[name]
end

.reset!Object

Reset defaults and shared values



42
43
44
45
# File 'lib/vault-tools/config.rb', line 42

def self.reset!
  @@defaults = {}
  @@shared   = {}
end

.sidekiq_concurrencyFixnum

The number of threads to use in Sidekiq workers.

Returns:

  • (Fixnum)

    The number of threads from the SIDEKIQ_CONCURRENCY environment variable or 25 if no variable is defined.



199
200
201
# File 'lib/vault-tools/config.rb', line 199

def self.sidekiq_concurrency
  int('SIDEKIQ_CONCURRENCY') || 25
end

.test?Bool

The RACK_ENV environment variable is used to determine whether the service is in test mode or not.

Returns:

  • (Bool)

    True if the service is in test mode.



106
107
108
# File 'lib/vault-tools/config.rb', line 106

def self.test?
  self['RACK_ENV'] == 'test'
end

.time(name) ⇒ Time

An environment variable converted to a time.

Parameters:

  • name (String|Symbol)

    The name of the environment variable to fetch a boolean for.

Returns:

  • (Time)

    Time if the value is parseable, otherwise false.



185
186
187
188
189
# File 'lib/vault-tools/config.rb', line 185

def self.time(name)
  if self[name]
    Time.parse(self[name]) rescue Time.utc(*self[name].split('-'))
  end
end

.uri(name) ⇒ Object



191
192
193
# File 'lib/vault-tools/config.rb', line 191

def self.uri(name)
  self[name] && URI.parse(self[name])
end