From d7a2abd1be9e1e0da04b13b5dd45676146db2a55 Mon Sep 17 00:00:00 2001 From: tphoney Date: Tue, 3 Oct 2017 18:08:25 +0100 Subject: [PATCH] FM-6445 add a task --- Gemfile | 1 + README.md | 7 +++++++ spec/acceptance/sql_task_spec.rb | 24 +++++++++++++++++++++++ spec/spec_helper_acceptance.rb | 13 +++++++++++-- tasks/sql.json | 30 +++++++++++++++++++++++++++++ tasks/sql.rb | 33 ++++++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 spec/acceptance/sql_task_spec.rb create mode 100644 tasks/sql.json create mode 100755 tasks/sql.rb diff --git a/Gemfile b/Gemfile index a9f0161c79..a1f064ce51 100644 --- a/Gemfile +++ b/Gemfile @@ -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", '~> 1.0.0', :git => 'https://github.com/puppetlabs/beaker-task_helper.git' end gem 'puppet', *location_for(ENV['PUPPET_GEM_VERSION']) diff --git a/README.md b/README.md index 6561ed0099..7cbeaa30cd 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -1820,6 +1823,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. diff --git a/spec/acceptance/sql_task_spec.rb b/spec/acceptance/sql_task_spec.rb new file mode 100644 index 0000000000..4a1dd11390 --- /dev/null +++ b/spec/acceptance/sql_task_spec.rb @@ -0,0 +1,24 @@ +# run a test task +require 'spec_helper_acceptance' + +describe 'postgresql task' 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: '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{information_schema}, %r{performance_schema}, %r{Job completed. 1/1 nodes succeeded}]) + end + end +end diff --git a/spec/spec_helper_acceptance.rb b/spec/spec_helper_acceptance.rb index e1b08d4a53..77a3992062 100644 --- a/spec/spec_helper_acceptance.rb +++ b/spec/spec_helper_acceptance.rb @@ -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'] @@ -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. @@ -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 @@ -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" diff --git a/tasks/sql.json b/tasks/sql.json new file mode 100644 index 0000000000..1cd8a04a7b --- /dev/null +++ b/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]]" + } + } +} diff --git a/tasks/sql.rb b/tasks/sql.rb new file mode 100755 index 0000000000..c706016318 --- /dev/null +++ b/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