Skip to content

Commit c5754b6

Browse files
committed
Mongosh puts errors on stderr instead of stdout
1 parent ceb3189 commit c5754b6

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed

lib/puppet/provider/mongodb.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def self.mongo_eval(cmd, db = 'admin', retries = 10, host = nil)
179179
end
180180
end
181181

182-
raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd}" unless out
182+
raise Puppet::ExecutionFailure, "Could not evaluate MongoDB shell command: #{cmd}, with: #{e.message}" unless out
183183

184184
Puppet::Util::MongodbOutput.sanitize(out)
185185
end

lib/puppet/provider/mongodb_replset/mongo.rb

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -153,32 +153,33 @@ def get_hosts_status(members)
153153
members.select do |member|
154154
host = member['host']
155155
Puppet.debug "Checking replicaset member #{host} ..."
156-
status = rs_status(host)
157-
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'
158-
159-
if auth_enabled && status.key?('errmsg') && (status['errmsg'].include?('unauthorized') || status['errmsg'].include?('not authorized') || status['errmsg'].include?('requires authentication'))
160-
Puppet.warning "Host #{host} is available, but you are unauthorized because of authentication is enabled: #{auth_enabled}"
161-
alive.push(member)
162-
end
163-
164-
if status.key?('errmsg') && status['errmsg'].include?('no replset config has been received')
165-
Puppet.debug 'Mongo v4 rs.status() RS not initialized output'
166-
alive.push(member)
167-
end
168-
169-
if status.key?('set')
170-
raise Puppet::Error, "Can't configure replicaset #{name}, host #{host} is already part of another replicaset." if status['set'] != name
171-
172-
# This node is alive and supposed to be a member of our set
173-
Puppet.debug "Host #{host} is available for replset #{status['set']}"
174-
alive.push(member)
175-
elsif status.key?('info')
176-
Puppet.debug "Host #{host} is alive but unconfigured: #{status['info']}"
177-
alive.push(member)
156+
begin
157+
status = rs_status(host)
158+
159+
if status.key?('set')
160+
raise Puppet::Error, "Can't configure replicaset #{name}, host #{host} is already part of another replicaset." if status['set'] != name
161+
162+
# This node is alive and supposed to be a member of our set
163+
Puppet.debug "Host #{host} is available for replset #{status['set']}"
164+
alive.push(member)
165+
elsif status.key?('info')
166+
Puppet.debug "Host #{host} is alive but unconfigured: #{status['info']}"
167+
alive.push(member)
168+
end
169+
rescue Puppet::ExecutionFailure => e
170+
raise Puppet::Error, "Can't configure replicaset #{name}, host #{host} is not supposed to be part of a replicaset." if e.message =~ %r{not running with --replSet}
171+
172+
if auth_enabled && (e.message.include?('unauthorized') || e.message.include?('not authorized') || e.message.include?('requires authentication'))
173+
Puppet.warning "Host #{host} is available, but you are unauthorized because of authentication is enabled: #{auth_enabled}"
174+
alive.push(member)
175+
elsif e.message.include?('no replset config has been received')
176+
Puppet.debug 'Mongo v4 rs.status() RS not initialized output'
177+
alive.push(member)
178+
else
179+
Puppet.warning "Can't connect to replicaset member #{host}."
180+
Puppet.warning(e)
181+
end
178182
end
179-
rescue Puppet::ExecutionFailure => e
180-
Puppet.warning "Can't connect to replicaset member #{host}."
181-
Puppet.warning(e)
182183
end
183184
alive.uniq!
184185
dead = members - alive

0 commit comments

Comments
 (0)