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, config) ⇒ Object



50
51
52
53
54
55
56
57
58
59
# File 'lib/elastic_manager/config.rb', line 50

def check_settings(var, config)
  if var.casecmp('settings').zero?
    settings     = ENV[var]
    env_settings = json_parse(File.file?(settings) ? File.read(settings) : settings)
    log.debug "env settings: #{env_settings}"
    config['settings'].merge(env_settings)
  else
    ENV[var]
  end
end

#env_parser(config) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/elastic_manager/config.rb', line 61

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], config)
    end
  end

  config
end

#load_from_envObject



104
105
106
107
108
109
110
111
112
113
# File 'lib/elastic_manager/config.rb', line 104

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

  config = make_default_config
  config = env_parser(config)
  config = validate_config(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
44
45
46
47
48
# 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']              = '30'
  default['force']              = 'false'
  default['timeout']['write']   = '2'
  default['timeout']['connect'] = '3'
  default['timeout']['read']    = '120'
  default['daysago']            = ''
  default['settings']           = {
    'box_types' => {
      'ingest' => 'hot',
      'store'  => 'warm'
    },
    'indices' => {}
  }

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

#validate_config(config) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/elastic_manager/config.rb', line 77

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

  if !config['from'].empty? && !config['to'].empty?
    log.debug 'will use from and to'
    %w[from to].each do |key|
      config[key] = Date.strptime(config[key], '%Y-%m-%d')
    rescue ArgumentError => e
      fail_and_exit("can't parse date #{key}: #{e.message}")
    end
  elsif config['from'].empty? && !config['to'].empty?
    fail_and_exit('not enough env variables: FROM')
  elsif !config['from'].empty? && config['to'].empty?
    fail_and_exit('not enough env variables: TO')
  elsif !config['daysago'].empty?
    log.debug 'will use daysago'
    config['from'], config['to'] = nil
    config['daysago'] = config['daysago'].to_i
  else
    fail_and_exit('not enough env variables: FROM-TO or DAYSAGO')
  end

  config
end