newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / manifests / validate_db_connection.pp
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).
4 #
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,
13   $run_as            = undef,
14   $sleep             = 2,
15   $tries             = 10,
16   $create_db_first   = true
17 ) {
18   include postgresql::client
19   include postgresql::params
20
21   warning('postgresql::validate_db_connection is deprecated, please use postgresql_conn_validator.')
22
23   $psql_path = $postgresql::params::psql_path
24   $module_workdir = $postgresql::params::module_workdir
25   $validcon_script_path = $postgresql::client::validcon_script_path
26
27   $cmd_init = "${psql_path} --tuples-only --quiet "
28   $cmd_host = $database_host ? {
29     undef   => '',
30     default => "-h ${database_host} ",
31   }
32   $cmd_user = $database_username ? {
33     undef   => '',
34     default => "-U ${database_username} ",
35   }
36   $cmd_port = $database_port ? {
37     undef   => '',
38     default => "-p ${database_port} ",
39   }
40   $cmd_dbname = $database_name ? {
41     undef   => "--dbname ${postgresql::params::default_database} ",
42     default => "--dbname ${database_name} ",
43   }
44   $pass_env = $database_password ? {
45     undef   => undef,
46     default => "PGPASSWORD=${database_password}",
47   }
48   $cmd = join([$cmd_init, $cmd_host, $cmd_user, $cmd_port, $cmd_dbname], ' ')
49   $validate_cmd = "${validcon_script_path} ${sleep} ${tries} '${cmd}'"
50
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)
54
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)
61     } else {
62       $env = join_keys_to_values( $connect_settings, '=')
63     }
64   } else {
65     $env = $pass_env
66   }
67
68   $exec_name = "validate postgres connection for ${database_username}@${database_host}:${database_port}/${database_name}"
69
70   exec { $exec_name:
71     command     => "echo 'Unable to connect to defined database using: ${cmd}' && false",
72     unless      => $validate_cmd,
73     cwd         => $module_workdir,
74     environment => $env,
75     logoutput   => 'on_failure',
76     user        => $run_as,
77     path        => '/bin:/usr/bin:/usr/local/bin',
78     timeout     => $timeout,
79     require     => Class['postgresql::client'],
80   }
81
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
86   # on the first run.
87   #
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]
93   }
94 }