Skip to content

Commit f159b0e

Browse files
author
Johan De Wit
committed
fix unexpected token error due to changed output
1 parent 2a47714 commit f159b0e

File tree

12 files changed

+31
-23
lines changed

12 files changed

+31
-23
lines changed

lib/facter/is_master.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def get_options_from_config(file)
4646
e = File.exist?('/root/.mongoshrc.js') ? 'load(\'/root/.mongoshrc.js\'); ' : ''
4747

4848
# Check if the mongodb server is responding:
49-
Facter::Core::Execution.exec("mongosh --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?
5252
Facter::Core::Execution.exec("mongosh --quiet #{options} --eval \"#{e}db.isMaster().ismaster\"")

lib/puppet/provider/mongodb_database/mongodb.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def self.instances
1010
require 'json'
1111

1212
pre_cmd = 'db.getMongo().setReadPref("primaryPreferred")'
13-
dbs = JSON.parse mongo_eval("#{pre_cmd};printjson(db.getMongo().getDBs())")
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_replset/mongo.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def get_hosts_status(members)
161161
status = rs_status(host)
162162
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'
163163

164-
if auth_enabled && status.key?('errmsg') && (status['errmsg'].include?('requires authentication') || status['errmsg'].include?('requires authentication') || status['errmsg'].include?('requires authentication'))
164+
if auth_enabled && status.key?('errmsg') && (status['errmsg'].include?('requires authentication') || status['errmsg'].include?('not authorized on admin') || status['errmsg'].include?('requires authentication'))
165165
Puppet.warning "Host #{host} is available, but you are unauthorized because of authentication is enabled: #{auth_enabled}"
166166
alive.push(member)
167167
end
@@ -388,7 +388,7 @@ def mongo_command(command, host, retries = 4)
388388

389389
def self.mongo_command(command, host = nil, retries = 4)
390390
begin
391-
output = mongo_eval("printjson(#{command})", 'admin', retries, host)
391+
output = mongo_eval("EJSON.stringify(#{command})", 'admin', retries, host)
392392
rescue Puppet::ExecutionFailure => e
393393
Puppet.debug "Got an exception: #{e}"
394394
raise

lib/puppet/provider/mongodb_shard/mongo.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ 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})"]
155+
args << ['--eval', "EJSON.stringify(#{command})"]
156156
output = mongosh(args.flatten)
157157
rescue Puppet::ExecutionFailure => e
158158
raise unless e =~ %r{Error: couldn't connect to server} && wait <= (2**max_wait)

lib/puppet/provider/mongodb_user/mongodb.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,23 @@ 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(/requires authentication/)) { '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+
Puppet.debug("Result of out in self.instances: #{out}")
19+
Puppet.debug("Type of out in self.instances: #{out.class}")
20+
Puppet.debug("Methods of out in self.instances: #{out.methods}")
21+
Puppet.debug("String of out in self.instances: #{out.to_s} has type #{out.to_s.class}")
22+
Puppet.debug("Json of out in self.instances: #{out.to_json} has type #{out.to_s.class}")
1823

19-
return [] if auth_enabled && out.include?('requires authentication')
24+
25+
return [] if auth_enabled && (out.include?('requires authentication') || out.include?('not authorized on admin'))
2026

2127
users = JSON.parse out
28+
Puppet.debug("Result of users in self.instances: #{users}")
29+
Puppet.debug("Type of users in self.instances: #{users.class}")
2230

2331
users.map do |user|
2432
new(name: user['_id'],

spec/acceptance/database_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class { 'mongodb::server': }
2323
end
2424

2525
it 'creates the databases' do
26-
shell("mongo testdb1 --eval 'printjson(db.getMongo().getDBs())'")
27-
shell("mongo testdb2 --eval 'printjson(db.getMongo().getDBs())'")
26+
shell("mongo testdb1 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
27+
shell("mongo testdb2 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
2828
end
2929
end
3030

@@ -50,8 +50,8 @@ class { 'mongodb::server':
5050
end
5151

5252
it 'creates the database' do
53-
shell("mongo testdb1 --port 27018 --eval 'printjson(db.getMongo().getDBs())'")
54-
shell("mongo testdb2 --port 27018 --eval 'printjson(db.getMongo().getDBs())'")
53+
shell("mongo testdb1 --port 27018 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
54+
shell("mongo testdb2 --port 27018 --eval 'EJSON.stringify(db.getMongo().getDBs())'")
5555
end
5656
end
5757
end

spec/acceptance/replset_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class { 'mongodb::client': }
4848
}
4949
EOS
5050
apply_manifest_on(hosts_as('master'), pp, catch_failures: true)
51-
on(hosts_as('master'), 'mongo --quiet --eval "printjson(rs.conf())"') do |r|
51+
on(hosts_as('master'), 'mongo --quiet --eval "EJSON.stringify(rs.conf())"') do |r|
5252
expect(r.stdout).to match %r{#{hosts[0]}:27017}
5353
expect(r.stdout).to match %r{#{hosts[1]}:27017}
5454
end
@@ -60,14 +60,14 @@ class { 'mongodb::client': }
6060
end
6161

6262
it 'checks the data on the master' do
63-
on hosts_as('master'), %{mongo --verbose --eval 'printjson(db.test.findOne({name:"test1"}))'} do |r|
63+
on hosts_as('master'), %{mongo --verbose --eval 'EJSON.stringify(db.test.findOne({name:"test1"}))'} do |r|
6464
expect(r.stdout).to match %r{some value}
6565
end
6666
end
6767

6868
it 'checks the data on the slave' do
6969
sleep(10)
70-
on hosts_as('slave'), %{mongo --verbose --eval 'db.getMongo().setReadPref("primaryPreferred"); printjson(db.test.findOne({name:"test1"}))'} do |r|
70+
on hosts_as('slave'), %{mongo --verbose --eval 'db.getMongo().setReadPref("primaryPreferred"); EJSON.stringify(db.test.findOne({name:"test1"}))'} do |r|
7171
expect(r.stdout).to match %r{some value}
7272
end
7373
end
@@ -177,7 +177,7 @@ class { 'mongodb::server':
177177
EOS
178178
apply_manifest_on(hosts_as('master'), pp, catch_failures: true)
179179
apply_manifest_on(hosts_as('master'), pp, catch_changes: true)
180-
on(hosts_as('master'), 'mongo --quiet --eval "load(\'/root/.mongoshrc.js\');printjson(rs.conf())"') do |r|
180+
on(hosts_as('master'), 'mongo --quiet --eval "load(\'/root/.mongoshrc.js\');EJSON.stringify(rs.conf())"') do |r|
181181
expect(r.stdout).to match %r{#{hosts[0]}:27017}
182182
expect(r.stdout).to match %r{#{hosts[1]}:27017}
183183
end
@@ -189,14 +189,14 @@ class { 'mongodb::server':
189189
end
190190

191191
it 'checks the data on the master' do
192-
on hosts_as('master'), %{mongo test --verbose --eval 'load("/root/.mongoshrc.js");printjson(db.dummyData.findOne())'} do |r|
192+
on hosts_as('master'), %{mongo test --verbose --eval 'load("/root/.mongoshrc.js");EJSON.stringify(db.dummyData.findOne())'} do |r|
193193
expect(r.stdout).to match %r{created_by_puppet}
194194
end
195195
end
196196

197197
it 'checks the data on the slave' do
198198
sleep(10)
199-
on hosts_as('slave'), %{mongo test --verbose --eval 'load("/root/.mongoshrc.js");db.getMongo().setReadPref("primaryPreferred");printjson(db.dummyData.findOne())'} do |r|
199+
on hosts_as('slave'), %{mongo test --verbose --eval 'load("/root/.mongoshrc.js");db.getMongo().setReadPref("primaryPreferred");EJSON.stringify(db.dummyData.findOne())'} do |r|
200200
expect(r.stdout).to match %r{created_by_puppet}
201201
end
202202
end

spec/acceptance/server_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ class { 'mongodb::client': }
151151
it { is_expected.to contain 'db.auth(\'admin\', \'password\')' }
152152
end
153153

154-
describe command("mongo admin --quiet --eval \"load('/root/.mongorc.js');printjson(db.getUser('admin')['customData'])\"") do
154+
describe command("mongo admin --quiet --eval \"load('/root/.mongorc.js');EJSON.stringify(db.getUser('admin')['customData'])\"") do
155155
its(:exit_status) { is_expected.to eq 0 }
156156
its(:stdout) { is_expected.to match "{ \"createdBy\" : \"Puppet Mongodb_user['User admin on db admin']\" }\n" }
157157
end

spec/acceptance/sharding_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class { 'mongodb::client': }
4646
EOS
4747

4848
apply_manifest_on(hosts_as('router'), pp, catch_failures: true)
49-
on(hosts_as('router'), 'mongo --quiet --eval "printjson(sh.status())"') do |r|
49+
on(hosts_as('router'), 'mongo --quiet --eval "EJSON.stringify(sh.status())"') do |r|
5050
expect(r.stdout).to match %r{foo/shard:27018}
5151
expect(r.stdout).to match %r{foo\.toto}
5252
end

spec/unit/puppet/provider/mongodb_database/mongodb_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
tmp = Tempfile.new('test')
3939
mongodconffile = tmp.path
4040
allow(provider.class).to receive(:mongod_conf_file).and_return(mongodconffile)
41-
allow(provider.class).to receive(:mongo_eval).with('db.getMongo().setReadPref("primaryPreferred");printjson(db.getMongo().getDBs())').and_return(raw_dbs)
41+
allow(provider.class).to receive(:mongo_eval).with('db.getMongo().setReadPref("primaryPreferred");EJSON.stringify(db.getMongo().getDBs())').and_return(raw_dbs)
4242
allow(provider.class).to receive(:db_ismaster).and_return(true)
4343
end
4444

0 commit comments

Comments
 (0)