defaults_file methods removed from every provider (old & new) and moved

the method to the global mysql.rb which is now included in every provider
file.

Class from the global mysql.rb file is a parent of every provider.
This commit is contained in:
Tamas Szasz 2013-11-16 19:09:09 +01:00
parent 4ac8879734
commit fbfc5d831f
4 changed files with 22 additions and 55 deletions

View file

@ -1,4 +1,5 @@
Puppet::Type.type(:database).provide(:mysql) do require File.expand_path(File.join(File.dirname(__FILE__), '..', 'mysql'))
Puppet::Type.type(:database).provide(:mysql, :parent => Puppet::Provider::Mysql) do
desc 'Manages MySQL database.' desc 'Manages MySQL database.'
defaultfor :kernel => 'Linux' defaultfor :kernel => 'Linux'
@ -6,18 +7,6 @@ Puppet::Type.type(:database).provide(:mysql) do
optional_commands :mysql => 'mysql' optional_commands :mysql => 'mysql'
optional_commands :mysqladmin => 'mysqladmin' optional_commands :mysqladmin => 'mysqladmin'
def self.defaults_file
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf"
else
nil
end
end
def defaults_file
self.class.defaults_file
end
def self.instances def self.instances
mysql([defaults_file, '-NBe', 'show databases'].compact).split("\n").collect do |name| mysql([defaults_file, '-NBe', 'show databases'].compact).split("\n").collect do |name|
new(:name => name) new(:name => name)

View file

@ -3,7 +3,8 @@
# user@host => global # user@host => global
# user@host/db => per-db # user@host/db => per-db
Puppet::Type.type(:database_grant).provide(:mysql) do require File.expand_path(File.join(File.dirname(__FILE__), '..', 'mysql'))
Puppet::Type.type(:database_grant).provide(:mysql, :parent => Puppet::Provider::Mysql) do
desc 'Uses mysql as database.' desc 'Uses mysql as database.'
@ -195,16 +196,4 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
end end
end end
# Optional defaults file
def self.defaults_file
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf"
else
nil
end
end
def defaults_file
self.class.defaults_file
end
end end

View file

@ -1,4 +1,5 @@
Puppet::Type.type(:database_user).provide(:mysql) do require File.expand_path(File.join(File.dirname(__FILE__), '..', 'mysql'))
Puppet::Type.type(:database_user).provide(:mysql, :parent => Puppet::Provider::Mysql) do
desc 'manage users for a mysql database.' desc 'manage users for a mysql database.'
@ -15,19 +16,19 @@ Puppet::Type.type(:database_user).provide(:mysql) do
end end
def create def create
merged_name = @resource[:name].sub('@', "'@'") merged_name = self.class.cmd_user(@resource[:name])
password_hash = @resource.value(:password_hash) password_hash = @resource.value(:password_hash)
max_user_connections = @resource.value(:max_user_connections) || 0 max_user_connections = @resource.value(:max_user_connections) || 0
mysql([defaults_file, 'mysql', '-e', "grant usage on *.* to '#{merged_name}' identified by PASSWORD mysql([defaults_file, 'mysql', '-e', "grant usage on *.* to #{merged_name} identified by PASSWORD
'#{password_hash}' with max_user_connections #{max_user_connections}"].compact) '#{password_hash}' with max_user_connections #{max_user_connections}"].compact)
exists? ? (return true) : (return false) exists? ? (return true) : (return false)
end end
def destroy def destroy
merged_name = @resource[:name].sub('@', "'@'") merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, 'mysql', '-e', "drop user '#{merged_name}'"].compact) mysql([defaults_file, 'mysql', '-e', "drop user #{merged_name}"].compact)
exists? ? (return false) : (return true) exists? ? (return false) : (return true)
end end
@ -37,7 +38,7 @@ Puppet::Type.type(:database_user).provide(:mysql) do
end end
def password_hash=(string) def password_hash=(string)
mysql([defaults_file, 'mysql', '-e', "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub('@', "'@'"), string ] ].compact) mysql([defaults_file, 'mysql', '-e', "SET PASSWORD FOR #{self.class.cmd_user(@resource[:name])} = '#{string}'"].compact)
password_hash == string ? (return true) : (return false) password_hash == string ? (return true) : (return false)
end end
@ -47,7 +48,7 @@ Puppet::Type.type(:database_user).provide(:mysql) do
end end
def max_user_connections=(int) def max_user_connections=(int)
mysql([defaults_file, "mysql", "-e", "grant usage on *.* to '%s' with max_user_connections #{int}" % [ @resource[:name].sub("@", "'@'")] ].compact).chomp mysql([defaults_file, "mysql", "-e", "grant usage on *.* to %s with max_user_connections #{int}" % [ self.class.cmd_user(@resource[:name])] ].compact).chomp
max_user_connections == int ? (return true) : (return false) max_user_connections == int ? (return true) : (return false)
end end
@ -61,16 +62,4 @@ Puppet::Type.type(:database_user).provide(:mysql) do
mysqladmin([defaults_file, 'flush-privileges'].compact) mysqladmin([defaults_file, 'flush-privileges'].compact)
end end
# Optional defaults file
def self.defaults_file
if File.file?("#{Facter.value(:root_home)}/.my.cnf")
"--defaults-extra-file=#{Facter.value(:root_home)}/.my.cnf"
else
nil
end
end
def defaults_file
self.class.defaults_file
end
end end

View file

@ -78,36 +78,36 @@ Puppet::Type.type(:mysql_user).provide(:mysql, :parent => Puppet::Provider::Mysq
mk_resource_methods mk_resource_methods
def password_hash=(string) def password_hash=(string)
merged_name = @resource[:name].sub('@', "'@'") merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "SET PASSWORD FOR '#{merged_name}' = '#{string}'"].compact) mysql([defaults_file, '-e', "SET PASSWORD FOR #{merged_name} = '#{string}'"].compact)
password_hash == string ? (return true) : (return false) password_hash == string ? (return true) : (return false)
end end
def max_user_connections=(int) def max_user_connections=(int)
merged_name = @resource[:name].sub('@', "'@'") merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' WITH MAX_USER_CONNECTIONS #{int}"].compact).chomp mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO #{merged_name} WITH MAX_USER_CONNECTIONS #{int}"].compact).chomp
max_user_connections == int ? (return true) : (return false) max_user_connections == int ? (return true) : (return false)
end end
def max_connections_per_hour=(int) def max_connections_per_hour=(int)
merged_name = @resource[:name].sub('@', "'@'") merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' WITH MAX_CONNECTIONS_PER_HOUR #{int}"].compact).chomp mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO #{merged_name} WITH MAX_CONNECTIONS_PER_HOUR #{int}"].compact).chomp
max_connections_per_hour == int ? (return true) : (return false) max_connections_per_hour == int ? (return true) : (return false)
end end
def max_queries_per_hour=(int) def max_queries_per_hour=(int)
merged_name = @resource[:name].sub('@', "'@'") merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' WITH MAX_QUERIES_PER_HOUR #{int}"].compact).chomp mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO #{merged_name} WITH MAX_QUERIES_PER_HOUR #{int}"].compact).chomp
max_queries_per_hour == int ? (return true) : (return false) max_queries_per_hour == int ? (return true) : (return false)
end end
def max_updates_per_hour=(int) def max_updates_per_hour=(int)
merged_name = @resource[:name].sub('@', "'@'") merged_name = self.class.cmd_user(@resource[:name])
mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO '#{merged_name}' WITH MAX_UPDATES_PER_HOUR #{int}"].compact).chomp mysql([defaults_file, '-e', "GRANT USAGE ON *.* TO #{merged_name} WITH MAX_UPDATES_PER_HOUR #{int}"].compact).chomp
max_updates_per_hour == int ? (return true) : (return false) max_updates_per_hour == int ? (return true) : (return false)
end end