Skip to content

Instantly share code, notes, and snippets.

@seancribbs
Created March 11, 2009 02:55
Show Gist options
  • Save seancribbs/77271 to your computer and use it in GitHub Desktop.
Save seancribbs/77271 to your computer and use it in GitHub Desktop.
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