Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add new params and full hiera support #38

Open
wants to merge 13 commits into from

4 participants

@level99

This adds 3 new params and full Hiera support.
$packages which allows overriding the package list
$rules which allows specifying a hash of logrotate rules
$hieramerge which allows hiera hash merging of logrotate rules

I've also updated the rspec tests and cleaned up the code some for a bit more standard behavior (init.pp).

This SHOULD be backwards compatible with existing usage.

Please see updated documentation for new feature usage.

@level99

fixed up rspec tests for older puppet versions

@dstockman

Hihi @rodjek, any chance you'll merge these changes? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
9 .travis.yml
@@ -8,9 +8,16 @@ notifications:
- tim@github.com
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
View
1  Gemfile
@@ -4,4 +4,5 @@ puppetversion = ENV.key?('PUPPET_VERSION') ? "= #{ENV['PUPPET_VERSION']}" : ['>=
gem 'puppet-lint'
gem 'rspec-puppet'
+gem 'hiera'
gem 'puppet', puppetversion
View
9 Gemfile-puppet27
@@ -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
View
137 README.md
@@ -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.
```
@@ -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,
@@ -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 => 'test@example.com',
+ 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 : 'test@example.com'
+ 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'
+```
View
92 Rakefile
@@ -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
View
43 manifests/base.pp
@@ -1,43 +0,0 @@
-# Internal: Install logrotate and configure it to read from /etc/logrotate.d
-#
-# Examples
-#
-# include logrotate::base
-class logrotate::base {
- package { 'logrotate':
- ensure => latest,
- }
-
- File {
- owner => 'root',
- group => 'root',
- require => Package['logrotate'],
- }
-
- 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';
- }
-
- case $::osfamily {
- 'Debian': {
- include logrotate::defaults::debian
- }
- 'RedHat': {
- include logrotate::defaults::redhat
- }
- 'SuSE': {
- include logrotate::defaults::suse
- }
- default: { }
- }
-}
View
68 manifests/init.pp
@@ -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];
+ }
+
+}
+
View
9 manifests/rule.pp
@@ -401,11 +401,14 @@
#############################################################################
#
- include logrotate::base
+ # Backwards compatibility
+ if ! defined('::logrotate') {
+ include '::logrotate'
+ }
case $rotate_every {
'hour', 'hourly': {
- include logrotate::hourly
+ include ::logrotate::hourly
$rule_path = "/etc/logrotate.d/hourly/${name}"
file { "/etc/logrotate.d/${name}":
@@ -427,6 +430,6 @@
group => 'root',
mode => '0444',
content => template('logrotate/etc/logrotate.d/rule.erb'),
- require => Class['logrotate::base'],
+ require => Class['logrotate'],
}
}
View
35 manifests/rules.pp
@@ -0,0 +1,35 @@
+# Class: logrotate::rules
+#
+# This class loads user given logrotate rules
+#
+# PRIVATE CLASS: do not call directly
+#
+# See the primary logrotate module documentation for usage and examples.
+#
+class logrotate::rules(
+
+ $rules = $::logrotate::rules,
+ $hieramerge = $::logrotate::hieramerge
+
+) {
+
+ # NOTE: hiera_hash does not work as expected in a parameterized class
+ # definition; so we call it here.
+ #
+ # http://docs.puppetlabs.com/hiera/1/puppet.html#limitations
+ # https://tickets.puppetlabs.com/browse/HI-118
+ #
+ if $hieramerge {
+ $x_rules = hiera_hash('logrotate::rules', $rules)
+
+ # Fall back to user given class parameter / priority based hiera lookup
+ } else {
+ $x_rules = $rules
+ }
+
+ if $x_rules {
+ create_resources('logrotate::rule', $x_rules)
+ }
+
+}
+
View
53 spec/classes/base_spec.rb → spec/classes/logrotate_spec.rb
@@ -1,6 +1,31 @@
require 'spec_helper'
-describe 'logrotate::base' do
+describe 'logrotate' do
+
+ let :default_params do
+ {
+ :packages => '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 => 'test@example.com',
+ size => '100k',
+ sharedscripts => true,
+ postrotate => '/etc/init.d/httpd restart',
+ }
+ },
+ :hieramerge => true,
+ }
+
+ end
+
it do
should contain_package('logrotate').with_ensure('latest')
@@ -29,24 +54,43 @@
'source' => 'puppet:///modules/logrotate/etc/cron.daily/logrotate',
'require' => 'Package[logrotate]',
})
+
+ should contain_class('logrotate::rules').with({
+ 'require' => 'Package[logrotate]',
+ })
+
end
context 'on Debian' do
let(:facts) { {:osfamily => 'Debian'} }
- it { should contain_class('logrotate::defaults::debian') }
+ it {
+ should contain_class('logrotate::defaults::debian').with({
+ 'require' => 'Package[logrotate]',
+ })
+ }
+
end
context 'on RedHat' do
let(:facts) { {:osfamily => 'RedHat'} }
- it { should contain_class('logrotate::defaults::redhat') }
+ it {
+ should contain_class('logrotate::defaults::redhat').with({
+ 'require' => 'Package[logrotate]',
+ })
+ }
end
context 'on SuSE' do
let(:facts) { {:osfamily => 'SuSE'} }
- it { should contain_class('logrotate::defaults::suse') }
+ it {
+ should contain_class('logrotate::defaults::suse').with({
+ 'require' => 'Package[logrotate]',
+ })
+ }
+
end
context 'on Gentoo' do
@@ -56,4 +100,5 @@
it { should_not contain_class('logrotate::defaults::redhat') }
it { should_not contain_class('logrotate::defaults::suse') }
end
+
end
View
89 spec/classes/rules_spec.rb
@@ -0,0 +1,89 @@
+require 'spec_helper'
+hiera_file = 'spec/fixtures/hiera/hiera.yaml'
+
+# NOTE: not supported on puppet versions <= 2.6
+
+describe 'logrotate::rules', :type => :class, :unsupported => 2.6 do
+
+ describe 'with no rules' do
+ it { should have_logrotate__rule_resource_count(0) }
+ end
+
+ describe 'with rules and no hiera lookup' do
+ let(:params) { {
+ :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' => 'test@example.com',
+ 'size' => '100k',
+ 'sharedscripts' => true,
+ 'postrotate' => '/etc/init.d/httpd restart',
+ }
+ },
+ :hieramerge => false,
+ } }
+
+ it 'should have logrotate rule: messages' do
+ should contain_logrotate__rule('messages').with({
+ :path => '/var/log/messages',
+ :rotate => 5,
+ :rotate_every => 'week',
+ :postrotate => '/usr/bin/killall -HUP syslogd',
+ })
+ end
+
+ it 'should have logrotate rule: apache' do
+ should contain_logrotate__rule('apache').with({
+ :path => '/var/log/httpd/*.log',
+ :rotate => 5,
+ :mail => 'test@example.com',
+ :size => '100k',
+ :sharedscripts => true,
+ :postrotate => '/etc/init.d/httpd restart',
+ })
+ end
+
+ end
+
+ describe 'with rules and explicit hiera lookup' do
+ let (:hiera_config) { hiera_file }
+
+ hiera = Hiera.new(:config => hiera_file)
+ hieramerge = hiera.lookup('logrotate::hieramerge', nil, nil)
+ rules = hiera.lookup('logrotate::rules', nil, nil)
+
+ let(:params) { {
+ :rules => rules,
+ :hieramerge => hieramerge,
+ } }
+
+ it 'should have logrotate rule: messages' do
+ should contain_logrotate__rule('messages').with({
+ :path => '/var/log/messages',
+ :rotate => 5,
+ :rotate_every => 'week',
+ :postrotate => '/usr/bin/killall -HUP syslogd',
+ })
+ end
+
+ it 'should have logrotate rule: apache' do
+ should contain_logrotate__rule('apache').with({
+ :path => '/var/log/httpd/*.log',
+ :rotate => 5,
+ :mail => 'test@example.com',
+ :size => '100k',
+ :sharedscripts => true,
+ :postrotate => '/etc/init.d/httpd restart',
+ })
+ end
+
+ end
+
+end
View
3  spec/defines/rule_spec.rb
@@ -14,7 +14,8 @@
let(:params) { {:path => '/var/log/foo.log'} }
it do
- should contain_class('logrotate::base')
+ # rspec can't handled if ! defined test ?
+ #should contain_class('logrotate')
should contain_file('/etc/logrotate.d/test').with({
'owner' => 'root',
'group' => 'root',
View
8 spec/fixtures/hiera/hiera.yaml
@@ -0,0 +1,8 @@
+---
+:backends:
+ - yaml
+:hierarchy:
+ - test
+
+:yaml:
+ :datadir: 'spec/fixtures/hiera'
View
15 spec/fixtures/hiera/test.yaml
@@ -0,0 +1,15 @@
+--
+logrotate::hieramerge: true
+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 : 'test@example.com'
+ size : '100k'
+ sharedscripts : true
+ postrotate : '/etc/init.d/httpd restart'
View
12 spec/spec_helper.rb
@@ -1,6 +1,14 @@
require 'rspec-puppet'
+require 'hiera'
+
+fixture_path = File.expand_path(File.join(__FILE__, '..', 'fixtures'))
RSpec.configure do |c|
- c.module_path = File.expand_path(File.join(__FILE__, '..', 'fixtures', 'modules'))
- c.manifest_dir = File.expand_path(File.join(__FILE__, '..', 'fixtures', 'manifests'))
+ c.module_path = File.join(fixture_path, 'modules')
+ c.manifest_dir = File.join(fixture_path, 'manifests')
+
+ # Skip running certain tests on unsupported puppet versions
+ c.filter_run_excluding :unsupported => lambda {|version|
+ (ENV['PUPPET_VERSION'] =~ /^#{version.to_s}/)
+ }
end
Something went wrong with that request. Please try again.