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

Add new params and full hiera support #38

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
9 changes: 8 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ notifications:
- [email protected]
env:
- PUPPET_VERSION=2.6.18
- PUPPET_VERSION=2.7.24
- PUPPET_VERSION=3.0.2
- PUPPET_VERSION=3.1.1
- PUPPET_VERSION=3.2.4
- PUPPET_VERSION=3.3.2
- PUPPET_VERSION=3.4.1
- PUPPET_VERSION=3.5.1
- PUPPET_VERSION=3.6.1

matrix:
include:
- env: PUPPET_VERSION=2.7.24
rvm: 1.8.7
gemfile: Gemfile-puppet27
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ puppetversion = ENV.key?('PUPPET_VERSION') ? "= #{ENV['PUPPET_VERSION']}" : ['>=

gem 'puppet-lint'
gem 'rspec-puppet'
gem 'hiera'
gem 'puppet', puppetversion
9 changes: 9 additions & 0 deletions Gemfile-puppet27
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
source 'https://rubygems.org'

puppetversion = ENV.key?('PUPPET_VERSION') ? "= #{ENV['PUPPET_VERSION']}" : ['>= 3.2']

gem 'puppet-lint'
gem 'rspec-puppet'
gem 'hiera'
gem 'hiera-puppet'
gem 'puppet', puppetversion
137 changes: 133 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ exceptions:
* Instead of 'daily', 'weekly', 'monthly' or 'yearly', there is a
`rotate_every` parameter (see documentation below).

## logrotate::rule
## Usage

Two different methods are supported for managing the logrotate rules.

### Using Code: `logrotate::rule` Define or `logrotate::rules` Parameter

The only thing you'll need to deal with, this type configures a logrotate rule.
Using this type will automatically include a private class that will install
Using this type will automatically include the logrotate class that will install
and configure logrotate for you.

```
Expand Down Expand Up @@ -118,9 +122,9 @@ uncompresscmd - The String command to be used to uncompress log files

Further details about these options can be found by reading `man 8 logrotate`.

### Examples
#### Example: Using `logrotate::rule` Define

```
```puppet
logrotate::rule { 'messages':
path => '/var/log/messages',
rotate => 5,
Expand All @@ -137,3 +141,128 @@ logrotate::rule { 'apache':
postrotate => '/etc/init.d/httpd restart',
}
```

#### Example: Using `logrotate::rules` Parameter

```puppet
class {
logrotate: rules => {
messages => {
path => '/var/log/messages',
rotate => 5,
rotate_every => 'week',
postrotate => '/usr/bin/killall -HUP syslogd',
},
apache => {
path => '/var/log/httpd/*.log',
rotate => 5,
mail => '[email protected]',
size => '100k',
sharedscripts => true,
postrotate => '/etc/init.d/httpd restart',
}
}
}

```


### Using Hiera

A hiera hash may be used to manage the logrotate rules.
Hash merging may also be enabled, which supports layering the rules.

Examples using:
- YAML backend
- an environment called __production__
- a __/etc/puppet/hiera.yaml__ hierarchy configuration:

```yaml
:hierarchy:
- "%{environment}"
- "defaults"
```

#### Load module

Load the module via Puppet Code or your ENC.

```puppet
include ::logrotate
```

#### Configure Hiera YAML __(defaults.yaml)__

These defaults will apply to all systems.

```yaml
logrotate::hieramerge: true
logrotate::rules:
yum:
path : '/var/log/yum.log'
missingok : true
ifempty : false
size : '30k'
rotate_every : 'year'
create : true
create_mode : '0600'
create_owner : 'root'
create_group : 'root'
syslog:
path :
- '/var/log/cron'
- '/var/log/messages'
- '/var/log/secure'
- '/var/log/spooler'
- '/var/log/maillog'
rotate : 5
rotate_every : 'week'
postrotate : '/usr/bin/killall -HUP syslogd'
apache:
path : '/var/log/httpd/*.log'
rotate : 5
mail : '[email protected]'
size : '100k'
sharedscripts : true
postrotate : '/etc/init.d/httpd restart'
```

#### Configure Hiera YAML __(production.yaml)__

This will only apply to the production environment.
In this example we are:
- inheriting/preserving the __yum__ rule
- overriding the __syslog__ rule
- disabling the __apache__ rule

```yaml
logrotate::rules:
syslog:
path :
- '/var/log/cron'
- '/var/log/messages'
- '/var/log/secure'
- '/var/log/spooler'
- '/var/log/maillog'
rotate : 5
rotate_every : 'day'
postrotate : '/usr/bin/killall -HUP syslogd'
apache:
ensure : 'absent'
```

If you have Hiera version >= 1.2.0, set `logrotate::hieramerge` to true and enable [Hiera Deeper Merging](http://docs.puppetlabs.com/hiera/1/lookup_types.html#deep-merging-in-hiera--120) you may conditionally override any setting.

In this example we are:
- inheriting/preserving the __yum__ rule
- overriding the __syslog:rotate_every__ setting
- inheriting/preserving all other __syslog__ settings
- disabling the __apache__ rule

```yaml
logrotate::rules:
syslog:
rotate_every : 'day'
apache:
ensure : 'absent'
```
92 changes: 92 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/usr/bin/ruby
#
# Run several types of tests againsts a puppet repo (module or master
# config).
#
# original author: Thomas Van Doren
#

require 'rake'

# default
desc 'Run all tests.'
task :default => ['check:syntax', 'check:lint', 'check:spec']

# shortcuts
task :syntax => ['check:syntax']
task :lint => ['check:lint']
task :spec => ['check:spec']

# help
desc 'Show available tasks and exit.'
task :help do
system('rake -T')
end

# clean, clobber
require 'rake/clean'
CLEAN.include('doc')
CLOBBER.include('')

namespace :check do
# syntax
desc 'Validate syntax for all manifests.'
task :syntax do
successes = []
failures = []
Dir.glob('manifests/**/*.pp').each do |puppet_file|
puts "Checking syntax for #{puppet_file}"

# Run syntax checker in subprocess.
system("puppet parser validate #{puppet_file}")

# Keep track of the results.
if $?.success?
successes << puppet_file
else
failures << puppet_file
end
end

# Print the results.
total_manifests = successes.count + failures.count
puts "#{total_manifests} files total."
puts "#{successes.count} files succeeded."
puts "#{failures.count} files failed:"
puts
failures.each do |filename|
puts filename
end

# Fail the task if any files failed syntax check.
if failures.count > 0
fail("#{failures.count} files failed syntax check.")
end
end

# lint
require 'puppet-lint/tasks/puppet-lint'
PuppetLint.configuration.ignore_paths = ["vendor/**/*.pp"]

# spec
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/*/*_spec.rb'
end
end

# doc
desc 'Build rdoc documentation.'
task :doc do
cwd = Dir.pwd
manifest_dir = "#{cwd}/manifests"
module_path = "#{cwd}/modules"
system("puppet doc --mode rdoc --manifestdir #{manifest_dir} --modulepath #{module_path}")
end

# build
desc 'Build package for puppet forge.'
task :build do
system('puppet module build .')
end
43 changes: 0 additions & 43 deletions manifests/base.pp

This file was deleted.

68 changes: 68 additions & 0 deletions manifests/init.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# Class: logrotate
#
# Install logrotate and configure it to read from /etc/logrotate.d
#
# Examples
#
# include logrotate
#
class logrotate(

$packages = 'logrotate',
$rules = undef,
$hieramerge = false

) {

package { $packages:
ensure => latest,
}

File {
owner => 'root',
group => 'root',
require => Package[$packages],
}

file {
'/etc/logrotate.conf':
ensure => file,
mode => '0444',
source => 'puppet:///modules/logrotate/etc/logrotate.conf';
'/etc/logrotate.d':
ensure => directory,
mode => '0755';
'/etc/cron.daily/logrotate':
ensure => file,
mode => '0555',
source => 'puppet:///modules/logrotate/etc/cron.daily/logrotate';
}

# default rules
case $::osfamily {
'Debian': {
class { '::logrotate::defaults::debian':
require => Package[$packages];
}
}
'RedHat': {
class { '::logrotate::defaults::redhat':
require => Package[$packages];
}
}
'SuSE': {
class { '::logrotate::defaults::suse':
require => Package[$packages];
}
}
default: { }
}

# user specified rules
class { '::logrotate::rules':
require => Package[$packages];
}

}

Loading