Class: Puppet::Util::PostgresqlValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet/util/postgresql_validator.rb

Overview

postgresql_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ PostgresqlValidator

Returns a new instance of PostgresqlValidator.



6
7
8
# File 'lib/puppet/util/postgresql_validator.rb', line 6

def initialize(resource)
  @resource = resource
end

Instance Attribute Details

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/puppet/util/postgresql_validator.rb', line 4

def resource
  @resource
end

Instance Method Details

#attempt_connection(sleep_length, tries) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/puppet/util/postgresql_validator.rb', line 38

def attempt_connection(sleep_length, tries)
  (0..tries - 1).each do |_try|
    Puppet.debug "PostgresqlValidator.attempt_connection: Attempting connection to #{@resource[:db_name]}"
    Puppet.debug "PostgresqlValidator.attempt_connection: #{build_validate_cmd}"
    result = execute_command
    if result && !result.empty?
      Puppet.debug "PostgresqlValidator.attempt_connection: Connection to #{@resource[:db_name] || parse_connect_settings.select { |elem| elem.match %r{PGDATABASE} }} successful!"
      return true
    else
      Puppet.warning "PostgresqlValidator.attempt_connection: Sleeping for #{sleep_length} seconds"
      sleep sleep_length
    end
  end
  false
end

#build_psql_cmdObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/puppet/util/postgresql_validator.rb', line 10

def build_psql_cmd
  final_cmd = []

  cmd_init = "#{@resource[:psql_path]} --tuples-only --quiet --no-psqlrc"

  final_cmd.push cmd_init

  cmd_parts = {
    host: "--host #{@resource[:host]}",
    port: "--port #{@resource[:port]}",
    db_username: "--username #{@resource[:db_username]}",
    db_name: "--dbname #{@resource[:db_name]}",
    command: "--command '#{@resource[:command]}'",
  }

  cmd_parts.each do |k, v|
    final_cmd.push v if @resource[k]
  end

  final_cmd.join ' '
end

#parse_connect_settingsObject



32
33
34
35
36
# File 'lib/puppet/util/postgresql_validator.rb', line 32

def parse_connect_settings
  c_settings = @resource[:connect_settings] || {}
  c_settings['PGPASSWORD'] = @resource[:db_password] if @resource[:db_password]
  c_settings.map { |k, v| "#{k}=#{v}" }
end