From efa0a0f543fc493891e3fe3f15bff9785b04145f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Fr=C3=B6hner?= Date: Wed, 12 Aug 2020 09:41:05 +0200 Subject: [PATCH] fix cron management --- manifests/config.pp | 21 +++++---------- manifests/hourly.pp | 30 ++++++++++----------- manifests/init.pp | 51 +++++++++++++++++++----------------- spec/classes/hourly_spec.rb | 52 ------------------------------------- spec/classes/init_spec.rb | 30 +++++++++++++++++++++ 5 files changed, 78 insertions(+), 106 deletions(-) delete mode 100644 spec/classes/hourly_spec.rb diff --git a/manifests/config.pp b/manifests/config.pp index f4abad04..46e6d453 100644 --- a/manifests/config.pp +++ b/manifests/config.pp @@ -2,10 +2,6 @@ class logrotate::config { assert_private() - $manage_cron_daily = $logrotate::manage_cron_daily - $logrotate_conf = $logrotate::logrotate_conf - $config = $logrotate::config - file { $logrotate::rules_configdir: ensure => directory, owner => $logrotate::root_user, @@ -15,18 +11,15 @@ mode => $logrotate::rules_configdir_mode, } - $cron_ensure = $manage_cron_daily ? { - true => 'present', - false => 'absent' - } - - logrotate::cron { 'daily': - ensure => $cron_ensure, + if $logrotate::manage_cron_daily { + logrotate::cron { 'daily': + ensure => $logrotate::ensure_cron_daily, + } } - if $config { - logrotate::conf { $logrotate_conf: - * => $config, + if $logrotate::config { + logrotate::conf { $logrotate::logrotate_conf: + * => $logrotate::config, } } } diff --git a/manifests/hourly.pp b/manifests/hourly.pp index 6117ad81..3105efcd 100644 --- a/manifests/hourly.pp +++ b/manifests/hourly.pp @@ -6,35 +6,33 @@ # Examples # # # Set up hourly logrotate jobs -# include logrotate::hourly +# class { 'logrotate': +# manage_cron_hourly => true, +# } # # # Remove hourly logrotate job support -# class { 'logrotate::hourly': -# ensure => absent, +# class { 'logrotate': +# manage_cron_hourly => true, +# ensure_cron_hourly => absent, # } class logrotate::hourly ( - Enum['present','absent'] $ensure = 'present' ) { - $manage_cron_hourly = $logrotate::manage_cron_hourly - - $dir_ensure = $ensure ? { - 'absent' => $ensure, + $dir_ensure = $logrotate::ensure_cron_hourly ? { + 'absent' => $logrotate::ensure_cron_hourly, 'present' => 'directory' } - $cron_ensure = $manage_cron_hourly ? { - true => $ensure, - false => 'absent' - } - file { "${logrotate::rules_configdir}/hourly": ensure => $dir_ensure, owner => 'root', group => 'root', mode => $logrotate::rules_configdir_mode, } - logrotate::cron { 'hourly': - ensure => $cron_ensure, - require => File["${logrotate::rules_configdir}/hourly"], + + if $logrotate::manage_cron_hourly { + logrotate::cron { 'hourly': + ensure => $logrotate::ensure_cron_hourly, + require => File["${logrotate::rules_configdir}/hourly"], + } } } diff --git a/manifests/init.pp b/manifests/init.pp index 2e105c46..f338d914 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,34 +1,37 @@ # class logrotate ( - String $ensure = present, - Boolean $hieramerge = false, - Boolean $manage_cron_daily = true, - Boolean $manage_cron_hourly = true, - Boolean $create_base_rules = true, - Boolean $purge_configdir = false, - String $package = 'logrotate', - Hash $rules = {}, - Optional[Hash] $config = undef, - Integer[0,23] $cron_daily_hour = $logrotate::params::cron_daily_hour, - Integer[0,59] $cron_daily_minute = $logrotate::params::cron_daily_minute, - Integer[0,59] $cron_hourly_minute = $logrotate::params::cron_hourly_minute, - Stdlib::Filemode $cron_file_mode = $logrotate::params::cron_file_mode, - String $configdir = $logrotate::params::configdir, - String $logrotate_bin = $logrotate::params::logrotate_bin, - String $logrotate_conf = $logrotate::params::logrotate_conf, - Stdlib::Filemode $logrotate_conf_mode = $logrotate::params::logrotate_conf_mode, - Boolean $manage_package = $logrotate::params::manage_package, - String $rules_configdir = $logrotate::params::rules_configdir, - Stdlib::Filemode $rules_configdir_mode = $logrotate::params::rules_configdir_mode, - String $root_user = $logrotate::params::root_user, - String $root_group = $logrotate::params::root_group, - Array[String[1]] $logrotate_args = [], - Boolean $cron_always_output = false, + String $ensure = present, + Boolean $hieramerge = false, + Boolean $manage_cron_daily = true, + Boolean $manage_cron_hourly = true, + Enum[present,absent] $ensure_cron_daily = 'present', + Enum[present,absent] $ensure_cron_hourly = 'present', + Boolean $create_base_rules = true, + Boolean $purge_configdir = false, + String $package = 'logrotate', + Hash $rules = {}, + Optional[Hash] $config = undef, + Integer[0,23] $cron_daily_hour = $logrotate::params::cron_daily_hour, + Integer[0,59] $cron_daily_minute = $logrotate::params::cron_daily_minute, + Integer[0,59] $cron_hourly_minute = $logrotate::params::cron_hourly_minute, + Stdlib::Filemode $cron_file_mode = $logrotate::params::cron_file_mode, + String $configdir = $logrotate::params::configdir, + String $logrotate_bin = $logrotate::params::logrotate_bin, + String $logrotate_conf = $logrotate::params::logrotate_conf, + Stdlib::Filemode $logrotate_conf_mode = $logrotate::params::logrotate_conf_mode, + Boolean $manage_package = $logrotate::params::manage_package, + String $rules_configdir = $logrotate::params::rules_configdir, + Stdlib::Filemode $rules_configdir_mode = $logrotate::params::rules_configdir_mode, + String $root_user = $logrotate::params::root_user, + String $root_group = $logrotate::params::root_group, + Array[String[1]] $logrotate_args = [], + Boolean $cron_always_output = false, ) inherits logrotate::params { contain logrotate::install contain logrotate::config contain logrotate::rules contain logrotate::defaults + contain logrotate::hourly Class['::logrotate::install'] -> Class['::logrotate::config'] diff --git a/spec/classes/hourly_spec.rb b/spec/classes/hourly_spec.rb deleted file mode 100644 index 50c78e87..00000000 --- a/spec/classes/hourly_spec.rb +++ /dev/null @@ -1,52 +0,0 @@ -require 'spec_helper' -require 'shared_examples' - -describe 'logrotate::hourly' do - context 'supported operating systems' do - on_supported_os.each do |os, facts| - context "on #{os}" do - let(:facts) do - facts - end - - let(:pre_condition) { 'class { "::logrotate": }' } - - context 'with default values' do - it do - is_expected.to contain_file('/etc/logrotate.d/hourly').with( - 'ensure' => 'directory', - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0755' - ) - end - - it do - is_expected.to contain_file('/etc/cron.hourly/logrotate').with( - 'ensure' => 'present', - 'owner' => 'root', - 'group' => 'root', - 'mode' => '0700' - ) - end - end - - context 'with ensure => absent' do - let(:params) { { ensure: 'absent' } } - - it { is_expected.to contain_file('/etc/logrotate.d/hourly').with_ensure('absent') } - it { is_expected.to contain_file('/etc/cron.hourly/logrotate').with_ensure('absent') } - end - - context 'with ensure => foo' do - let(:params) { { ensure: 'foo' } } - - include_context 'config file' do - let(:config_file) { '/etc/cron.hourly/logrotate' } - end - it_behaves_like 'error match', 'ensure', 'Enum' - end - end - end - end -end diff --git a/spec/classes/init_spec.rb b/spec/classes/init_spec.rb index 9997fe08..44e7fa0d 100644 --- a/spec/classes/init_spec.rb +++ b/spec/classes/init_spec.rb @@ -15,6 +15,24 @@ it { is_expected.to contain_class("logrotate::#{classs}") } end + it do + is_expected.to contain_file('/etc/logrotate.d/hourly').with( + 'ensure' => 'directory', + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0755' + ) + end + + it do + is_expected.to contain_file('/etc/cron.hourly/logrotate').with( + 'ensure' => 'present', + 'owner' => 'root', + 'group' => 'root', + 'mode' => '0700' + ) + end + it do is_expected.to contain_package('logrotate').with_ensure('present') @@ -81,6 +99,18 @@ with_rotate_every('daily') } end + + context 'with ensure => absent' do + let(:params) { + { + manage_cron_hourly: true, + ensure_cron_hourly: 'absent', + } + } + + it { is_expected.to contain_file('/etc/logrotate.d/hourly').with_ensure('absent') } + it { is_expected.to contain_file('/etc/cron.hourly/logrotate').with_ensure('absent') } + end end end end