Skip to content

Commit 7630480

Browse files
Johan De Witstevenpost
authored andcommitted
Replace mongo shell with mongosh
1 parent e39337c commit 7630480

File tree

21 files changed

+86
-87
lines changed

21 files changed

+86
-87
lines changed

REFERENCE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ The following parameters are available in the `mongodb::globals` class:
8787

8888
* [`server_package_name`](#-mongodb--globals--server_package_name)
8989
* [`client_package_name`](#-mongodb--globals--client_package_name)
90+
* [`client_version`](#-mongodb--globals--client_version)
9091
* [`mongod_service_manage`](#-mongodb--globals--mongod_service_manage)
9192
* [`service_enable`](#-mongodb--globals--service_enable)
9293
* [`service_ensure`](#-mongodb--globals--service_ensure)
@@ -124,6 +125,14 @@ Data type: `Any`
124125

125126

126127

128+
Default value: `undef`
129+
130+
##### <a name="-mongodb--globals--client_version"></a>`client_version`
131+
132+
Data type: `Any`
133+
134+
Version of the client package to install.
135+
127136
Default value: `undef`
128137

129138
##### <a name="-mongodb--globals--mongod_service_manage"></a>`mongod_service_manage`

lib/facter/is_master.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ def get_options_from_config(file)
3939

4040
Facter.add('mongodb_is_master') do
4141
setcode do
42-
if %w[mongo mongod].all? { |m| Facter::Util::Resolution.which m }
42+
if %w[mongosh mongod].all? { |m| Facter::Util::Resolution.which m }
4343
file = mongod_conf_file
4444
if file
4545
options = get_options_from_config(file)
46-
e = File.exist?('/root/.mongorc.js') ? 'load(\'/root/.mongorc.js\'); ' : ''
46+
e = File.exist?('/root/.mongoshrc.js') ? 'load(\'/root/.mongoshrc.js\'); ' : ''
4747

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

5151
if $CHILD_STATUS.success?
52-
Facter::Core::Execution.exec("mongo --quiet #{options} --eval \"#{e}db.isMaster().ismaster\"")
52+
Facter::Core::Execution.exec("mongosh --quiet #{options} --eval \"#{e}db.isMaster().ismaster\"")
5353
else
5454
'not_responding'
5555
end

lib/puppet/provider/mongodb.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
class Puppet::Provider::Mongodb < Puppet::Provider
99
# Without initvars commands won't work.
1010
initvars
11-
commands mongo: 'mongo'
11+
commands mongosh: 'mongosh'
1212

1313
# Optional defaults file
14-
def self.mongorc_file
15-
"load('#{Facter.value(:root_home)}/.mongorc.js'); " if File.file?("#{Facter.value(:root_home)}/.mongorc.js")
14+
def self.mongoshrc_file
15+
"load('#{Facter.value(:root_home)}/.mongoshrc.js'); " if File.file?("#{Facter.value(:root_home)}/.mongoshrc.js")
1616
end
1717

18-
def mongorc_file
19-
self.class.mongorc_file
18+
def mongoshrc_file
19+
self.class.mongoshrc_file
2020
end
2121

2222
def self.mongod_conf_file
@@ -74,7 +74,7 @@ def self.tls_invalid_hostnames(config = nil)
7474
config['tlsallowInvalidHostnames']
7575
end
7676

77-
def self.mongo_cmd(db, host, cmd)
77+
def self.mongosh_cmd(db, host, cmd)
7878
config = mongo_conf
7979

8080
args = [db, '--quiet', '--host', host]
@@ -101,7 +101,7 @@ def self.mongo_cmd(db, host, cmd)
101101
end
102102

103103
args += ['--eval', cmd]
104-
mongo(args)
104+
mongosh(args)
105105
end
106106

107107
def self.conn_string
@@ -137,9 +137,9 @@ def self.conn_string
137137

138138
def self.db_ismaster
139139
cmd_ismaster = 'db.isMaster().ismaster'
140-
cmd_ismaster = mongorc_file + cmd_ismaster if mongorc_file
140+
cmd_ismaster = mongoshrc_file + cmd_ismaster if mongoshrc_file
141141
db = 'admin'
142-
res = mongo_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
142+
res = mongosh_cmd(db, conn_string, cmd_ismaster).to_s.split(%r{\n}).last.chomp
143143
res.eql?('true')
144144
end
145145

@@ -156,14 +156,14 @@ def self.auth_enabled(config = nil)
156156
def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
157157
retry_count = retries
158158
retry_sleep = 3
159-
cmd = mongorc_file + cmd if mongorc_file
159+
cmd = mongoshrc_file + cmd if mongoshrc_file
160160

161161
out = nil
162162
begin
163163
out = if host
164-
mongo_cmd(db, host, cmd)
164+
mongosh_cmd(db, host, cmd)
165165
else
166-
mongo_cmd(db, conn_string, cmd)
166+
mongosh_cmd(db, conn_string, cmd)
167167
end
168168
rescue StandardError => e
169169
retry_count -= 1

lib/puppet/provider/mongodb_database/mongodb.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
def self.instances
1010
require 'json'
1111

12-
pre_cmd = 'try { rs.secondaryOk() } catch (err) { rs.slaveOk() }'
13-
dbs = JSON.parse mongo_eval("#{pre_cmd};printjson(db.getMongo().getDBs())")
12+
pre_cmd = 'db.getMongo().setReadPref("primaryPreferred")'
13+
dbs = JSON.parse mongo_eval("#{pre_cmd};EJSON.stringify(db.getMongo().getDBs())")
1414

1515
dbs['databases'].map do |db|
1616
new(name: db['name'],

lib/puppet/provider/mongodb_shard/mongo.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
mk_resource_methods
1515

16-
commands mongo: 'mongo'
16+
commands mongosh: 'mongosh'
1717

1818
def initialize(value = {})
1919
super(value)
@@ -152,8 +152,8 @@ def self.mongo_command(command, host = nil, _retries = 4)
152152
args = []
153153
args << '--quiet'
154154
args << ['--host', host] if host
155-
args << ['--eval', "printjson(#{command})"]
156-
output = mongo(args.flatten)
155+
args << ['--eval', "EJSON.stringify(#{command})"]
156+
output = mongosh(args.flatten)
157157
rescue Puppet::ExecutionFailure => e
158158
raise unless e =~ %r{Error: couldn't connect to server} && wait <= (2**max_wait)
159159

lib/puppet/provider/mongodb_user/mongodb.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ def self.instances
1010
require 'json'
1111

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

1717
out = mongo_eval(script)
18-
19-
return [] if auth_enabled && out.include?('not authorized on admin')
18+
return [] if auth_enabled && (out.include?('requires authentication') || out.include?('not authorized on admin'))
2019

2120
users = JSON.parse out
2221

manifests/client/params.pp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,10 @@
33
# @api private
44
#
55
class mongodb::client::params inherits mongodb::globals {
6-
$package_ensure = pick($mongodb::globals::version, 'present')
6+
$package_ensure = pick($mongodb::globals::client_version, 'present')
77
$manage_package = pick($mongodb::globals::manage_package, $mongodb::globals::manage_package_repo, false)
88

9-
if $manage_package {
10-
$package_name = "mongodb-${mongodb::globals::edition}-shell"
11-
} else {
12-
$package_name = $facts['os']['family'] ? {
13-
'Debian' => 'mongodb-clients',
14-
'Redhat' => "mongodb-${mongodb::globals::edition}-shell",
15-
default => 'mongodb',
16-
}
17-
}
9+
# the new mongosh package is the same for all distros.
10+
# and it follows its own versioning
11+
$package_name = 'mongodb-mongosh'
1812
}

manifests/globals.pp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#
33
# @param server_package_name
44
# @param client_package_name
5+
# @param client_version
6+
# Version of the client package to install.
57
# @param mongod_service_manage
68
# @param service_enable
79
# @param service_ensure
@@ -28,6 +30,7 @@
2830
class mongodb::globals (
2931
$server_package_name = undef,
3032
$client_package_name = undef,
33+
$client_version = undef,
3134

3235
$mongod_service_manage = undef,
3336
$service_enable = undef,

manifests/params.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
]
2323
$handle_creds = true
2424
$store_creds = false
25-
$rcfile = "${facts['root_home']}/.mongorc.js"
25+
$rcfile = "${facts['root_home']}/.mongoshrc.js"
2626
$dbpath_fix = false
2727

2828
$manage_package = pick($mongodb::globals::manage_package, $mongodb::globals::manage_package_repo, false)

manifests/server/config.pp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
if $handle_creds {
180180
file { $rcfile:
181181
ensure => file,
182-
content => template('mongodb/mongorc.js.erb'),
182+
content => template('mongodb/mongoshrc.js.erb'),
183183
owner => 'root',
184184
group => 'root',
185185
mode => '0600',

0 commit comments

Comments
 (0)