Class: Logtastic::Setup

Inherits:
Object
  • Object
show all
Defined in:
lib/logtastic/setup.rb

Defined Under Namespace

Classes: AliasAlreadyExists, Error

Constant Summary collapse

DEFAULT_ILM_POLICY =
{
  "policy" => {
    "phases" => {
      "hot" => {
        "actions" => {
          "rollover" => {
            "max_size" => "50gb",
            "max_age" => "30d"
          }
        }
      }
    }
  }
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(elasticsearch, template: nil, ilm: nil) ⇒ Setup

Returns a new instance of Setup.



25
26
27
28
29
# File 'lib/logtastic/setup.rb', line 25

def initialize(elasticsearch, template: nil, ilm: nil)
  @elasticsearch = elasticsearch
  @template = template || {}
  @ilm = ilm || {}
end

Instance Method Details

#create_rollover_alias?Boolean

Returns:

  • (Boolean)


140
141
142
143
144
# File 'lib/logtastic/setup.rb', line 140

def create_rollover_alias?
  return false unless ilm_enabled?

  !@elasticsearch.indices.exists_alias?(name: rollover_alias)
end

#create_rollover_alias_argsObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/logtastic/setup.rb', line 146

def create_rollover_alias_args
  {
    index: "<#{rollover_alias}-#{ilm_pattern}>",
    body: {
      "aliases" => {
        rollover_alias => {
          "is_write_index" => true
        }
      }
    }
  }
end

#ilm_enabled?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/logtastic/setup.rb', line 44

def ilm_enabled?
  @ilm.fetch(:enabled, true)
end

#ilm_overwrite?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/logtastic/setup.rb', line 48

def ilm_overwrite?
  @ilm.fetch(:overwrite, false)
end

#ilm_patternObject



60
61
62
# File 'lib/logtastic/setup.rb', line 60

def ilm_pattern
  @ilm.fetch(:pattern, "{now/d}-000001")
end

#ilm_policy_bodyObject



64
65
66
67
68
69
70
71
72
# File 'lib/logtastic/setup.rb', line 64

def ilm_policy_body
  policy_file = @ilm.fetch(:policy_file, DEFAULT_ILM_POLICY)

  if policy_file.is_a?(Hash)
    policy_file
  else
    JSON.load(policy_file)
  end
end

#ilm_policy_idObject



52
53
54
# File 'lib/logtastic/setup.rb', line 52

def ilm_policy_id
  @ilm.fetch(:policy_name, "logtastic")
end

#perform!Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/logtastic/setup.rb', line 31

def perform!
  @elasticsearch.xpack.ilm.put_policy(put_ilm_policy_args) if put_ilm_policy?
  @elasticsearch.indices.put_template(put_template_args) if put_template?

  begin
    @elasticsearch.indices.create(create_rollover_alias_args) if create_rollover_alias?
  rescue Elasticsearch::Transport::Transport::Errors::BadRequest
    raise AliasAlreadyExists, "An index exists with the same name as the alias [#{rollover_alias}]"
  end

  true
end

#put_ilm_policy?Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
81
82
83
84
# File 'lib/logtastic/setup.rb', line 74

def put_ilm_policy?
  return false unless ilm_enabled?
  return true if ilm_overwrite?

  begin
    @elasticsearch.xpack.ilm.get_policy(policy_id: ilm_policy_id)
    false
  rescue Elasticsearch::Transport::Transport::Errors::NotFound
    true
  end
end

#put_ilm_policy_argsObject



86
87
88
89
90
91
# File 'lib/logtastic/setup.rb', line 86

def put_ilm_policy_args
  {
    policy_id: ilm_policy_id,
    body: ilm_policy_body
  }
end

#put_template?Boolean

Returns:

  • (Boolean)


126
127
128
129
130
# File 'lib/logtastic/setup.rb', line 126

def put_template?
  return true if template_overwrite?

  !@elasticsearch.indices.exists_template?(name: template_name)
end

#put_template_argsObject



132
133
134
135
136
137
138
# File 'lib/logtastic/setup.rb', line 132

def put_template_args
  {
    name: template_name,
    create: !template_overwrite?,
    body: template_body
  }
end

#rollover_aliasObject



56
57
58
# File 'lib/logtastic/setup.rb', line 56

def rollover_alias
  @ilm.fetch(:rollover_alias, "logtastic")
end

#template_bodyObject



101
102
103
104
105
106
107
# File 'lib/logtastic/setup.rb', line 101

def template_body
  JSON.load(@template.dig(:json, :path)).tap do |base_template|
    index_patterns = Array(template_pattern)
    base_template["index_patterns"] = index_patterns unless index_patterns.empty?
    base_template["settings"]["index"].merge!(template_index_settings)
  end
end

#template_index_settingsObject



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/logtastic/setup.rb', line 109

def template_index_settings
  {}.tap do |hash|
    if ilm_enabled?
      hash.merge!(
        "lifecycle.name" => ilm_policy_id,
        "lifecycle.rollover_alias" => rollover_alias
      )
    end

    hash.merge!(@template.dig(:settings, :index)) if @template.dig(:settings, :index)
  end
end

#template_nameObject



93
94
95
# File 'lib/logtastic/setup.rb', line 93

def template_name
  @template.fetch(:name, "logtastic")
end

#template_overwrite?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/logtastic/setup.rb', line 122

def template_overwrite?
  @template.fetch(:overwrite, false)
end

#template_patternObject



97
98
99
# File 'lib/logtastic/setup.rb', line 97

def template_pattern
  @template.fetch(:pattern, "logtastic-*")
end