Module: Config

Includes:
Logging
Included in:
ElasticManager
Defined in:
lib/elastic_manager/config.rb

Overview

Read, validate and merge with default config

Constant Summary collapse

PARAMS =
%w[
  TASK
  INDICES
  FROM
  TO
  DAYSAGO
  ES_URL
  TIMEOUT_WRITE
  TIMEOUT_CONNECT
  TIMEOUT_READ
  RETRY
  SLEEP
  FORCE
  SETTINGS
].freeze

Constants included from Logging

Logging::SEVERITY_COLORS

Instance Method Summary collapse

Methods included from Logging

configure_logger_for, #log, log_level, logger_for

Instance Method Details

#check_settings(var) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/elastic_manager/config.rb', line 45

def check_settings(var)
  if var.casecmp('settings').zero?
    json_parse(ENV[var])
  else
    ENV[var]
  end
end

#env_parser(config) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/elastic_manager/config.rb', line 53

def env_parser(config)
  PARAMS.each do |var|
    next if ENV[var] == '' || ENV[var].nil?

    vars = var.split('_')

    if vars.length == 2
      config[vars[0].downcase][vars[1].downcase] = ENV[var]
    elsif vars.length == 1
      config[vars[0].downcase] = check_settings(vars[0])
    end
  end

  config
end

#exit_if_invalid(config) ⇒ Object

def present?

!blank?

end



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

def exit_if_invalid(config)
  if config['task'].empty? || config['indices'].empty?
    fail_and_exit('not enough env variables. TASK, INDICES')
  end

  unless (config['from'].empty? && config['to'].empty?) || config['daysago'].empty?
    fail_and_exit('not enough env variables. FROM/TO or DAYSAGO')
  end
end

#load_from_envObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/elastic_manager/config.rb', line 83

def load_from_env
  log.debug 'will load config from ENV variables'

  config = make_default_config
  config = env_parser(config)
  exit_if_invalid(config)

  log.debug "env config: #{config.inspect}"
  config
end

#make_default_configObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/elastic_manager/config.rb', line 27

def make_default_config
  default = Hash.new { |hash, key| hash[key] = Hash.new(&hash.default_proc) }

  default['es']['url']          = 'http://127.0.0.1:9200'
  default['retry']              = '10'
  default['sleep']              = '60'
  default['force']              = 'false'
  default['timeout']['write']   = '2'
  default['timeout']['connect'] = '3'
  default['timeout']['read']    = '120'
  default['daysago']            = ''
  default['settings']           = {}
  default['daysago']            = ''

  log.debug "default config: #{default.inspect}"
  default
end