Module: Fluent::ElasticsearchIndexTemplate
- Included in:
- Plugin::ElasticsearchOutput
- Defined in:
- lib/fluent/plugin/elasticsearch_index_template.rb
Instance Method Summary collapse
- #get_template(template_file) ⇒ Object
- #retry_install(max_retries) ⇒ Object
- #template_exists?(name) ⇒ Boolean
- #template_install(name, template_file, overwrite) ⇒ Object
- #template_put(name, template) ⇒ Object
- #templates_hash_install(templates, overwrite) ⇒ Object
Instance Method Details
#get_template(template_file) ⇒ Object
3 4 5 6 7 8 9 |
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 3 def get_template(template_file) if !File.exists?(template_file) raise "If you specify a template_name you must specify a valid template file (checked '#{template_file}')!" end file_contents = IO.read(template_file).gsub(/\n/,'') JSON.parse(file_contents) end |
#retry_install(max_retries) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 18 def retry_install(max_retries) return unless block_given? retries = 0 begin yield rescue Fluent::Plugin::ElasticsearchOutput::ConnectionFailure, Timeout::Error => e @_es = nil @_es_info = nil if retries < max_retries retries += 1 sleep 2**retries log.warn "Could not push template(s) to Elasticsearch, resetting connection and trying again. #{e.}" retry end raise Fluent::Plugin::ElasticsearchOutput::ConnectionFailure, "Could not push template(s) to Elasticsearch after #{retries} retries. #{e.}" end end |
#template_exists?(name) ⇒ Boolean
11 12 13 14 15 16 |
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 11 def template_exists?(name) client.indices.get_template(:name => name) return true rescue Elasticsearch::Transport::Transport::Errors::NotFound return false end |
#template_install(name, template_file, overwrite) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 40 def template_install(name, template_file, overwrite) if overwrite template_put(name, get_template(template_file)) log.info("Template '#{name}' overwritten with #{template_file}.") return end if !template_exists?(name) template_put(name, get_template(template_file)) log.info("Template configured, but no template installed. Installed '#{name}' from #{template_file}.") else log.info("Template configured and already installed.") end end |
#template_put(name, template) ⇒ Object
36 37 38 |
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 36 def template_put(name, template) client.indices.put_template(:name => name, :body => template) end |
#templates_hash_install(templates, overwrite) ⇒ Object
54 55 56 57 58 |
# File 'lib/fluent/plugin/elasticsearch_index_template.rb', line 54 def templates_hash_install(templates, overwrite) templates.each do |key, value| template_install(key, value, overwrite) end end |