Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FM-6445 add a task #930

Merged
merged 1 commit into from Dec 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -51,6 +51,7 @@ group :system_tests do
gem "beaker-hostgenerator", *location_for(ENV['BEAKER_HOSTGENERATOR_VERSION'])
gem "beaker-abs", *location_for(ENV['BEAKER_ABS_VERSION'] || '~> 0.1')
gem "puppet-blacksmith", '~> 3.4', :require => false
gem "beaker-task_helper"
end

gem 'puppet', *location_for(ENV['PUPPET_GEM_VERSION'])
Expand Down
7 changes: 7 additions & 0 deletions README.md
Expand Up @@ -20,6 +20,7 @@
* [Defined Types](#defined-types)
* [Types](#types)
* [Functions](#functions)
* [Tasks](#tasks)
5. [Limitations - OS compatibility, etc.](#limitations)
6. [Development - Guide for contributing to the module](#development)
* [Contributors - List of module contributors](#contributors)
Expand Down Expand Up @@ -365,6 +366,8 @@ The postgresql module comes with many options for configuring the server. While
* [postgresql_password](#function-postgresql_password)
* [postgresql_acls_to_resources_hash](#function-postgresql_acls_to_resources_hashacl_array-id-order_offset)

**Tasks:**

### Classes

#### postgresql::client
Expand Down Expand Up @@ -1828,6 +1831,10 @@ This internal function converts a list of `pg_hba.conf` based ACLs (passed in as

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

### Tasks

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.

## Limitations

Works with versions of PostgreSQL from 8.1 through 9.5.
Expand Down
24 changes: 24 additions & 0 deletions spec/acceptance/sql_task_spec.rb
@@ -0,0 +1,24 @@
# run a test task
require 'spec_helper_acceptance'

describe 'postgresql task', if: puppet_version =~ %r{(5\.\d\.\d)} && !pe_install? do
describe 'sql task' do
pp = <<-EOS
class { 'postgresql::server': } ->
postgresql::server::db { 'spec1':
user => 'root1',
password => postgresql_password('root1', 'password'),
}
EOS

it 'sets up a postgres db' do
apply_manifest(pp, :catch_failures => true)
end

it 'execute some sql' do
# equates to 'psql -c "SELECT table_name FROM information_schema.tables WHERE table_schema = 'information_schema';" --password --host localhost --dbname=spec1 --username root1'
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')
expect_multiple_regexes(result: result, regexes: [%r{count}, %r{1 row}, %r{Job completed. 1/1 nodes succeeded|Ran on 1 node}])
end
end
end
13 changes: 11 additions & 2 deletions spec/spec_helper_acceptance.rb
Expand Up @@ -2,9 +2,10 @@
require 'beaker-rspec/helpers/serverspec'
require 'beaker/puppet_install_helper'
require 'beaker/module_install_helper'
require 'beaker/task_helper'

run_puppet_install_helper
install_ca_certs unless ENV['PUPPET_INSTALL_TYPE'] =~ /pe/i
install_ca_certs unless pe_install?

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

Expand Down Expand Up @@ -35,10 +36,18 @@ def module_dependencies_from_metadata
end
end


install_bolt_on(hosts) unless pe_install?
install_module_on(hosts)
install_module_dependencies_on(hosts)
install_module_from_forge_on(hosts,'puppetlabs/apt','< 4.2.0')

DEFAULT_PASSWORD = if default[:hypervisor] == 'vagrant'
'vagrant'
elsif default[:hypervisor] == 'vcloud'
'Qu@lity!'
end

class String
# Provide ability to remove indentation from strings, for the purpose of
# left justifying heredoc blocks.
Expand Down Expand Up @@ -78,6 +87,7 @@ def psql(psql_cmd, user = 'postgres', exit_codes = [0,1], &block)

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

hosts.each do |host|
on host, "/bin/touch #{host['puppetpath']}/hiera.yaml"
on host, 'chmod 755 /root'
if fact_on(host, 'osfamily') == 'Debian'
on host, "echo \"en_US ISO-8859-1\nen_NG.UTF-8 UTF-8\nen_US.UTF-8 UTF-8\n\" > /etc/locale.gen"
Expand Down
30 changes: 30 additions & 0 deletions tasks/sql.json
@@ -0,0 +1,30 @@
{
"description": "Allows you to execute arbitary SQL",
"input_method": "stdin",
"parameters": {
"database": {
"description": "Database to connect to",
"type": "Optional[String[1]]"
},
"host": {
"description": "Hostname to connect to",
"type": "Optional[String[1]]"
},
"password": {
"description": "The password",
"type": "Optional[String[1]]"
},
"port": {
"description": "The port",
"type": "Optional[String[1]]"
},
"sql": {
"description": "The SQL you want to execute",
"type": "String[1]"
},
"user": {
"description": "The user",
"type": "Optional[String[1]]"
}
}
}
33 changes: 33 additions & 0 deletions tasks/sql.rb
@@ -0,0 +1,33 @@
#!/opt/puppetlabs/puppet/bin/ruby
require 'json'
require 'open3'
require 'puppet'

def get(sql, database, user, port, password, host)
env_hash = {'PGPASSWORD' => password} unless password.nil?
cmd_string = "psql -c \"#{sql}\""
cmd_string << " --dbname=#{database}" unless database.nil?
cmd_string << " --username=#{user}" unless user.nil?
cmd_string << " --port=#{port}" unless port.nil?
cmd_string << " --host=#{host}" unless host.nil?
stdout, stderr, status = Open3.capture3(env_hash, cmd_string)
raise Puppet::Error, stderr if status != 0
{ status: stdout.strip }
end

params = JSON.parse(STDIN.read)
database = params['database']
host = params['host']
password = params['password']
port = params['port']
sql = params['sql']
user = params['user']

begin
result = get(sql, database, user, port, password, host)
puts result.to_json
exit 0
rescue Puppet::Error => e
puts({ status: 'failure', error: e.message }.to_json)
exit 1
end