X-Git-Url: https://git.openstreetmap.org./chef.git/blobdiff_plain/10e1976e6e9fd98e058831e93fbec2eafd9c8167..03b7d5e985a34d01dccfd03dc11488015e97f7df:/cookbooks/postgresql/libraries/postgresql.rb diff --git a/cookbooks/postgresql/libraries/postgresql.rb b/cookbooks/postgresql/libraries/postgresql.rb index 6960ec989..70d38135b 100644 --- a/cookbooks/postgresql/libraries/postgresql.rb +++ b/cookbooks/postgresql/libraries/postgresql.rb @@ -1,12 +1,12 @@ -require "chef/mixin/command" +require "chef/mixin/shell_out" class Chef class PostgreSQL - include Chef::Mixin::Command + include Chef::Mixin::ShellOut TABLE_PRIVILEGES = [ :select, :insert, :update, :delete, :truncate, :references, :trigger - ] + ].freeze def initialize(cluster) @cluster = cluster @@ -16,37 +16,42 @@ class Chef # Create argument array args = [] - # Build the arguments - args.push("--command=\"#{options[:command].gsub('"', '\\"')}\"") if options[:command] - args.push("--file=#{options[:file]}") if options[:file] + # Add the cluster + args.push("--cluster") + args.push(@cluster) - # Get the database to use - database = options[:database] || "template1" + # Set output format + args.push("--no-align") unless options.fetch(:align, true) - # Build the command to run - command = "/usr/bin/psql --cluster #{@cluster} #{args.join(' ')} #{database}" + # Add any SQL command to execute + if options[:command] + args.push("--command") + args.push(options[:command]) + end + + # Add any file to execute SQL commands from + if options[:file] + args.push("--file") + args.push(options[:file]) + end + + # Add the database name + args.push(options[:database] || "template1") # Get the user and group to run as user = options[:user] || "postgres" group = options[:group] || "postgres" # Run the command - run_command(:command => command, :user => user, :group => group) + shell_out!("/usr/bin/psql", *args, :user => user, :group => group) end def query(sql, options = {}) - # Get the database to use - database = options[:database] || "template1" - - # Construct the command string - command = "/usr/bin/psql --cluster #{@cluster} --no-align --command='#{sql}' #{database}" - # Run the query - status, stdout, stderr = output_of_command(command, :user => "postgres", :group => "postgres") - handle_command_failures(status, "STDOUT: #{stdout}\nSTDERR: #{stderr}", :output_on_failure => true) + result = execute(options.merge(:command => sql, :align => false)) # Split the output into lines - lines = stdout.split("\n") + lines = result.stdout.split("\n") # Remove the "(N rows)" line from the end lines.pop @@ -93,6 +98,14 @@ class Chef end end + def tablespaces + @tablespaces ||= query("SELECT spcname, usename FROM pg_tablespace AS t INNER JOIN pg_user AS u ON t.spcowner = u.usesysid").each_with_object({}) do |tablespace, tablespaces| + tablespaces[tablespace[:spcname]] = { + :owner => tablespace[:usename] + } + end + end + def tables(database) @tables ||= {} @tables[database] ||= query("SELECT n.nspname, c.relname, u.usename, c.relacl FROM pg_class AS c INNER JOIN pg_user AS u ON c.relowner = u.usesysid INNER JOIN pg_namespace AS n ON c.relnamespace = n.oid", :database => database).each_with_object({}) do |table, tables| @@ -118,7 +131,7 @@ class Chef permissions[user] = { "a" => :insert, "r" => :select, "w" => :update, "d" => :delete, "D" => :truncate, "x" => :references, "t" => :trigger - }.values_at(*(privileges.chars)).compact + }.values_at(*privileges.chars).compact end end end