Skip to content

Commit 8852a0b

Browse files
committed
Fix rubocop offenses
1 parent 9e9b38b commit 8852a0b

File tree

116 files changed

+2828
-2188
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+2828
-2188
lines changed

lib/facter/es_facts.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'net/http'
24
require 'json'
35
require 'yaml'
@@ -27,15 +29,13 @@ def self.get_httpport(config)
2729

2830
return false, false if !config[enabled].nil? && config[enabled] == 'false'
2931
return config[httpport], ssl?(config) unless config[httpport].nil?
32+
3033
['9200', ssl?(config)]
3134
end
3235

3336
# Entrypoint for custom fact populator
3437
#
3538
# This is a super old function but works; disable a bunch of checks.
36-
# rubocop:disable Lint/HandleExceptions
37-
# rubocop:disable Metrics/CyclomaticComplexity
38-
# rubocop:disable Metrics/PerceivedComplexity
3939
def self.run
4040
dir_prefix = '/etc/elasticsearch'
4141
# httpports is a hash of port_number => ssl?
@@ -104,24 +104,24 @@ def self.run
104104
nodes_data['http']['bound_address'].each { |i| http_bound_addresses << i }
105105
nodes_data['transport']['bound_address'].each { |i| transport_bound_addresses << i }
106106
transport_publish_addresses << nodes_data['transport']['publish_address'] unless nodes_data['transport']['publish_address'].nil?
107-
transportports << nodes_data['settings']['transport']['tcp']['port'] unless nodes_data['settings']['transport']['tcp'].nil? or nodes_data['settings']['transport']['tcp']['port'].nil?
107+
transportports << nodes_data['settings']['transport']['tcp']['port'] unless nodes_data['settings']['transport']['tcp'].nil? || nodes_data['settings']['transport']['tcp']['port'].nil?
108108

109109
node = {
110-
'http_ports' => httpports.keys,
111-
'transport_ports' => transportports,
112-
'http_bound_addresses' => http_bound_addresses,
113-
'transport_bound_addresses' => transport_bound_addresses,
110+
'http_ports' => httpports.keys,
111+
'transport_ports' => transportports,
112+
'http_bound_addresses' => http_bound_addresses,
113+
'transport_bound_addresses' => transport_bound_addresses,
114114
'transport_publish_addresses' => transport_publish_addresses,
115-
json_data['name'] => {
116-
'settings' => nodes_data['settings'],
117-
'http' => nodes_data['http'],
115+
json_data['name'] => {
116+
'settings' => nodes_data['settings'],
117+
'http' => nodes_data['http'],
118118
'transport' => nodes_data['transport']
119119
}
120120
}
121121
nodes.merge! node
122122
end
123123
end
124-
rescue
124+
rescue StandardError
125125
end
126126
Facter.add(:elasticsearch) do
127127
setcode do
@@ -130,8 +130,6 @@ def self.run
130130
nodes unless nodes.empty?
131131
end
132132
end
133-
# rubocop:enable Metrics/CyclomaticComplexity
134-
# rubocop:enable Metrics/PerceivedComplexity
135133
end
136134

137135
EsFacts.run
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
1+
# frozen_string_literal: true
2+
13
require 'puppet/util/feature'
24
require 'puppet/util/package'
35

46
shield_plugin_dir = '/usr/share/elasticsearch/plugins/shield'
57

6-
Puppet.features.add(:elasticsearch_shield_users_native) {
7-
File.exist? shield_plugin_dir and
8-
Dir[shield_plugin_dir + '/*.jar'].map do |file|
9-
File.basename(file, '.jar').split('-')
10-
end.select do |parts|
11-
parts.include? 'shield'
12-
end.any? do |parts|
13-
parts.last =~ /^[\d.]+$/ and
14-
Puppet::Util::Package.versioncmp(parts.last, '2.3') >= 0
15-
end
16-
}
8+
Puppet.features.add(:elasticsearch_shield_users_native) do
9+
return false unless File.exist?(shield_plugin_dir)
10+
11+
jars = Dir["#{shield_plugin_dir}/*.jar"]
12+
jar_parts = jars.map do |file|
13+
File.basename(file, '.jar').split('-')
14+
end
15+
shield_components = jar_parts.select do |parts|
16+
parts.include? 'shield'
17+
end
18+
shield_components.any? do |parts|
19+
parts.last =~ %r{^[\d.]+$} &&
20+
Puppet::Util::Package.versioncmp(parts.last, '2.3') >= 0
21+
end
22+
end

lib/puppet/parser/functions/array_suffix.rb

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,38 @@
1+
# frozen_string_literal: true
2+
13
# Top-level Puppet functions
24
module Puppet::Parser::Functions
35
newfunction(
46
:array_suffix,
5-
:type => :rvalue,
6-
:doc => <<-EOS
7-
This function applies a suffix to all elements in an array.
7+
type: :rvalue,
8+
doc: <<~EOS
9+
This function applies a suffix to all elements in an array.
810
9-
*Examples:*
11+
*Examples:*
1012
11-
array_suffix(['a','b','c'], 'p')
13+
array_suffix(['a','b','c'], 'p')
1214
13-
Will return: ['ap','bp','cp']
15+
Will return: ['ap','bp','cp']
1416
15-
@return Array
17+
@return Array
1618
EOS
1719
) do |arguments|
1820
# Technically we support two arguments but only first is mandatory ...
19-
raise(Puppet::ParseError, 'array_suffix(): Wrong number of arguments ' \
20-
"given (#{arguments.size} for 1)") if arguments.empty?
21+
if arguments.empty?
22+
raise(Puppet::ParseError, 'array_suffix(): Wrong number of arguments ' \
23+
"given (#{arguments.size} for 1)")
24+
end
2125

2226
array = arguments[0]
2327

24-
unless array.is_a?(Array)
25-
raise Puppet::ParseError, "array_suffix(): expected first argument to be an Array, got #{array.inspect}"
26-
end
28+
raise Puppet::ParseError, "array_suffix(): expected first argument to be an Array, got #{array.inspect}" unless array.is_a?(Array)
2729

2830
suffix = arguments[1] if arguments[1]
2931

30-
if suffix
31-
unless suffix.is_a? String
32-
raise Puppet::ParseError, "array_suffix(): expected second argument to be a String, got #{suffix.inspect}"
33-
end
34-
end
32+
raise Puppet::ParseError, "array_suffix(): expected second argument to be a String, got #{suffix.inspect}" if suffix && !(suffix.is_a? String)
3533

3634
# Turn everything into string same as join would do ...
37-
result = array.collect do |i|
35+
result = array.map do |i|
3836
i = i.to_s
3937
suffix ? i + suffix : i
4038
end

lib/puppet/parser/functions/concat_merge.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
# frozen_string_literal: true
2+
13
# Top-level Puppet functions
24
module Puppet::Parser::Functions
35
newfunction(
46
:concat_merge,
5-
:type => :rvalue,
6-
:doc => <<-'ENDHEREDOC') do |args|
7+
type: :rvalue,
8+
doc: <<-'ENDHEREDOC') do |args|
79
Merges two or more hashes together concatenating duplicate keys
810
with array values and returns the resulting hash.
911
@@ -21,9 +23,7 @@ module Puppet::Parser::Functions
2123
@return String
2224
ENDHEREDOC
2325

24-
if args.length < 2
25-
raise Puppet::ParseError, "concat_merge(): wrong number of arguments (#{args.length}; must be at least 2)"
26-
end
26+
raise Puppet::ParseError, "concat_merge(): wrong number of arguments (#{args.length}; must be at least 2)" if args.length < 2
2727

2828
concat_merge = proc do |hash1, hash2|
2929
hash1.merge(hash2) do |_key, old_value, new_value|
@@ -37,11 +37,9 @@ module Puppet::Parser::Functions
3737

3838
result = {}
3939
args.each do |arg|
40-
next if arg.is_a? String and arg.empty? # empty string is synonym for puppet's undef
40+
next if arg.is_a?(String) && arg.empty? # empty string is synonym for puppet's undef
4141
# If the argument was not a hash, skip it.
42-
unless arg.is_a?(Hash)
43-
raise Puppet::ParseError, "concat_merge: unexpected argument type #{arg.class}, only expects hash arguments"
44-
end
42+
raise Puppet::ParseError, "concat_merge: unexpected argument type #{arg.class}, only expects hash arguments" unless arg.is_a?(Hash)
4543

4644
result = concat_merge.call(result, arg)
4745
end

lib/puppet/parser/functions/deep_implode.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', '..'))
24

35
require 'puppet_x/elastic/deep_implode'
@@ -6,8 +8,8 @@
68
module Puppet::Parser::Functions
79
newfunction(
810
:deep_implode,
9-
:type => :rvalue,
10-
:doc => <<-'ENDHEREDOC') do |args|
11+
type: :rvalue,
12+
doc: <<-'ENDHEREDOC') do |args|
1113
Recursively flattens all keys of a hash into a dot-notated
1214
hash, deeply merging duplicate key values by natively combining
1315
them and returns the resulting hash.
@@ -29,15 +31,11 @@ module Puppet::Parser::Functions
2931
@return Hash
3032
ENDHEREDOC
3133

32-
if args.length != 1
33-
raise Puppet::ParseError, "deep_implode(): wrong number of arguments (#{args.length}; must be 1)"
34-
end
34+
raise Puppet::ParseError, "deep_implode(): wrong number of arguments (#{args.length}; must be 1)" if args.length != 1
3535

3636
arg = args[0]
3737

38-
unless arg.is_a? Hash
39-
raise Puppet::ParseError, 'deep_implode: unexpected argument type, only expects hashes'
40-
end
38+
raise Puppet::ParseError, 'deep_implode: unexpected argument type, only expects hashes' unless arg.is_a? Hash
4139

4240
return {} if arg.empty?
4341

lib/puppet/parser/functions/es_plugin_name.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..', '..'))
24

35
require 'puppet_x/elastic/plugin_parsing'
@@ -6,8 +8,8 @@
68
module Puppet::Parser::Functions
79
newfunction(
810
:es_plugin_name,
9-
:type => :rvalue,
10-
:doc => <<-'ENDHEREDOC') do |args|
11+
type: :rvalue,
12+
doc: <<-'ENDHEREDOC') do |args|
1113
Given a string, return the best guess at what the directory name
1214
will be for the given plugin. Any arguments past the first will
1315
be fallbacks (using the same logic) should the first fail.
@@ -29,7 +31,7 @@ module Puppet::Parser::Functions
2931
end
3032

3133
ret = args.select do |arg|
32-
arg.is_a? String and not arg.empty?
34+
arg.is_a?(String) && !arg.empty?
3335
end.first
3436

3537
if ret
Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,38 @@
1+
# frozen_string_literal: true
2+
13
# Top-level Puppet functions
24
module Puppet::Parser::Functions
35
newfunction(
46
:plugin_dir,
5-
:type => :rvalue,
6-
:doc => <<-EOS
7+
type: :rvalue,
8+
doc: <<-EOS
79
Extracts the end plugin directory of the name
810
911
@return String
1012
EOS
1113
) do |arguments|
12-
if arguments.empty?
13-
raise(Puppet::ParseError, 'plugin_dir(): No arguments given')
14-
elsif arguments.size > 2
15-
raise(Puppet::ParseError, "plugin_dir(): Too many arguments given (#{arguments.size})")
16-
else
17-
18-
unless arguments[0].is_a?(String)
19-
raise(Puppet::ParseError, 'plugin_dir(): Requires string as first argument')
20-
end
14+
raise(Puppet::ParseError, 'plugin_dir(): No arguments given') if arguments.empty?
15+
raise(Puppet::ParseError, "plugin_dir(): Too many arguments given (#{arguments.size})") if arguments.size > 2
16+
raise(Puppet::ParseError, 'plugin_dir(): Requires string as first argument') unless arguments[0].is_a?(String)
2117

22-
plugin_name = arguments[0]
23-
items = plugin_name.split('/')
18+
plugin_name = arguments[0]
19+
items = plugin_name.split('/')
2420

25-
return items[0] if items.count == 1
21+
return items[0] if items.count == 1
2622

27-
plugin = items[1]
28-
endname = if plugin.include?('-') # example elasticsearch-head
29-
if plugin.start_with?('elasticsearch-')
30-
plugin.gsub('elasticsearch-', '')
31-
elsif plugin.start_with?('es-')
32-
plugin.gsub('es-', '')
33-
else
34-
plugin
35-
end
23+
plugin = items[1]
24+
endname = if plugin.include?('-') # example elasticsearch-head
25+
if plugin.start_with?('elasticsearch-')
26+
plugin.gsub('elasticsearch-', '')
27+
elsif plugin.start_with?('es-')
28+
plugin.gsub('es-', '')
3629
else
3730
plugin
3831
end
32+
else
33+
plugin
34+
end
3935

40-
return endname
41-
end
36+
return endname
4237
end
4338
end

lib/puppet/provider/elastic_parsedfile.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
require 'puppet/provider/parsedfile'
24

35
# Parent class for Elasticsearch-based providers that need to access

lib/puppet/provider/elastic_plugin.rb

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# frozen_string_literal: true
2+
13
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', '..'))
24

35
require 'uri'
@@ -6,7 +8,6 @@
68

79
# Generalized parent class for providers that behave like Elasticsearch's plugin
810
# command line tool.
9-
# rubocop:disable Metrics/ClassLength
1011
class Puppet::Provider::ElasticPlugin < Puppet::Provider
1112
# Elasticsearch's home directory.
1213
#
@@ -41,16 +42,20 @@ def exists?
4142
# in one. Therefore, we need to find the first "sub" plugin that
4243
# indicates which version of x-pack this is.
4344
properties = properties_files.sort.map do |prop_file|
44-
IO.readlines(prop_file).map(&:strip).reject do |line|
45-
line.start_with?('#') or line.empty?
46-
end.map do |property|
45+
lines = File.readlines(prop_file).map(&:strip).reject do |line|
46+
line.start_with?('#') || line.empty?
47+
end
48+
lines = lines.map do |property|
4749
property.split('=')
48-
end.reject do |pairs|
49-
pairs.length != 2
50-
end.to_h
51-
end.find { |prop| prop.key? 'version' }
50+
end
51+
lines = lines.select do |pairs|
52+
pairs.length == 2
53+
end
54+
lines.to_h
55+
end
56+
properties = properties.find { |prop| prop.key? 'version' }
5257

53-
if properties and properties['version'] != plugin_version
58+
if properties && properties['version'] != plugin_version
5459
debug "Elasticsearch plugin #{@resource[:name]} not version #{plugin_version}, reinstalling"
5560
destroy
5661
return false
@@ -90,7 +95,7 @@ def install_args
9095
def proxy_args(url)
9196
parsed = URI(url)
9297
%w[http https].map do |schema|
93-
[:host, :port, :user, :password].map do |param|
98+
%i[host port user password].map do |param|
9499
option = parsed.send(param)
95100
"-D#{schema}.proxy#{param.to_s.capitalize}=#{option}" unless option.nil?
96101
end
@@ -137,7 +142,7 @@ def with_environment(&block)
137142
saved_vars = {}
138143

139144
# Use 'java_home' param if supplied, otherwise default to Elasticsearch shipped JDK
140-
env_vars['JAVA_HOME'] = if @resource[:java_home].nil? or @resource[:java_home] == ''
145+
env_vars['JAVA_HOME'] = if @resource[:java_home].nil? || @resource[:java_home] == ''
141146
"#{homedir}/jdk"
142147
else
143148
@resource[:java_home]

0 commit comments

Comments
 (0)