Skip to content

Commit

Permalink
APERTA-9087 Coerce and enforce arrays in env vars to reduce complexity
Browse files Browse the repository at this point in the history
  • Loading branch information
tadp committed Mar 28, 2017
1 parent 6685998 commit 2355d3b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ APP_NAME=Aperta
CAPYBARA_SERVER_PORT=31337
RAILS_SECRET_TOKEN=secret-token-goes-here
REDIS_PROVIDER="redis://localhost:6379"
REDIS_SENTINEL_ENABLED='false'
REDIS_SENTINELS="['server1', 'server2']"
SIDEKIQ_CONCURRENCY=5

APEX_FTP_ENABLED="false"
Expand Down
12 changes: 4 additions & 8 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
# YAML is part of the Ruby standard library and it is used here to convert
# "['server1', 'server2', 'server3']" into ['server1', 'server2', 'server3']
require 'yaml'
sentinels = YAML::load(ENV['REDIS_SENTINELS'])
Sidekiq.configure_server do |config|
if sentinels.present?
sentinel_list = sentinels.map do |sentinel_host|
if TahiEnv.redis_sentinel_enabled?
sentinel_list = TahiEnv.redis_sentinels.map do |sentinel_host|
{ host: sentinel_host, port: ENV['REDIS_PORT'] }
end
config.redis = {
Expand Down Expand Up @@ -33,8 +29,8 @@
end

Sidekiq.configure_client do |config|
if sentinels.present?
sentinel_list = sentinels.map do |sentinel_host|
if TahiEnv.redis_sentinel_enabled?
sentinel_list = TahiEnv.redis_sentinels.map do |sentinel_host|
{ host: sentinel_host, port: ENV['REDIS_PORT'] }
end
config.redis = {
Expand Down
6 changes: 4 additions & 2 deletions lib/tahi_env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require File.dirname(__FILE__) + '/tahi_env/env_var'
require File.dirname(__FILE__) + '/tahi_env/optional_env_var'
require File.dirname(__FILE__) + '/tahi_env/required_env_var'
require File.dirname(__FILE__) + '/tahi_env/array_validator'
require File.dirname(__FILE__) + '/tahi_env/boolean_validator'
require File.dirname(__FILE__) + '/tahi_env/presence_validator'

Expand Down Expand Up @@ -34,7 +35,7 @@ def self.validate!
# TahiEnv.APP_NAME # returns the raw env variable
# TahiEnv.app_name # returns the coerced env variable value
#
# The second form above makes more sense when accesing booleans:
# The second form above makes more sense when accessing booleans:
#
# required :FOO_ENABLED, :boolean
#
Expand Down Expand Up @@ -149,7 +150,8 @@ def self.validate!
required :PUSHER_VERBOSE_LOGGING, :boolean

# Redis
optional :REDIS_SENTINELS
optional :REDIS_SENTINEL_ENABLED, :boolean, default: false
required :REDIS_SENTINELS, :array, default: [], if: :redis_sentinel_enabled?

# Salesforce
optional :SALESFORCE_ENABLED, :boolean, default: true
Expand Down
17 changes: 17 additions & 0 deletions lib/tahi_env/array_validator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require File.dirname(__FILE__) + '/../tahi_env'

class TahiEnv
class ArrayValidator < ActiveModel::EachValidator
def validate_each(record, attribute, value)
if value
unless YAML::load(value).kind_of?(Array)
message = options[:message] || "Environment Variable: #{attribute} was expected to be set to a string that contains an array of strings, but was set to #{value.inspect}. Allowed array value is in the format \"['server1', 'server2' ,'server3']\"."
record.errors.add :base, message
end
else
message = options[:message] || "Environment Variable: #{attribute} was expected to be set to a string that contains an array of strings, but was not set. Allowed array value is in the format \"['server1', 'server2' ,'server3']\"."
record.errors.add :base, message
end
end
end
end
8 changes: 7 additions & 1 deletion lib/tahi_env/dsl_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ def required(key, *args)
)
register_env_var(required_env_var)

validation_args = required_env_var.boolean? ? { boolean: true } : { presence: true }
validation_args = if required_env_var.boolean?
{ boolean: true }
elsif required_env_var.array?
{ array: true }
else
{ presence: true }
end

validation_args[:if] = if_method if if_method
validates key, **validation_args
Expand Down
12 changes: 12 additions & 0 deletions lib/tahi_env/env_var.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ def value
default_value
elsif boolean?
converted_boolean_value
elsif array?
converted_array_value
else
raw_value_from_env
end
Expand All @@ -30,6 +32,10 @@ def raw_value_from_env
ENV[@key]
end

def array?
@type == :array
end

def boolean?
@type == :boolean
end
Expand All @@ -42,6 +48,12 @@ def to_s

private

def converted_array_value
# This is used to convert a string that contains
# an array into a Ruby array object
YAML::load(raw_value_from_env)
end

def converted_boolean_value
['true', '1'].include?(raw_value_from_env.downcase)
end
Expand Down

0 comments on commit 2355d3b

Please sign in to comment.