cleanup module to work better on newer puppet versions

This commit is contained in:
mh 2013-01-03 16:49:04 +01:00
parent 50a1943175
commit 47c385f479
13 changed files with 88 additions and 399 deletions

View file

@ -1,121 +0,0 @@
# common/manifests/defines/concatenated_file.pp -- create a file from snippets
# stored in a directory
#
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
# TODO:
# * create the directory in _part too
# This resource collects file snippets from a directory ($dir) and concatenates
# them in lexical order of their names into a new file ($name). This can be
# used to collect information from disparate sources, when the target file
# format doesn't allow includes.
#
# concatenated_file_part can be used to easily configure content for this.
#
# If no $dir is specified, the target name with '.d' appended will be used.
#
# The $dir is purged by puppet and will only contain explicitely configured
# files. This can be overridden by defining the directory before the
# concatenated_file.
#
# Depend on File[$name] to change if and only if its contents change. Notify
# Exec["concat_${name}"] if you want to force an update.
#
# Usage:
# concatenated_file { "/etc/some.conf":
# dir => "/etc/some.conf.d",
# }
define concatenated_file (
$ensure = 'present',
# where the snippets are located
$dir = '',
# a file with content to prepend
$header = '',
# a file with content to append
$footer = '',
# default permissions for the target file
$mode = 0644, $owner = root, $group = 0
)
{
include common::moduledir::common::cf
$dir_real = $dir ? { '' => "${name}.d", default => $dir }
$tmp_file_name = regsubst($dir_real, '/', '_', 'G')
$tmp_file = "${common::moduledir::module_dir_path}/${tmp_file_name}"
if defined(File[$dir_real]) {
debug("${dir_real} already defined")
} else {
file {
$dir_real:
ensure => $ensure ? {
'present' => directory,
default => $ensure
},
checksum => mtime,
recurse => true, purge => true, force => true,
mode => $mode, owner => $owner, group => $group,
notify => Exec["concat_${name}"];
}
}
file {
$tmp_file:
ensure => $ensure, checksum => md5,
mode => $mode, owner => $owner, group => $group;
# decouple the actual file from the generation process by using a
# temporary file and puppet's source mechanism. This ensures that events
# for notify/subscribe will only be generated when there is an actual
# change.
$name:
ensure => $ensure, checksum => md5,
source => $tmp_file,
mode => $mode, owner => $owner, group => $group,
require => File[$tmp_file];
}
if $ensure == 'present' {
# if there is a header or footer file, add it
$additional_cmd = $header ? {
'' => $footer ? {
'' => '',
default => "| cat - '${footer}' "
},
default => $footer ? {
'' => "| cat '${header}' - ",
default => "| cat '${header}' - '${footer}' "
}
}
# use >| to force clobbering the target file
exec { "concat_${name}":
command => "/usr/bin/find ${dir_real} -maxdepth 1 -type f ! -name '*puppettmp' -print0 | sort -z | xargs -0 cat ${additional_cmd} >| ${tmp_file}",
subscribe => [ File[$dir_real] ],
before => File[$tmp_file],
alias => [ "concat_${dir_real}"],
loglevel => info
}
}
}
# Add a snippet called $name to the concatenated_file at $dir.
# The file can be referenced as File["cf_part_${name}"]
define concatenated_file_part (
$dir, $content = '', $ensure = present,
$mode = 0644, $owner = root, $group = 0
)
{
file { "${dir}/${name}":
ensure => $ensure, content => $content,
mode => $mode, owner => $owner, group => $group,
alias => "cf_part_${name}",
notify => Exec["concat_${dir}"],
}
}

View file

@ -1,55 +0,0 @@
# common/manifests/defines/config_file.pp -- create a config file with default permissions
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
# A simple wrapper to give all configuration files common defaults.
#
# Usage:
# config_file { filename:
# content => "....\n",
# }
#
# Examples:
#
# To create the file /etc/vservers/${vs_name}/context with specific
# content:
#
# config_file { "/etc/vservers/${vs_name}/context":
# content => "${context}\n",
# notify => Exec["vs_restart_${vs_name}"],
# require => Exec["vs_create_${vs_name}"];
# }
#
# To create the file /etc/apache2/sites-available/munin-stats with the
# content pulled from a template:
#
# config_file { "/etc/apache2/sites-available/munin-stats":
# content => template("apache/munin-stats"),
# require => Package["apache2"],
# notify => Exec["reload-apache2"]
# }
define config_file ($content = '', $source = '', $ensure = 'present') {
file { $name:
ensure => $ensure,
# keep old versions on the server
backup => puppet,
# default permissions for config files
mode => 0644, owner => root, group => 0,
# really detect changes to this file
checksum => md5,
}
case $source {
'': { }
default: { File[$name] { source => $source } }
}
case $content {
'': { }
default: { File[$name] { content => $content } }
}
}

View file

@ -1,54 +0,0 @@
# common/manifests/defines/line.pp -- a trivial mechanism to ensure a line exists in a file
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
# Ensures that a specific line is present or absent in a file. This can
# be very brittle, since even small changes can throw this off.
#
# If the line is not present yet, it will be appended to the file.
#
# The name of the define is not used. Just keep it (globally) unique and
# descriptive.
#
# Use this only for very trivial stuff. Usually replacing the whole file
# is a more stable solution with less maintenance headaches afterwards.
#
# Usage:
# line { description:
# file => "filename",
# line => "content",
# ensure => {absent,*present*}
# }
#
# Example:
# The following ensures that the line "allow ^$munin_host$" exists in
# /etc/munin/munin-node.conf, and if there are any changes notify the
# service for a restart
#
# line {
# allow_munin_host:
# file => "/etc/munin/munin-node.conf",
# line => "allow ^$munin_host$",
# ensure => present,
# notify => Service[munin-node],
# require => Package[munin-node];
# }
#
# Code with fixes gathered at
# http://reductivelabs.com/trac/puppet/wiki/Recipes/SimpleText
define line($file, $line, $ensure = 'present') {
case $ensure {
default : { err ( "unknown ensure value '${ensure}'" ) }
present: {
exec { "echo '${line}' >> '${file}'":
unless => "grep -qFx '${line}' '${file}'"
}
}
absent: {
$subst_line = regsubst($line,'(/|\.)','\\\1','G')
exec { "sed -i '/${subst_line}/d' '${file}'":
onlyif => "grep -qFx '${line}' '${file}'"
}
}
}
}

View file

@ -1,12 +0,0 @@
# common/manifests/defines/link.pp -- create a link
#
# Copyleft (C) 2009 immerdaadmin <admin@immerda.ch>
# See LICENSE for the full license granted to you.
# Usage:
# link { "/usr/bin/convert": target => "/usr/local/bin/convert" }
# e.g. equivalent to bash# ln -s /usr/bin/convert /usr/local/bin/convert
define link($target = absent) {
file{$name: ensure => $target; }
}

View file

@ -1,48 +0,0 @@
# common/manifests/defines/modules_dir.pp -- create a default directory
# for storing module specific information
#
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
# A module_dir is a storage place for all the stuff a module might want to
# store. According to the FHS, this should go to /var/lib. Since this is a part
# of puppet, the full path is /var/lib/puppet/modules/${name}. Every module
# should # prefix its module_dirs with its name.
#
# By default, the module_dir is loaded from "puppet:///${name}/module_dir". If
# that doesn't exist an empty directory is taken as source. The directory is
# purged so that modules do not have to worry about removing cruft.
#
# Usage:
# include common::moduledir
# module_dir { ["common", "common/dir1", "common/dir2" ]: }
#
# You may refer to a file in module_dir by using :
# file { "${common::moduledir::module_dir_path}/somedir/somefile": }
define module_dir (
$mode = 0644, $owner = root, $group = 0
)
{
include common::moduledir
$dir = "${common::moduledir::module_dir_path}/${name}"
if defined(File[$dir]) {
debug("${dir} already defined")
} else {
file {
$dir:
checksum => mtime,
# ignore the placeholder
recurse => true, purge => true, force => true,
mode => $mode, owner => $owner, group => $group;
}
}
}
# alias for compatibility
define modules_dir (
$mode = 0644, $owner = root, $group = 0
)
{
module_dir { $name: mode => $mode, owner => $owner, group => $group }
}

View file

@ -1,47 +0,0 @@
# common/manifests/defines/modules_file.pp -- use a modules_dir to store module
# specific files
#
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
# Put a file into module-local storage.
#
# Usage:
# modules_file { "module/file":
# source => "puppet:///...",
# mode => 644, # default
# owner => root, # default
# group => 0, # default
# }
define module_file (
$source,
$ensure = present,
$m_alias = undef,
$mode = 0644, $owner = root, $group = 0
)
{
include common::moduledir
file {
"${common::moduledir::module_dir_path}/${name}":
source => $source,
ensure => $ensure,
alias => $m_alias,
mode => $mode, owner => $owner, group => $group;
}
}
# alias for compatibility
define modules_file (
$source,
$ensure = present,
$m_alias = undef,
$mode = 0644, $owner = root, $group = 0
)
{
module_file { $name:
source => $source,
ensure => $ensure,
alias => $m_alias,
mode => $mode, owner => $owner, group => $group
}
}

View file

@ -1,40 +0,0 @@
# common/manifests/defines/replace.pp -- replace a pattern in a file with a string
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
# A hack to replace all occurrences of a regular expression in a file with a
# specified string. Sometimes it can be less effort to replace only a single
# value in a huge config file instead of creating a template out of it. Still,
# creating a template is often better than this hack.
#
# This define uses perl regular expressions.
#
# Use this only for very trivial stuff. Usually replacing the whole file is a
# more stable solution with less maintenance headaches afterwards.
#
# Usage:
#
# replace { description:
# file => "filename",
# pattern => "regexp",
# replacement => "replacement"
#
# Example:
# To replace the current port in /etc/munin/munin-node.conf
# with a new port, but only disturbing the file when needed:
#
# replace { set_munin_node_port:
# file => "/etc/munin/munin-node.conf",
# pattern => "^port (?!$port)[0-9]*",
# replacement => "port $port"
# }
define replace($file, $pattern, $replacement) {
$pattern_no_slashes = slash_escape($pattern)
$replacement_no_slashes = slash_escape($replacement)
exec { "replace_${pattern}_${file}":
command => "/usr/bin/perl -pi -e 's/${pattern_no_slashes}/${replacement_no_slashes}/' '${file}'",
onlyif => "/usr/bin/perl -ne 'BEGIN { \$ret = 1; } \$ret = 0 if /${pattern_no_slashes}/ && ! /\\Q${replacement_no_slashes}\\E/; END { exit \$ret; }' '${file}'",
alias => "exec_$name",
}
}

View file

@ -1,6 +0,0 @@
# common/manifests/init.pp - Define common infrastructure for modules
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
import "defines/*.pp"

34
manifests/module_dir.pp Normal file
View file

@ -0,0 +1,34 @@
# common/manifests/modules_dir.pp -- create a default directory
# for storing module specific information
#
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
# A module_dir is a storage place for all the stuff a module might want to
# store. According to the FHS, this should go to /var/lib. Since this is a part
# of puppet, the full path is /var/lib/puppet/modules/${name}. Every module
# should # prefix its module_dirs with its name.
#
# Usage:
# include common::moduledir
# module_dir { ["common", "common/dir1", "common/dir2" ]: }
#
# You may refer to a file in module_dir by using :
# file { "${common::moduledir::module_dir_path}/somedir/somefile": }
define common::module_dir(
$owner = root,
$group = 0,
$mode = 0644
) {
include common::moduledir
file {
"${common::moduledir::module_dir_path}/${name}":
ensure => directory,
recurse => true,
purge => true,
force => true,
owner => $owner,
group => $group,
mode => $mode;
}
}

37
manifests/module_file.pp Normal file
View file

@ -0,0 +1,37 @@
# common/manifests/module_file.pp -- use a modules_dir to store module
# specific files
#
# Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at>
# See LICENSE for the full license granted to you.
# Put a file into module-local storage.
#
# Usage:
# common::module_file { "module/file":
# source => "puppet:///...",
# mode => 644, # default
# owner => root, # default
# group => 0, # default
# }
define common::module_file (
$ensure = present,
$source = undef,
$owner = root,
$group = 0,
$mode = 0644
){
include common::moduledir
file {
"${common::moduledir::module_dir_path}/${name}":
ensure => $ensure,
}
if $ensure != 'absent' {
File["${common::moduledir::module_dir_path}/${name}"]{
source => $source,
owner => $owner,
group => $group,
mode => $mode,
}
}
}

View file

@ -1,13 +1,18 @@
# setup root for module_dirs
class common::moduledir {
# Use this variable to reference the base path. Thus you are safe from any
# changes.
$module_dir_path = '/var/lib/puppet/modules'
# Use this variable to reference the base path. Thus you are safe from any
# changes.
$module_dir_path = '/var/lib/puppet/modules'
# Module programmers can use /var/lib/puppet/modules/$modulename to save
# module-local data, e.g. for constructing config files
file{$module_dir_path:
ensure => directory,
recurse => true, purge => true, force => true,
mode => 0755, owner => root, group => 0;
}
# Module programmers can use /var/lib/puppet/modules/$modulename to save
# module-local data, e.g. for constructing config files
file{$module_dir_path:
ensure => directory,
recurse => true,
purge => true,
force => true,
owner => root,
group => 0,
mode => '0755';
}
}

View file

@ -1,4 +1,4 @@
# setup a common dir
class common::moduledir::common{
include ::common::moduledir
module_dir{'common': }
common::module_dir{'common': }
}

View file

@ -1,4 +0,0 @@
class common::moduledir::common::cf {
include ::common::moduledir::common
module_dir{'common/cf': }
}