Skip to content

Commit

Permalink
SIRE-9538 MongoDB 6 support using mongosh instead of old mongo client
Browse files Browse the repository at this point in the history
Reviewed-by: muspelkat
  • Loading branch information
Bernd Eilers committed Jun 22, 2023
1 parent 358830c commit 1d06cb3
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 173 deletions.
9 changes: 3 additions & 6 deletions lib/puppet/provider/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ def self.mongo_cmd(db, host, cmd)
end

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

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

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

out = nil
Expand All @@ -179,14 +178,12 @@ def self.mongo_eval(cmd, db = 'admin', retries = 2, host = nil)
end
end


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

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

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

Expand Down
17 changes: 1 addition & 16 deletions lib/puppet/provider/mongodb_database/mongodb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,7 @@
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("JSON.stringify(db.getMongo().getDBs())")
Puppet.warning "#### DBS #{mongo_eval_result} ####"

dbs = JSON.parse mongo_eval_result

Expand All @@ -45,7 +30,7 @@ def self.prefetch(resources)

def create
if db_ismaster
out = mongo_eval('db.dummyData.insert({"created_by_puppet": 1})', @resource[:name])
out = mongo_eval('db.dummyData.insertOne({"created_by_puppet": 1})', @resource[:name])
raise "Failed to create DB '#{@resource[:name]}'\n#{out}" if %r{writeError} =~ out
else
Puppet.warning 'Database creation is available only from master host'
Expand Down
2 changes: 1 addition & 1 deletion 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')) || status.include?('command replSetGetStatus requires authentication')
if auth_enabled && status.key?('errmsg') && (status['errmsg'].include?('unauthorized') || status['errmsg'].include?('not authorized') || status['errmsg'].include?('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
18 changes: 0 additions & 18 deletions lib/puppet/util/mongodb_output.rb

This file was deleted.

15 changes: 8 additions & 7 deletions manifests/server.pp
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,13 @@
$admin_password
}
if $create_admin and ($service_ensure == 'running' or $service_ensure == true) {
mongodb::db { 'admin':
user => $admin_username,
auth_mechanism => $admin_auth_mechanism,
password => $admin_password_unsensitive,
roles => $admin_roles,
update_password => $admin_update_password,
mongodb_user { "admin user":

Check warning on line 130 in manifests/server.pp

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

double quoted string containing no variables (check: double_quoted_strings)
ensure => present,
username => $admin_username,
database => 'admin',
roles => $admin_roles,
auth_mechanism => $auth_mechanism,

Check warning on line 135 in manifests/server.pp

View workflow job for this annotation

GitHub Actions / Puppet / Static validations

top-scope variable being used without an explicit namespace (check: variable_scope)
password => $admin_password,
}

# Make sure it runs before other DB creation
Expand Down Expand Up @@ -170,7 +171,7 @@

# Make sure that the ordering is correct
if $create_admin {
Class['mongodb::replset'] -> Mongodb::Db['admin']
Class['mongodb::replset'] -> Mongodb_user['admin user']
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions manifests/server/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,10 @@
$admin_password
}
if $handle_creds {
if versioncmp($version, '6.0') >= 0 {
$major_version_greater6 = true
if versioncmp($version, '5.0') >= 0 {
$major_version_greater5 = true
} else {
$major_version_greater6 = false
$major_version_greater5 = false
}
file { $rcfile:
ensure => file,
Expand Down
2 changes: 1 addition & 1 deletion manifests/server/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}
unless defined(Package[$mongosh_package_name]) {
package { 'mongodb_mongosh':
ensure => latest,
ensure => present,
name => $mongosh_package_name,
tag => 'mongodb_mongosh_package',
}
Expand Down
67 changes: 0 additions & 67 deletions spec/unit/puppet/util/mongodb_output_spec.rb

This file was deleted.

49 changes: 0 additions & 49 deletions templates/mongorc.js.erb

This file was deleted.

8 changes: 3 additions & 5 deletions templates/mongoshrc.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (authRequired()) {
<%- if @replset -%>
// rs.slaveOk + rs secondaryOk have been deprecated, use setReadPref when running mongodb version >=5
try {
<%- if @major_version_greater6 -%>
<%- if @major_version_greater5 -%>
db.getMongo().setReadPref('nearest')
<%- else -%>
rs.secondaryOk()
Expand All @@ -56,10 +56,8 @@ if (authRequired()) {
}
<%- else -%>
try {
var prev_db = db
db = db.getSiblingDB('admin')
db.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive %>')
db = db.getSiblingDB(prev_db)
admin = db.getSiblingDB('admin')
admin.auth('<%= @admin_username %>', '<%= @admin_password_unsensitive %>')
}
catch (err) {
// This isn't catching authentication errors as I'd expect...
Expand Down

0 comments on commit 1d06cb3

Please sign in to comment.