X-Git-Url: https://git.openstreetmap.org./chef.git/blobdiff_plain/faf8ae12e85eabb050b0f5eceb2cb67ad1de5261..571e5982cc82c4fbefff0d463895a029a68921fe:/cookbooks/mysql/resources/database.rb?ds=sidebyside diff --git a/cookbooks/mysql/resources/database.rb b/cookbooks/mysql/resources/database.rb index 60236d8dd..69e4e795f 100644 --- a/cookbooks/mysql/resources/database.rb +++ b/cookbooks/mysql/resources/database.rb @@ -1,14 +1,14 @@ # -# Cookbook Name:: mysql +# Cookbook:: mysql # Resource:: mysql_database # -# Copyright 2013, OpenStreetMap Foundation +# Copyright:: 2013, OpenStreetMap Foundation # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # -# http://www.apache.org/licenses/LICENSE-2.0 +# https://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, @@ -17,8 +17,71 @@ # limitations under the License. # -actions :create, :drop +unified_mode true + default_action :create -attribute :database, :kind_of => String, :name_attribute => true -attribute :permissions, :kind_of => Hash, :default => {} +property :database, :kind_of => String, :name_property => true +property :permissions, :kind_of => Hash, :default => {} + +action :create do + unless mysql_databases.include?(new_resource.database) + converge_by("create #{new_resource}") do + Chef::Log.info("Creating #{new_resource}") + mysql_execute(:command => "CREATE DATABASE `#{new_resource.database}`") + end + end + + current_permissions = mysql_databases[new_resource.database]&.dig(:permissions) || {} + + new_permissions = Hash[new_resource.permissions.collect do |user, privileges| + [mysql_canonicalise_user(user), privileges] + end] + + current_permissions.each_key do |user| + next if new_permissions[user] + + converge_by("revoke all for #{user} on #{new_resource}") do + Chef::Log.info("Revoking all for #{user} on #{new_resource}") + mysql_execute(:command => "REVOKE ALL ON `#{new_resource.database}`.* FROM #{user}") + end + end + + new_permissions.each do |user, new_privileges| + current_privileges = current_permissions[user] || {} + new_privileges = Array(new_privileges) + + if new_privileges.include?(:all) + new_privileges |= (OpenStreetMap::MySQL::DATABASE_PRIVILEGES - [:grant]) + end + + OpenStreetMap::MySQL::DATABASE_PRIVILEGES.each do |privilege| + if new_privileges.include?(privilege) + unless current_privileges.include?(privilege) + converge_by("grant #{privilege} for #{user} on mysql database #{new_resource}") do + Chef::Log.info("Granting #{privilege} for #{user} on mysql database #{new_resource}") + mysql_execute(:command => "GRANT #{mysql_privilege_name(privilege)} ON `#{new_resource.database}`.* TO #{user}") + end + end + elsif current_privileges.include?(privilege) + converge_by("revoke #{privilege} for #{user} on #{new_resource}") do + Chef::Log.info("Revoking #{privilege} for #{user} on #{new_resource}") + mysql_execute(:command => "REVOKE #{mysql_privilege_name(privilege)} ON `#{new_resource.database}`.* FROM #{user}") + end + end + end + end +end + +action :drop do + if mysql_databases.include?(new_resource.database) + converge_by("drop #{new_resource}") do + Chef::Log.info("Dropping #{new_resource}") + mysql_execute(:command => "DROP DATABASE `#{new_resource.database}`") + end + end +end + +action_class do + include OpenStreetMap::MySQL +end