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

View file

@ -54,8 +54,20 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
commands :mysql => '/usr/bin/mysql'
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
mysqladmin "flush-privileges"
mysqladmin(defaults_file, "flush-privileges")
end
# this parses the
@ -101,24 +113,24 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
name = split_name(@resource[:name])
case name[:type]
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],
]
])
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],
]
])
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],
]
])
end
mysql_flush
end
end
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
def row_exists?
@ -130,7 +142,7 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
if name[:type] == :column
fields << :column
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
def all_privs_set?
@ -156,15 +168,15 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
case name[:type]
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
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
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
return privs
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
if privs.match(/^$/)
@ -213,7 +225,7 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
if !revoke.empty?
#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
set = privs - currently_set
@ -245,7 +257,7 @@ Puppet::Type.type(:mysql_grant).provide(:mysql) do
#puts "stmt:", stmt
if !set.empty?
mysql "mysql", "-Be", stmt
mysql(defaults_file, "mysql", "-Be", stmt)
mysql_flush
end
end

View file

@ -8,11 +8,23 @@ Puppet::Type.type(:mysql_user).provide(:mysql,
commands :mysql => '/usr/bin/mysql'
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
def self.instances
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|
process.each do |line|
users << new( query_line_to_hash(line) )
@ -31,13 +43,13 @@ Puppet::Type.type(:mysql_user).provide(:mysql,
end
def mysql_flush
mysqladmin "flush-privileges"
mysqladmin(defaults_file,"flush-privileges")
end
def query
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|
process.each do |line|
unless result.empty?
@ -51,17 +63,17 @@ Puppet::Type.type(:mysql_user).provide(:mysql,
end
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
end
def destroy
mysql "mysql", "-e", "drop user '%s'" % @resource[:name].sub("@", "'@'")
mysql(defaults_file, "mysql", "-e", "drop user '%s'" % @resource[:name].sub("@", "'@'"))
mysql_flush
end
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
def password_hash
@ -69,7 +81,7 @@ Puppet::Type.type(:mysql_user).provide(:mysql,
end
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
end
end