File: sql.rb

package info (click to toggle)
puppet-module-puppetlabs-postgresql 10.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 940 kB
  • sloc: ruby: 731; sh: 66; makefile: 2
file content (37 lines) | stat: -rwxr-xr-x 982 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/ruby
# frozen_string_literal: true

require 'json'
require 'open3'
require 'puppet'

def get(sql, database, user, port, password, host)
  env_hash = {}
  env_hash['PGPASSWORD'] = password unless password.nil?
  cmd_string = ['psql', '-c', sql]
  cmd_string << "--dbname=#{database}" unless database.nil?
  cmd_string << "--username=#{user}" unless user.nil?
  cmd_string << "--port=#{port}" unless port.nil?
  cmd_string << "--host=#{host}" unless host.nil?
  stdout, stderr, status = Open3.capture3(env_hash, *cmd_string)
  raise Puppet::Error, stderr if status != 0

  { status: stdout.strip }
end

params = JSON.parse($stdin.read)
database = params['database']
host = params['host']
password = params['password']
port = params['port']
sql = params['sql']
user = params['user']

begin
  result = get(sql, database, user, port, password, host)
  puts result.to_json
  exit 0
rescue Puppet::Error => e
  puts({ status: 'failure', error: e.message }.to_json)
  exit 1
end