Skip to content

Commit 411feeb

Browse files
author
Johan De Wit
committed
fix rubocop
1 parent a6f7f05 commit 411feeb

File tree

5 files changed

+27
-53
lines changed

5 files changed

+27
-53
lines changed

lib/puppet/provider/mongodb.rb

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ def self.mongosh_cmd(db, host, cmd)
101101
end
102102

103103
args += ['--eval', cmd]
104-
out = mongosh(args)
105-
out
104+
mongosh(args)
106105
end
107106

108107
def self.conn_string
@@ -141,17 +140,10 @@ def self.db_ismaster
141140
cmd_ismaster = mongoshrc_file + cmd_ismaster if mongoshrc_file
142141
db = 'admin'
143142

144-
full_command = if mongoshrc_file
145-
mongoshrc_file + cmd_ismaster
146-
else
147-
cmd_ismaster
148-
end
149143
begin
150-
res = mongosh_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
144+
res = mongosh_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
151145
rescue StandardError => e
152-
if self.auth_enabled && e.message =~ %r{Authentication failed}
153-
res = mongosh_cmd(db, conn_string, 'db.isMaster().ismaster').to_s.chomp
154-
end
146+
res = mongosh_cmd(db, conn_string, 'db.isMaster().ismaster').to_s.chomp if auth_enabled && e.message =~ %r{Authentication failed}
155147
end
156148

157149
res.eql?('true')
@@ -167,23 +159,18 @@ def self.auth_enabled(config = nil)
167159
end
168160

169161
def self.rs_initiated?
162+
# TODO: not used yet, generates a stack level to deep error
170163
cmd_status = "rs.status('localhost').set"
171164
cmd_status = mongoshrc_file + cmd_status if mongoshrc_file
172165
db = 'admin'
173-
res = mongosh_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
166+
res = mongosh_cmd(db, conn_string, cmd_status).to_s.split(%r{\n}).last.chomp
174167

175168
# Retry command without authentication when mongorc_file is set and authentication failed
176-
if mongorc_file && res =~ %r{Authentication failed}
177-
res = mongosh_cmd(db, conn_string, "rs.status('localhost').set").to_s.chomp
178-
end
169+
res = mongosh_cmd(db, conn_string, "rs.status('localhost').set").to_s.chomp if mongorc_file && res =~ %r{Authentication failed}
179170

180171
res == @resource[:name]
181172
end
182173

183-
def rs_initiated?
184-
self.rs_initiated?
185-
end
186-
187174
# Mongo Command Wrapper
188175
def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
189176
retry_count = retries
@@ -201,7 +188,7 @@ def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
201188
rescue StandardError => e
202189
# When using the rc file, we get this eror because in most cases the admin user is not created yet
203190
# Can/must we move this out of the resue block ?
204-
if self.auth_enabled && e.message =~ %r{Authentication failed}
191+
if auth_enabled && e.message =~ %r{Authentication failed}
205192
out = if host
206193
mongosh_cmd(db, host, no_auth_cmd)
207194
else

lib/puppet/provider/mongodb_database/mongodb.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ def self.instances
1616
new(name: db['name'],
1717
ensure: :present)
1818
end
19-
rescue => e
20-
Puppet.warning("Getting instances of mongodb_database failed: #{e}")
21-
[]
19+
rescue StandardError => e
20+
Puppet.warning("Getting instances of mongodb_database failed: #{e}")
21+
[]
2222
end
2323

2424
# Assign prefetched dbs based on name.
@@ -39,12 +39,12 @@ def create
3939
begin
4040
out = mongo_eval('db.dummyData.insertOne({"created_by_puppet": 1})', @resource[:name])
4141
rescue StandardError => e
42-
if auth_enabled && e.message =~ %r{not authorized on admin to execute commanda} && @resource[:name] == 'admin'
42+
if auth_enabled && e.message =~ %r{not authorized on admin to execute command} && @resource[:name] == 'admin'
4343
Puppet.warning 'Skipping database creation for admin, need admin user first when security is enabled'
4444
@property_hash[:ensure] = :present
4545
@property_hash[:name] = @resource[:name]
46-
else
47-
raise "Failed to create DB '#{@resource[:name]}'\n#{out}" if %r{writeError} =~ out
46+
elsif %r{writeError} =~ out
47+
raise "Failed to create DB '#{@resource[:name]}'\n#{out}"
4848
end
4949
end
5050
else

lib/puppet/provider/mongodb_replset/mongo.rb

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ def self.replset_properties
133133
conn_string = conn_string # rubocop:disable Lint/SelfAssignment
134134
begin
135135
output = mongo_command('rs.conf()', conn_string)
136+
if output['members']
137+
return {
138+
name: output['_id'], # replica set name
139+
ensure: :present,
140+
members: output['members'],
141+
settings: output['settings'],
142+
provider: :mongo
143+
}
144+
end
145+
nil
136146
rescue Puppet::ExecutionFailure => e
137147
if e.message =~ %r{command replSetGetConfig requires authentication} || e.message =~ %r{not authorized on admin to execute command}
138148
output = mongo_command('rs.status()', conn_string)
@@ -141,28 +151,16 @@ def self.replset_properties
141151
output['members'].each do |m|
142152
memb << { 'host' => m['name'] }
143153
end
144-
{
154+
return {
145155
name: output['set'],
146156
ensure: :present,
147157
members: memb,
148-
#settings: @resource[:settings],
149158
provider: :mongo
150159
}
151160
end
152-
else
153161
nil
154162
end
155163
end
156-
if output['members']
157-
return {
158-
name: output['_id'], # replica set name
159-
ensure: :present,
160-
members: output['members'],
161-
settings: output['settings'],
162-
provider: :mongo
163-
}
164-
end
165-
nil
166164
end
167165

168166
def get_hosts_status(members)
@@ -255,7 +253,6 @@ def set_members
255253
return
256254
end
257255

258-
#Puppet.debug("REPLICASET DEBUG BIG Is replicaset initiated ? #{rs_initiated?}")
259256
# When no replicaset is initiated yet, and authenticatoin is anabled,
260257
# mongo_eval still adds the mongorcsh.js. This gives an 'MongoServerError: Authentication failed.' error.
261258
# In this stage, we only can connect to localhost, and only rs.status() and rs.initiate() is possible.

spec/classes/mongos_spec.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,7 @@
2626
# install
2727
it { is_expected.to contain_class('mongodb::mongos::install') }
2828

29-
if facts[:os]['release']['major'] =~ %r{(10)}
30-
it { is_expected.to contain_package('mongodb_mongos').with_ensure('6.0').with_name(package_name).with_tag('mongodb_package') }
31-
else
32-
it { is_expected.to contain_package('mongodb_mongos').with_ensure('6.0').with_name(package_name).with_tag('mongodb_package') }
33-
end
29+
it { is_expected.to contain_package('mongodb_mongos').with_ensure('6.0').with_name(package_name).with_tag('mongodb_package') }
3430

3531
# config
3632
it { is_expected.to contain_class('mongodb::mongos::config') }
@@ -91,11 +87,7 @@
9187

9288
it { is_expected.to compile.with_all_deps }
9389

94-
if facts[:os]['release']['major'] =~ %r{(10)}
95-
it { is_expected.to contain_package('mongodb_mongos').with_name('mongo-foo').with_ensure('6.0').with_tag('mongodb_package') }
96-
else
97-
it { is_expected.to contain_package('mongodb_mongos').with_name('mongo-foo').with_ensure('6.0').with_tag('mongodb_package') }
98-
end
90+
it { is_expected.to contain_package('mongodb_mongos').with_name('mongo-foo').with_ensure('6.0').with_tag('mongodb_package') }
9991
end
10092

10193
context 'service_manage => false' do

spec/classes/server_spec.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@
4646
describe 'with defaults' do
4747
it_behaves_like 'server classes'
4848

49-
if facts[:os]['family'] == 'RedHat' || facts[:os]['family'] == 'Suse'
50-
it { is_expected.to contain_package('mongodb_server').with_ensure('6.0').with_name('mongodb-org-server').with_tag('mongodb_package') }
51-
elsif facts[:os]['release']['major'] =~ %r{(10)}
49+
if facts[:os]['family'] == 'RedHat' || facts[:os]['family'] == 'Suse' || facts[:os]['release']['major'] =~ %r{(10)}
5250
it { is_expected.to contain_package('mongodb_server').with_ensure('6.0').with_name('mongodb-org-server').with_tag('mongodb_package') }
5351
else
5452
it { is_expected.to contain_package('mongodb_server').with_ensure('6.0').with_name('mongodb-server').with_tag('mongodb_package') }

0 commit comments

Comments
 (0)