1 # This type validates that a successful postgres connection can be established
2 # between the node on which this resource is run and a specified postgres
3 # instance (host/port/user/password/database name).
5 # See README.md for more details.
6 define postgresql::validate_db_connection(
7 $database_host = undef,
8 $database_name = undef,
9 $database_password = undef,
10 $database_username = undef,
11 $database_port = undef,
12 $connect_settings = undef,
16 $create_db_first = true
18 include postgresql::client
19 include postgresql::params
21 warning('postgresql::validate_db_connection is deprecated, please use postgresql_conn_validator.')
23 $psql_path = $postgresql::params::psql_path
24 $module_workdir = $postgresql::params::module_workdir
25 $validcon_script_path = $postgresql::client::validcon_script_path
27 $cmd_init = "${psql_path} --tuples-only --quiet "
28 $cmd_host = $database_host ? {
30 default => "-h ${database_host} ",
32 $cmd_user = $database_username ? {
34 default => "-U ${database_username} ",
36 $cmd_port = $database_port ? {
38 default => "-p ${database_port} ",
40 $cmd_dbname = $database_name ? {
41 undef => "--dbname ${postgresql::params::default_database} ",
42 default => "--dbname ${database_name} ",
44 $pass_env = $database_password ? {
46 default => "PGPASSWORD=${database_password}",
48 $cmd = join([$cmd_init, $cmd_host, $cmd_user, $cmd_port, $cmd_dbname], ' ')
49 $validate_cmd = "${validcon_script_path} ${sleep} ${tries} '${cmd}'"
51 # This is more of a safety valve, we add a little extra to compensate for the
52 # time it takes to run each psql command.
53 $timeout = (($sleep + 2) * $tries)
55 # Combine $database_password and $connect_settings into an array of environment
56 # variables, ensure $database_password is last, allowing it to override a password
57 # from the $connect_settings hash
58 if $connect_settings != undef {
59 if $pass_env != undef {
60 $env = concat(join_keys_to_values( $connect_settings, '='), $pass_env)
62 $env = join_keys_to_values( $connect_settings, '=')
68 $exec_name = "validate postgres connection for ${database_username}@${database_host}:${database_port}/${database_name}"
71 command => "echo 'Unable to connect to defined database using: ${cmd}' && false",
72 unless => $validate_cmd,
73 cwd => $module_workdir,
75 logoutput => 'on_failure',
77 path => '/bin:/usr/bin:/usr/local/bin',
79 require => Class['postgresql::client'],
82 # This is a little bit of puppet magic. What we want to do here is make
83 # sure that if the validation and the database instance creation are being
84 # applied on the same machine, then the database resource is applied *before*
85 # the validation resource. Otherwise, the validation is guaranteed to fail
88 # We accomplish this by using Puppet's resource collection syntax to search
89 # for the Database resource in our current catalog; if it exists, the
90 # appropriate relationship is created here.
91 if($create_db_first) {
92 Postgresql::Server::Database<|title == $database_name|> -> Exec[$exec_name]