|
4 | 4 | #
|
5 | 5 | # See README.md for more details.
|
6 | 6 | define postgresql::validate_db_connection(
|
7 |
| - $database_host, |
8 |
| - $database_name, |
9 |
| - $database_password, |
10 |
| - $database_username, |
11 |
| - $database_port = 5432 |
| 7 | + $database_host = undef, |
| 8 | + $database_name = 'postgres', |
| 9 | + $database_password = undef, |
| 10 | + $database_username = undef, |
| 11 | + $database_port = undef, |
| 12 | + $run_as = undef, |
| 13 | + $sleep = 2, |
| 14 | + $tries = 10, |
| 15 | + $create_db_first = true |
12 | 16 | ) {
|
13 | 17 | require postgresql::client
|
14 | 18 |
|
15 | 19 | $psql_path = $postgresql::params::psql_path
|
16 | 20 |
|
17 |
| - # TODO: port to ruby |
18 |
| - $psql = "${psql_path} --tuples-only --quiet -h ${database_host} -U ${database_username} -p ${database_port} --dbname ${database_name}" |
| 21 | + $cmd_init = "${psql_path} --tuples-only --quiet " |
| 22 | + $cmd_host = $database_host ? { |
| 23 | + default => "-h ${database_host} ", |
| 24 | + undef => "" |
| 25 | + } |
| 26 | + $cmd_user = $database_username ? { |
| 27 | + default => "-U ${database_username} ", |
| 28 | + undef => "" |
| 29 | + } |
| 30 | + $cmd_port = $database_port ? { |
| 31 | + default => "-p ${database_port} ", |
| 32 | + undef => "" |
| 33 | + } |
| 34 | + $cmd_dbname = $database_name ? { |
| 35 | + default => "--dbname ${database_name} ", |
| 36 | + undef => "" |
| 37 | + } |
| 38 | + $env = $database_password ? { |
| 39 | + default => "PGPASSWORD=${database_password}", |
| 40 | + undef => undef, |
| 41 | + } |
| 42 | + $cmd = join([$cmd_init, $cmd_host, $cmd_user, $cmd_port, $cmd_dbname]) |
| 43 | + $validate_cmd = "/usr/local/bin/validate_postgresql_connection.sh ${sleep} ${tries} '${cmd}'" |
| 44 | + |
| 45 | + # This is more of a safety valve, we add a little extra to compensate for the |
| 46 | + # time it takes to run each psql command. |
| 47 | + $timeout = (($sleep + 2) * $tries) |
19 | 48 |
|
20 | 49 | $exec_name = "validate postgres connection for ${database_host}/${database_name}"
|
21 | 50 | exec { $exec_name:
|
22 |
| - command => '/bin/false', |
23 |
| - unless => "/bin/echo \"SELECT 1\" | ${psql}", |
| 51 | + command => "echo 'Unable to connect to defined database using: ${cmd}' && false", |
| 52 | + unless => $validate_cmd, |
24 | 53 | cwd => '/tmp',
|
25 |
| - environment => "PGPASSWORD=${database_password}", |
| 54 | + environment => $env, |
26 | 55 | logoutput => 'on_failure',
|
| 56 | + user => $run_as, |
| 57 | + path => '/bin', |
| 58 | + timeout => $timeout, |
27 | 59 | require => Package['postgresql-client'],
|
28 | 60 | }
|
29 | 61 |
|
|
36 | 68 | # We accomplish this by using Puppet's resource collection syntax to search
|
37 | 69 | # for the Database resource in our current catalog; if it exists, the
|
38 | 70 | # appropriate relationship is created here.
|
39 |
| - Postgresql::Server::Database<|title == $database_name|> -> Exec[$exec_name] |
| 71 | + if($create_db_first) { |
| 72 | + Postgresql::Server::Database<|title == $database_name|> -> Exec[$exec_name] |
| 73 | + } |
40 | 74 | }
|
0 commit comments