use the defaults file if available

This commit is contained in:
mh 2013-05-20 14:32:43 +02:00
parent 1e8e9f472c
commit 86ef2c3dd2
3 changed files with 63 additions and 27 deletions

View file

@ -7,11 +7,23 @@ Puppet::Type.type(:mysql_database).provide(:mysql,
commands :mysqladmin => '/usr/bin/mysqladmin' commands :mysqladmin => '/usr/bin/mysqladmin'
commands :mysql => '/usr/bin/mysql' commands :mysql => '/usr/bin/mysql'
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
# retrieve the current set of mysql users # retrieve the current set of mysql users
def self.instances def self.instances
dbs = [] dbs = []
cmd = "#{command(:mysql)} mysql -NBe 'show databases'" cmd = "#{command(:mysql)} mysql #{defaults_file} -NBe 'show databases'"
execpipe(cmd) do |process| execpipe(cmd) do |process|
process.each do |line| process.each do |line|
dbs << new( { :ensure => :present, :name => line.chomp } ) dbs << new( { :ensure => :present, :name => line.chomp } )
@ -26,7 +38,7 @@ Puppet::Type.type(:mysql_database).provide(:mysql,
:ensure => :absent :ensure => :absent
} }
cmd = "#{command(:mysql)} mysql -NBe 'show databases'" cmd = "#{command(:mysql)} #{defaults_file} mysql -NBe 'show databases'"
execpipe(cmd) do |process| execpipe(cmd) do |process|
process.each do |line| process.each do |line|
if line.chomp.eql?(@resource[:name]) if line.chomp.eql?(@resource[:name])
@ -38,14 +50,14 @@ Puppet::Type.type(:mysql_database).provide(:mysql,
end end
def create def create
mysqladmin "create", @resource[:name] mysqladmin(defaults_file, "create", @resource[:name])
end end
def destroy def destroy
mysqladmin "-f", "drop", @resource[:name] mysqladmin(defaults_file, "-f", "drop", @resource[:name])
end end
def exists? def exists?
if mysql("mysql", "-NBe", "show databases").match(/^#{@resource[:name]}$/) if mysql(defaults_file, "mysql" ,"-NBe", "show databases").match(/^#{@resource[:name]}$/)
true true
else else
false false

View file

@ -54,8 +54,20 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
commands :mysql => '/usr/bin/mysql' commands :mysql => '/usr/bin/mysql'
commands :mysqladmin => '/usr/bin/mysqladmin' commands :mysqladmin => '/usr/bin/mysqladmin'
# Optional defaults file
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 mysql_flush def mysql_flush
mysqladmin "flush-privileges" mysqladmin(defaults_file, "flush-privileges")
end end
# this parses the # this parses the
@ -101,24 +113,24 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
name = split_name(@resource[:name]) name = split_name(@resource[:name])
case name[:type] case name[:type]
when :user when :user
mysql "mysql", "-e", "INSERT INTO user (host, user) VALUES ('%s', '%s')" % [ mysql(defaults_file, "mysql", "-e", "INSERT INTO user (host, user) VALUES ('%s', '%s')" % [
name[:host], name[:user], name[:host], name[:user],
] ])
when :db when :db
mysql "mysql", "-e", "INSERT INTO db (host, user, db) VALUES ('%s', '%s', '%s')" % [ mysql(defaults_file, "mysql", "-e", "INSERT INTO db (host, user, db) VALUES ('%s', '%s', '%s')" % [
name[:host], name[:user], name[:db], name[:host], name[:user], name[:db],
] ])
when :column when :column
mysql "mysql", "-e", "INSERT INTO columns_priv (host, user, db, table, column_name) VALUES ('%s', '%s', '%s', '%s', '%s')" % [ mysql(defaults_file, "mysql", "-e", "INSERT INTO columns_priv (host, user, db, table, column_name) VALUES ('%s', '%s', '%s', '%s', '%s')" % [
name[:host], name[:user], name[:db], name[:table], name[:column], name[:host], name[:user], name[:db], name[:table], name[:column],
] ])
end end
mysql_flush mysql_flush
end end
end end
def destroy def destroy
mysql "mysql", "-e", "REVOKE ALL ON '%s'.* FROM '%s@%s'" % [ @resource[:privileges], @resource[:database], @resource[:name], @resource[:host] ] mysql(defaults_file, "mysql", "-e", "REVOKE ALL ON '%s'.* FROM '%s@%s'" % [ @resource[:privileges], @resource[:database], @resource[:name], @resource[:host] ])
end end
def row_exists? def row_exists?
@ -130,7 +142,7 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
if name[:type] == :column if name[:type] == :column
fields << :column fields << :column
end end
not mysql( "mysql", "-NBe", 'SELECT "1" FROM %s WHERE %s' % [ name[:type], fields.map do |f| "%s = '%s'" % [f, name[f]] end.join(' AND ')]).empty? not mysql(defaults_file, "mysql", "-NBe", 'SELECT "1" FROM %s WHERE %s' % [ name[:type], fields.map do |f| "%s = '%s'" % [f, name[f]] end.join(' AND ')]).empty?
end end
def all_privs_set? def all_privs_set?
@ -156,15 +168,15 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
case name[:type] case name[:type]
when :user when :user
privs = mysql "mysql", "-Be", 'select * from user where user="%s" and host="%s"' % [ name[:user], name[:host] ] privs = mysql(defaults_file, "mysql", "-Be", 'select * from user where user="%s" and host="%s"' % [ name[:user], name[:host] ])
when :db when :db
privs = mysql "mysql", "-Be", 'select * from db where user="%s" and host="%s" and db="%s"' % [ name[:user], name[:host], name[:db] ] privs = mysql(defaults_file, "mysql", "-Be", 'select * from db where user="%s" and host="%s" and db="%s"' % [ name[:user], name[:host], name[:db] ])
when :tables_priv when :tables_priv
privs = mysql "mysql", "-NBe", 'select Table_priv from tables_priv where User="%s" and Host="%s" and Db="%s" and Table_name="%s"' % [ name[:user], name[:host], name[:db], name[:table_name] ] privs = mysql(defaults_file, "mysql", "-NBe", 'select Table_priv from tables_priv where User="%s" and Host="%s" and Db="%s" and Table_name="%s"' % [ name[:user], name[:host], name[:db], name[:table_name] ])
privs = privs.chomp.downcase privs = privs.chomp.downcase
return privs return privs
when :columns when :columns
privs = mysql "mysql", "-Be", 'select * from columns_priv where User="%s" and Host="%s" and Db="%s" and Table_name="%s" and Column_name="%s"' % [ name[:user], name[:host], name[:db], name[:table], name[:column] ] privs = mysql(defaults_file, "mysql", "-Be", 'select * from columns_priv where User="%s" and Host="%s" and Db="%s" and Table_name="%s" and Column_name="%s"' % [ name[:user], name[:host], name[:db], name[:table], name[:column] ])
end end
if privs.match(/^$/) if privs.match(/^$/)
@ -213,7 +225,7 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
if !revoke.empty? if !revoke.empty?
#puts "Revoking table privs: ", revoke #puts "Revoking table privs: ", revoke
mysql "mysql", "-e", "REVOKE %s ON %s.%s FROM '%s'@'%s'" % [ revoke.join(", "), name[:db], name[:table_name], name[:user], name[:host] ] mysql(defaults_file, "mysql", "-e", "REVOKE %s ON %s.%s FROM '%s'@'%s'" % [ revoke.join(", "), name[:db], name[:table_name], name[:user], name[:host] ])
end end
set = privs - currently_set set = privs - currently_set
@ -245,7 +257,7 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
#puts "stmt:", stmt #puts "stmt:", stmt
if !set.empty? if !set.empty?
mysql "mysql", "-Be", stmt mysql(defaults_file, "mysql", "-Be", stmt)
mysql_flush mysql_flush
end end
end end

View file

@ -8,11 +8,23 @@ Puppet::Type.type(:mysql_user).provide(:mysql,
commands :mysql => '/usr/bin/mysql' commands :mysql => '/usr/bin/mysql'
commands :mysqladmin => '/usr/bin/mysqladmin' commands :mysqladmin => '/usr/bin/mysqladmin'
# Optional defaults file
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
# retrieve the current set of mysql users # retrieve the current set of mysql users
def self.instances def self.instances
users = [] users = []
cmd = "#{command(:mysql)} mysql -NBe 'select concat(user, \"@\", host), password from user'" cmd = "#{command(:mysql)} #{defaults_file} mysql -NBe 'select concat(user, \"@\", host), password from user'"
execpipe(cmd) do |process| execpipe(cmd) do |process|
process.each do |line| process.each do |line|
users << new( query_line_to_hash(line) ) users << new( query_line_to_hash(line) )
@ -31,13 +43,13 @@ Puppet::Type.type(:mysql_user).provide(:mysql,
end end
def mysql_flush def mysql_flush
mysqladmin "flush-privileges" mysqladmin(defaults_file,"flush-privileges")
end end
def query def query
result = {} result = {}
cmd = "#{command(:mysql)} -NBe 'select concat(user, \"@\", host), password from user where concat(user, \"@\", host) = \"%s\"'" % @resource[:name] cmd = "#{command(:mysql)} #{defaults_file} -NBe 'select concat(user, \"@\", host), password from user where concat(user, \"@\", host) = \"%s\"'" % @resource[:name]
execpipe(cmd) do |process| execpipe(cmd) do |process|
process.each do |line| process.each do |line|
unless result.empty? unless result.empty?
@ -51,17 +63,17 @@ Puppet::Type.type(:mysql_user).provide(:mysql,
end end
def create def create
mysql "mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.should(:password_hash) ] mysql(defaults_file, "mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.should(:password_hash) ])
mysql_flush mysql_flush
end end
def destroy def destroy
mysql "mysql", "-e", "drop user '%s'" % @resource[:name].sub("@", "'@'") mysql(defaults_file, "mysql", "-e", "drop user '%s'" % @resource[:name].sub("@", "'@'"))
mysql_flush mysql_flush
end end
def exists? def exists?
not mysql("mysql", "-NBe", "select '1' from user where CONCAT(user, '@', host) = '%s'" % @resource[:name]).empty? not mysql(defaults_file, "mysql", "-NBe", "select '1' from user where CONCAT(user, '@', host) = '%s'" % @resource[:name]).empty?
end end
def password_hash def password_hash
@ -69,7 +81,7 @@ Puppet::Type.type(:mysql_user).provide(:mysql,
end end
def password_hash=(string) def password_hash=(string)
mysql "mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] mysql(defaults_file, "mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ])
mysql_flush mysql_flush
end end
end end