b843e6e32d60aa09e9dcdd495009c250c94c958f
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / manifests / validate_db_connection.pp
1 # Define: postgresql::validate_db_connection
2 #
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).
6 #
7 # Parameters:
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
15 #                             established against
16 #
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.
21 #
22 # Actions:
23 #
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.
28 #
29 # Requires:
30 #
31 #  `psql` commandline tool (will automatically install the system's postgres
32 #                           client package if it is not already installed.)
33 #
34 # Sample Usage:
35 #
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',
41 #  }
42 #
43
44 define postgresql::validate_db_connection(
45   $database_host,
46   $database_name,
47   $database_password,
48   $database_username,
49   $database_port = 5432
50 ) {
51   require postgresql::client
52
53   # TODO: port to ruby
54   $psql = "${postgresql::params::psql_path} --tuples-only --quiet -h ${database_host} -U ${database_username} -p ${database_port} --dbname ${database_name}"
55
56   $exec_name = "validate postgres connection for ${database_host}/${database_name}"
57   exec { $exec_name:
58     command     => '/bin/false',
59     unless      => "/bin/echo \"SELECT 1\" | ${psql}",
60     cwd         => '/tmp',
61     environment => "PGPASSWORD=${database_password}",
62     logoutput   => 'on_failure',
63     require     => Package['postgresql-client'],
64   }
65
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
70   # on the first run.
71   #
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]
76 }
77