1 Puppet::Type.type(:postgresql_psql).provide(:ruby) do
4 if ((! resource[:unless]) or (resource[:unless].empty?))
5 if (resource[:refreshonly])
6 # So, if there's no 'unless', and we're in "refreshonly" mode,
7 # we need to return the target command here. If we don't,
8 # then Puppet will generate an event indicating that this
9 # property has changed.
10 return resource[:command]
13 # if we're not in refreshonly mode, then we return nil,
14 # which will cause Puppet to sync this property. This
15 # is what we want if there is no 'unless' value specified.
19 output, status = run_unless_sql_command(resource[:unless])
22 self.fail("Error evaluating 'unless' clause: '#{output}'")
24 result_count = output.strip.to_i
26 # If the 'unless' query returned rows, then we don't want to execute
27 # the 'command'. Returning the target 'command' here will cause
28 # Puppet to treat this property as already being 'insync?', so it
29 # won't call the setter to run the 'command' later.
30 return resource[:command]
33 # Returning 'nil' here will cause Puppet to see this property
34 # as out-of-sync, so it will call the setter later.
39 output, status = run_sql_command(val)
42 self.fail("Error executing SQL; psql returned #{status}: '#{output}'")
47 def run_unless_sql_command(sql)
48 # for the 'unless' queries, we wrap the user's query in a 'SELECT COUNT',
49 # which makes it easier to parse and process the output.
50 run_sql_command('SELECT COUNT(*) FROM (' << sql << ') count')
53 def run_sql_command(sql)
54 command = [resource[:psql_path]]
55 command.push("-d", resource[:db]) if resource[:db]
56 command.push("-t", "-c", sql)
59 Dir.chdir resource[:cwd] do
60 Puppet::Util::SUIDManager.run_and_capture(command, resource[:psql_user], resource[:psql_group])
63 Puppet::Util::SUIDManager.run_and_capture(command, resource[:psql_user], resource[:psql_group])