2011-05-25 08:22:43 +02:00
|
|
|
Puppet::Type.type(:database).provide(:mysql) do
|
2012-02-09 20:26:00 +01:00
|
|
|
desc "Manages MySQL database."
|
2011-05-25 08:22:43 +02:00
|
|
|
|
|
|
|
defaultfor :kernel => 'Linux'
|
|
|
|
|
2011-11-16 16:38:15 +01:00
|
|
|
optional_commands :mysql => 'mysql'
|
2012-02-09 20:26:00 +01:00
|
|
|
optional_commands :mysqladmin => 'mysqladmin'
|
|
|
|
|
2013-01-10 20:51:59 +01:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
def self.instances
|
2013-01-10 20:51:59 +01:00
|
|
|
mysql([defaults_file, '-NBe', "show databases"].compact).split("\n").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-01-10 20:51:59 +01:00
|
|
|
mysql([defaults_file, '-NBe', "create database `#{@resource[:name]}` character set #{resource[:charset]}"].compact)
|
2011-05-25 08:22:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2013-01-10 20:51:59 +01:00
|
|
|
mysqladmin([defaults_file, '-f', 'drop', @resource[:name]].compact)
|
2011-05-25 08:22:43 +02:00
|
|
|
end
|
|
|
|
|
2012-02-09 20:26:00 +01:00
|
|
|
def charset
|
2013-01-10 20:51:59 +01:00
|
|
|
mysql([defaults_file, '-NBe', "show create database `#{resource[:name]}`"].compact).match(/.*?(\S+)\s(?:COLLATE.*)?\*\//)[1]
|
2012-02-09 20:26:00 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def charset=(value)
|
2013-01-10 20:51:59 +01:00
|
|
|
mysql([defaults_file, '-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}"].compact)
|
2012-02-09 20:26:00 +01:00
|
|
|
end
|
|
|
|
|
2011-05-25 08:22:43 +02:00
|
|
|
def exists?
|
|
|
|
begin
|
2013-01-10 20:51:59 +01:00
|
|
|
mysql([defaults_file, '-NBe', "show databases"].compact).match(/^#{@resource[:name]}$/)
|
2011-05-25 08:22:43 +02:00
|
|
|
rescue => e
|
|
|
|
debug(e.message)
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|