Skip to content

Commit ec5450e

Browse files
committed
Use facts for mysqld version discovery
Per https://tickets.puppetlabs.com/browse/MODULES-3441, the mysql module has behaviour which varies by server version. The version is discovered by running mysqld -V. On hosts without a MySQL server package install, this fails, which means that contrary to the README, it's not actually possible to use this module to manage a remote db. This PR moves the version string discovery into a new fact, mysqld_version which is used by the provider. This makes it possible to configure the db version with a custom fact when a remote db (eg AWS RDS) is being managed.
1 parent 52477c0 commit ec5450e

File tree

5 files changed

+36
-4
lines changed

5 files changed

+36
-4
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ host=localhost
152152
password=secret
153153
```
154154

155+
This module uses the `mysqld_version` fact to discover the server version being used. By default, this is set to the output of `mysqld -V`. If you're working with a remote MySQL server, you may need to set a custom fact for `mysqld_version` to ensure correct behaviour.
156+
155157
When working with a remote server, do *not* use the `mysql::server` class in your Puppet manifests.
156158

157159
### Specify passwords

lib/facter/mysqld_version.rb

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Facter.add("mysqld_version") do
2+
setcode do
3+
Facter::Util::Resolution.exec('mysqld -V')
4+
end
5+
end

lib/puppet/provider/mysql.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def mysqld_type
3333
def self.mysqld_version_string
3434
# we cache the result ...
3535
return @mysqld_version_string unless @mysqld_version_string.nil?
36-
@mysqld_version_string = mysqld(['-V'].compact)
36+
@mysqld_version_string = Facter.value(:mysqld_version)
3737
return @mysqld_version_string
3838
end
3939

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "spec_helper"
2+
3+
describe Facter::Util::Fact do
4+
before {
5+
Facter.clear
6+
}
7+
8+
describe "mysqld_version" do
9+
context 'with value' do
10+
before :each do
11+
Facter::Util::Resolution.stubs(:exec).with('mysqld -V').returns('mysqld Ver 5.5.49-37.9 for Linux on x86_64 (Percona Server (GPL), Release 37.9, Revision efa0073)')
12+
end
13+
it {
14+
expect(Facter.fact(:mysqld_version).value).to eq('mysqld Ver 5.5.49-37.9 for Linux on x86_64 (Percona Server (GPL), Release 37.9, Revision efa0073)')
15+
}
16+
end
17+
18+
end
19+
20+
end

spec/unit/puppet/provider/mysql_user/mysql_spec.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -200,10 +200,15 @@
200200
end
201201

202202
describe 'self.mysqld_version' do
203-
it 'queries mysql if unset' do
203+
it 'uses the mysqld_version fact if unset' do
204204
provider.class.instance_variable_set(:@mysqld_version_string, nil)
205-
provider.class.expects(:mysqld).with(['-V'])
206-
expect(provider.mysqld_version).to be_nil
205+
Facter.stubs(:value).with(:mysqld_version).returns('5.6.24')
206+
expect(provider.mysqld_version).to eq '5.6.24'
207+
end
208+
it 'returns nil if the mysqld_version fact is absent' do
209+
provider.class.instance_variable_set(:@mysqld_version_string, nil)
210+
Facter.stubs(:value).with(:mysqld_version).returns(nil)
211+
expect(provider.mysqld_version).to eq nil
207212
end
208213
it 'returns 5.7.6 for "mysqld Ver 5.7.6 for Linux on x86_64 (MySQL Community Server (GPL))"' do
209214
provider.class.instance_variable_set(:@mysqld_version_string, 'mysqld Ver 5.7.6 for Linux on x86_64 (MySQL Community Server (GPL))')

0 commit comments

Comments
 (0)