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