]> git.openstreetmap.org Git - chef.git/blob - cookbooks/postgresql/libraries/postgresql.rb
Update bundle
[chef.git] / cookbooks / postgresql / libraries / postgresql.rb
1 require "chef/mixin/command"
2
3 class Chef
4   class PostgreSQL
5     include Chef::Mixin::Command
6
7     TABLE_PRIVILEGES = [
8       :select, :insert, :update, :delete, :truncate, :references, :trigger
9     ]
10
11     def initialize(cluster)
12       @cluster = cluster
13     end
14
15     def execute(options)
16       # Create argument array
17       args = []
18
19       # Build the arguments
20       args.push("--command=\"#{options[:command].gsub('"', '\\"')}\"") if options[:command]
21       args.push("--file=#{options[:file]}") if options[:file]
22
23       # Get the database to use
24       database = options[:database] || "template1"
25
26       # Build the command to run
27       command = "/usr/bin/psql --cluster #{@cluster} #{args.join(' ')} #{database}"
28
29       # Get the user and group to run as
30       user = options[:user] || "postgres"
31       group = options[:group] || "postgres"
32
33       # Run the command
34       run_command(:command => command, :user => user, :group => group)
35     end
36
37     def query(sql, options = {})
38       # Get the database to use
39       database = options[:database] || "template1"
40
41       # Construct the command string
42       command = "/usr/bin/psql --cluster #{@cluster} --no-align --command='#{sql}' #{database}"
43
44       # Run the query
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)
47
48       # Split the output into lines
49       lines = stdout.split("\n")
50
51       # Remove the "(N rows)" line from the end
52       lines.pop
53
54       # Get the field names
55       fields = lines.shift.split("|")
56
57       # Extract the record data
58       lines.collect do |line|
59         record = {}
60         fields.zip(line.split("|")) { |name, value| record[name.to_sym] = value }
61         record
62       end
63     end
64
65     def users
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"
72         }
73       end
74     end
75
76     def databases
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]
83         }
84       end
85     end
86
87     def extensions(database)
88       @extensions ||= {}
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]
92         }
93       end
94     end
95
96     def tables(database)
97       @tables ||= {}
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]}"
100
101         tables[name] = {
102           :owner => table[:usename],
103           :permissions => parse_acl(table[:relacl] || "{}")
104         }
105       end
106     end
107
108     private
109
110     def parse_acl(acl)
111       acl.sub(/^\{(.*)\}$/, "\\1").split(",").each_with_object({}) do |entry, permissions|
112         entry = entry.sub(/^"(.*)"$/) { Regexp.last_match[1].gsub(/\\"/, '"') }.sub(/\/.*$/, "")
113         user, privileges = entry.split("=")
114
115         user = user.sub(/^"(.*)"$/, "\\1")
116         user = "public" if user == ""
117
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
122       end
123     end
124   end
125 end