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

39 lines
1 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'
commands :mysql => 'mysql'
commands :mysqladmin => 'mysqladmin'
def create
mysql "mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ]
mysql_flush
end
def destroy
mysql "mysql", "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'")
mysql_flush
end
def exists?
not mysql("mysql", "-NBe", "select '1' from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).empty?
end
def password_hash
mysql("mysql", "-NBe", "select password from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).chomp
end
def password_hash=(string)
mysql "mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ]
mysql_flush
end
private
def mysql_flush
mysqladmin "flush-privileges"
end
end