Skip to content

Commit bf1cbf0

Browse files
author
willmeek
authored
Merge pull request #930 from tphoney/FM_6445
FM-6445 add a task
2 parents 8604f39 + ca873fc commit bf1cbf0

File tree

6 files changed

+106
-2
lines changed

6 files changed

+106
-2
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ group :system_tests do
5151
gem "beaker-hostgenerator", *location_for(ENV['BEAKER_HOSTGENERATOR_VERSION'])
5252
gem "beaker-abs", *location_for(ENV['BEAKER_ABS_VERSION'] || '~> 0.1')
5353
gem "puppet-blacksmith", '~> 3.4', :require => false
54+
gem "beaker-task_helper"
5455
end
5556

5657
gem 'puppet', *location_for(ENV['PUPPET_GEM_VERSION'])

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
@@ -1828,6 +1831,10 @@ This internal function converts a list of `pg_hba.conf` based ACLs (passed in as
18281831

18291832
**This function should only be used internally by the module**.
18301833

1834+
### Tasks
1835+
1836+
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.
1837+
18311838
## Limitations
18321839

18331840
Works with versions of PostgreSQL from 8.1 through 9.5.

spec/acceptance/sql_task_spec.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# run a test task
2+
require 'spec_helper_acceptance'
3+
4+
describe 'postgresql task', if: puppet_version =~ %r{(5\.\d\.\d)} && !pe_install? do
5+
describe 'sql task' do
6+
pp = <<-EOS
7+
class { 'postgresql::server': } ->
8+
postgresql::server::db { 'spec1':
9+
user => 'root1',
10+
password => postgresql_password('root1', 'password'),
11+
}
12+
EOS
13+
14+
it 'sets up a postgres db' do
15+
apply_manifest(pp, :catch_failures => true)
16+
end
17+
18+
it 'execute some sql' do
19+
# equates to 'psql -c "SELECT table_name FROM information_schema.tables WHERE table_schema = 'information_schema';" --password --host localhost --dbname=spec1 --username root1'
20+
result = run_task(task_name: 'postgresql::sql', params: 'sql="SELECT count(table_name) FROM information_schema.tables;" host=localhost user=root1 password=password user=root1 database=spec1')
21+
expect_multiple_regexes(result: result, regexes: [%r{count}, %r{1 row}, %r{Job completed. 1/1 nodes succeeded|Ran on 1 node}])
22+
end
23+
end
24+
end

spec/spec_helper_acceptance.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
require 'beaker-rspec/helpers/serverspec'
33
require 'beaker/puppet_install_helper'
44
require 'beaker/module_install_helper'
5+
require 'beaker/task_helper'
56

67
run_puppet_install_helper
7-
install_ca_certs unless ENV['PUPPET_INSTALL_TYPE'] =~ /pe/i
8+
install_ca_certs unless pe_install?
89

910
UNSUPPORTED_PLATFORMS = ['AIX','windows','Solaris','Suse']
1011

@@ -35,10 +36,18 @@ def module_dependencies_from_metadata
3536
end
3637
end
3738

39+
40+
install_bolt_on(hosts) unless pe_install?
3841
install_module_on(hosts)
3942
install_module_dependencies_on(hosts)
4043
install_module_from_forge_on(hosts,'puppetlabs/apt','< 4.2.0')
4144

45+
DEFAULT_PASSWORD = if default[:hypervisor] == 'vagrant'
46+
'vagrant'
47+
elsif default[:hypervisor] == 'vcloud'
48+
'Qu@lity!'
49+
end
50+
4251
class String
4352
# Provide ability to remove indentation from strings, for the purpose of
4453
# left justifying heredoc blocks.
@@ -78,6 +87,7 @@ def psql(psql_cmd, user = 'postgres', exit_codes = [0,1], &block)
7887

7988
# Configure all nodes in nodeset
8089
c.before :suite do
90+
run_puppet_access_login(user: 'admin') if pe_install?
8191
# Set up selinux if appropriate.
8292
if fact('osfamily') == 'RedHat' && fact('selinux') == 'true'
8393
pp = <<-EOS
@@ -109,7 +119,6 @@ def psql(psql_cmd, user = 'postgres', exit_codes = [0,1], &block)
109119
end
110120

111121
hosts.each do |host|
112-
on host, "/bin/touch #{host['puppetpath']}/hiera.yaml"
113122
on host, 'chmod 755 /root'
114123
if fact_on(host, 'osfamily') == 'Debian'
115124
on host, "echo \"en_US ISO-8859-1\nen_NG.UTF-8 UTF-8\nen_US.UTF-8 UTF-8\n\" > /etc/locale.gen"

tasks/sql.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
"host": {
10+
"description": "Hostname to connect to",
11+
"type": "Optional[String[1]]"
12+
},
13+
"password": {
14+
"description": "The password",
15+
"type": "Optional[String[1]]"
16+
},
17+
"port": {
18+
"description": "The port",
19+
"type": "Optional[String[1]]"
20+
},
21+
"sql": {
22+
"description": "The SQL you want to execute",
23+
"type": "String[1]"
24+
},
25+
"user": {
26+
"description": "The user",
27+
"type": "Optional[String[1]]"
28+
}
29+
}
30+
}

tasks/sql.rb

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

0 commit comments

Comments
 (0)