Add support for debian defaults file.
Debian defines a defaults file in /etc/mysql/debian.cnf that can be used for system admin tasks. Use it if it's there.
This commit is contained in:
parent
8d97f5dca8
commit
8e404d464b
3 changed files with 50 additions and 23 deletions
|
@ -7,31 +7,40 @@ Puppet::Type.type(:database).provide(:mysql) do
|
|||
optional_commands :mysql => 'mysql'
|
||||
optional_commands :mysqladmin => 'mysqladmin'
|
||||
|
||||
def defaults_file
|
||||
case Facter.value(:operatingsystem)
|
||||
when "Debian", "Ubuntu"
|
||||
return "--defaults-file=/etc/mysql/debian.cnf"
|
||||
else
|
||||
return ""
|
||||
end
|
||||
end
|
||||
|
||||
def self.instances
|
||||
mysql('-NBe', "show databases").split("\n").collect do |name|
|
||||
mysql(defaults_file, '-NBe', "show databases").split("\n").collect do |name|
|
||||
new(:name => name)
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
mysql('-NBe', "create database `#{@resource[:name]}` character set #{resource[:charset]}")
|
||||
mysql(defaults_file, '-NBe', "create database `#{@resource[:name]}` character set #{resource[:charset]}")
|
||||
end
|
||||
|
||||
def destroy
|
||||
mysqladmin('-f', 'drop', @resource[:name])
|
||||
mysqladmin(defaults_file, '-f', 'drop', @resource[:name])
|
||||
end
|
||||
|
||||
def charset
|
||||
mysql('-NBe', "show create database `#{resource[:name]}`").match(/.*?(\S+)\s\*\//)[1]
|
||||
mysql(defaults_file, '-NBe', "show create database `#{resource[:name]}`").match(/.*?(\S+)\s\*\//)[1]
|
||||
end
|
||||
|
||||
def charset=(value)
|
||||
mysql('-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}")
|
||||
mysql(defaults_file, '-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}")
|
||||
end
|
||||
|
||||
def exists?
|
||||
begin
|
||||
mysql('-NBe', "show databases").match(/^#{@resource[:name]}$/)
|
||||
mysql(defaults_file, '-NBe', "show databases").match(/^#{@resource[:name]}$/)
|
||||
rescue => e
|
||||
debug(e.message)
|
||||
return nil
|
||||
|
|
|
@ -12,6 +12,15 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
optional_commands :mysql => 'mysql'
|
||||
optional_commands :mysqladmin => 'mysqladmin'
|
||||
|
||||
def defaults_file
|
||||
case Facter.value(:operatingsystem)
|
||||
when "Debian", "Ubuntu"
|
||||
return "--defaults-file=/etc/mysql/debian.cnf"
|
||||
else
|
||||
return ""
|
||||
end
|
||||
end
|
||||
|
||||
def self.prefetch(resources)
|
||||
@user_privs = query_user_privs
|
||||
@db_privs = query_db_privs
|
||||
|
@ -34,19 +43,19 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
end
|
||||
|
||||
def self.query_user_privs
|
||||
results = mysql("mysql", "-Be", "describe user")
|
||||
results = mysql(defaults_file, "-Be", "describe user")
|
||||
column_names = results.split(/\n/).map { |l| l.chomp.split(/\t/)[0] }
|
||||
@user_privs = column_names.delete_if { |e| !( e =~/_priv$/) }
|
||||
end
|
||||
|
||||
def self.query_db_privs
|
||||
results = mysql("mysql", "-Be", "describe db")
|
||||
results = mysql(defaults_file, "-Be", "describe db")
|
||||
column_names = results.split(/\n/).map { |l| l.chomp.split(/\t/)[0] }
|
||||
@db_privs = column_names.delete_if { |e| !(e =~/_priv$/) }
|
||||
end
|
||||
|
||||
def mysql_flush
|
||||
mysqladmin "flush-privileges"
|
||||
mysqladmin defaults_file, "flush-privileges"
|
||||
end
|
||||
|
||||
# this parses the
|
||||
|
@ -74,11 +83,11 @@ Puppet::Type.type(:database_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, "-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, "-e", "INSERT INTO db (host, user, db) VALUES ('%s', '%s', '%s')" % [
|
||||
name[:host], name[:user], name[:db],
|
||||
]
|
||||
end
|
||||
|
@ -87,7 +96,7 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
end
|
||||
|
||||
def destroy
|
||||
mysql "mysql", "-e", "REVOKE ALL ON '%s'.* FROM '%s@%s'" % [ @resource[:privileges], @resource[:database], @resource[:name], @resource[:host] ]
|
||||
mysql defaults_file, "-e", "REVOKE ALL ON '%s'.* FROM '%s@%s'" % [ @resource[:privileges], @resource[:database], @resource[:name], @resource[:host] ]
|
||||
end
|
||||
|
||||
def row_exists?
|
||||
|
@ -96,7 +105,7 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
if name[:type] == :db
|
||||
fields << :db
|
||||
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, "-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?
|
||||
|
@ -118,9 +127,9 @@ Puppet::Type.type(:database_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, "-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, "-Be", 'select * from db where user="%s" and host="%s" and db="%s"' % [ name[:user], name[:host], name[:db] ]
|
||||
end
|
||||
|
||||
if privs.match(/^$/)
|
||||
|
@ -171,7 +180,7 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
|||
# puts "set:", set
|
||||
stmt = stmt << set << where
|
||||
|
||||
mysql "mysql", "-Be", stmt
|
||||
mysql defaults_file, "-Be", stmt
|
||||
mysql_flush
|
||||
end
|
||||
end
|
||||
|
|
|
@ -7,36 +7,45 @@ Puppet::Type.type(:database_user).provide(:mysql) do
|
|||
optional_commands :mysql => 'mysql'
|
||||
optional_commands :mysqladmin => 'mysqladmin'
|
||||
|
||||
def defaults_file
|
||||
case Facter.value(:operatingsystem)
|
||||
when "Debian", "Ubuntu"
|
||||
return "--defaults-file=/etc/mysql/debian.cnf"
|
||||
else
|
||||
return ""
|
||||
end
|
||||
end
|
||||
|
||||
def self.instances
|
||||
users = mysql("mysql", '-BNe' "select concat(User, '@',Host) as User from mysql.user").split("\n")
|
||||
users = mysql(defaults_file, '-BNe' "select concat(User, '@',Host) as User from mysql.user").split("\n")
|
||||
users.select{ |user| user =~ /.+@/ }.collect do |name|
|
||||
new(:name => name)
|
||||
end
|
||||
end
|
||||
|
||||
def create
|
||||
mysql("mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ])
|
||||
mysql(defaults_file, "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ])
|
||||
end
|
||||
|
||||
def destroy
|
||||
mysql("mysql", "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") )
|
||||
mysql(defaults_file, "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") )
|
||||
end
|
||||
|
||||
def password_hash
|
||||
mysql("mysql", "-NBe", "select password from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).chomp
|
||||
mysql(defaults_file, "-NBe", "select password from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).chomp
|
||||
end
|
||||
|
||||
def password_hash=(string)
|
||||
mysql("mysql", "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] )
|
||||
mysql(defaults_file, "-e", "SET PASSWORD FOR '%s' = '%s'" % [ @resource[:name].sub("@", "'@'"), string ] )
|
||||
end
|
||||
|
||||
def exists?
|
||||
not mysql("mysql", "-NBe", "select '1' from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).empty?
|
||||
not mysql(defaults_file, "-NBe", "select '1' from user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).empty?
|
||||
end
|
||||
|
||||
def flush
|
||||
@property_hash.clear
|
||||
mysqladmin "flush-privileges"
|
||||
mysqladmin(defaults_file, "flush-privileges")
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue