From 80f47d5450714457dba5819e5914762e3812ebf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sven=20B=C3=BCsing?= Date: Wed, 23 Sep 2020 14:47:27 +0200 Subject: [PATCH] fix: Use rs.secondaryOk on versions that deprecated rs.slaveOk 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. --- lib/puppet/provider/mongodb_database/mongodb.rb | 4 +++- spec/acceptance/replset_spec.rb | 4 ++-- .../puppet/provider/mongodb_database/mongodb_spec.rb | 2 +- templates/mongorc.js.erb | 10 ++++++++-- 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/lib/puppet/provider/mongodb_database/mongodb.rb b/lib/puppet/provider/mongodb_database/mongodb.rb index d7ec407f1..b306b9395 100644 --- a/lib/puppet/provider/mongodb_database/mongodb.rb +++ b/lib/puppet/provider/mongodb_database/mongodb.rb @@ -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'], diff --git a/spec/acceptance/replset_spec.rb b/spec/acceptance/replset_spec.rb index baeb701b4..f47b218f4 100644 --- a/spec/acceptance/replset_spec.rb +++ b/spec/acceptance/replset_spec.rb @@ -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 @@ -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 diff --git a/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb b/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb index de6d5f717..332c78263 100644 --- a/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb +++ b/spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb @@ -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 diff --git a/templates/mongorc.js.erb b/templates/mongorc.js.erb index 2d34cb2f9..2f683acb1 100644 --- a/templates/mongorc.js.erb +++ b/templates/mongorc.js.erb @@ -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 %>')