Skip to content

Commit

Permalink
Merge pull request #172 from m0dular/SUP-3875-psql_path
Browse files Browse the repository at this point in the history
(SUP-3875) Consider /sbin for runuser path
  • Loading branch information
m0dular authored Dec 6, 2022
2 parents dc421dc + 92b2a1a commit 45327b1
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions files/psql_metrics
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,30 @@ module PuppetMetricsCollector
#
# @return [Exec::Result] The result of the SQL statement.
def exec_psql(query, database: nil, timeout: @timeout)
runuser_path = if File.executable?('/usr/sbin/runuser')
'/usr/sbin/runuser'
elsif File.executable?('/sbin/runuser')
'/sbin/runuser'
else
nil
end
psql_command = [@psql, '--file=-',
'--no-align', '--no-psqlrc',
'--pset=pager=off', '--set=ON_ERROR_STOP=on',
'--single-transaction', '--tuples-only', '--quiet']
psql_command += ["--dbname=#{database}"] unless database.nil?

command_line = ['/usr/sbin/runuser', '-u', 'pe-postgres',
command_line = [runuser_path, '-u', 'pe-postgres',
'--', *psql_command]

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

Exec.exec_cmd(*command_line, stdin_data: query, env: env, timeout: timeout + 1)
if runuser_path
Exec.exec_cmd(*command_line, stdin_data: query, env: env, timeout: timeout + 1)
else
$stderr.puts('WARN: no runuser executable found')
end
end

# Add an error message to a result hash
Expand Down

0 comments on commit 45327b1

Please sign in to comment.