validate_db_connection.pp 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $psql_path = $postgresql::params::psql_path
  21. $validcon_script_path = $postgresql::client::validcon_script_path
  22. $cmd_init = "${psql_path} --tuples-only --quiet "
  23. $cmd_host = $database_host ? {
  24. undef => '',
  25. default => "-h ${database_host} ",
  26. }
  27. $cmd_user = $database_username ? {
  28. undef => '',
  29. default => "-U ${database_username} ",
  30. }
  31. $cmd_port = $database_port ? {
  32. undef => '',
  33. default => "-p ${database_port} ",
  34. }
  35. $cmd_dbname = $database_name ? {
  36. undef => "--dbname ${postgresql::params::default_database} ",
  37. default => "--dbname ${database_name} ",
  38. }
  39. $pass_env = $database_password ? {
  40. undef => undef,
  41. default => "PGPASSWORD=${database_password}",
  42. }
  43. $cmd = join([$cmd_init, $cmd_host, $cmd_user, $cmd_port, $cmd_dbname], ' ')
  44. $validate_cmd = "${validcon_script_path} ${sleep} ${tries} '${cmd}'"
  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)
  48. # Combine $database_password and $connect_settings into an array of environment
  49. # variables, ensure $database_password is last, allowing it to override a password
  50. # from the $connect_settings hash
  51. if $connect_settings != undef {
  52. if $pass_env != undef {
  53. $env = concat(join_keys_to_values( $connect_settings, '='), $pass_env)
  54. } else {
  55. $env = join_keys_to_values( $connect_settings, '=')
  56. }
  57. } else {
  58. $env = $pass_env
  59. }
  60. $exec_name = "validate postgres connection for ${database_username}@${database_host}:${database_port}/${database_name}"
  61. exec { $exec_name:
  62. command => "echo 'Unable to connect to defined database using: ${cmd}' && false",
  63. unless => $validate_cmd,
  64. cwd => '/tmp',
  65. environment => $env,
  66. logoutput => 'on_failure',
  67. user => $run_as,
  68. path => '/bin:/usr/bin:/usr/local/bin',
  69. timeout => $timeout,
  70. require => Class['postgresql::client'],
  71. }
  72. # This is a little bit of puppet magic. What we want to do here is make
  73. # sure that if the validation and the database instance creation are being
  74. # applied on the same machine, then the database resource is applied *before*
  75. # the validation resource. Otherwise, the validation is guaranteed to fail
  76. # on the first run.
  77. #
  78. # We accomplish this by using Puppet's resource collection syntax to search
  79. # for the Database resource in our current catalog; if it exists, the
  80. # appropriate relationship is created here.
  81. if($create_db_first) {
  82. Postgresql::Server::Database<|title == $database_name|> -> Exec[$exec_name]
  83. }
  84. }