annotate modules/mysql/tasks/sql.rb @ 389:668df4711671

Update MySQL modules
author IBBoard <dev@ibboard.co.uk>
date Mon, 03 Jan 2022 17:16:21 +0000
parents 48d3a1948e4d
children adf6fe9bbc17
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
244
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 #!/opt/puppetlabs/puppet/bin/ruby
389
668df4711671 Update MySQL modules
IBBoard <dev@ibboard.co.uk>
parents: 244
diff changeset
2 # frozen_string_literal: true
668df4711671 Update MySQL modules
IBBoard <dev@ibboard.co.uk>
parents: 244
diff changeset
3
244
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 require 'json'
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 require 'open3'
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 require 'puppet'
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 def get(sql, database, user, password)
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 cmd = ['mysql', '-e', "#{sql} "]
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 cmd << "--database=#{database}" unless database.nil?
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 cmd << "--user=#{user}" unless user.nil?
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 cmd << "--password=#{password}" unless password.nil?
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 stdout, stderr, status = Open3.capture3(*cmd)
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 raise Puppet::Error, _("stderr: '%{stderr}'" % { stderr: stderr }) if status != 0
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 { status: stdout.strip }
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 end
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 params = JSON.parse(STDIN.read)
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 database = params['database']
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 user = params['user']
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 password = params['password']
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 sql = params['sql']
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 begin
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 result = get(sql, database, user, password)
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 puts result.to_json
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 exit 0
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 rescue Puppet::Error => e
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 puts({ status: 'failure', error: e.message }.to_json)
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 exit 1
48d3a1948e4d Update MySQL module
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 end