Class: Puppet::Provider::ElasticPlugin

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

Instance Method Summary collapse

Instance Method Details

#batch_capable?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/puppet/provider/elastic_plugin.rb', line 185

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

#createObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/puppet/provider/elastic_plugin.rb', line 116

def create
  es_version
  commands = []
  if is2x?
    commands << "-Des.path.conf=#{homedir}"
    if @resource[:proxy]
      commands += proxy_args(@resource[:proxy])
    end
  end
  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

  writepluginfile
end

#destroyObject



147
148
149
150
151
# File 'lib/puppet/provider/elastic_plugin.rb', line 147

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

#es_versionObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/puppet/provider/elastic_plugin.rb', line 153

def es_version
  return @es_version if @es_version
  es_save = ENV['ES_INCLUDE']
  java_save = ENV['JAVA_HOME']

  os = Facter.value('osfamily')
  if os == 'OpenBSD'
    ENV['JAVA_HOME'] = javapathhelper('-h', 'elasticsearch').chomp
    ENV['ES_INCLUDE'] = '/etc/elasticsearch/elasticsearch.in.sh'
  end
  begin
    version = es('-version')
  rescue
    ENV['ES_INCLUDE'] = es_save if es_save
    ENV['JAVA_HOME'] = java_save if java_save
    raise "Unknown ES version. Got #{version.inspect}"
  ensure
    ENV['ES_INCLUDE'] = es_save if es_save
    ENV['JAVA_HOME'] = java_save if java_save
    @es_version = version.scan(/\d+\.\d+\.\d+(?:\-\S+)?/).first
    debug "Found ES version #{@es_version}"
  end
end

#exists?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/puppet/provider/elastic_plugin.rb', line 15

def exists?
  es_version
  if !File.exists?(pluginfile)
    debug "Plugin file #{pluginfile} does not exist"
    return false
  elsif File.exists?(pluginfile) && readpluginfile != pluginfile_content
    debug "Got #{readpluginfile} Expected #{pluginfile_content}. Removing for reinstall"
    self.destroy
    return false
  else
    debug "Plugin exists"
    return true
  end
end

#homedirObject



6
7
8
9
10
11
12
13
# File 'lib/puppet/provider/elastic_plugin.rb', line 6

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

#install1xObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/puppet/provider/elastic_plugin.rb', line 68

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

#install2xObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/puppet/provider/elastic_plugin.rb', line 88

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

#is1x?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/puppet/provider/elastic_plugin.rb', line 177

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

#is2x?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/puppet/provider/elastic_plugin.rb', line 181

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

#plugin_version(plugin_name) ⇒ Object



189
190
191
192
193
194
# File 'lib/puppet/provider/elastic_plugin.rb', line 189

def plugin_version(plugin_name)
  _vendor, _plugin, version = plugin_name.split('/')
  return @es_version if is2x? && version.nil?
  return version.scan(/\d+\.\d+\.\d+(?:\-\S+)?/).first unless version.nil?
  return false
end

#pluginfileObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/puppet/provider/elastic_plugin.rb', line 41

def pluginfile
  if @resource[:plugin_path]
    File.join(
      @resource[:plugin_dir],
      @resource[:plugin_path],
      '.name'
    )
  else
    File.join(
      @resource[:plugin_dir],
      Puppet_X::Elastic::plugin_name(@resource[:name]),
      '.name'
    )
  end
end

#pluginfile_contentObject



30
31
32
33
34
35
36
37
38
39
# File 'lib/puppet/provider/elastic_plugin.rb', line 30

def pluginfile_content
  return @resource[:name] if is1x?

  if @resource[:name].split("/").count == 1 # Official plugin
    version = plugin_version(@resource[:name])
    return "#{@resource[:name]}/#{version}"
  else
    return @resource[:name]
  end
end

#proxy_args(url) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/puppet/provider/elastic_plugin.rb', line 104

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

#readpluginfileObject



63
64
65
66
# File 'lib/puppet/provider/elastic_plugin.rb', line 63

def readpluginfile
  f = File.open(pluginfile)
  f.readline
end

#with_environment(&block) ⇒ Object

Run a command wrapped in necessary env vars



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/puppet/provider/elastic_plugin.rb', line 197

def with_environment(&block)
  env_vars = {
    'ES_JAVA_OPTS' => [],
  }
  saved_vars = {}

  if not is2x?
    env_vars['ES_JAVA_OPTS'] << "-Des.path.conf=#{homedir}"
    if @resource[:proxy]
      env_vars['ES_JAVA_OPTS'] += proxy_args(@resource[:proxy])
    end
  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.call

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

  return ret
end

#writepluginfileObject



57
58
59
60
61
# File 'lib/puppet/provider/elastic_plugin.rb', line 57

def writepluginfile
  File.open(pluginfile, 'w') do |file|
    file.write pluginfile_content
  end
end