module-puppetlabs-mysql/lib/puppet/provider/database/mysql.rb
Hunter Haugen ca2b16c691 Patch providers for absent my.cnf
If the /root/.my.cnf file does not exist but is specified by the
`--defaults-file` argument, the mysql calls will fail. The
`mysql::config` class creates this file, but if the custom resources are
used without including our classes then it will still break.

This allows users to use our custom resources without having to use our
classes.
2013-01-10 15:31:21 -08:00

52 lines
1.3 KiB
Ruby

Puppet::Type.type(:database).provide(:mysql) do
desc "Manages MySQL database."
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
def create
mysql([defaults_file, '-NBe', "create database `#{@resource[:name]}` character set #{resource[:charset]}"].compact)
end
def destroy
mysqladmin([defaults_file, '-f', 'drop', @resource[:name]].compact)
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
def exists?
begin
mysql([defaults_file, '-NBe', "show databases"].compact).match(/^#{@resource[:name]}$/)
rescue => e
debug(e.message)
return nil
end
end
end