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").inject({}) do |users,user|
67 users[user[:usename]] = {
68 :superuser => user[:usesuper] == "t",
69 :createdb => user[:usercreatedb] == "t",
70 :createrole => user[:usecatupd] == "t",
71 :replication => user[:userepl] == "t"
78 @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").inject({}) do |databases,database|
79 databases[database[:datname]] = {
80 :owner => database[:usename],
81 :encoding => database[:encoding],
82 :collate => database[:datcollate],
83 :ctype => database[:datctype]
89 def extensions(database)
91 @extensions[database] ||= query("SELECT extname, extversion FROM pg_extension", :database => database).inject({}) do |extensions,extension|
92 extensions[extension[:extname]] = {
93 :version => extension[:extversion]
101 @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).inject({}) do |tables,table|
102 name = "#{table[:nspname]}.#{table[:relname]}"
105 :owner => table[:usename],
106 :permissions => parse_acl(table[:relacl] || "{}")
116 acl.sub(/^\{(.*)\}$/, "\\1").split(",").inject({}) do |permissions, entry|
117 entry = entry.sub(/^"(.*)"$/) { $1.gsub(/\\"/, '"') }.sub(/\/.*$/, "")
118 user, privileges = entry.split("=")
120 user = user.sub(/^"(.*)"$/, "\\1")
121 user = "public" if user == ""
123 permissions[user] = {
124 "a" => :insert, "r" => :select, "w" => :update, "d" => :delete,
125 "D" => :truncate, "x" => :references, "t" => :trigger
126 }.values_at(*(privileges.chars)).compact