Skip to content

Commit

Permalink
SIRE-9538 fix replset initiation with new mogosh client
Browse files Browse the repository at this point in the history
Reviewed-by: muspelkat
  • Loading branch information
Bernd Eilers committed Jun 21, 2023
1 parent 58882c9 commit 358830c
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 32 deletions.
2 changes: 1 addition & 1 deletion lib/facter/is_master.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def get_options_from_config(file)
e = File.exist?('/root/.mongorc.js') ? 'load(\'/root/.mongorc.js\'); ' : ''

# Check if the mongodb server is responding:
Facter::Core::Execution.exec("mongo --quiet #{options} --eval \"#{e}printjson(db.adminCommand({ ping: 1 }))\"")
Facter::Core::Execution.exec("mongo --quiet #{options} --eval \"#{e}EJSON.stringify(db.adminCommand({ ping: 1 }))\"")

if $CHILD_STATUS.success?
Facter::Core::Execution.exec("mongo --quiet #{options} --eval \"#{e}db.isMaster().ismaster\"")
Expand Down
12 changes: 8 additions & 4 deletions lib/puppet/provider/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def self.mongo_cmd(db, host, cmd)
end

args += ['--eval', "\"#{cmd}\""]
Puppet.warning "mongosh: #{args}"
mongo(args)
end

Expand Down Expand Up @@ -156,9 +157,9 @@ def self.auth_enabled(config = nil)
end

# Mongo Command Wrapper
def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
def self.mongo_eval(cmd, db = 'admin', retries = 2, host = nil)
retry_count = retries
retry_sleep = 3
retry_sleep = 1
cmd = mongorc_file + cmd if mongorc_file

out = nil
Expand All @@ -172,17 +173,20 @@ def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
retry_count -= 1
if retry_count.positive?
Puppet.debug "Request failed: '#{e.message}' Retry: '#{retries - retry_count}'"
out = { 'errmsg' => "#{e.message}" }
sleep retry_sleep
retry
end
end


raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd}" unless out

Puppet::Util::MongodbOutput.sanitize(out)
# Puppet::Util::MongodbOutput.sanitize(out)
out
end

def mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
def mongo_eval(cmd, db = 'admin', retries = 2, host = nil)
self.class.mongo_eval(cmd, db, retries, host)
end

Expand Down
32 changes: 17 additions & 15 deletions lib/puppet/provider/mongodb_database/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@
def self.instances
require 'json'

pre_cmd = '
try {
version=parseInt(db.version().split(\'.\')[0])
if (version>=5){
db.getMongo().setReadPref(\'nearest\')
}
else{
rs.secondaryOk()
}
}
catch (err) {
rs.slaveOk()
}
'.squeeze(' ')
mongo_eval_result = mongo_eval("#{pre_cmd};JSON.stringify(db.getMongo().getDBs())")
# pre_cmd = '
# try {
# version=parseInt(db.version().split(\'.\')[0])
# if (version>=5){
# db.getMongo().setReadPref(\'nearest\')
# }
# else{
# rs.secondaryOk()
# }
# }
# catch (err) {
# rs.slaveOk()
# }
# '.squeeze(' ')
mongo_eval_result = mongo_eval("JSON.stringify(db.getMongo().getDBs())")
Puppet.warning "#### DBS #{mongo_eval_result} ####"

dbs = JSON.parse mongo_eval_result

dbs['databases'].map do |db|
Expand Down
8 changes: 6 additions & 2 deletions lib/puppet/provider/mongodb_replset/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def get_hosts_status(members)
status = rs_status(host)
raise Puppet::Error, "Can't configure replicaset #{name}, host #{host} is not supposed to be part of a replicaset." if status.key?('errmsg') && status['errmsg'] == 'not running with --replSet'

if auth_enabled && status.key?('errmsg') && (status['errmsg'].include?('unauthorized') || status['errmsg'].include?('not authorized') || status['errmsg'].include?('requires authentication'))
if auth_enabled && status.key?('errmsg') && (status['errmsg'].include?('unauthorized') || status['errmsg'].include?('not authorized') || status['errmsg'].include?('requires authentication')) || status.include?('command replSetGetStatus requires authentication')
Puppet.warning "Host #{host} is available, but you are unauthorized because of authentication is enabled: #{auth_enabled}"
alive.push(member)
end
Expand Down Expand Up @@ -394,6 +394,10 @@ def self.mongo_command(command, host = nil, retries = 4)
output = '{}' if output == "\nnull\n"

# Parse the JSON output and return
JSON.parse(output)
begin
JSON.parse(output)
rescue
output
end
end
end
2 changes: 1 addition & 1 deletion lib/puppet/provider/mongodb_shard/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def self.mongo_command(command, host = nil, _retries = 4)
args = []
args << '--quiet'
args << ['--host', host] if host
args << ['--eval', "printjson(#{command})"]
args << ['--eval', "EJSON.stringify(#{command})"]
output = mongo(args.flatten)
rescue Puppet::ExecutionFailure => e
raise unless e =~ %r{Error: couldn't connect to server} && wait <= (2**max_wait)
Expand Down
2 changes: 1 addition & 1 deletion lib/puppet/provider/mongodb_user/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def self.instances
require 'json'

if db_ismaster
script = 'printjson(db.system.users.find().toArray())'
script = 'EJSON.stringify(db.system.users.find().toArray())'
# A hack to prevent prefetching failures until admin user is created
script = "try {#{script}} catch (e) { if (e.message.match(/not authorized on admin/)) { 'not authorized on admin' } else {throw e}}" if auth_enabled

Expand Down
6 changes: 6 additions & 0 deletions manifests/server/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
$tls_invalid_hostnames = $mongodb::server::tls_invalid_hostnames
$tls_mode = $mongodb::server::tls_mode
$storage_engine = $mongodb::server::storage_engine
$version = $mongodb::globals::version

File {
owner => $user,
Expand Down Expand Up @@ -174,6 +175,11 @@
$admin_password
}
if $handle_creds {
if versioncmp($version, '6.0') >= 0 {
$major_version_greater6 = true
} else {
$major_version_greater6 = false
}
file { $rcfile:
ensure => file,
content => template('mongodb/mongoshrc.js.erb'),
Expand Down
12 changes: 10 additions & 2 deletions manifests/server/install.pp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# PRIVATE CLASS: do not call directly
class mongodb::server::install {
$package_ensure = $mongodb::server::package_ensure
$package_name = $mongodb::server::package_name
$package_ensure = $mongodb::server::package_ensure
$package_name = $mongodb::server::package_name
$mongosh_package_name = $mongodb::server::mongosh_package_name

case $package_ensure {
true: {
Expand Down Expand Up @@ -33,4 +34,11 @@
tag => 'mongodb_package',
}
}
unless defined(Package[$mongosh_package_name]) {
package { 'mongodb_mongosh':
ensure => latest,
name => $mongosh_package_name,
tag => 'mongodb_mongosh_package',
}
}
}
9 changes: 3 additions & 6 deletions templates/mongoshrc.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,12 @@ if (authRequired()) {
<%- if @replset -%>
// rs.slaveOk + rs secondaryOk have been deprecated, use setReadPref when running mongodb version >=5
try {
version=parseInt(db.version().split('.')[0])
if (version>=5){
<%- if @major_version_greater6 -%>
db.getMongo().setReadPref('nearest')
}
else{
<%- else -%>
rs.secondaryOk()
}
<%- end -%>
}

catch (err) {
rs.slaveOk()
}
Expand Down

0 comments on commit 358830c

Please sign in to comment.