Skip to content

Commit fc89e0c

Browse files
tphoneypmcmaw
authored andcommitted
FM-6445 add a task
1 parent 38c77b4 commit fc89e0c

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
@@ -1,3 +1,10 @@
1+
## Supported Release 5.3.0
2+
### Summary
3+
Adds tasks to the Postgres module.
4+
5+
#### Added
6+
- Adds the execute sql task.
7+
18
## Supported Release 5.2.0
29
### Summary
310
Adds several new features including some work around OS support. Also includes a couple of fixes to tests and the removal of unsupported Ubuntu versions.

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* [Defined Types](#defined-types)
2121
* [Types](#types)
2222
* [Functions](#functions)
23+
* [Tasks](#tasks)
2324
5. [Limitations - OS compatibility, etc.](#limitations)
2425
6. [Development - Guide for contributing to the module](#development)
2526
* [Contributors - List of module contributors](#contributors)
@@ -365,6 +366,8 @@ The postgresql module comes with many options for configuring the server. While
365366
* [postgresql_password](#function-postgresql_password)
366367
* [postgresql_acls_to_resources_hash](#function-postgresql_acls_to_resources_hashacl_array-id-order_offset)
367368

369+
**Tasks:**
370+
368371
### Classes
369372

370373
#### postgresql::client
@@ -1820,6 +1823,10 @@ This internal function converts a list of `pg_hba.conf` based ACLs (passed in as
18201823

18211824
**This function should only be used internally by the module**.
18221825

1826+
### Tasks
1827+
1828+
The Postgresql 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.
1829+
18231830
## Limitations
18241831

18251832
Works with versions of PostgreSQL from 8.1 through 9.5.

metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "puppetlabs-postgresql",
3-
"version": "5.2.0",
3+
"version": "5.3.0",
44
"author": "Inkling/Puppet Labs",
55
"summary": "Offers support for basic management of PostgreSQL databases.",
66
"license": "Apache-2.0",

spec/acceptance/sql_task_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# run a test task
2+
require 'spec_helper_acceptance'
3+
4+
describe 'puppet_conf task' do
5+
describe 'puppet configuration file', if: pe_install? && puppet_version =~ %r{(5\.\d\.\d)} do
6+
pp = <<-EOS
7+
class { 'postgresql::server': }
8+
EOS
9+
10+
apply_manifest(pp, :catch_failures => true)
11+
12+
it 'set/get a puppet configuration' do
13+
result = run_task(task_name: 'sql', params: 'sql="show databases;"')
14+
expect_multiple_regexes(result: result, regexes: [%r{information_schema}, %r{performance_schema}, %r{Job completed. 1/1 nodes succeeded}])
15+
end
16+
end
17+
end

spec/spec_helper_acceptance.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
require 'beaker/puppet_install_helper'
44
require 'beaker/module_install_helper'
55

6+
def install_bolt_on(hosts)
7+
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
8+
end
9+
10+
def pe_install?
11+
ENV['PUPPET_INSTALL_TYPE'] =~ %r{pe}i
12+
end
13+
14+
def puppet_version
15+
(on default, puppet('--version')).output.chomp
16+
end
17+
618
run_puppet_install_helper
719
install_ca_certs unless ENV['PUPPET_INSTALL_TYPE'] =~ /pe/i
820

@@ -35,10 +47,48 @@ def module_dependencies_from_metadata
3547
end
3648
end
3749

50+
51+
install_bolt_on(hosts) unless ENV['PUPPET_INSTALL_TYPE'] =~ %r{pe}i
3852
install_module_on(hosts)
3953
install_module_dependencies_on(hosts)
4054
install_module_from_forge_on(hosts,'puppetlabs/apt','< 4.2.0')
4155

56+
DEFAULT_PASSWORD = if default[:hypervisor] == 'vagrant'
57+
'vagrant'
58+
elsif default[:hypervisor] == 'vcloud'
59+
'Qu@lity!'
60+
end
61+
62+
def run_puppet_access_login(user:, password: '~!@#$%^*-/ aZ', lifetime: '5y')
63+
on(master, puppet('access', 'login', '--username', user, '--lifetime', lifetime), stdin: password)
64+
end
65+
66+
def pe_install?
67+
ENV['PUPPET_INSTALL_TYPE'] =~ %r{pe}i
68+
end
69+
70+
def run_task(task_name:, params: nil, password: DEFAULT_PASSWORD)
71+
if pe_install?
72+
run_puppet_task(task_name: task_name, params: params)
73+
else
74+
run_bolt_task(task_name: task_name, params: params, password: password)
75+
end
76+
end
77+
78+
def run_bolt_task(task_name:, params: nil, password: DEFAULT_PASSWORD)
79+
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
80+
end
81+
82+
def run_puppet_task(task_name:, params: nil)
83+
on(master, puppet('task', 'run', task_name, '--nodes', fact_on(master, 'fqdn'), params.to_s), acceptable_exit_codes: [0, 1]).stdout
84+
end
85+
86+
def expect_multiple_regexes(result:, regexes:)
87+
regexes.each do |regex|
88+
expect(result).to match(regex)
89+
end
90+
end
91+
4292
class String
4393
# Provide ability to remove indentation from strings, for the purpose of
4494
# left justifying heredoc blocks.
@@ -78,6 +128,7 @@ def psql(psql_cmd, user = 'postgres', exit_codes = [0,1], &block)
78128

79129
# Configure all nodes in nodeset
80130
c.before :suite do
131+
run_puppet_access_login(user: 'admin') if pe_install?
81132
# Set up selinux if appropriate.
82133
if fact('osfamily') == 'RedHat' && fact('selinux') == 'true'
83134
pp = <<-EOS

tasks/sql.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
"sql": {
10+
"description": "The SQL you want to execute",
11+
"type": "String[1]"
12+
},
13+
"user": {
14+
"description": "The user",
15+
"type": "Optional[String[1]]"
16+
}
17+
}
18+
}

tasks/sql.rb

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

0 commit comments

Comments
 (0)