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

53 lines
1.3 KiB
Ruby
Raw Normal View History

2011-05-25 08:22:43 +02:00
Puppet::Type.type(:database).provide(:mysql) do
desc "Manages MySQL database."
2011-05-25 08:22:43 +02:00
defaultfor :kernel => 'Linux'
optional_commands :mysql => 'mysql'
optional_commands :mysqladmin => 'mysqladmin'
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
def self.instances
mysql([defaults_file, '-NBe', "show databases"].compact).split("\n").collect do |name|
new(:name => name)
end
end
2011-05-25 08:22:43 +02:00
def create
mysql([defaults_file, '-NBe', "create database `#{@resource[:name]}` character set #{resource[:charset]}"].compact)
2011-05-25 08:22:43 +02:00
end
def destroy
mysqladmin([defaults_file, '-f', 'drop', @resource[:name]].compact)
2011-05-25 08:22:43 +02:00
end
def charset
mysql([defaults_file, '-NBe', "show create database `#{resource[:name]}`"].compact).match(/.*?(\S+)\s(?:COLLATE.*)?\*\//)[1]
end
def charset=(value)
mysql([defaults_file, '-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}"].compact)
end
2011-05-25 08:22:43 +02:00
def exists?
begin
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