]> git.openstreetmap.org Git - chef.git/blob - cookbooks/postgresql/libraries/postgresql.rb
70d38135b7cbfa9c69cc32807f84325368b494d1
[chef.git] / cookbooks / postgresql / libraries / postgresql.rb
1 require "chef/mixin/shell_out"
2
3 class Chef
4   class PostgreSQL
5     include Chef::Mixin::ShellOut
6
7     TABLE_PRIVILEGES = [
8       :select, :insert, :update, :delete, :truncate, :references, :trigger
9     ].freeze
10
11     def initialize(cluster)
12       @cluster = cluster
13     end
14
15     def execute(options)
16       # Create argument array
17       args = []
18
19       # Add the cluster
20       args.push("--cluster")
21       args.push(@cluster)
22
23       # Set output format
24       args.push("--no-align") unless options.fetch(:align, true)
25
26       # Add any SQL command to execute
27       if options[:command]
28         args.push("--command")
29         args.push(options[:command])
30       end
31
32       # Add any file to execute SQL commands from
33       if options[:file]
34         args.push("--file")
35         args.push(options[:file])
36       end
37
38       # Add the database name
39       args.push(options[:database] || "template1")
40
41       # Get the user and group to run as
42       user = options[:user] || "postgres"
43       group = options[:group] || "postgres"
44
45       # Run the command
46       shell_out!("/usr/bin/psql", *args, :user => user, :group => group)
47     end
48
49     def query(sql, options = {})
50       # Run the query
51       result = execute(options.merge(:command => sql, :align => false))
52
53       # Split the output into lines
54       lines = result.stdout.split("\n")
55
56       # Remove the "(N rows)" line from the end
57       lines.pop
58
59       # Get the field names
60       fields = lines.shift.split("|")
61
62       # Extract the record data
63       lines.collect do |line|
64         record = {}
65         fields.zip(line.split("|")) { |name, value| record[name.to_sym] = value }
66         record
67       end
68     end
69
70     def users
71       @users ||= query("SELECT * FROM pg_user").each_with_object({}) do |user, users|
72         users[user[:usename]] = {
73           :superuser => user[:usesuper] == "t",
74           :createdb => user[:usercreatedb] == "t",
75           :createrole => user[:usecatupd] == "t",
76           :replication => user[:userepl] == "t"
77         }
78       end
79     end
80
81     def databases
82       @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|
83         databases[database[:datname]] = {
84           :owner => database[:usename],
85           :encoding => database[:encoding],
86           :collate => database[:datcollate],
87           :ctype => database[:datctype]
88         }
89       end
90     end
91
92     def extensions(database)
93       @extensions ||= {}
94       @extensions[database] ||= query("SELECT extname, extversion FROM pg_extension", :database => database).each_with_object({}) do |extension, extensions|
95         extensions[extension[:extname]] = {
96           :version => extension[:extversion]
97         }
98       end
99     end
100
101     def tablespaces
102       @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|
103         tablespaces[tablespace[:spcname]] = {
104           :owner => tablespace[:usename]
105         }
106       end
107     end
108
109     def tables(database)
110       @tables ||= {}
111       @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|
112         name = "#{table[:nspname]}.#{table[:relname]}"
113
114         tables[name] = {
115           :owner => table[:usename],
116           :permissions => parse_acl(table[:relacl] || "{}")
117         }
118       end
119     end
120
121     private
122
123     def parse_acl(acl)
124       acl.sub(/^\{(.*)\}$/, "\\1").split(",").each_with_object({}) do |entry, permissions|
125         entry = entry.sub(/^"(.*)"$/) { Regexp.last_match[1].gsub(/\\"/, '"') }.sub(%r{/.*$}, "")
126         user, privileges = entry.split("=")
127
128         user = user.sub(/^"(.*)"$/, "\\1")
129         user = "public" if user == ""
130
131         permissions[user] = {
132           "a" => :insert, "r" => :select, "w" => :update, "d" => :delete,
133           "D" => :truncate, "x" => :references, "t" => :trigger
134         }.values_at(*privileges.chars).compact
135       end
136     end
137   end
138 end