module Config

Read, validate and merge with default config

Constants

PARAMS

Public Instance Methods

check_settings(var, config) click to toggle source
# 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) click to toggle source
# 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_env() click to toggle source
# 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_config() click to toggle source
# 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) click to toggle source
# 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