Skip to content

Commit 92b2a1a

Browse files
committed
(SUP-3875) Consider /sbin for runuser path
Prior to this commit, the runuser path was hard-coded to /usr/sbin/runuser. However, at least Ubuntu 18.04 uses /sbin/runuser, so this commit will check for an executable at either location. It will log a warning if neither is found and not execute the command.
1 parent dc421dc commit 92b2a1a

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

files/psql_metrics

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,30 @@ module PuppetMetricsCollector
143143
#
144144
# @return [Exec::Result] The result of the SQL statement.
145145
def exec_psql(query, database: nil, timeout: @timeout)
146+
runuser_path = if File.executable?('/usr/sbin/runuser')
147+
'/usr/sbin/runuser'
148+
elsif File.executable?('/sbin/runuser')
149+
'/sbin/runuser'
150+
else
151+
nil
152+
end
146153
psql_command = [@psql, '--file=-',
147154
'--no-align', '--no-psqlrc',
148155
'--pset=pager=off', '--set=ON_ERROR_STOP=on',
149156
'--single-transaction', '--tuples-only', '--quiet']
150157
psql_command += ["--dbname=#{database}"] unless database.nil?
151158

152-
command_line = ['/usr/sbin/runuser', '-u', 'pe-postgres',
159+
command_line = [runuser_path, '-u', 'pe-postgres',
153160
'--', *psql_command]
154161

155162
env = { 'PGOPTIONS' => "-c statement_timeout=#{timeout}s",
156163
'PGTZ' => 'GMT' }
157164

158-
Exec.exec_cmd(*command_line, stdin_data: query, env: env, timeout: timeout + 1)
165+
if runuser_path
166+
Exec.exec_cmd(*command_line, stdin_data: query, env: env, timeout: timeout + 1)
167+
else
168+
$stderr.puts('WARN: no runuser executable found')
169+
end
159170
end
160171

161172
# Add an error message to a result hash

0 commit comments

Comments
 (0)