]> git.openstreetmap.org Git - chef.git/blob - cookbooks/mysql/resources/database.rb
Delete mediawiki link refresh service and timer
[chef.git] / cookbooks / mysql / resources / database.rb
1 #
2 # Cookbook:: mysql
3 # Resource:: mysql_database
4 #
5 # Copyright:: 2013, OpenStreetMap Foundation
6 #
7 # Licensed under the Apache License, Version 2.0 (the "License");
8 # you may not use this file except in compliance with the License.
9 # You may obtain a copy of the License at
10 #
11 # https://www.apache.org/licenses/LICENSE-2.0
12 #
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
18 #
19
20 unified_mode true
21
22 default_action :create
23
24 property :database, :kind_of => String, :name_property => true
25 property :permissions, :kind_of => Hash, :default => {}
26
27 action :create do
28   unless mysql_databases.include?(new_resource.database)
29     converge_by("create #{new_resource}") do
30       Chef::Log.info("Creating #{new_resource}")
31       mysql_execute(:command => "CREATE DATABASE `#{new_resource.database}`")
32     end
33   end
34
35   current_permissions = mysql_databases[new_resource.database]&.dig(:permissions) || {}
36
37   new_permissions = Hash[new_resource.permissions.collect do |user, privileges|
38     [mysql_canonicalise_user(user), privileges]
39   end]
40
41   current_permissions.each_key do |user|
42     next if new_permissions[user]
43
44     converge_by("revoke all for #{user} on #{new_resource}") do
45       Chef::Log.info("Revoking all for #{user} on #{new_resource}")
46       mysql_execute(:command => "REVOKE ALL ON `#{new_resource.database}`.* FROM #{user}")
47     end
48   end
49
50   new_permissions.each do |user, new_privileges|
51     current_privileges = current_permissions[user] || {}
52     new_privileges = Array(new_privileges)
53
54     if new_privileges.include?(:all)
55       new_privileges |= (OpenStreetMap::MySQL::DATABASE_PRIVILEGES - [:grant])
56     end
57
58     OpenStreetMap::MySQL::DATABASE_PRIVILEGES.each do |privilege|
59       if new_privileges.include?(privilege)
60         unless current_privileges.include?(privilege)
61           converge_by("grant #{privilege} for #{user} on mysql database #{new_resource}") do
62             Chef::Log.info("Granting #{privilege} for #{user} on mysql database #{new_resource}")
63             mysql_execute(:command => "GRANT #{mysql_privilege_name(privilege)} ON `#{new_resource.database}`.* TO #{user}")
64           end
65         end
66       elsif current_privileges.include?(privilege)
67         converge_by("revoke #{privilege} for #{user} on #{new_resource}") do
68           Chef::Log.info("Revoking #{privilege} for #{user} on #{new_resource}")
69           mysql_execute(:command => "REVOKE #{mysql_privilege_name(privilege)} ON `#{new_resource.database}`.* FROM #{user}")
70         end
71       end
72     end
73   end
74 end
75
76 action :drop do
77   if mysql_databases.include?(new_resource.database)
78     converge_by("drop #{new_resource}") do
79       Chef::Log.info("Dropping #{new_resource}")
80       mysql_execute(:command => "DROP DATABASE `#{new_resource.database}`")
81     end
82   end
83 end
84
85 action_class do
86   include OpenStreetMap::MySQL
87 end