Skip to content

Commit 426bbc6

Browse files
author
tphoney
committed
FM_6450 adds the execute SQL task
1 parent 0765497 commit 426bbc6

File tree

7 files changed

+128
-1
lines changed

7 files changed

+128
-1
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
44
and this project adheres to [Semantic Versioning](http://semver.org).
55

6+
## Supported Release [5.1.0]
7+
### Summary
8+
This release adds Tasks to the Mysql module.
9+
10+
#### Added
11+
- Adds the execute sql task.
12+
613
## Supported Release [5.0.0]
714
### Summary
815
This is a major release that adds support for string translation. Currently the only supported language besides

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,10 @@ Determines the MySQL version by parsing the output from `mysql --version`
12821282

12831283
Generates a unique id, based on the node's MAC address, which can be used as `server_id`. This fact will *always* return `0` on nodes that have only loopback interfaces. Because those nodes aren't connected to the outside world, this shouldn't cause any conflicts.
12841284

1285+
### Tasks
1286+
1287+
The MySQL module has an example task that allows a user to execute arbitary SQL against a database. Please refer to to the [PE documentation](https://puppet.com/docs/pe/2017.3/orchestrator/running_tasks.html) or [Bolt documentation](https://puppet.com/docs/bolt/latest/bolt.html) on how to execute a task.
1288+
12851289
## Limitations
12861290

12871291
This module has been tested on:

metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "puppetlabs-mysql",
3-
"version": "5.0.0",
3+
"version": "5.1.0",
44
"author": "Puppet Labs",
55
"summary": "Installs, configures, and manages the MySQL service.",
66
"license": "Apache-2.0",

spec/acceptance/sql_task_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# run a test task
2+
require 'spec_helper_acceptance'
3+
4+
describe 'mysql tasks' do
5+
describe 'execute some sql', if: pe_install? && puppet_version =~ %r{(5\.\d\.\d)} do
6+
pp = <<-EOS
7+
class { 'mysql::server': root_password => 'password' }
8+
mysql::db { 'spec1':
9+
user => 'root1',
10+
password => 'password',
11+
}
12+
EOS
13+
14+
it "sets up a mysql instance" do
15+
apply_manifest(pp, catch_failures: true)
16+
end
17+
18+
it 'execute arbitary sql' do
19+
result = run_task(task_name: 'mysql::sql', params: 'sql="show databases;" password=password')
20+
expect_multiple_regexes(result: result, regexes: [%r{information_schema}, %r{performance_schema}, %r{Job completed. 1/1 nodes succeeded}])
21+
end
22+
end
23+
end

spec/spec_helper_acceptance.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,17 @@
44
require 'beaker/module_install_helper'
55
require 'beaker/i18n_helper'
66

7+
def install_bolt_on(hosts)
8+
on(hosts, "/opt/puppetlabs/puppet/bin/gem install --source http://rubygems.delivery.puppetlabs.net bolt -v '> 0.0.1'", acceptable_exit_codes: [0, 1]).stdout
9+
end
10+
11+
def pe_install?
12+
ENV['PUPPET_INSTALL_TYPE'] =~ %r{pe}i
13+
end
14+
715
run_puppet_install_helper
816
install_ca_certs unless ENV['PUPPET_INSTALL_TYPE'] =~ %r{pe}i
17+
install_bolt_on(hosts) unless pe_install?
918
install_module_on(hosts)
1019
install_module_dependencies_on(hosts)
1120

@@ -15,6 +24,38 @@ def puppet_version
1524
(on default, puppet('--version')).output.chomp
1625
end
1726

27+
DEFAULT_PASSWORD = if default[:hypervisor] == 'vagrant'
28+
'vagrant'
29+
elsif default[:hypervisor] == 'vcloud'
30+
'Qu@lity!'
31+
end
32+
33+
def run_puppet_access_login(user:, password: '~!@#$%^*-/ aZ', lifetime: '5y')
34+
on(master, puppet('access', 'login', '--username', user, '--lifetime', lifetime), stdin: password)
35+
end
36+
37+
def run_task(task_name:, params: nil, password: DEFAULT_PASSWORD)
38+
if pe_install?
39+
run_puppet_task(task_name: task_name, params: params)
40+
else
41+
run_bolt_task(task_name: task_name, params: params, password: password)
42+
end
43+
end
44+
45+
def run_bolt_task(task_name:, params: nil, password: DEFAULT_PASSWORD)
46+
on(master, "/opt/puppetlabs/puppet/bin/bolt task run #{task_name} --modules /etc/puppetlabs/code/modules/service --nodes localhost --password #{password} #{params}", acceptable_exit_codes: [0, 1]).stdout # rubocop:disable Metrics/LineLength
47+
end
48+
49+
def run_puppet_task(task_name:, params: nil)
50+
on(master, puppet('task', 'run', task_name, '--nodes', fact_on(master, 'fqdn'), params.to_s), acceptable_exit_codes: [0, 1]).stdout
51+
end
52+
53+
def expect_multiple_regexes(result:, regexes:)
54+
regexes.each do |regex|
55+
expect(result).to match(regex)
56+
end
57+
end
58+
1859
RSpec.configure do |c|
1960
# Readable test descriptions
2061
c.formatter = :documentation
@@ -27,6 +68,7 @@ def puppet_version
2768

2869
# Configure all nodes in nodeset
2970
c.before :suite do
71+
run_puppet_access_login(user: 'admin') if pe_install?
3072
hosts.each do |host|
3173
# This will be removed, this is temporary to test localisation.
3274
if (fact('osfamily') == 'Debian' || fact('osfamily') == 'RedHat') && (puppet_version >= '4.10.5' && puppet_version < '5.2.0')

tasks/sql.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"description": "Allows you to execute arbitary SQL",
3+
"input_method": "stdin",
4+
"parameters": {
5+
"database": {
6+
"description": "Database to connect to",
7+
"type": "Optional[String[1]]"
8+
},
9+
"user": {
10+
"description": "The user",
11+
"type": "Optional[String[1]]"
12+
},
13+
"password": {
14+
"description": "The password",
15+
"type": "Optional[String[1]]"
16+
},
17+
"sql": {
18+
"description": "The SQL you want to execute",
19+
"type": "String[1]"
20+
}
21+
}
22+
}

tasks/sql.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/opt/puppetlabs/puppet/bin/ruby
2+
require 'json'
3+
require 'open3'
4+
require 'puppet'
5+
6+
def get(sql, database, user, password)
7+
cmd_string = "mysql -e \"#{sql}\""
8+
cmd_string << " --database=#{database}" unless database.nil?
9+
cmd_string << " --user=#{user}" unless user.nil?
10+
cmd_string << " --password=#{password}" unless password.nil?
11+
stdout, stderr, status = Open3.capture3(cmd_string)
12+
raise Puppet::Error, _("stderr: '#{stderr}'") if status != 0
13+
{ status: stdout.strip }
14+
end
15+
16+
params = JSON.parse(STDIN.read)
17+
database = params['database']
18+
user = params['user']
19+
password = params['password']
20+
sql = params['sql']
21+
22+
begin
23+
result = get(sql, database, user, password)
24+
puts result.to_json
25+
exit 0
26+
rescue Puppet::Error => e
27+
puts({ status: 'failure', error: e.message }.to_json)
28+
exit 1
29+
end

0 commit comments

Comments
 (0)