newer pg module
[mirror/dsa-puppet.git] / 3rdparty / modules / postgresql / lib / puppet / type / postgresql_psql.rb
1 Puppet::Type.newtype(:postgresql_psql) do
2
3   newparam(:name) do
4     desc "An arbitrary tag for your own reference; the name of the message."
5     isnamevar
6   end
7
8   newproperty(:command) do
9     desc 'The SQL command to execute via psql.'
10
11     defaultto { @resource[:name] }
12
13     # If needing to run the SQL command, return a fake value that will trigger
14     # a sync, else return the expected SQL command so no sync takes place
15     def retrieve
16       if @resource.should_run_sql
17         return :notrun
18       else
19         return self.should
20       end
21     end
22
23     def sync
24       output, status = provider.run_sql_command(value)
25       self.fail("Error executing SQL; psql returned #{status}: '#{output}'") unless status == 0
26     end
27   end
28
29   newparam(:unless) do
30     desc "An optional SQL command to execute prior to the main :command; " +
31         "this is generally intended to be used for idempotency, to check " +
32         "for the existence of an object in the database to determine whether " +
33         "or not the main SQL command needs to be executed at all."
34
35     # Return true if a matching row is found
36     def matches(value)
37       output, status = provider.run_unless_sql_command(value)
38       self.fail("Error evaluating 'unless' clause, returned #{status}: '#{output}'") unless status == 0
39
40       result_count = output.strip.to_i
41       self.debug("Found #{result_count} row(s) executing 'unless' clause")
42       result_count > 0
43     end
44   end
45
46   newparam(:onlyif) do
47     desc "An optional SQL command to execute prior to the main :command; " +
48         "this is generally intended to be used for idempotency, to check " +
49         "for the existence of an object in the database to determine whether " +
50         "or not the main SQL command needs to be executed at all."
51
52     # Return true if a matching row is found
53     def matches(value)
54       output, status = provider.run_unless_sql_command(value)
55       status = output.exitcode if status.nil?
56
57       self.fail("Error evaluating 'onlyif' clause, returned #{status}: '#{output}'") unless status == 0
58
59       result_count = output.strip.to_i
60       self.debug("Found #{result_count} row(s) executing 'onlyif' clause")
61       result_count > 0
62     end
63   end
64
65   newparam(:connect_settings) do
66     desc "Connection settings that will be used when connecting to postgres"
67   end
68
69   newparam(:db) do
70     desc "The name of the database to execute the SQL command against, this overrides any PGDATABASE value in connect_settings"
71   end
72
73   newparam(:port) do
74     desc "The port of the database server to execute the SQL command against, this overrides any PGPORT value in connect_settings."
75   end
76
77   newparam(:search_path) do
78     desc "The schema search path to use when executing the SQL command"
79   end
80
81   newparam(:psql_path) do
82     desc "The path to psql executable."
83     defaultto("psql")
84   end
85
86   newparam(:psql_user) do
87     desc "The system user account under which the psql command should be executed."
88     defaultto("postgres")
89   end
90
91   newparam(:psql_group) do
92     desc "The system user group account under which the psql command should be executed."
93     defaultto("postgres")
94   end
95
96   newparam(:cwd, :parent => Puppet::Parameter::Path) do
97     desc "The working directory under which the psql command should be executed."
98     defaultto("/tmp")
99   end
100
101   newparam(:environment) do
102     desc "Any additional environment variables you want to set for a
103       SQL command. Multiple environment variables should be
104       specified as an array."
105
106     validate do |values|
107       Array(values).each do |value|
108         unless value =~ /\w+=/
109           raise ArgumentError, "Invalid environment setting '#{value}'"
110         end
111       end
112     end
113   end
114
115   newparam(:refreshonly, :boolean => true) do
116     desc "If 'true', then the SQL will only be executed via a notify/subscribe event."
117
118     defaultto(:false)
119     newvalues(:true, :false)
120   end
121
122   def should_run_sql(refreshing = false)
123     onlyif_param = @parameters[:onlyif]
124     unless_param = @parameters[:unless]
125     return false if !onlyif_param.nil? && !onlyif_param.value.nil? && !onlyif_param.matches(onlyif_param.value)
126     return false if !unless_param.nil? && !unless_param.value.nil? && unless_param.matches(unless_param.value)
127     return false if !refreshing && @parameters[:refreshonly].value == :true
128     true
129   end
130
131   def refresh
132     self.property(:command).sync if self.should_run_sql(true)
133   end
134
135 end