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

52 lines
1.4 KiB
Ruby
Raw Normal View History

2011-05-25 08:22:43 +02:00
Puppet::Type.type(:database_user).provide(:mysql) do
desc "manage users for a mysql database."
defaultfor :kernel => 'Linux'
optional_commands :mysql => 'mysql'
optional_commands :mysqladmin => 'mysqladmin'
2011-05-25 08:22:43 +02:00
def defaults_file
case Facter.value(:operatingsystem)
when "Debian", "Ubuntu"
return "--defaults-file=/etc/mysql/debian.cnf"
else
return ""
end
end
def self.instances
users = mysql(defaults_file, '-BNe' "select concat(User, '@',Host) as User from mysql.user").split("\n")
users.select{ |user| user =~ /.+@/ }.collect do |name|
new(:name => name)
end
end
2011-05-25 08:22:43 +02:00
def create
mysql(defaults_file, "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ])
2011-05-25 08:22:43 +02:00
end
2011-05-25 08:22:43 +02:00
def destroy
mysql(defaults_file, "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") )
2011-05-25 08:22:43 +02:00
end
2011-05-25 08:22:43 +02:00
def password_hash
mysql(defaults_file, "-NBe", "select password from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).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, "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] )
2011-05-25 08:22:43 +02:00
end
def exists?
not mysql(defaults_file, "-NBe", "select '1' from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).empty?
end
2011-05-25 08:22:43 +02:00
def flush
@property_hash.clear
mysqladmin(defaults_file, "flush-privileges")
2011-05-25 08:22:43 +02:00
end
2011-05-25 08:22:43 +02:00
end