Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make logrotate.conf into a template with defined type #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions README.md
Expand Up @@ -13,6 +13,13 @@ exceptions:
* Instead of 'daily', 'weekly', 'monthly' or 'yearly', there is a
`rotate_every` parameter (see documentation below).

## logrotate::conf

You may, optionally, define logrotate defaults using this defined type.
Parameters are the same as those for logrotate::rule.
Using this type will automatically include a private class that will install
and configure logrotate for you.

## logrotate::rule

The only thing you'll need to deal with, this type configures a logrotate rule.
Expand All @@ -32,7 +39,7 @@ compressext - The extention String to be appended to the rotated log files
after they have been compressed (optional).
compressoptions - A String of command line options to be passed to the
compression program specified in `compresscmd` (optional).
copy - A Boolean specifying whether logrotate should just take a
copy - A Boolean specifying whether logrotate should just take a
copy of the log file and not touch the original (optional).
copytruncate - A Boolean specifying whether logrotate should truncate the
original log file after taking a copy (optional).
Expand All @@ -56,7 +63,7 @@ extension - Log files with this extension String are allowed to keep it
after rotation (optional).
ifempty - A Boolean specifying whether the log file should be rotated
even if it is empty (optional).
mail - The email address String that logs that are about to be
mail - The email address String that logs that are about to be
rotated out of existence are emailed to (optional).
mailfirst - A Boolean that when used with `mail` has logrotate email the
just rotated file rather than the about to expire file
Expand All @@ -82,7 +89,7 @@ prerotate - A command String that should be executed by /bin/sh before
firstaction - A command String that should be executed by /bin/sh once
before all log files that match the wildcard pattern are
rotated (optional).
lastaction - A command String that should be execute by /bin/sh once
lastaction - A command String that should be execute by /bin/sh once
after all the log files that match the wildcard pattern are
rotated (optional).
rotate - The Integer number of rotated log files to keep on disk
Expand All @@ -94,7 +101,7 @@ rotate_every - How often the log files should be rotated as a String.
size - The String size a log file has to reach before it will be
rotated (optional). The default units are bytes, append k,
M or G for kilobytes, megabytes or gigabytes respectively.
sharedscripts - A Boolean specifying whether logrotate should run the
sharedscripts - A Boolean specifying whether logrotate should run the
postrotate and prerotate scripts for each matching file or
just once (optional).
shred - A Boolean specifying whether logs should be deleted with
Expand All @@ -112,6 +119,13 @@ Further details about these options can be found by reading `man 8 logrotate`.
### Examples

```
logrotate::conf { '/etc/logrotate.conf':
rotate => 10,
rotate_every => 'week',
ifempty => true,
dateext => true,
}

logrotate::rule { 'messages':
path => '/var/log/messages',
rotate => 5,
Expand Down
15 changes: 0 additions & 15 deletions files/etc/logrotate.conf

This file was deleted.

23 changes: 17 additions & 6 deletions manifests/base.pp
@@ -1,11 +1,22 @@
# Internal: Install logrotate and configure it to read from /etc/logrotate.d
#
# see logrotate::rule for description of options.
#
# Examples
#
# include logrotate::base
class logrotate::base {
class logrotate::base (
$ensure = 'latest'
) {

case $ensure {
'latest': { $_ensure = 'latest' }
false,'absent': { $_ensure = 'absent' }
default: { $_ensure = 'presest' }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo - should be "present"

}

package { 'logrotate':
ensure => latest,
ensure => $_ensure,
}

File {
Expand All @@ -14,11 +25,11 @@
require => Package['logrotate'],
}

if !defined( Logrotate::Conf['/etc/logrotate.conf'] ) {
logrotate::conf {'/etc/logrotate.conf': }
}

file {
'/etc/logrotate.conf':
ensure => file,
mode => '0444',
source => 'puppet:///modules/logrotate/etc/logrotate.conf';
'/etc/logrotate.d':
ensure => directory,
mode => '0755';
Expand Down
278 changes: 278 additions & 0 deletions manifests/conf.pp
@@ -0,0 +1,278 @@
# Internal: Install and configure logrotate defaults file, usually
# /etc/logrotate.conf
#
# see logrotate::rule for description of options.
#
# Examples
#
# logrotate::conf{'/etc/logrotate.conf':}
#
define logrotate::conf (
$ensure = 'present',
$compress = 'undef',
$compresscmd = 'undef',
$compressext = 'undef',
$compressoptions = 'undef',
$copy = 'undef',
$copytruncate = 'undef',
$create = true,
$create_mode = 'undef',
$create_owner = 'undef',
$create_group = 'undef',
$dateext = 'undef',
$dateformat = 'undef',
$delaycompress = 'undef',
$extension = 'undef',
$ifempty = 'undef',
$mail = 'undef',
$mailfirst = 'undef',
$maillast = 'undef',
$maxage = 'undef',
$minsize = 'undef',
$missingok = 'undef',
$olddir = 'undef',
$postrotate = 'undef',
$prerotate = 'undef',
$firstaction = 'undef',
$lastaction = 'undef',
$rotate = '4',
$rotate_every = 'weekly',
$size = 'undef',
$sharedscripts = 'undef',
$shred = 'undef',
$shredcycles = 'undef',
$start = 'undef',
$uncompresscmd = 'undef'
) {

#############################################################################
# SANITY CHECK VALUES

if $name !~ /^[a-zA-Z0-9\._\/-]+$/ {
fail("Logrotate::Rule[${name}]: namevar must be alphanumeric")
}

case $ensure {
'present','file': {}
'absent': {}
default: {
fail("Logrotate::Rule[${name}]: invalid ensure value")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All instances of Logrotate::Rule in the fail messages in this class should be Logrotate::Conf

}
}

case $compress {
'undef': {}
true: { $_compress = 'compress' }
false: { $_compress = 'nocompress' }
default: {
fail("Logrotate::Rule[${name}]: compress must be a boolean")
}
}

case $copy {
'undef': {}
true: { $_copy = 'copy' }
false: { $_copy = 'nocopy' }
default: {
fail("Logrotate::Rule[${name}]: copy must be a boolean")
}
}

case $copytruncate {
'undef': {}
true: { $_copytruncate = 'copytruncate' }
false: { $_copytruncate = 'nocopytruncate' }
default: {
fail("Logrotate::Rule[${name}]: copytruncate must be a boolean")
}
}

case $create {
'undef': {}
true: { $_create = 'create' }
false: { $_create = 'nocreate' }
default: {
fail("Logrotate::Rule[${name}]: create must be a boolean")
}
}

case $delaycompress {
'undef': {}
true: { $_delaycompress = 'delaycompress' }
false: { $_delaycompress = 'nodelaycompress' }
default: {
fail("Logrotate::Rule[${name}]: delaycompress must be a boolean")
}
}

case $dateext {
'undef': {}
true: { $_dateext = 'dateext' }
false: { $_dateext = 'nodateext' }
default: {
fail("Logrotate::Rule[${name}]: dateext must be a boolean")
}
}

case $mail {
'undef': {}
false: { $_mail = 'nomail' }
default: {
$_mail = "mail ${mail}"
}
}

case $missingok {
'undef': {}
true: { $_missingok = 'missingok' }
false: { $_missingok = 'nomissingok' }
default: {
fail("Logrotate::Rule[${name}]: missingok must be a boolean")
}
}

case $olddir {
'undef': {}
false: { $_olddir = 'noolddir' }
default: {
$_olddir = "olddir ${olddir}"
}
}

case $sharedscripts {
'undef': {}
true: { $_sharedscripts = 'sharedscripts' }
false: { $_sharedscripts = 'nosharedscripts' }
default: {
fail("Logrotate::Rule[${name}]: sharedscripts must be a boolean")
}
}

case $shred {
'undef': {}
true: { $_shred = 'shred' }
false: { $_shred = 'noshred' }
default: {
fail("Logrotate::Rule[${name}]: shred must be a boolean")
}
}

case $ifempty {
'undef': {}
true: { $_ifempty = 'ifempty' }
false: { $_ifempty = 'notifempty' }
default: {
fail("Logrotate::Rule[${name}]: ifempty must be a boolean")
}
}

case $rotate_every {
'undef': {}
'day': { $_rotate_every = 'daily' }
'week': { $_rotate_every = 'weekly' }
'month': { $_rotate_every = 'monthly' }
'year': { $_rotate_every = 'yearly' }
'daily', 'weekly','monthly','yearly': { $_rotate_every = $rotate_every }
default: {
fail("Logrotate::Rule[${name}]: invalid rotate_every value")
}
}

case $maxage {
'undef': {}
/^\d+$/: {}
default: {
fail("Logrotate::Rule[${name}]: maxage must be an integer")
}
}

case $minsize {
'undef': {}
/^\d+[kMG]?$/: {}
default: {
fail("Logrotate::Rule[${name}]: minsize must match /\\d+[kMG]?/")
}
}

case $rotate {
'undef': {}
/^\d+$/: {}
default: {
fail("Logrotate::Rule[${name}]: rotate must be an integer")
}
}

case $size {
'undef': {}
/^\d+[kMG]?$/: {}
default: {
fail("Logrotate::Rule[${name}]: size must match /\\d+[kMG]?/")
}
}

case $shredcycles {
'undef': {}
/^\d+$/: {}
default: {
fail("Logrotate::Rule[${name}]: shredcycles must be an integer")
}
}

case $start {
'undef': {}
/^\d+$/: {}
default: {
fail("Logrotate::Rule[${name}]: start must be an integer")
}
}

case $mailfirst {
'undef',false: {}
true: {
if $maillast == true {
fail("Logrotate::Rule[${name}]: Can't set both mailfirst and maillast")
}

$_mailfirst = 'mailfirst'
}
default: {
fail("Logrotate::Rule[${name}]: mailfirst must be a boolean")
}
}

case $maillast {
'undef',false: {}
true: {
$_maillast = 'maillast'
}
default: {
fail("Logrotate::Rule[${name}]: maillast must be a boolean")
}
}

if ($create_group != 'undef') and ($create_owner == 'undef') {
fail("Logrotate::Rule[${name}]: create_group requires create_owner")
}

if ($create_owner != 'undef') and ($create_mode == 'undef') {
fail("Logrotate::Rule[${name}]: create_owner requires create_mode")
}

if ($create_mode != 'undef') and ($create != true) {
fail("Logrotate::Rule[${name}]: create_mode requires create")
}

#
####################################################################

include logrotate::base

file { $name:
ensure => $ensure,
owner => 'root',
group => 'root',
mode => '0444',
content => template('logrotate/etc/logrotate.conf.erb'),
require => Package['logrotate'],
}
}