1 require "chef/mixin/shell_out"
5 include Chef::Mixin::ShellOut
8 :select, :insert, :update, :delete, :truncate, :references, :trigger
11 def initialize(cluster)
16 @cluster.split("/").first.to_f
20 # Create argument array
24 args.push("--cluster")
28 args.push("--no-align") unless options.fetch(:align, true)
30 # Add any SQL command to execute
32 args.push("--command")
33 args.push(options[:command])
36 # Add any file to execute SQL commands from
39 args.push(options[:file])
42 # Add the database name
43 args.push(options[:database] || "template1")
45 # Get the user and group to run as
46 user = options[:user] || "postgres"
47 group = options[:group] || "postgres"
50 shell_out!("/usr/bin/psql", *args, :user => user, :group => group)
53 def query(sql, options = {})
55 result = execute(options.merge(:command => sql, :align => false))
57 # Split the output into lines
58 lines = result.stdout.split("\n")
60 # Remove the "(N rows)" line from the end
64 fields = lines.shift.split("|")
66 # Extract the record data
67 lines.collect do |line|
69 fields.zip(line.split("|")) { |name, value| record[name.to_sym] = value }
75 @users ||= query("SELECT * FROM pg_user").each_with_object({}) do |user, users|
76 users[user[:usename]] = {
77 :superuser => user[:usesuper] == "t",
78 :createdb => user[:usercreatedb] == "t",
79 :createrole => user[:usecatupd] == "t",
80 :replication => user[:userepl] == "t"
86 @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|
87 databases[database[:datname]] = {
88 :owner => database[:usename],
89 :encoding => database[:encoding],
90 :collate => database[:datcollate],
91 :ctype => database[:datctype]
96 def extensions(database)
98 @extensions[database] ||= query("SELECT extname, extversion FROM pg_extension", :database => database).each_with_object({}) do |extension, extensions|
99 extensions[extension[:extname]] = {
100 :version => extension[:extversion]
106 @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|
107 tablespaces[tablespace[:spcname]] = {
108 :owner => tablespace[:usename]
115 @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|
116 name = "#{table[:nspname]}.#{table[:relname]}"
119 :owner => table[:usename],
120 :permissions => parse_acl(table[:relacl] || "{}")
128 acl.sub(/^\{(.*)\}$/, "\\1").split(",").each_with_object({}) do |entry, permissions|
129 entry = entry.sub(/^"(.*)"$/) { Regexp.last_match[1].gsub(/\\"/, '"') }.sub(%r{/.*$}, "")
130 user, privileges = entry.split("=")
132 user = user.sub(/^"(.*)"$/, "\\1")
133 user = "public" if user == ""
135 permissions[user] = {
136 "a" => :insert, "r" => :select, "w" => :update, "d" => :delete,
137 "D" => :truncate, "x" => :references, "t" => :trigger
138 }.values_at(*privileges.chars).compact