Skip to content

Commit

Permalink
fix: Use rs.secondaryOk on versions that deprecated rs.slaveOk
Browse files Browse the repository at this point in the history
rs.slaveOk has been deprecated and warnings are all over the place e.g.
in the mongodb_is_master fact.
Which makes the fact and /root/.mongrc.js unusable.

Use rs.secondaryOK by default and fall back gracefully on versions which
only support rs.slaveOk yet.
  • Loading branch information
svenbs committed Oct 6, 2020
1 parent dd349b5 commit 80f47d5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/puppet/provider/mongodb_database/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

def self.instances
require 'json'
dbs = JSON.parse mongo_eval('rs.slaveOk();printjson(db.getMongo().getDBs())')

pre_cmd = 'try { rs.secondaryOk() } catch (err) { rs.slaveOk() }'
dbs = JSON.parse mongo_eval(pre_cmd + ';printjson(db.getMongo().getDBs())')

dbs['databases'].map do |db|
new(name: db['name'],
Expand Down
4 changes: 2 additions & 2 deletions spec/acceptance/replset_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class { 'mongodb::client': }

it 'checks the data on the slave' do
sleep(10)
on hosts_as('slave'), %{mongo --verbose --eval 'rs.slaveOk(); printjson(db.test.findOne({name:"test1"}))'} do |r|
on hosts_as('slave'), %{mongo --verbose --eval 'try { rs.secondaryOk() } catch (err) { rs.slaveOk() }; printjson(db.test.findOne({name:"test1"}))'} do |r|
expect(r.stdout).to match %r{some value}
end
end
Expand Down Expand Up @@ -194,7 +194,7 @@ class { 'mongodb::server':

it 'checks the data on the slave' do
sleep(10)
on hosts_as('slave'), %{mongo test --verbose --eval 'load("/root/.mongorc.js");rs.slaveOk();printjson(db.dummyData.findOne())'} do |r|
on hosts_as('slave'), %{mongo test --verbose --eval 'load("/root/.mongorc.js");try { rs.secondaryOk() } catch (err) { rs.slaveOk() };printjson(db.dummyData.findOne())'} do |r|
expect(r.stdout).to match %r{created_by_puppet}
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
tmp = Tempfile.new('test')
mongodconffile = tmp.path
allow(provider.class).to receive(:mongod_conf_file).and_return(mongodconffile)
allow(provider.class).to receive(:mongo_eval).with('rs.slaveOk();printjson(db.getMongo().getDBs())').and_return(raw_dbs)
allow(provider.class).to receive(:mongo_eval).with('try { rs.secondaryOk() } catch (err) { rs.slaveOk() };printjson(db.getMongo().getDBs())').and_return(raw_dbs)
allow(provider.class).to receive(:db_ismaster).and_return(true)
end

Expand Down
10 changes: 8 additions & 2 deletions templates/mongorc.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ function authRequired() {
}

if (authRequired()) {
<%- if @replset -%>
// rs.slaveOk has been deprecated, use secondaryOk if available
try {
<% if @replset -%>
rs.secondaryOk()
}
catch (err) {
rs.slaveOk()
<% end -%>
}
<%- end -%>
try {
var prev_db = db
db = db.getSiblingDB('admin')
db.auth('<%= @admin_username %>', '<%= @admin_password %>')
Expand Down

0 comments on commit 80f47d5

Please sign in to comment.