244
|
1 #!/opt/puppetlabs/puppet/bin/ruby
|
389
|
2 # frozen_string_literal: true
|
|
3
|
244
|
4 require 'json'
|
|
5 require 'open3'
|
|
6 require 'puppet'
|
|
7
|
|
8 def get(sql, database, user, password)
|
|
9 cmd = ['mysql', '-e', "#{sql} "]
|
|
10 cmd << "--database=#{database}" unless database.nil?
|
|
11 cmd << "--user=#{user}" unless user.nil?
|
|
12 cmd << "--password=#{password}" unless password.nil?
|
|
13 stdout, stderr, status = Open3.capture3(*cmd)
|
|
14 raise Puppet::Error, _("stderr: '%{stderr}'" % { stderr: stderr }) if status != 0
|
|
15 { status: stdout.strip }
|
|
16 end
|
|
17
|
|
18 params = JSON.parse(STDIN.read)
|
|
19 database = params['database']
|
|
20 user = params['user']
|
|
21 password = params['password']
|
|
22 sql = params['sql']
|
|
23
|
|
24 begin
|
|
25 result = get(sql, database, user, password)
|
|
26 puts result.to_json
|
|
27 exit 0
|
|
28 rescue Puppet::Error => e
|
|
29 puts({ status: 'failure', error: e.message }.to_json)
|
|
30 exit 1
|
|
31 end
|