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'
|
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
optional_commands :mysql => 'mysql'
|
2011-11-16 16:38:15 +01:00
|
|
|
optional_commands :mysqladmin => 'mysqladmin'
|
2011-05-25 08:22:43 +02:00
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
def self.instances
|
2012-08-16 03:03:37 +02:00
|
|
|
users = mysql("mysql", '-BNe' "select concat(User, '@',Host) as User from mysql.user").split("\n")
|
2012-03-15 07:05:20 +01:00
|
|
|
users.select{ |user| user =~ /.+@/ }.collect do |name|
|
2012-02-09 20:26:00 +01:00
|
|
|
new(:name => name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-25 08:22:43 +02:00
|
|
|
def create
|
2012-08-16 03:03:37 +02:00
|
|
|
mysql("mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ])
|
2011-05-25 08:22:43 +02:00
|
|
|
end
|
2012-02-09 20:26:00 +01:00
|
|
|
|
2011-05-25 08:22:43 +02:00
|
|
|
def destroy
|
2012-08-16 03:03:37 +02:00
|
|
|
mysql("mysql", "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") )
|
2011-05-25 08:22:43 +02:00
|
|
|
end
|
2012-02-09 20:26:00 +01:00
|
|
|
|
2011-05-25 08:22:43 +02:00
|
|
|
def password_hash
|
2012-08-16 03:03:37 +02:00
|
|
|
mysql("mysql", "-NBe", "select password from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).chomp
|
2011-05-25 08:22:43 +02:00
|
|
|
end
|
2012-02-09 20:26:00 +01:00
|
|
|
|
2011-05-25 08:22:43 +02:00
|
|
|
def password_hash=(string)
|
2012-08-16 03:03:37 +02:00
|
|
|
mysql("mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] )
|
2011-05-25 08:22:43 +02:00
|
|
|
end
|
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
def exists?
|
2012-08-16 03:03:37 +02:00
|
|
|
not mysql("mysql", "-NBe", "select '1' from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).empty?
|
2012-02-09 20:26:00 +01:00
|
|
|
end
|
2011-05-25 08:22:43 +02:00
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
def flush
|
|
|
|
@property_hash.clear
|
2012-08-16 03:03:37 +02:00
|
|
|
mysqladmin "flush-privileges"
|
2011-05-25 08:22:43 +02:00
|
|
|
end
|
2012-02-09 20:26:00 +01:00
|
|
|
|
2011-05-25 08:22:43 +02:00
|
|
|
end
|