module-puppetlabs-mysql/lib/puppet/provider/database_user/mysql.rb

66 lines
2.3 KiB
Ruby
Raw Normal View History

require File.expand_path(File.join(File.dirname(__FILE__), '..', 'mysql'))
Puppet::Type.type(:database_user).provide(:mysql, :parent => Puppet::Provider::Mysql) do
2011-05-25 08:22:43 +02:00
desc 'manage users for a mysql database.'
2011-05-25 08:22:43 +02:00
defaultfor :kernel => 'Linux'
commands :mysql => 'mysql'
commands :mysqladmin => 'mysqladmin'
2011-05-25 08:22:43 +02:00
def self.instances
users = mysql([defaults_file, 'mysql', '-BNe' "select concat(User, '@',Host) as User from mysql.user"].compact).split("\n")
users.select{ |user| user =~ /.+@/ }.collect do |name|
new(:name => name)
end
end
2011-05-25 08:22:43 +02:00
def create
merged_name = self.class.cmd_user(@resource[:name])
password_hash = @resource.value(:password_hash)
max_user_connections = @resource.value(:max_user_connections) || 0
mysql([defaults_file, 'mysql', '-e', "grant usage on *.* to #{merged_name} identified by PASSWORD
'#{password_hash}' with max_user_connections #{max_user_connections}"].compact)
exists? ? (return true) : (return false)
2011-05-25 08:22:43 +02:00
end
2011-05-25 08:22:43 +02:00
def destroy
merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, 'mysql', '-e', "drop user #{merged_name}"].compact)
exists? ? (return false) : (return true)
2011-05-25 08:22:43 +02:00
end
2011-05-25 08:22:43 +02:00
def password_hash
mysql([defaults_file, 'mysql', '-NBe', "select password from mysql.user where CONCAT(user, '@', host) = '#{@resource[:name]}'"].compact).chomp
2011-05-25 08:22:43 +02:00
end
2011-05-25 08:22:43 +02:00
def password_hash=(string)
mysql([defaults_file, 'mysql', '-e', "SET PASSWORD FOR #{self.class.cmd_user(@resource[:name])} = '#{string}'"].compact)
password_hash == string ? (return true) : (return false)
2011-05-25 08:22:43 +02:00
end
def max_user_connections
mysql([defaults_file, "mysql", "-NBe", "select max_user_connections from mysql.user where CONCAT(user, '@', host) = '#{@resource[:name]}'"].compact).chomp
end
def max_user_connections=(int)
mysql([defaults_file, "mysql", "-e", "grant usage on *.* to %s with max_user_connections #{int}" % [ self.class.cmd_user(@resource[:name])] ].compact).chomp
max_user_connections == int ? (return true) : (return false)
end
def exists?
not mysql([defaults_file, 'mysql', '-NBe', "select '1' from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)].compact).empty?
end
2011-05-25 08:22:43 +02:00
def flush
@property_hash.clear
mysqladmin([defaults_file, 'flush-privileges'].compact)
end
2011-05-25 08:22:43 +02:00
end