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 :mysql => 'mysql'
|
||||||
optional_commands :mysqladmin => 'mysqladmin'
|
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
|
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)
|
new(:name => name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
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
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
mysqladmin('-f', 'drop', @resource[:name])
|
mysqladmin(defaults_file, '-f', 'drop', @resource[:name])
|
||||||
end
|
end
|
||||||
|
|
||||||
def charset
|
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
|
end
|
||||||
|
|
||||||
def charset=(value)
|
def charset=(value)
|
||||||
mysql('-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}")
|
mysql(defaults_file, '-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}")
|
||||||
end
|
end
|
||||||
|
|
||||||
def exists?
|
def exists?
|
||||||
begin
|
begin
|
||||||
mysql('-NBe', "show databases").match(/^#{@resource[:name]}$/)
|
mysql(defaults_file, '-NBe', "show databases").match(/^#{@resource[:name]}$/)
|
||||||
rescue => e
|
rescue => e
|
||||||
debug(e.message)
|
debug(e.message)
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -12,6 +12,15 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
||||||
optional_commands :mysql => 'mysql'
|
optional_commands :mysql => 'mysql'
|
||||||
optional_commands :mysqladmin => 'mysqladmin'
|
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)
|
def self.prefetch(resources)
|
||||||
@user_privs = query_user_privs
|
@user_privs = query_user_privs
|
||||||
@db_privs = query_db_privs
|
@db_privs = query_db_privs
|
||||||
|
@ -34,19 +43,19 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.query_user_privs
|
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] }
|
column_names = results.split(/\n/).map { |l| l.chomp.split(/\t/)[0] }
|
||||||
@user_privs = column_names.delete_if { |e| !( e =~/_priv$/) }
|
@user_privs = column_names.delete_if { |e| !( e =~/_priv$/) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.query_db_privs
|
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] }
|
column_names = results.split(/\n/).map { |l| l.chomp.split(/\t/)[0] }
|
||||||
@db_privs = column_names.delete_if { |e| !(e =~/_priv$/) }
|
@db_privs = column_names.delete_if { |e| !(e =~/_priv$/) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def mysql_flush
|
def mysql_flush
|
||||||
mysqladmin "flush-privileges"
|
mysqladmin defaults_file, "flush-privileges"
|
||||||
end
|
end
|
||||||
|
|
||||||
# this parses the
|
# this parses the
|
||||||
|
@ -74,11 +83,11 @@ Puppet::Type.type(:database_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, "-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, "-e", "INSERT INTO db (host, user, db) VALUES ('%s', '%s', '%s')" % [
|
||||||
name[:host], name[:user], name[:db],
|
name[:host], name[:user], name[:db],
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
@ -87,7 +96,7 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
||||||
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, "-e", "REVOKE ALL ON '%s'.* FROM '%s@%s'" % [ @resource[:privileges], @resource[:database], @resource[:name], @resource[:host] ]
|
||||||
end
|
end
|
||||||
|
|
||||||
def row_exists?
|
def row_exists?
|
||||||
|
@ -96,7 +105,7 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
||||||
if name[:type] == :db
|
if name[:type] == :db
|
||||||
fields << :db
|
fields << :db
|
||||||
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, "-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?
|
||||||
|
@ -118,9 +127,9 @@ Puppet::Type.type(:database_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, "-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, "-Be", 'select * from db where user="%s" and host="%s" and db="%s"' % [ name[:user], name[:host], name[:db] ]
|
||||||
end
|
end
|
||||||
|
|
||||||
if privs.match(/^$/)
|
if privs.match(/^$/)
|
||||||
|
@ -171,7 +180,7 @@ Puppet::Type.type(:database_grant).provide(:mysql) do
|
||||||
# puts "set:", set
|
# puts "set:", set
|
||||||
stmt = stmt << set << where
|
stmt = stmt << set << where
|
||||||
|
|
||||||
mysql "mysql", "-Be", stmt
|
mysql defaults_file, "-Be", stmt
|
||||||
mysql_flush
|
mysql_flush
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,36 +7,45 @@ Puppet::Type.type(:database_user).provide(:mysql) do
|
||||||
optional_commands :mysql => 'mysql'
|
optional_commands :mysql => 'mysql'
|
||||||
optional_commands :mysqladmin => 'mysqladmin'
|
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
|
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|
|
users.select{ |user| user =~ /.+@/ }.collect do |name|
|
||||||
new(:name => name)
|
new(:name => name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
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
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
mysql("mysql", "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") )
|
mysql(defaults_file, "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") )
|
||||||
end
|
end
|
||||||
|
|
||||||
def password_hash
|
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
|
end
|
||||||
|
|
||||||
def password_hash=(string)
|
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
|
end
|
||||||
|
|
||||||
def exists?
|
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
|
end
|
||||||
|
|
||||||
def flush
|
def flush
|
||||||
@property_hash.clear
|
@property_hash.clear
|
||||||
mysqladmin "flush-privileges"
|
mysqladmin(defaults_file, "flush-privileges")
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue