1 require "chef/mixin/command"
5 include Chef::Mixin::Command
8 :select, :insert, :update, :delete, :truncate, :references, :trigger
11 def initialize(cluster)
16 # Create argument array
20 args.push("--command=\"#{options[:command].gsub('"', '\\"')}\"") if options[:command]
21 args.push("--file=#{options[:file]}") if options[:file]
23 # Get the database to use
24 database = options[:database] || "template1"
26 # Build the command to run
27 command = "/usr/bin/psql --cluster #{@cluster} #{args.join(' ')} #{database}"
29 # Get the user and group to run as
30 user = options[:user] || "postgres"
31 group = options[:group] || "postgres"
34 run_command(:command => command, :user => user, :group => group)
37 def query(sql, options = {})
38 # Get the database to use
39 database = options[:database] || "template1"
41 # Construct the command string
42 command = "/usr/bin/psql --cluster #{@cluster} --no-align --command='#{sql}' #{database}"
45 status, stdout, stderr = output_of_command(command, :user => "postgres", :group => "postgres")
46 handle_command_failures(status, "STDOUT: #{stdout}\nSTDERR: #{stderr}", :output_on_failure => true)
48 # Split the output into lines
49 lines = stdout.split("\n")
51 # Remove the "(N rows)" line from the end
55 fields = lines.shift.split("|")
57 # Extract the record data
58 lines.collect do |line|
60 fields.zip(line.split("|")) { |name, value| record[name.to_sym] = value }
66 @users ||= query("SELECT * FROM pg_user").each_with_object({}) do |user, users|
67 users[user[:usename]] = {
68 :superuser => user[:usesuper] == "t",
69 :createdb => user[:usercreatedb] == "t",
70 :createrole => user[:usecatupd] == "t",
71 :replication => user[:userepl] == "t"
77 @databases ||= query("SELECT d.datname, u.usename, d.encoding, d.datcollate, d.datctype FROM pg_database AS d INNER JOIN pg_user AS u ON d.datdba = u.usesysid").each_with_object({}) do |database, databases|
78 databases[database[:datname]] = {
79 :owner => database[:usename],
80 :encoding => database[:encoding],
81 :collate => database[:datcollate],
82 :ctype => database[:datctype]
87 def extensions(database)
89 @extensions[database] ||= query("SELECT extname, extversion FROM pg_extension", :database => database).each_with_object({}) do |extension, extensions|
90 extensions[extension[:extname]] = {
91 :version => extension[:extversion]
98 @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|
99 name = "#{table[:nspname]}.#{table[:relname]}"
102 :owner => table[:usename],
103 :permissions => parse_acl(table[:relacl] || "{}")
111 acl.sub(/^\{(.*)\}$/, "\\1").split(",").each_with_object({}) do |entry, permissions|
112 entry = entry.sub(/^"(.*)"$/) { Regexp.last_match[1].gsub(/\\"/, '"') }.sub(%r{/.*$}, "")
113 user, privileges = entry.split("=")
115 user = user.sub(/^"(.*)"$/, "\\1")
116 user = "public" if user == ""
118 permissions[user] = {
119 "a" => :insert, "r" => :select, "w" => :update, "d" => :delete,
120 "D" => :truncate, "x" => :references, "t" => :trigger
121 }.values_at(*privileges.chars).compact