Class: Puppet::Provider::ElasticPlugin

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/elastic_plugin.rb

Overview

Generalized parent class for providers that behave like Elasticsearch’s plugin command line tool. rubocop:disable Metrics/ClassLength

Instance Method Summary collapse

Instance Method Details

#batch_capable?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/puppet/provider/elastic_plugin.rb', line 173

def batch_capable?
  Puppet::Util::Package.versioncmp(es_version, '2.2.0') >= 0
end

#createObject

Install this plugin on the host. rubocop:disable Metrics/CyclomaticComplexity



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/puppet/provider/elastic_plugin.rb', line 126

def create
  commands = []
  commands += proxy_args(@resource[:proxy]) if is2x? and @resource[:proxy]
  commands << 'install'
  commands << '--batch' if batch_capable?
  commands += is1x? ? install1x : install2x
  debug("Commands: #{commands.inspect}")

  retry_count = 3
  retry_times = 0
  begin
    with_environment do
      plugin(commands)
    end
  rescue Puppet::ExecutionFailure => e
    retry_times += 1
    debug("Failed to install plugin. Retrying... #{retry_times} of #{retry_count}")
    sleep 2
    retry if retry_times < retry_count
    raise "Failed to install plugin. Received error: #{e.inspect}"
  end
end

#destroyObject

Remove this plugin from the host.



151
152
153
154
155
# File 'lib/puppet/provider/elastic_plugin.rb', line 151

def destroy
  with_environment do
    plugin(['remove', Puppet_X::Elastic.plugin_name(@resource[:name])])
  end
end

#es_versionObject

Determine the installed version of Elasticsearch on this host.



158
159
160
161
162
# File 'lib/puppet/provider/elastic_plugin.rb', line 158

def es_version
  Puppet_X::Elastic::EsVersioning.version(
    resource[:elasticsearch_package_name], resource.catalog
  )
end

#exists?Boolean

Returns:

  • (Boolean)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/puppet/provider/elastic_plugin.rb', line 23

def exists?
  # First, attempt to list whether the named plugin exists by finding a
  # plugin descriptor file, which each plugin should have. We must wildcard
  # the name to match meta plugins, see upstream issue for this change:
  # https://github.com/elastic/elasticsearch/pull/28022
  properties_files = Dir[File.join(@resource[:plugin_dir], plugin_path, '**', '*plugin-descriptor.properties')]
  return false if properties_files.empty?

  begin
    # Use the basic name format that the plugin tool supports in order to
    # determine the version from the resource name.
    plugin_version = Puppet_X::Elastic.plugin_version(@resource[:name])

    # Naively parse the Java .properties file to check version equality.
    # Because we don't have the luxury of installing arbitrary gems, perform
    # simple parse with a degree of safety checking in the call chain
    #
    # Note that x-pack installs "meta" plugins which bundle multiple plugins
    # in one. Therefore, we need to find the first "sub" plugin that
    # indicates which version of x-pack this is.
    properties = properties_files.sort.map do |prop_file|
      IO.readlines(prop_file).map(&:strip).reject do |line|
        line.start_with?('#') or line.empty?
      end.map do |property|
        property.split('=')
      end.reject do |pairs|
        pairs.length != 2
      end.to_h
    end.find { |prop| prop.key? 'version' }

    if properties and properties['version'] != plugin_version
      debug "Elasticsearch plugin #{@resource[:name]} not version #{plugin_version}, reinstalling"
      destroy
      return false
    end
  rescue ElasticPluginParseFailure
    debug "Failed to parse plugin version for #{@resource[:name]}"
  end

  # If there is no version string, we do not check version equality
  debug "No version found in #{@resource[:name]}, not enforcing any version"
  true
end

#homedirObject

Elasticsearch’s home directory.

Returns:

  • String



14
15
16
17
18
19
20
21
# File 'lib/puppet/provider/elastic_plugin.rb', line 14

def homedir
  case Facter.value('osfamily')
  when 'OpenBSD'
    '/usr/local/elasticsearch'
  else
    '/usr/share/elasticsearch'
  end
end

#install1xArray<String>

Intelligently returns the correct installation arguments for version 1 version of Elasticsearch.

Returns:

  • (Array<String>)

    arguments to pass to the plugin installation utility



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/puppet/provider/elastic_plugin.rb', line 76

def install1x
  if !@resource[:url].nil?
    [
      Puppet_X::Elastic.plugin_name(@resource[:name]),
      '--url',
      @resource[:url]
    ]
  elsif !@resource[:source].nil?
    [
      Puppet_X::Elastic.plugin_name(@resource[:name]),
      '--url',
      "file://#{@resource[:source]}"
    ]
  else
    [@resource[:name]]
  end
end

#install2xArray<String>

Intelligently returns the correct installation arguments for version 2 version of Elasticsearch.

Returns:

  • (Array<String>)

    arguments to pass to the plugin installation utility



99
100
101
102
103
104
105
106
107
# File 'lib/puppet/provider/elastic_plugin.rb', line 99

def install2x
  if !@resource[:url].nil?
    [@resource[:url]]
  elsif !@resource[:source].nil?
    ["file://#{@resource[:source]}"]
  else
    [@resource[:name]]
  end
end

#is1x?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/puppet/provider/elastic_plugin.rb', line 164

def is1x?
  Puppet::Util::Package.versioncmp(es_version, '2.0.0') < 0
end

#is2x?Boolean

Returns:

  • (Boolean)


168
169
170
171
# File 'lib/puppet/provider/elastic_plugin.rb', line 168

def is2x?
  (Puppet::Util::Package.versioncmp(es_version, '2.0.0') >= 0) && \
    (Puppet::Util::Package.versioncmp(es_version, '3.0.0') < 0)
end

#plugin_pathObject



67
68
69
# File 'lib/puppet/provider/elastic_plugin.rb', line 67

def plugin_path
  @resource[:plugin_path] || Puppet_X::Elastic.plugin_name(@resource[:name])
end

#proxy_args(url) ⇒ Object

Format proxy arguments for consumption by the elasticsearch plugin management tool (i.e., Java properties).

Returns:

  • Array of flags for command-line tools



114
115
116
117
118
119
120
121
122
# File 'lib/puppet/provider/elastic_plugin.rb', line 114

def proxy_args(url)
  parsed = URI(url)
  %w[http https].map do |schema|
    [:host, :port, :user, :password].map do |param|
      option = parsed.send(param)
      "-D#{schema}.proxy#{param.to_s.capitalize}=#{option}" unless option.nil?
    end
  end.flatten.compact
end

#with_environment(&block) ⇒ Object

Run a command wrapped in necessary env vars



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/puppet/provider/elastic_plugin.rb', line 178

def with_environment(&block)
  env_vars = {
    'ES_JAVA_OPTS' => @resource[:java_opts],
    'ES_PATH_CONF' => @resource[:configdir]
  }
  saved_vars = {}

  unless @resource[:java_home].nil? or @resource[:java_home] == ''
    env_vars['JAVA_HOME'] = @resource[:java_home]
  end

  if !is2x? and @resource[:proxy]
    env_vars['ES_JAVA_OPTS'] += proxy_args(@resource[:proxy])
  end

  env_vars['ES_JAVA_OPTS'] = env_vars['ES_JAVA_OPTS'].join(' ')

  env_vars.each do |env_var, value|
    saved_vars[env_var] = ENV[env_var]
    ENV[env_var] = value
  end

  ret = block.yield

  saved_vars.each do |env_var, value|
    ENV[env_var] = value
  end

  ret
end