]> git.openstreetmap.org Git - chef.git/blob - cookbooks/mysql/providers/user.rb
Switch OTRS to letsencrypt
[chef.git] / cookbooks / mysql / providers / user.rb
1 #
2 # Cookbook Name:: mysql
3 # Provider:: mysql_user
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 #     http://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 use_inline_resources
21
22 def load_current_resource
23   @mysql = Chef::MySQL.new
24
25   @current_resource = Chef::Resource::MysqlUser.new(new_resource.name)
26   @current_resource.user(new_resource.user)
27   if (mysql_user = @mysql.users[@current_resource.user])
28     Chef::MySQL::USER_PRIVILEGES.each do |privilege|
29       @current_resource.send(privilege, mysql_user[privilege])
30     end
31   end
32   @current_resource
33 end
34
35 action :create do
36   user = @mysql.canonicalise_user(new_resource.user)
37   password = new_resource.password ? "IDENTIFIED BY '#{new_resource.password}'" : ""
38
39   unless @mysql.users.include?(user)
40     converge_by("create #{new_resource}") do
41       Chef::Log.info("Creating #{new_resource}")
42       @mysql.execute(:command => "CREATE USER #{user} #{password}")
43     end
44   end
45
46   Chef::MySQL::USER_PRIVILEGES.each do |privilege|
47     next if new_resource.send(privilege) == @current_resource.send(privilege)
48
49     if new_resource.send(privilege)
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     else
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