Skip to content

Loading…

Patch providers for absent my.cnf #141

Merged
merged 3 commits into from

2 participants

@hunner
Puppet Labs member

If the /root/.my.cnf file does not exist but is specified by the
--defaults-file argument, the mysql calls will fail. The
mysql::config class creates this file, but if the custom resources are
used without including our classes then it will still break.

This allows users to use our custom resources without having to use our
classes.

@hunner hunner closed this
hunner added some commits
@hunner hunner Patch providers for absent my.cnf
If the /root/.my.cnf file does not exist but is specified by the
`--defaults-file` argument, the mysql calls will fail. The
`mysql::config` class creates this file, but if the custom resources are
used without including our classes then it will still break.

This allows users to use our custom resources without having to use our
classes.
ca2b16c
@hunner hunner Updating specs for .my.cnf cea7780
@hunner hunner reopened this
@glarizza glarizza commented on the diff
spec/unit/puppet/provider/database_grant/mysql_spec.rb
@@ -14,10 +14,11 @@
)
@provider = provider_class.new(@resource)
Facter.stubs(:value).with(:root_home).returns(root_home)
+ File.stubs(:file?).with("#{root_home}/.my.cnf").returns(true)
@glarizza Puppet Labs member

There aren't any tests checking for the CONVERSE behavior (where --defaults-file wasn't being called). We should at least test that behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@glarizza
Puppet Labs member

Looks good with the new tests, I'll merge this in.

@glarizza glarizza merged commit c302efd into puppetlabs:master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 10, 2013
  1. @hunner

    Patch providers for absent my.cnf

    hunner committed
    If the /root/.my.cnf file does not exist but is specified by the
    `--defaults-file` argument, the mysql calls will fail. The
    `mysql::config` class creates this file, but if the custom resources are
    used without including our classes then it will still break.
    
    This allows users to use our custom resources without having to use our
    classes.
  2. @hunner

    Updating specs for .my.cnf

    hunner committed
  3. @hunner
View
26 lib/puppet/provider/database/mysql.rb
@@ -1,5 +1,4 @@
Puppet::Type.type(:database).provide(:mysql) do
-
desc "Manages MySQL database."
defaultfor :kernel => 'Linux'
@@ -7,31 +6,43 @@
optional_commands :mysql => 'mysql'
optional_commands :mysqladmin => 'mysqladmin'
+ 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 self.instances
- mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-NBe', "show databases").split("\n").collect do |name|
+ mysql([defaults_file, '-NBe', "show databases"].compact).split("\n").collect do |name|
new(:name => name)
end
end
def create
- mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-NBe', "create database `#{@resource[:name]}` character set #{resource[:charset]}")
+ mysql([defaults_file, '-NBe', "create database `#{@resource[:name]}` character set #{resource[:charset]}"].compact)
end
def destroy
- mysqladmin("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-f', 'drop', @resource[:name])
+ mysqladmin([defaults_file, '-f', 'drop', @resource[:name]].compact)
end
def charset
- mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-NBe', "show create database `#{resource[:name]}`").match(/.*?(\S+)\s(?:COLLATE.*)?\*\//)[1]
+ mysql([defaults_file, '-NBe', "show create database `#{resource[:name]}`"].compact).match(/.*?(\S+)\s(?:COLLATE.*)?\*\//)[1]
end
def charset=(value)
- mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}")
+ mysql([defaults_file, '-NBe', "alter database `#{resource[:name]}` CHARACTER SET #{value}"].compact)
end
def exists?
begin
- mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", '-NBe', "show databases").match(/^#{@resource[:name]}$/)
+ mysql([defaults_file, '-NBe', "show databases"].compact).match(/^#{@resource[:name]}$/)
rescue => e
debug(e.message)
return nil
@@ -39,4 +50,3 @@ def exists?
end
end
-
View
36 lib/puppet/provider/database_grant/mysql.rb
@@ -34,19 +34,19 @@ def db_privs
end
def self.query_user_privs
- results = mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-Be", "describe user")
+ results = mysql([defaults_file, "mysql", "-Be", "describe user"].compact)
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("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-Be", "describe db")
+ results = mysql([defaults_file, "mysql", "-Be", "describe db"].compact)
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 "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "flush-privileges"
+ mysqladmin([defaults_file, "flush-privileges"].compact)
end
# this parses the
@@ -74,20 +74,20 @@ def create_row
name = split_name(@resource[:name])
case name[:type]
when :user
- mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "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],
- ]
+ ]].compact)
when :db
- mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "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],
- ]
+ ]].compact)
end
mysql_flush
end
end
def destroy
- mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "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] ]].compact)
end
def row_exists?
@@ -96,7 +96,7 @@ def row_exists?
if name[:type] == :db
fields << :db
end
- not mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "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 ')]].compact).empty?
end
def all_privs_set?
@@ -118,9 +118,9 @@ def privileges
case name[:type]
when :user
- privs = mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-Be", 'select * from mysql.user where user="%s" and host="%s"' % [ name[:user], name[:host] ]
+ privs = mysql([defaults_file, "mysql", "-Be", 'select * from mysql.user where user="%s" and host="%s"' % [ name[:user], name[:host] ]].compact)
when :db
- privs = mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-Be", 'select * from mysql.db where user="%s" and host="%s" and db="%s"' % [ name[:user], name[:host], name[:db] ]
+ privs = mysql([defaults_file, "mysql", "-Be", 'select * from mysql.db where user="%s" and host="%s" and db="%s"' % [ name[:user], name[:host], name[:db] ]].compact)
end
if privs.match(/^$/)
@@ -172,7 +172,7 @@ def privileges=(privs)
stmt = stmt << set << where
validate_privs privs, all_privs
- mysql "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-Be", stmt
+ mysql([defaults_file, "mysql", "-Be", stmt].compact)
mysql_flush
end
@@ -195,4 +195,16 @@ def validate_privs(set_privs, all_privs)
end
end
+ # 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
+
end
View
26 lib/puppet/provider/database_user/mysql.rb
@@ -8,35 +8,47 @@
optional_commands :mysqladmin => 'mysqladmin'
def self.instances
- users = mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", '-BNe' "select concat(User, '@',Host) as User from mysql.user").split("\n")
+ users = mysql([defaults_file, "mysql", '-BNe' "select concat(User, '@',Host) as User from mysql.user"].compact).split("\n")
users.select{ |user| user =~ /.+@/ }.collect do |name|
new(:name => name)
end
end
def create
- mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ])
+ mysql([defaults_file, "mysql", "-e", "create user '%s' identified by PASSWORD '%s'" % [ @resource[:name].sub("@", "'@'"), @resource.value(:password_hash) ]].compact)
end
def destroy
- mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") )
+ mysql([defaults_file, "mysql", "-e", "drop user '%s'" % @resource.value(:name).sub("@", "'@'") ].compact)
end
def password_hash
- mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-NBe", "select password from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).chomp
+ mysql([defaults_file, "mysql", "-NBe", "select password from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)].compact).chomp
end
def password_hash=(string)
- mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "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 ] ].compact)
end
def exists?
- not mysql("--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "mysql", "-NBe", "select '1' from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)).empty?
+ not mysql([defaults_file, "mysql", "-NBe", "select '1' from mysql.user where CONCAT(user, '@', host) = '%s'" % @resource.value(:name)].compact).empty?
end
def flush
@property_hash.clear
- mysqladmin "--defaults-file=#{Facter.value(:root_home)}/.my.cnf", "flush-privileges"
+ mysqladmin([defaults_file, "flush-privileges"].compact)
+ end
+
+ # 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
end
View
37 spec/unit/puppet/provider/database_grant/mysql_spec.rb
@@ -14,10 +14,11 @@
)
@provider = provider_class.new(@resource)
Facter.stubs(:value).with(:root_home).returns(root_home)
+ File.stubs(:file?).with("#{root_home}/.my.cnf").returns(true)
@glarizza Puppet Labs member

There aren't any tests checking for the CONVERSE behavior (where --defaults-file wasn't being called). We should at least test that behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
it 'should query privilegess from the database' do
- provider_class.expects(:mysql) .with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'describe user').returns <<-EOT
+ provider_class.expects(:mysql) .with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'describe user']).returns <<-EOT
Field Type Null Key Default Extra
Host char(60) NO PRI
User char(16) NO PRI
@@ -26,7 +27,7 @@
Insert_priv enum('N','Y') NO N
Update_priv enum('N','Y') NO N
EOT
- provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'describe db').returns <<-EOT
+ provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'describe db']).returns <<-EOT
Field Type Null Key Default Extra
Host char(60) NO PRI
Db char(64) NO PRI
@@ -40,7 +41,7 @@
end
it 'should query set priviliges' do
- provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'select * from mysql.user where user="user" and host="host"').returns <<-EOT
+ provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'select * from mysql.user where user="user" and host="host"']).returns <<-EOT
Host User Password Select_priv Insert_priv Update_priv
host user Y N Y
EOT
@@ -48,7 +49,7 @@
end
it 'should recognize when all priviliges are set' do
- provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'select * from mysql.user where user="user" and host="host"').returns <<-EOT
+ provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'select * from mysql.user where user="user" and host="host"']).returns <<-EOT
Host User Password Select_priv Insert_priv Update_priv
host user Y Y Y
EOT
@@ -56,7 +57,7 @@
end
it 'should recognize when all privileges are not set' do
- provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'select * from mysql.user where user="user" and host="host"').returns <<-EOT
+ provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', 'select * from mysql.user where user="user" and host="host"']).returns <<-EOT
Host User Password Select_priv Insert_priv Update_priv
host user Y N Y
EOT
@@ -64,23 +65,31 @@
end
it 'should be able to set all privileges' do
- provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-NBe', 'SELECT "1" FROM user WHERE user="user" AND host="host"').returns "1\n"
- provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'Y', Update_priv = 'Y' where user=\"user\" and host=\"host\"")
- provider_class.expects(:mysqladmin).with("--defaults-file=#{root_home}/.my.cnf", "flush-privileges")
+ provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-NBe', 'SELECT "1" FROM user WHERE user="user" AND host="host"']).returns "1\n"
+ provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'Y', Update_priv = 'Y' where user=\"user\" and host=\"host\""])
+ provider_class.expects(:mysqladmin).with(["--defaults-file=#{root_home}/.my.cnf", "flush-privileges"])
@provider.privileges=(['all'])
end
it 'should be able to set partial privileges' do
- provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-NBe', 'SELECT "1" FROM user WHERE user="user" AND host="host"').returns "1\n"
- provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'N', Update_priv = 'Y' where user=\"user\" and host=\"host\"")
- provider_class.expects(:mysqladmin).with("--defaults-file=#{root_home}/.my.cnf", "flush-privileges")
+ provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-NBe', 'SELECT "1" FROM user WHERE user="user" AND host="host"']).returns "1\n"
+ provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'N', Update_priv = 'Y' where user=\"user\" and host=\"host\""])
+ provider_class.expects(:mysqladmin).with(["--defaults-file=#{root_home}/.my.cnf", "flush-privileges"])
@provider.privileges=(['Select_priv', 'Update_priv'])
end
it 'should be case insensitive' do
- provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-NBe', 'SELECT "1" FROM user WHERE user="user" AND host="host"').returns "1\n"
- provider_class.expects(:mysql).with("--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'Y', Update_priv = 'Y' where user=\"user\" and host=\"host\"")
- provider_class.expects(:mysqladmin).with("--defaults-file=#{root_home}/.my.cnf", 'flush-privileges')
+ provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-NBe', 'SELECT "1" FROM user WHERE user="user" AND host="host"']).returns "1\n"
+ provider_class.expects(:mysql).with(["--defaults-file=#{root_home}/.my.cnf", 'mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'Y', Update_priv = 'Y' where user=\"user\" and host=\"host\""])
+ provider_class.expects(:mysqladmin).with(["--defaults-file=#{root_home}/.my.cnf", 'flush-privileges'])
@provider.privileges=(['SELECT_PRIV', 'insert_priv', 'UpDaTe_pRiV'])
end
+
+ it 'should not pass --defaults-file if $root_home/.my.cnf is absent' do
+ File.stubs(:file?).with("#{root_home}/.my.cnf").returns(false)
+ provider_class.expects(:mysql).with(['mysql', '-NBe', 'SELECT "1" FROM user WHERE user="user" AND host="host"']).returns "1\n"
+ provider_class.expects(:mysql).with(['mysql', '-Be', "update user set Select_priv = 'Y', Insert_priv = 'N', Update_priv = 'Y' where user=\"user\" and host=\"host\""])
+ provider_class.expects(:mysqladmin).with(["flush-privileges"])
+ @provider.privileges=(['Select_priv', 'Update_priv'])
+ end
end
Something went wrong with that request. Please try again.