Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tphoney
committed
Oct 16, 2017
1 parent
460229d
commit d7a2abd
Showing
6 changed files
with
106 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]]" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |