]> git.openstreetmap.org Git - chef.git/blob - cookbooks/mysql/resources/user.rb
Replace cgiirc with a form frontend for webchat.oftc.net
[chef.git] / cookbooks / mysql / resources / user.rb
1 #
2 # Cookbook:: mysql
3 # Resource:: mysql_user
4 #
5 # Copyright:: 2012, 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 default_action :create
21
22 property :user, :kind_of => String, :name_property => true
23 property :password, :kind_of => String
24
25 OpenStreetMap::MySQL::USER_PRIVILEGES.each do |privilege|
26   property privilege, :default => false
27 end
28
29 action :create do
30   user = mysql_canonicalise_user(new_resource.user)
31   password = new_resource.password ? "IDENTIFIED BY '#{new_resource.password}'" : ""
32
33   unless mysql_users.include?(user)
34     converge_by("create #{new_resource}") do
35       Chef::Log.info("Creating #{new_resource}")
36       mysql_execute(:command => "CREATE USER #{user} #{password}")
37     end
38   end
39
40   current_privileges = mysql_users.fetch(new_resource.user, {})
41
42   new_privileges = Hash[OpenStreetMap::MySQL::USER_PRIVILEGES.collect do |privilege|
43     [privilege, new_resource.send(privilege)]
44   end]
45
46   new_privileges.each do |privilege, new_enabled|
47     old_enabled = current_privileges.fetch(privilege, false)
48
49     if new_enabled && !old_enabled
50       converge_by("grant #{privilege} for #{new_resource}") do
51         Chef::Log.info("Granting #{privilege} for #{new_resource}")
52         mysql_execute(:command => "GRANT #{mysql_privilege_name(privilege)} ON *.* TO #{user}")
53       end
54     elsif old_enabled && !new_enabled
55       converge_by("revoke #{privilege} for #{new_resource}") do
56         Chef::Log.info("Revoking #{privilege} for #{new_resource}")
57         mysql_execute(:command => "REVOKE #{mysql_privilege_name(privilege)} ON *.* FROM #{user}")
58       end
59     end
60   end
61 end
62
63 action :drop do
64   user = mysql_canonicalise_user(new_resource.user)
65
66   if mysql_users.include?(user)
67     converge_by("drop #{new_resource}") do
68       Chef::Log.info("Dropping #{new_resource}")
69       mysql_execute(:command => "DROP USER #{user}")
70     end
71   end
72 end
73
74 action_class do
75   include OpenStreetMap::MySQL
76 end