Skip to content
This repository was archived by the owner on Sep 29, 2020. It is now read-only.

Commit 8c5e8ba

Browse files
committed
Improve version handling to avoid multiple puppet runs for some situations
When an administrator knows the version of collectd that will be used or at least the minimum version available the need for two puppet runs before convergence can be avoided or at least minimised. Instead of using the fact in the templates they now use a class variable set to one of (in priority order): * collectd_real_version (i.e. the fact) * version (the semver matched part of it only) * minimum_version (undef by default) Existing behaviour is preserved except for a corner case where version is set to something specific and collectd is not yet installed. In this case puppet will only take one run and assume the version specified when creating the templates references voxpupuli#162
1 parent 07378b4 commit 8c5e8ba

File tree

7 files changed

+108
-9
lines changed

7 files changed

+108
-9
lines changed

README.md

+14-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ declaration:
2727

2828
```puppet
2929
class { '::collectd':
30-
purge => true,
31-
recurse => true,
32-
purge_config => true,
30+
purge => true,
31+
recurse => true,
32+
purge_config => true,
33+
minimum_version => '5.4',
3334
}
3435
```
3536

@@ -38,6 +39,10 @@ the default configurations shipped in collectd.conf and use
3839
custom configurations stored in conf.d. From here you can set up
3940
additional plugins as shown below.
4041

42+
Specifying the version or minimum_version of collectd as shown above reduces the need for
43+
two puppet runs to coverge. See [Puppet needs two runs to correctly write my conf, why?](#puppet-needs-two-runs-to-correctly-write-my-conf,-why?) below.
44+
45+
4146
Simple Plugins
4247
--------------
4348

@@ -1168,7 +1173,12 @@ See metadata.json for supported platforms
11681173

11691174
##Known issues
11701175

1171-
Some plugins will need two runs of Puppet to fully generate the configuration for collectd. See [this issue](https://github.com/puppet-community/puppet-collectd/issues/162).
1176+
###Puppet needs two runs to correctly write my conf, why?
1177+
1178+
Some plugins will need two runs of Puppet to fully generate the configuration for collectd. See [this issue](https://github.com/pdxcat/puppet-module-collectd/issues/162).
1179+
This can be avoided by specifying an explicit version (`$version`) or a minimum version (`$minimum_version`) for the collectd class. e.g. Setting either of these to 1.2.3 will
1180+
make this module assume on the first run (when the fact responsible to provide the collectd version is not yet available) that your systems are running collectd 1.2.3
1181+
and generate the configuration accordingly.
11721182

11731183
##Development
11741184

lib/facter/collectd_version.rb lib/facter/collectd_real_version.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Fact: collectd_version
1+
# Fact: collectd_real_version
22
#
33
# Purpose: Retrieve collectd version if installed
44
#
55
# Resolution:
66
#
77
# Caveats: not well tested
88
#
9-
Facter.add(:collectd_version) do
9+
Facter.add(:collectd_real_version) do
1010
setcode do
1111
if Facter::Util::Resolution.which('collectd')
1212
collectd_help = Facter::Util::Resolution.exec('collectd -h') and collectd_help =~ /^collectd ([\w.]+), http:\/\/collectd.org\//

manifests/init.pp

+10
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,22 @@
1515
$write_queue_limit_low = undef,
1616
$package_name = $collectd::params::package,
1717
$version = installed,
18+
$minimum_version = undef,
1819
) inherits collectd::params {
1920

2021
$plugin_conf_dir = $collectd::params::plugin_conf_dir
2122
validate_bool($purge_config, $fqdnlookup)
2223
validate_array($include, $typesdb)
2324

25+
# Version for templates
26+
$collectd_version = pick(
27+
$::collectd_real_version, # Fact takes precedence
28+
regsubst(
29+
regsubst($version,'^(absent|held|installed|latest|present|purged)$', ''), # standard package resource ensure value? - strip and return undef
30+
'^\d+(?:\.\d+){1.2}', '\0'), # specific package version? return only semantic version parts
31+
$minimum_version,
32+
'1.0')
33+
2434
package { $package_name:
2535
ensure => $version,
2636
name => $package_name,
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require 'spec_helper'
2+
3+
describe 'test::collectd_version' do
4+
5+
let :facts do
6+
{:osfamily => 'RedHat'}
7+
end
8+
9+
it { should compile }
10+
11+
context 'when no explicit value is specified' do
12+
it { should contain_file('collectd_version.tmp').with_content(/^1\.0$/) }
13+
end
14+
15+
context 'when minimum_version is specified' do
16+
let :params do
17+
{
18+
:version => 'installed',
19+
:minimum_version => '5.4',
20+
}
21+
end
22+
it { should contain_file('collectd_version.tmp').with_content(/^5\.4$/) }
23+
end
24+
25+
context 'when version is explicit and greater than minimum_version' do
26+
let :params do
27+
{
28+
:version => '5.6.3',
29+
:minimum_version => '5.4',
30+
}
31+
end
32+
it { should contain_file('collectd_version.tmp').with_content(/^5\.6\.3$/) }
33+
end
34+
35+
context 'when version is explicit and less than minimum_version' do
36+
let :params do
37+
{
38+
:version => '5.3',
39+
:minimum_version => '5.4',
40+
}
41+
end
42+
it { should contain_file('collectd_version.tmp').with_content(/^5\.3$/) }
43+
end
44+
45+
context 'when collectd_real_version is available' do
46+
let :facts do
47+
{
48+
:osfamily => 'Redhat',
49+
:collectd_real_version => '5.6',
50+
}
51+
end
52+
let :params do
53+
{
54+
:minimum_version => '5.4'
55+
}
56+
end
57+
it { should contain_file('collectd_version.tmp').with_content(/^5\.6$/) }
58+
end
59+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# class used solely to test the collectd_version expansion in init.pp
2+
# Note that fact collectd_real_version is also used by init.pp
3+
# Write the generated value to a template so we can test it
4+
class test::collectd_version(
5+
$version = undef,
6+
$minimum_version = undef,
7+
) {
8+
class { '::collectd':
9+
version => $version,
10+
minimum_version => $minimum_version,
11+
}
12+
13+
file { 'collectd_version.tmp':
14+
ensure => file,
15+
path => '/tmp/collectd_version.tmp',
16+
content => template('test/collectd_version.tmp.erb'),
17+
require => Class['Collectd'],
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<%= scope.lookupvar('::collectd::collectd_version') %>

spec/unit/collectd_version_spec.rb spec/unit/collectd_real_version_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
require 'spec_helper'
22

3-
describe 'collectd_version', :type => :fact do
3+
describe 'collectd_real_version', :type => :fact do
44
before { Facter.clear }
55
after { Facter.clear }
66

77
it 'should be 5.1.0 according to output' do
88
Facter::Util::Resolution.stubs(:which).with("collectd").returns("/usr/sbin/collectd")
99
sample_collectd_help = File.read(fixtures('facts','collectd_help'))
1010
Facter::Util::Resolution.stubs(:exec).with("collectd -h").returns(sample_collectd_help)
11-
expect(Facter.fact(:collectd_version).value).to eq('5.1.0')
11+
expect(Facter.fact(:collectd_real_version).value).to eq('5.1.0')
1212

1313
end
1414

1515
it 'should be 5.1.0.git according to output' do
1616
Facter::Util::Resolution.stubs(:which).with("collectd").returns("/usr/sbin/collectd")
1717
sample_collectd_help_git = File.read(fixtures('facts','collectd_help_git'))
1818
Facter::Util::Resolution.stubs(:exec).with("collectd -h").returns(sample_collectd_help_git)
19-
expect(Facter.fact(:collectd_version).value).to eq('5.1.0.git')
19+
expect(Facter.fact(:collectd_real_version).value).to eq('5.1.0.git')
2020
end
2121

2222

0 commit comments

Comments
 (0)