-
-
Save corey/77278 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
define :mysql_database, :action => 'create' do | |
def mysql(command, config=nil, ) | |
"mysql --defaults-file=#{config} --execute=#{command.inspect}" | |
end | |
include_recipe "mysql::client" | |
# For now, use the super-user conf | |
case @node[:platform] | |
when "debian", "ubuntu" | |
mysql_conf = "/etc/mysql/debian.cnf" | |
end | |
if params[:action] == 'create' | |
execute "creating database #{params[:name]}" do | |
command mysql("CREATE DATABASE IF NOT EXISTS #{params[:name]}", mysql_conf) | |
end | |
end | |
if params[:action] == 'delete' | |
execute "dropping database #{params[:name]}" do | |
command mysql("DROP DATABASE #{params[:name]}", mysql_conf) | |
end | |
end | |
if params[:action] =~ /create|modify/ && params[:grant] && params[:grant][:user] | |
permissions = params[:grant][:permissions].join(", ") || "ALL PRIVILEGES" | |
cmd = "GRANT #{permissions} ON #{params[:name]}.* TO #{params[:grant][:user]}" | |
cmd << " IDENTIFIED BY '#{params[:grant][:password]}'" if params[:grant][:password] | |
cmd << " IDENTIFIED BY PASSWORD '#{params[:grant][:hashed_password]}'" if params[:grant][:hashed_password] | |
cmd << " WITH GRANT OPTION" if params[:grant][:grant] == true | |
execute "granting permissions to #{params[:grant][:user]}" do | |
command mysql(cmd, mysql_conf) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment