1 # Define: postgresql::validate_db_connection
3 # This type validates that a successful postgres connection can be established
4 # between the node on which this resource is run and a specified postgres
5 # instance (host/port/user/password/database name).
8 # [*database_host*] - the hostname or IP address of the machine where the
9 # postgres server should be running.
10 # [*database_port*] - the port on which postgres server should be
11 # listening (defaults to 5432).
12 # [*database_username*] - the postgres username
13 # [*database_password*] - the postgres user's password
14 # [*database_name*] - the database name that the connection should be
17 # NOTE: to some degree this type assumes that you've created the corresponding
18 # postgres database instance that you are validating by using the
19 # `postgresql::db` or `postgresql::database` type provided by this module
20 # elsewhere in your manifests.
24 # Attempts to establish a connection to the specified postgres database. If
25 # a connection cannot be established, the resource will fail; this allows you
26 # to use it as a dependency for other resources that would be negatively
27 # impacted if they were applied without the postgres connection being available.
31 # `psql` commandline tool (will automatically install the system's postgres
32 # client package if it is not already installed.)
36 # postgresql::validate_db_connection { 'validate my postgres connection':
37 # database_host => 'my.postgres.host',
38 # database_username => 'mydbuser',
39 # database_password => 'mydbpassword',
40 # database_name => 'mydbname',
44 define postgresql::validate_db_connection(
51 require postgresql::client
54 $psql = "${postgresql::params::psql_path} --tuples-only --quiet -h ${database_host} -U ${database_username} -p ${database_port} --dbname ${database_name}"
56 $exec_name = "validate postgres connection for ${database_host}/${database_name}"
58 command => '/bin/false',
59 unless => "/bin/echo \"SELECT 1\" | ${psql}",
61 environment => "PGPASSWORD=${database_password}",
62 logoutput => 'on_failure',
63 require => Package['postgresql-client'],
66 # This is a little bit of puppet magic. What we want to do here is make
67 # sure that if the validation and the database instance creation are being
68 # applied on the same machine, then the database resource is applied *before*
69 # the validation resource. Otherwise, the validation is guaranteed to fail
72 # We accomplish this by using Puppet's resource collection syntax to search
73 # for the Database resource in our current catalog; if it exists, the
74 # appropriate relationship is created here.
75 Database<|title == $database_name|> -> Exec[$exec_name]