Skip to content

Commit 5014ec8

Browse files
committed
Fix all remaining rubocop & foodcritic errors
Fix all remaining rubocop & foodcritic errors, fixes sous-chefs#365.
1 parent 749a56b commit 5014ec8

26 files changed

+144
-154
lines changed

.rubocop.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Encoding:
1616
LineLength:
1717
Max: 180
1818

19+
# Longer classes aren't _so_ bad
20+
Metrics/ClassLength:
21+
Max: 125
22+
1923
# Increase allowed lines in a method. Short methods are good, but 10 lines
2024
# is a bit too low.
2125
MethodLength:
@@ -30,9 +34,12 @@ RedundantReturn:
3034
IfUnlessModifier:
3135
Enabled: false
3236

33-
# Raise allowed CyclomaticComplexity to 10.
37+
# Raise allowed CyclomaticComplexity & Perceivedto 10.
3438
CyclomaticComplexity:
35-
Max: 10
39+
Max: 15
40+
41+
Metrics/PerceivedComplexity:
42+
Max: 15
3643

3744
# Disable Single Space before first arg
3845
SingleSpaceBeforeFirstArg:
@@ -46,6 +53,10 @@ WordArray:
4653
UnusedBlockArgument:
4754
Enabled: false
4855

56+
# allow both hash syntaxes
57+
Style/HashSyntax:
58+
Enabled: false
59+
4960
# There are too many non-ruby files that run up against rubocop rules in a cookbook
5061
AllCops:
5162
Include:

Rakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ rescue Exception => e
6666
if e.class.to_s =~ /^Timeout::|^Kitchen::|LoadError/
6767
STDERR.puts "[!] Omitting Kitchen tasks [#{e.class}: #{e.message} at #{e.backtrace.first}]\n\n"
6868
else
69-
raise e
69+
fail e
7070
end
7171
end

attributes/default.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
default['elasticsearch']['install_type'] = 'tarball'
44

55
# platform_family keyed download URLs
6-
default['elasticsearch']['download_urls']['debian'] = "https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-%s.deb"
7-
default['elasticsearch']['download_urls']['rhel'] = "https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-%s.noarch.rpm"
8-
default['elasticsearch']['download_urls']['tar'] = "https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-%s.tar.gz"
6+
default['elasticsearch']['download_urls']['debian'] = 'https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-%s.deb'
7+
default['elasticsearch']['download_urls']['rhel'] = 'https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-%s.noarch.rpm'
8+
default['elasticsearch']['download_urls']['tar'] = 'https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-%s.tar.gz'
99

1010
# platform_family keyed download checksums
1111
default['elasticsearch']['checksums']['1.4.5']['debian'] = '68dce951181e9802e94fd83b894f4b628394fc44bb01c77eb61fdbd1940d94b5'

libraries/helpers.rb

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module ElasticsearchCookbook
2+
# Helper methods included by various providers and passed to the template engine
23
module Helpers
3-
44
def determine_version(new_resource, node)
55
if new_resource.version
66
new_resource.version.to_s
77
elsif node['elasticsearch'] && node['elasticsearch']['version']
88
node['elasticsearch']['version'].to_s
99
else
10-
raise 'could not determine version of elasticsearch to install'
10+
fail 'could not determine version of elasticsearch to install'
1111
end
1212
end
1313

@@ -17,7 +17,7 @@ def determine_install_type(new_resource, node)
1717
elsif node['elasticsearch'] && node['elasticsearch']['install_type']
1818
node['elasticsearch']['install_type'].to_s
1919
else
20-
raise 'could not determine how to install elasticsearch (package? tarball?)'
20+
fail 'could not determine how to install elasticsearch (package? tarball?)'
2121
end
2222
end
2323

@@ -57,15 +57,15 @@ def determine_download_checksum(new_resource, node)
5757
end
5858

5959
def get_tarball_home_dir(new_resource, node)
60-
new_resource.dir || node.ark[:prefix_home]
60+
new_resource.dir || node['ark']['prefix_home']
6161
end
6262

6363
def get_tarball_root_dir(new_resource, node)
64-
new_resource.dir || node.ark[:prefix_root]
64+
new_resource.dir || node['ark']['prefix_root']
6565
end
6666

6767
# This method takes a hash, but will convert to mash
68-
def print_value(data, key, options={})
68+
def print_value(data, key, options = {})
6969
separator = options[:separator] || ': '
7070

7171
final_value = format_value(find_value(data, key))
@@ -86,16 +86,15 @@ def find_value(data, key)
8686
end
8787

8888
def format_value(value)
89-
unless value.nil?
90-
if value.is_a?(Array)
91-
value.join(',').to_s
92-
elsif value.respond_to?(:empty?) && value.empty?
93-
nil # anything that answers to empty? should be nil again
94-
else
95-
value.to_s
96-
end
89+
if value.nil?
90+
nil # just pass through nil
91+
elsif value.is_a?(Array)
92+
value.join(',').to_s
93+
elsif value.respond_to?(:empty?) && value.empty?
94+
nil # anything that answers to empty? should be nil again
95+
else
96+
value.to_s
9797
end
9898
end
99-
10099
end
101100
end

libraries/matchers.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,42 @@
11
# ChefSpec is a tool to unit test cookbooks in conjunction with rspec
2-
# Learn more on the README or at https://github.com/sethvargo/chefspec.
2+
# Learn more on the README or at https://github.com/sethvargo/chefspec.
33
if defined?(ChefSpec)
44
def create_elasticsearch_user(resource_name)
55
ChefSpec::Matchers::ResourceMatcher.new(:elasticsearch_user, :create, resource_name)
66
end
7+
78
def remove_elasticsearch_user(resource_name)
89
ChefSpec::Matchers::ResourceMatcher.new(:elasticsearch_user, :remove, resource_name)
910
end
11+
1012
def install_elasticsearch(resource_name)
1113
ChefSpec::Matchers::ResourceMatcher.new(:elasticsearch_install, :install, resource_name)
1214
end
15+
1316
def remove_elasticsearch(resource_name)
1417
ChefSpec::Matchers::ResourceMatcher.new(:elasticsearch_install, :remove, resource_name)
1518
end
19+
1620
def manage_elasticsearch_configure(resource_name)
1721
ChefSpec::Matchers::ResourceMatcher.new(:elasticsearch_configure, :manage, resource_name)
1822
end
23+
1924
def remove_elasticsearch_configure(resource_name)
2025
ChefSpec::Matchers::ResourceMatcher.new(:elasticsearch_configure, :remove, resource_name)
2126
end
27+
2228
def configure_elasticsearch_service(resource_name)
2329
ChefSpec::Matchers::ResourceMatcher.new(:elasticsearch_service, :configure, resource_name)
2430
end
31+
2532
def remove_elasticsearch_service(resource_name)
2633
ChefSpec::Matchers::ResourceMatcher.new(:elasticsearch_service, :remove, resource_name)
2734
end
35+
2836
def install_elasticsearch_plugin(resource_name)
2937
ChefSpec::Matchers::ResourceMatcher.new(:elasticsearch_plugin, :install, resource_name)
3038
end
39+
3140
def remove_elasticsearch_plugin(resource_name)
3241
ChefSpec::Matchers::ResourceMatcher.new(:elasticsearch_plugin, :remove, resource_name)
3342
end

libraries/provider_configure.rb

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ class Provider::ElasticsearchConfigure < Chef::Provider::LWRPBase
55

66
action :manage do
77
converge_by('configure elasticsearch instance') do
8-
touched_resources = []
9-
108
# if a subdir parameter is missing but dir is set, infer the subdir name
119
# then go and be sure it's also set in the YML hash if it wasn't given there
1210
if new_resource.dir && !new_resource.path_conf
@@ -32,13 +30,14 @@ class Provider::ElasticsearchConfigure < Chef::Provider::LWRPBase
3230

3331
# calculation for memory allocation; 50% or 31g, whatever is smaller
3432
unless new_resource.allocated_memory
35-
half = ((node.memory.total.to_i * 0.5 ).floor / 1024)
36-
new_resource.allocated_memory (half > 31000 ? "31g" : "#{half}m")
33+
half = ((node['memory']['total'].to_i * 0.5).floor / 1024)
34+
malloc_str = (half > 31_000 ? '31g' : "#{half}m")
35+
new_resource.allocated_memory malloc_str
3736
end
3837

3938
# Create ES directories
4039
#
41-
[ new_resource.path_conf, new_resource.path_logs ].each do |path|
40+
[new_resource.path_conf, new_resource.path_logs].each do |path|
4241
d = directory path do
4342
owner new_resource.user
4443
group new_resource.group
@@ -66,39 +65,37 @@ class Provider::ElasticsearchConfigure < Chef::Provider::LWRPBase
6665
new_resource.updated_by_last_action(true) if d.updated_by_last_action?
6766
end
6867

69-
shell_template = template "elasticsearch.in.sh" do
68+
shell_template = template 'elasticsearch.in.sh' do
7069
path "#{new_resource.path_conf}/elasticsearch.in.sh"
7170
source new_resource.template_elasticsearch_env
7271
cookbook 'elasticsearch'
7372
owner new_resource.user
7473
group new_resource.group
7574
mode 0755
76-
variables({
77-
java_home: new_resource.java_home,
78-
es_home: new_resource.es_home,
79-
es_config: new_resource.path_conf,
80-
allocated_memory: new_resource.allocated_memory,
81-
Xms: new_resource.allocated_memory,
82-
Xmx: new_resource.allocated_memory,
83-
Xss: new_resource.thread_stack_size,
84-
gc_settings: new_resource.gc_settings,
85-
env_options: new_resource.env_options
86-
})
75+
variables(java_home: new_resource.java_home,
76+
es_home: new_resource.es_home,
77+
es_config: new_resource.path_conf,
78+
allocated_memory: new_resource.allocated_memory,
79+
Xms: new_resource.allocated_memory,
80+
Xmx: new_resource.allocated_memory,
81+
Xss: new_resource.thread_stack_size,
82+
gc_settings: new_resource.gc_settings,
83+
env_options: new_resource.env_options)
8784
action :nothing
8885
end
8986
shell_template.run_action(:create)
9087
new_resource.updated_by_last_action(true) if shell_template.updated_by_last_action?
9188

9289
# Create ES logging file
9390
#
94-
logging_template = template "logging.yml" do
91+
logging_template = template 'logging.yml' do
9592
path "#{new_resource.path_conf}/logging.yml"
9693
source new_resource.template_logging_yml
9794
cookbook 'elasticsearch'
9895
owner new_resource.user
9996
group new_resource.group
10097
mode 0755
101-
variables({logging: new_resource.logging})
98+
variables(logging: new_resource.logging)
10299
action :nothing
103100
end
104101
logging_template.run_action(:create)
@@ -113,22 +110,20 @@ class Provider::ElasticsearchConfigure < Chef::Provider::LWRPBase
113110
Chef::Log.warn("Please change the following to strings in order to work with this Elasticsearch cookbook: #{found_symbols.join(',')}")
114111
end
115112

116-
yml_template = template "elasticsearch.yml" do
113+
yml_template = template 'elasticsearch.yml' do
117114
path "#{new_resource.path_conf}/elasticsearch.yml"
118115
source new_resource.template_elasticsearch_yml
119116
cookbook 'elasticsearch'
120117
owner new_resource.user
121118
group new_resource.group
122119
mode 0755
123120
helpers(ElasticsearchCookbook::Helpers)
124-
variables({
125-
config: merged_configuration
126-
})
121+
variables(config: merged_configuration)
127122
action :nothing
128123
end
129124
yml_template.run_action(:create)
130125
new_resource.updated_by_last_action(true) if yml_template.updated_by_last_action?
131126
end
132127
end
133-
end
134-
end
128+
end
129+
end

libraries/provider_install.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Provider::ElasticsearchInstall < Chef::Provider::LWRPBase
1515
elsif install_type == 'package'
1616
install_package_wrapper_action
1717
else
18-
raise "#{install_type} is not a valid install type"
18+
fail "#{install_type} is not a valid install type"
1919
end
2020
end
2121
end
@@ -28,7 +28,7 @@ class Provider::ElasticsearchInstall < Chef::Provider::LWRPBase
2828
elsif install_type == 'package'
2929
remove_package_wrapper_action
3030
else
31-
raise "#{install_type} is not a valid install type"
31+
fail "#{install_type} is not a valid install type"
3232
end
3333
end
3434
end
@@ -90,7 +90,7 @@ def install_tarball_wrapper_action
9090
include_recipe 'ark'
9191
include_recipe 'curl'
9292

93-
ark_r = ark "elasticsearch" do
93+
ark_r = ark 'elasticsearch' do
9494
url determine_download_url(new_resource, node)
9595
owner new_resource.owner
9696
group new_resource.group
@@ -105,7 +105,7 @@ def install_tarball_wrapper_action
105105
target = "#{new_resource.dir}/elasticsearch-#{determine_version(new_resource, node)}"
106106
binary = "#{target}/bin/elasticsearch"
107107

108-
::File.directory?(link) && ::File.symlink?(link) && ::File.readlink(link) == target && ::File.exists?(binary)
108+
::File.directory?(link) && ::File.symlink?(link) && ::File.readlink(link) == target && ::File.exist?(binary)
109109
end
110110
action :nothing
111111
end

libraries/provider_plugin.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11

22
class Chef
3+
# Chef Provider for installing an elasticsearch plugin
34
class Provider::ElasticsearchPlugin < Chef::Provider::LWRPBase
45
include ElasticsearchCookbook::Helpers
56
include Chef::Mixin::ShellOut
67

78
action :install do
89
name = new_resource.plugin_name
910
version = new_resource.version ? "/#{new_resource.version}" : nil
10-
url = new_resource.url ? " -url #{new_resource.url}" : nil
11+
url = new_resource.url ? " -url #{new_resource.url}" : nil
1112

12-
raise 'Could not determine the plugin directory. Please set plugin_dir on this resource.' unless new_resource.plugin_dir
13+
fail 'Could not determine the plugin directory. Please set plugin_dir on this resource.' unless new_resource.plugin_dir
1314
converge_by("install plugin #{name}") do
14-
15-
plugin_exists = Dir.entries(new_resource.plugin_dir).any? do |plugin|
16-
next if plugin =~ /^\./
17-
name.include? plugin
18-
end rescue false
15+
plugin_exists = begin
16+
Dir.entries(new_resource.plugin_dir).any? do |plugin|
17+
next if plugin =~ /^\./
18+
name.include? plugin
19+
end
20+
rescue
21+
false
22+
end
1923

2024
unless plugin_exists
2125
# automatically raises on error, logs command output

0 commit comments

Comments
 (0)