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'
|
|
|
|
|
2013-07-03 21:59:17 +02:00
|
|
|
commands :mysql => 'mysql'
|
|
|
|
commands :mysqladmin => 'mysqladmin'
|
2011-05-25 08:22:43 +02:00
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
def self.instances
|
2013-01-10 20:51:59 +01:00
|
|
|
users = mysql([defaults_file, "mysql", '-BNe' "select concat(User, '@',Host) as User from mysql.user"].compact).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
|
2013-07-03 21:59:17 +02:00
|
|
|
merged_name = @resource[:name].sub("@", "'@'")
|
|
|
|
password_hash = @resource.value(:password_hash)
|
|
|
|
mysql([defaults_file, "mysql", "-e", "create user '#{merged_name}' identified by PASSWORD '#{password_hash}'"].compact)
|
|
|
|
|
|
|
|
exists? ? (return true) : (return false)
|
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
|
2013-07-03 21:59:17 +02:00
|
|
|
merged_name = @resource[:name].sub("@", "'@'")
|
|
|
|
mysql([defaults_file, "mysql", "-e", "drop user '#{merged_name}'"].compact)
|
|
|
|
|
|
|
|
exists? ? (return false) : (return true)
|
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
|
2013-07-03 21:59:17 +02:00
|
|
|
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
|
2012-02-09 20:26:00 +01:00
|
|
|
|
2011-05-25 08:22:43 +02:00
|
|
|
def password_hash=(string)
|
2013-01-10 20:51:59 +01:00
|
|
|
mysql([defaults_file, "mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] ].compact)
|
2013-07-03 21:59:17 +02:00
|
|
|
|
|
|
|
password_hash == string ? (return true) : (return false)
|
2011-05-25 08:22:43 +02:00
|
|
|
end
|
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
def exists?
|
2013-01-10 20:51:59 +01:00
|
|
|
not mysql([defaults_file, "mysql", "-NBe", "select '1' from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)].compact).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
|
2013-01-10 20:51:59 +01:00
|
|
|
mysqladmin([defaults_file, "flush-privileges"].compact)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Optional defaults file
|
|
|
|
def self.defaults_file
|
|
|
|
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
|
|
|
|
"--defaults-file=#{Facter.value(:root_home)}/.my.cnf"
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def defaults_file
|
|
|
|
self.class.defaults_file
|
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
|