Skip to content

Commit

Permalink
fix cron management
Browse files Browse the repository at this point in the history
  • Loading branch information
crazymind1337 committed Aug 13, 2020
1 parent 10fdb37 commit efa0a0f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 106 deletions.
21 changes: 7 additions & 14 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
}
}
}
30 changes: 14 additions & 16 deletions manifests/hourly.pp
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
}
}
}
51 changes: 27 additions & 24 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -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']
Expand Down
52 changes: 0 additions & 52 deletions spec/classes/hourly_spec.rb

This file was deleted.

30 changes: 30 additions & 0 deletions spec/classes/init_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit efa0a0f

Please sign in to comment.