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-10-09 22:44:54 +02:00
users = mysql ( " --defaults-file= #{ Facter . value ( :root_home ) } /.my.cnf " , " 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-10-09 22:44:54 +02:00
mysql ( " --defaults-file= #{ Facter . value ( :root_home ) } /.my.cnf " , " 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-10-09 22:44:54 +02:00
mysql ( " --defaults-file= #{ Facter . value ( :root_home ) } /.my.cnf " , " 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-10-09 22:44:54 +02:00
mysql ( " --defaults-file= #{ Facter . value ( :root_home ) } /.my.cnf " , " mysql " , " -NBe " , " select password from mysql.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-10-09 22:44:54 +02:00
mysql ( " --defaults-file= #{ Facter . value ( :root_home ) } /.my.cnf " , " 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-10-09 22:44:54 +02:00
not mysql ( " --defaults-file= #{ Facter . value ( :root_home ) } /.my.cnf " , " mysql " , " -NBe " , " select '1' from mysql.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-05-03 01:28:57 +02:00
mysqladmin " --defaults-file= #{ Facter . value ( :root_home ) } /.my.cnf " , " 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