Debian Codegraph @

lib/puppet/type/postgresql_psql.rb

raw
1 # frozen_string_literal: true
2
3 Puppet::Type.newtype(:postgresql_psql) do
4 newparam(:name) do
5 desc 'An arbitrary tag for your own reference; the name of the message.'
6 isnamevar
7 end
8
9 newproperty(:command) do
10 desc 'The SQL command to execute via psql.'
11
12 defaultto { @resource[:name] }
13
14 # If needing to run the SQL command, return a fake value that will trigger
15 # a sync, else return the expected SQL command so no sync takes place
16 def retrieve
17 if @resource.should_run_sql
18 :notrun
19 else
20 should
21 end
22 end
23
24 def sync
25 output, status = provider.run_sql_command(value)
26 raise("Error executing SQL; psql returned #{status}: '#{output}'") unless status.to_i.zero?
27 end
28 end
29
30 newparam(:unless) do
31 desc <<-DOC
32 An optional SQL command to execute prior to the main :command;
33 this is generally intended to be used for idempotency, to check
34 for the existence of an object in the database to determine whether
35 or not the main SQL command needs to be executed at all.'
36 DOC
37
38 # Return true if a matching row is found
39 def matches(value)
40 output, status = provider.run_unless_sql_command(value)
41 fail("Error evaluating 'unless' clause, returned #{status}: '#{output}'") unless status.to_i.zero? # rubocop:disable Style/SignalException
42
43 result_count = output.strip.to_i
44 debug("Found #{result_count} row(s) executing 'unless' clause")
45 result_count.positive?
46 end
47 end
48
49 newparam(:onlyif) do
50 desc <<-DOC
51 An optional SQL command to execute prior to the main :command;
52 this is generally intended to be used for idempotency, to check
53 for the existence of an object in the database to determine whether
54 or not the main SQL command needs to be executed at all.
55 DOC
56
57 # Return true if a matching row is found
58 def matches(value)
59 output, status = provider.run_unless_sql_command(value)
60 status = output.exitcode if status.nil?
61
62 raise("Error evaluating 'onlyif' clause, returned #{status}: '#{output}'") unless status.to_i.zero?
63
64 result_count = output.strip.to_i
65 debug("Found #{result_count} row(s) executing 'onlyif' clause")
66 result_count.positive?
67 end
68 end
69
70 newparam(:connect_settings) do
71 desc 'Connection settings that will be used when connecting to postgres'
72 end
73
74 newparam(:db) do
75 desc 'The name of the database to execute the SQL command against, this overrides any PGDATABASE value in connect_settings'
76 end
77
78 newparam(:port) do
79 desc 'The port of the database server to execute the SQL command against, this overrides any PGPORT value in connect_settings.'
80 end
81
82 newparam(:search_path) do
83 desc 'The schema search path to use when executing the SQL command'
84 end
85
86 newparam(:psql_path) do
87 desc 'The path to psql executable.'
88 defaultto('psql')
89 end
90
91 newparam(:psql_user) do
92 desc 'The system user account under which the psql command should be executed.'
93 defaultto('postgres')
94 end
95
96 newparam(:psql_group) do
97 desc 'The system user group account under which the psql command should be executed.'
98 defaultto('postgres')
99 end
100
101 newparam(:cwd, parent: Puppet::Parameter::Path) do
102 desc 'The working directory under which the psql command should be executed.'
103 defaultto('/tmp')
104 end
105
106 newparam(:environment) do
107 desc "Any additional environment variables you want to set for a
108 SQL command. Multiple environment variables should be
109 specified as an array."
110
111 validate do |values|
112 Array(values).each do |value|
113 raise ArgumentError, "Invalid environment setting '#{value}'" unless %r{\w+=}.match?(value)
114 end
115 end
116 end
117
118 newparam(:refreshonly, boolean: true) do
119 desc "If 'true', then the SQL will only be executed via a notify/subscribe event."
120
121 defaultto(:false)
122 newvalues(:true, :false)
123 end
124
125 newparam(:instance) do
126 desc 'The postgresql instance under which the psql command should be executed.'
127 defaultto('main')
128 end
129
130 newparam(:sensitive, boolean: true) do
131 desc "If 'true', then the executed command will not be echoed into the log. Use this to protect sensitive information passing through."
132
133 defaultto(:false)
134 newvalues(:true, :false)
135 end
136
137 autorequire(:anchor) do
138 ["postgresql::server::service::begin::#{self[:instance]}"]
139 end
140
141 autorequire(:service) do
142 ["postgresqld_instance_#{self[:instance]}"]
143 end
144
145 def should_run_sql(refreshing = false)
146 onlyif_param = @parameters[:onlyif]
147 unless_param = @parameters[:unless]
148 return false if !onlyif_param.nil? && !onlyif_param.value.nil? && !onlyif_param.matches(onlyif_param.value)
149 return false if !unless_param.nil? && !unless_param.value.nil? && unless_param.matches(unless_param.value)
150 return false if !refreshing && @parameters[:refreshonly].value == :true
151
152 true
153 end
154
155 def refresh
156 property(:command).sync if should_run_sql(true)
157 end
158
159 private
160
161 def set_sensitive_parameters(sensitive_parameters)
162 # Respect sensitive commands
163 if sensitive_parameters.include?(:unless)
164 sensitive_parameters.delete(:unless)
165 parameter(:unless).sensitive = true
166 end
167 super(sensitive_parameters)
168 end
169 end