From a51ca207337096785ce49898d2afbea29966037a Mon Sep 17 00:00:00 2001 From: Joshua Hoblitt Date: Mon, 14 Apr 2014 12:19:36 -0700 Subject: [PATCH] add $default param to smartd class --- README.md | 46 +++++++++++++++ manifests/init.pp | 2 + manifests/params.pp | 7 +++ spec/classes/smartd_spec.rb | 113 ++++++++++++++++++++++++++++++++---- templates/smartd.conf | 6 ++ 5 files changed, 162 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 71018a4..ee69bc5 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,51 @@ Smart daemon notifcation email address. Smart daemon problem mail notification frequency. Valid values are: `daily`,`once`,`diminishing` +#### `default` + +`Bool` defaults to: `true` if `$::smartmontools_version >= 5.43`, otherwise `false` + +Enables/disables the `DEFAULT` directive in the `smartd.conf` file. This +directive was added in the 5.43 release of smartmontools and is unsupported in +previous versions. + +If `default` is set to `false` the the values from the [`mail_to`](#mail-to) and [`warning_schedule`](#warning-schedule) parameters are set on the `DEVICESCAN` directive (if enabled) instead of the [absent] `DEFAULT` directive. + +Example `smartd.conf` content based on this setting: + +`default => true` +``` +# Managed by Puppet -- do not edit! +DEFAULT -m root -M daily +DEVICESCAN +``` + +`default => false` +``` +# Managed by Puppet -- do not edit! +DEVICESCAN -m root -M daily +``` + +Here is an example of the error message generated by the `DEFFAULT` directive +apearing in the configuration file of 5.42. + +``` +smartd 5.42 2011-10-20 r3458 [i686-linux-2.6.18-371.6.1.el5PAE] (local build) +Copyright (C) 2002-11 by Bruce Allen, http://smartmontools.sourceforge.net +Opened configuration file /etc/smartd.conf +Drive: DEFAULT, implied '-a' Directive on line 2 of file /etc/smartd.conf +Drive: DEVICESCAN, implied '-a' Directive on line 3 of file /etc/smartd.conf +Configuration file /etc/smartd.conf was parsed, found DEVICESCAN, scanning devices +Device: DEFAULT, unable to autodetect device type +``` + +This option would be better named `enable_default` but the `enable_` prefix was +no used so as to appear more consistent with the rest of the API. The API may +be refactored in a future major release to be more consistent with current API +naming best practices. + +Note that RHEL5 ships with 5.42 while RHEL6 ships with 5.43. + ### Pedantic Example ```puppet @@ -199,6 +244,7 @@ Smart daemon problem mail notification frequency. Valid values are: ], mail_to => 'root', warning_schedule => 'diminishing', + default => 'false', } ``` diff --git a/manifests/init.pp b/manifests/init.pp index 0071f20..71c6cf6 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -107,6 +107,7 @@ $devices = $smartd::params::devices, $mail_to = $smartd::params::mail_to, $warning_schedule = $smartd::params::warning_schedule, + $default = $smartd::params::default, ) inherits smartd::params { validate_re($ensure, '^present$|^latest$|^absent$|^purged$') validate_string($package_name) @@ -119,6 +120,7 @@ validate_string($mail_to) validate_re($warning_schedule, '^daily$|^once$|^diminishing$', '$warning_schedule must be either daily, once, or diminishing.') + validate_bool($default) case $ensure { 'present', 'latest': { diff --git a/manifests/params.pp b/manifests/params.pp index 987a969..a4c2726 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -22,6 +22,13 @@ $mail_to = 'root' $warning_schedule = 'daily' # other choices: once, diminishing + # smartd.conf < 5.43 does not support the 'DEFAULT' directive + if versioncmp($::smartmontools_version, 5.43) >= 0 { + $default = true + } else { + $default = false + } + case $::osfamily { 'FreeBSD': { $config_file = '/usr/local/etc/smartd.conf' diff --git a/spec/classes/smartd_spec.rb b/spec/classes/smartd_spec.rb index 9297c81..f4433f8 100644 --- a/spec/classes/smartd_spec.rb +++ b/spec/classes/smartd_spec.rb @@ -45,21 +45,21 @@ describe 'on a supported osfamily, default parameters' do describe 'for osfamily RedHat' do - let(:facts) {{ :osfamily => 'RedHat' }} + let(:facts) {{ :osfamily => 'RedHat', :smartmontools_version => '5.43' }} it_behaves_like 'default', {} it { should_not contain_shell_config('start_smartd') } end describe 'for osfamily Debian' do - let(:facts) {{ :osfamily => 'Debian' }} + let(:facts) {{ :osfamily => 'Debian', :smartmontools_version => '5.43' }} it_behaves_like 'default', {} it { should contain_shell_config('start_smartd') } end describe 'for osfamily FreeBSD' do - let(:facts) {{ :osfamily => 'FreeBSD' }} + let(:facts) {{ :osfamily => 'FreeBSD', :smartmontools_version => '5.43' }} it_behaves_like 'default', { :config_file => '/usr/local/etc/smartd.conf' } it { should_not contain_shell_config('start_smartd') } @@ -68,7 +68,7 @@ end describe 'on a supported osfamily, custom parameters' do - let(:facts) {{ :osfamily => 'RedHat' }} + let(:facts) {{ :osfamily => 'RedHat', :smartmontools_version => '5.43' }} describe 'ensure => present' do let(:params) {{ :ensure => 'present' }} @@ -136,12 +136,54 @@ end end - describe 'devicescan => false' do - let(:params) {{ :devicescan => false }} + describe 'devicescan =>' do + context '(default)' do + it do + should contain_file('/etc/smartd.conf'). + with_ensure('present'). + with_content(/^DEVICESCAN$/) + end + end - it { should contain_file('/etc/smartd.conf').with_ensure('present') } - it { should_not contain_file('/etc/smartd.conf').with_content(/^DEVICESCAN$/) } - end + context 'true' do + let(:params) {{ :devicescan => true }} + + it do + should contain_file('/etc/smartd.conf'). + with_ensure('present'). + with_content(/^DEVICESCAN$/) + end + + context 'default => false' do + before { params[:default] = false } + + it 'should have the same arguments as DEFAULT would have' do + should contain_file('/etc/smartd.conf'). + with_ensure('present'). + with_content(/^DEVICESCAN -m root -M daily$/) + end + end + end + + context 'false' do + let(:params) {{ :devicescan => false }} + + it do + should contain_file('/etc/smartd.conf'). + with_ensure('present'). + without_content(/^DEVICESCAN$/) + end + end + + context 'foo' do + let(:params) {{ :devicescan => 'foo' }} + it 'should fail' do + expect { + should raise_error(Puppet::Error, /is not a boolean../) + } + end + end + end # devicescan => describe 'devicescan_options => somevalue' do let(:params) {{ :devicescan_options => 'somevalue' }} @@ -256,6 +298,51 @@ end end + describe 'default => ' do + context '(default)' do + context 'fact smartmontool_version = "5.43"' do + before { facts[:smartmontools_version] = '5.43' } + it do + should contain_file('/etc/smartd.conf').with_ensure('present'). + with_content(/DEFAULT -m root -M daily/) + end + end + + context 'fact smartmontool_version = "5.42"' do + before { facts[:smartmontools_version] = '5.42' } + it do + should contain_file('/etc/smartd.conf').with_ensure('present'). + without_content(/DEFAULT -m root -M daily/) + end + end + end # (default) + + context 'true' do + let(:params) {{ :default => true }} + it do + should contain_file('/etc/smartd.conf').with_ensure('present'). + with_content(/DEFAULT -m root -M daily/) + end + end + + context 'false' do + let(:params) {{ :default => false }} + it do + should contain_file('/etc/smartd.conf').with_ensure('present'). + without_content(/DEFAULT -m root -M daily/) + end + end + + context 'foo' do + let(:params) {{ :default => 'foo' }} + it 'should fail' do + expect { + should raise_error(Puppet::Error, /is not a boolean../) + } + end + end + end # default => + end @@ -264,10 +351,11 @@ describe 'without params + megaraid facts' do let(:facts) do { - :osfamily=> 'RedHat', + :osfamily => 'RedHat', :megaraid_adapters => '1', :megaraid_virtual_drives => 'sdb,sda', :megaraid_physical_drives => '2,1', + :smartmontools_version => '5.43', } end @@ -283,13 +371,14 @@ end end - describe 'without params + megaraid facts' do + describe 'with params + megaraid facts' do let(:facts) do { - :osfamily=> 'RedHat', + :osfamily => 'RedHat', :megaraid_adapters => '1', :megaraid_virtual_drives => 'sdb,sda', :megaraid_physical_drives => '2,1', + :smartmontools_version => '5.43', } end let(:params) do diff --git a/templates/smartd.conf b/templates/smartd.conf index cde3780..7abf48e 100644 --- a/templates/smartd.conf +++ b/templates/smartd.conf @@ -1,5 +1,7 @@ # Managed by Puppet -- do not edit! +<% if @default -%> DEFAULT -m <%= @mail_to %> -M <%= @warning_schedule %> +<% end -%> <% @devices.each do |dev| -%> <% next if dev['device'] == 'megaraid' -%> <%= dev['device'] %><% if dev.has_key?('options') -%><%= ' ' + dev['options'] %><% end %> @@ -24,5 +26,9 @@ if megaraid_device and megaraid_device != '' and <% end -%> <% end -%> <% if @devicescan -%> +<% unless @default -%> +DEVICESCAN -m <%= @mail_to %> -M <%= @warning_schedule %><% if @devicescan_options %><%= ' ' + @devicescan_options %><% end %> +<% else -%> DEVICESCAN<% if @devicescan_options %><%= ' ' + @devicescan_options %><% end %> <% end -%> +<% end -%>