Skip to content

Commit 34f566f

Browse files
committed
Clean up postgresql validator for better perf and robustness
1 parent 25b816e commit 34f566f

File tree

1 file changed

+23
-32
lines changed

1 file changed

+23
-32
lines changed

lib/puppet/util/postgresql_validator.rb

+23-32
Original file line numberDiff line numberDiff line change
@@ -10,40 +10,41 @@ def initialize(resource)
1010
end
1111

1212
def build_psql_cmd
13-
final_cmd = []
14-
15-
cmd_init = "#{@resource[:psql_path]} --tuples-only --quiet --no-psqlrc"
16-
17-
final_cmd.push cmd_init
18-
19-
cmd_parts = {
20-
host: "--host #{@resource[:host]}",
21-
port: "--port #{@resource[:port]}",
22-
db_username: "--username #{@resource[:db_username]}",
23-
db_name: "--dbname #{@resource[:db_name]}",
24-
command: "--command '#{@resource[:command]}'",
13+
cmd = [@resource[:psql_path], '--tuples-only', '--quiet', '--no-psqlrc']
14+
15+
args = {
16+
host: '--host',
17+
port: '--port',
18+
db_username: '--username',
19+
db_name: '--dbname',
20+
command: '--command',
2521
}
2622

27-
cmd_parts.each do |k, v|
28-
final_cmd.push v if @resource[k]
23+
args.each do |k, v|
24+
if @resource[k]
25+
cmd.push v
26+
cmd.push @resource[k]
27+
end
2928
end
3029

31-
final_cmd.join ' '
30+
cmd
3231
end
3332

34-
def parse_connect_settings
35-
c_settings = @resource[:connect_settings] || {}
36-
c_settings['PGPASSWORD'] = @resource[:db_password] if @resource[:db_password]
37-
c_settings.map { |k, v| "#{k}=#{v}" }
33+
def connect_settings
34+
result = @resource[:connect_settings] || {}
35+
result['PGPASSWORD'] = @resource[:db_password] if @resource[:db_password]
36+
result
3837
end
3938

4039
def attempt_connection(sleep_length, tries)
4140
(0..tries - 1).each do |_try|
4241
Puppet.debug "PostgresqlValidator.attempt_connection: Attempting connection to #{@resource[:db_name]}"
43-
Puppet.debug "PostgresqlValidator.attempt_connection: #{build_validate_cmd}"
44-
result = execute_command
42+
cmd = build_psql_cmd
43+
Puppet.debug "PostgresqlValidator.attempt_connection: #{cmd.inspect}"
44+
result = Execution.execute(cmd, custom_environment: connect_settings, uid: @resource[:run_as])
45+
4546
if result && !result.empty?
46-
Puppet.debug "PostgresqlValidator.attempt_connection: Connection to #{@resource[:db_name] || parse_connect_settings.select { |elem| elem.include?('PGDATABASE') }} successful!"
47+
Puppet.debug "PostgresqlValidator.attempt_connection: Connection to #{@resource[:db_name] || connect_settings.select { |elem| elem.include?('PGDATABASE') }} successful!"
4748
return true
4849
else
4950
Puppet.warning "PostgresqlValidator.attempt_connection: Sleeping for #{sleep_length} seconds"
@@ -52,15 +53,5 @@ def attempt_connection(sleep_length, tries)
5253
end
5354
false
5455
end
55-
56-
private
57-
58-
def execute_command
59-
Execution.execute(build_validate_cmd, uid: @resource[:run_as])
60-
end
61-
62-
def build_validate_cmd
63-
"#{parse_connect_settings.join(' ')} #{build_psql_cmd} "
64-
end
6556
end
6657
end

0 commit comments

Comments
 (0)