@@ -10,40 +10,41 @@ def initialize(resource)
10
10
end
11
11
12
12
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' ,
25
21
}
26
22
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
29
28
end
30
29
31
- final_cmd . join ' '
30
+ cmd
32
31
end
33
32
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
38
37
end
39
38
40
39
def attempt_connection ( sleep_length , tries )
41
40
( 0 ..tries - 1 ) . each do |_try |
42
41
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
+
45
46
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!"
47
48
return true
48
49
else
49
50
Puppet . warning "PostgresqlValidator.attempt_connection: Sleeping for #{ sleep_length } seconds"
@@ -52,15 +53,5 @@ def attempt_connection(sleep_length, tries)
52
53
end
53
54
false
54
55
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
65
56
end
66
57
end
0 commit comments