Module: Utils

Includes:
Logging
Included in:
ElasticManager, Request::Elastic
Defined in:
lib/elastic_manager/utils.rb

Overview

Sharable methods

Constant Summary

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

#already?(response, state) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/elastic_manager/utils.rb', line 94

def already?(response, state)
  if %w[open close].include?(state)
    index = json_parse(response).first
    if index['status'] == state
      log.warn "#{index['index']} index status already #{state}"
      return true
    end
  elsif %w[hot warm].include?(state)
    index    = json_parse(response).first['index']
    box_type = @elastic.index_box_type(index)

    if box_type.nil?
      log.info "can't get box_type, look at the previous error for info, will skip #{index}"
      return true
    end

    if box_type == state
      log.warn "#{index['index']} index already #{state}"
      return true
    end
  else
    log.fatal "wrong state for check #{state}, bad coder"
    exit 1
  end

  false
end

#delete_without_snapshot?(index) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/elastic_manager/utils.rb', line 46

def delete_without_snapshot?(index)
  index_name = make_index_name(index)

  # ALARM! this can be enabled only for development logs, otherwise indices without snapshots can be deleted!
  if @config['settings']['indices']['_all'] &&
    @config['settings']['indices']['_all']['delete_without_snapshot']

    if true?(@config['settings']['indices']['_all']['delete_without_snapshot'])
      log.warn 'any index can be deleted without snapshot due global settings'
      return true
    end
  end

  if @config['settings']['indices'][index_name] &&
     @config['settings']['indices'][index_name]['delete_without_snapshot']

    if true?(@config['settings']['indices'][index_name]['delete_without_snapshot'])
      log.warn "#{index_name} index can be deleted without snapshot"
      return true
    end
  end

  false
end

#elastic_action_with_log(action, index, *params) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/elastic_manager/utils.rb', line 122

def elastic_action_with_log(action, index, *params)
  if @elastic.send(action, index, *params)
    log.info "#{index} #{action} succes"
  else
    log.error "#{index} #{action} fail"
    return false
  end

  true
end

#fail_and_exit(text) ⇒ Object



21
22
23
24
# File 'lib/elastic_manager/utils.rb', line 21

def fail_and_exit(text)
  log.fatal text
  exit 1
end

#index_exist?(response) ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
136
137
138
139
140
141
142
# File 'lib/elastic_manager/utils.rb', line 133

def index_exist?(response)
  if response.code == 200
    true
  elsif response.code == 404
    false
  else
    log.fatal "wtf in index_exist? response was: #{response.code} - #{response}"
    exit 1
  end
end

#json_parse(string) ⇒ Object



14
15
16
17
18
19
# File 'lib/elastic_manager/utils.rb', line 14

def json_parse(string)
  JSON.parse(string)
rescue JSON::ParserError => e
  log.fatal "json parse err: '''#{e.message}'''\n\t#{e.backtrace.join("\n\t")}"
  exit 1
end

#make_index_name(index) ⇒ Object



26
27
28
# File 'lib/elastic_manager/utils.rb', line 26

def make_index_name(index)
  index.split('-')[0..-2].join('-')
end

#prechecks(date_from, date_to) ⇒ Object



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

def prechecks(date_from, date_to)
  unless date_from.nil?
    if date_from > date_to
      log.fatal "wrong dates: date to is behind date from. from: #{date_from}, to: #{date_to}"
      exit 1
    end
  end

  return if true?(@config['force'])
  return if @elastic.green?

  fail_and_exit("elasticsearch on #{@config['es']['url']} is not green")
end

#prepare_varsObject



71
72
73
74
75
76
77
78
# File 'lib/elastic_manager/utils.rb', line 71

def prepare_vars
  indices   = @config['indices'].split(',').map(&:strip)
  daysago   = @config['daysago']
  date_from = @config['from']
  date_to   = @config['to']

  [indices, date_from, date_to, daysago]
end

#skip_index?(index, state) ⇒ Boolean

Returns:

  • (Boolean)


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

def skip_index?(index, state)
  index_name = make_index_name(index)

  if @config['settings']['indices'][index_name] &&
     @config['settings']['indices'][index_name]['skip'] &&
     @config['settings']['indices'][index_name]['skip'][state]

    if true?(@config['settings']['indices'][index_name]['skip'][state])
      log.warn "#{index_name} index #{state} skiped due settings"
      return true
    end
  end

  false
end

#true?(obj) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/elastic_manager/utils.rb', line 10

def true?(obj)
  obj.to_s.casecmp('true').zero?
end