diff options
-rw-r--r-- | README | 17 | ||||
-rw-r--r-- | lib/puppet/parser/functions/basename.rb | 12 | ||||
-rw-r--r-- | lib/puppet/parser/functions/dirname.rb | 10 | ||||
-rw-r--r-- | lib/puppet/parser/functions/re_escape.rb | 2 | ||||
-rw-r--r-- | lib/puppet/parser/functions/split.rb | 23 | ||||
-rw-r--r-- | manifests/defines/append_if_no_such_line.pp | 14 | ||||
-rw-r--r-- | manifests/defines/config_file.pp | 11 | ||||
-rw-r--r-- | manifests/defines/delete_lines.pp | 5 | ||||
-rw-r--r-- | manifests/defines/line.pp | 37 | ||||
-rw-r--r-- | manifests/defines/replace.pp | 10 |
10 files changed, 112 insertions, 29 deletions
@@ -1,17 +1,22 @@ -The common module installs various functions that are required by other modules. This -module should be installed before any of the other module. +The common module installs various functions that are required by other +modules. This module should be installed before any of the other module. To use this module, follow these directions: -1. Your modules directory will need all the files included in this repository placed - under a directory called "common" +1. Your modules directory will need all the files included in this + repository placed under a directory called "common" 2. Add the following line to manifests/site.pp: - import "modules.pp" + import "modules.pp" 3. Add the following line to manifests/modules.pp: - import "common" + import "common" + + +Author:: David Schmitt (mailto:david@dasz.at) +Copyright:: Copyright (c) 2007-2009 dasz.at OG +License:: 3-clause BSD diff --git a/lib/puppet/parser/functions/basename.rb b/lib/puppet/parser/functions/basename.rb index 226d6e5..dc72537 100644 --- a/lib/puppet/parser/functions/basename.rb +++ b/lib/puppet/parser/functions/basename.rb @@ -1,9 +1,15 @@ -# basename(string) : string -# basename(string[]) : string[] +# This function has two modes of operation: +# +# basename(string) : string # # Returns the last component of the filename given as argument, which must be -# formed using forward slashes (``/..) regardless of the separator used on the +# formed using forward slashes ("/") regardless of the separator used on the # local file system. +# +# basename(string[]) : string[] +# +# Returns an array of strings with the basename of each item from the argument. +# module Puppet::Parser::Functions newfunction(:basename, :type => :rvalue) do |args| if args[0].is_a?(Array) diff --git a/lib/puppet/parser/functions/dirname.rb b/lib/puppet/parser/functions/dirname.rb index 44b4a00..ea0d50b 100644 --- a/lib/puppet/parser/functions/dirname.rb +++ b/lib/puppet/parser/functions/dirname.rb @@ -1,9 +1,15 @@ -# dirname(string) : string -# dirname(string[]) : string[] +# This function has two modes of operation: +# +# dirname(string) : string # # Returns all components of the filename given as argument except the last # one. The filename must be formed using forward slashes (``/..) regardless of # the separator used on the local file system. +# +# dirname(string[]) : string[] +# +# Returns an array of strings with the basename of each item from the argument. +# module Puppet::Parser::Functions newfunction(:dirname, :type => :rvalue) do |args| if args[0].is_a?(Array) diff --git a/lib/puppet/parser/functions/re_escape.rb b/lib/puppet/parser/functions/re_escape.rb index 6e5904b..7bee90a 100644 --- a/lib/puppet/parser/functions/re_escape.rb +++ b/lib/puppet/parser/functions/re_escape.rb @@ -1,4 +1,4 @@ -# apply regexp escaping to a string +# apply ruby regexp escaping to a string module Puppet::Parser::Functions newfunction(:re_escape, :type => :rvalue) do |args| Regexp.escape(args[0]) diff --git a/lib/puppet/parser/functions/split.rb b/lib/puppet/parser/functions/split.rb new file mode 100644 index 0000000..bffecc1 --- /dev/null +++ b/lib/puppet/parser/functions/split.rb @@ -0,0 +1,23 @@ +# This function has two modes of operation: +# +# split($string, $delimiter) : $string +# +# Split the first argument on every $delimiter. $delimiter is interpreted as +# Ruby regular expression. +# +# split($string[], $delimiter) : $string[][] +# +# Returns an array of split results with the result of applying split to each +# item from the first argument. +# +# For long-term portability it is recommended to refrain from using Ruby's +# extended RE features. +module Puppet::Parser::Functions + newfunction(:split, :type => :rvalue) do |args| + if args[0].is_a?(Array) + args.collect do |a| a.split(/#{args[1]}/) end + else + args[0].split(/#{args[1]}/) + end + end +end diff --git a/manifests/defines/append_if_no_such_line.pp b/manifests/defines/append_if_no_such_line.pp new file mode 100644 index 0000000..6ccf9f9 --- /dev/null +++ b/manifests/defines/append_if_no_such_line.pp @@ -0,0 +1,14 @@ +# +# This define is only for "CFEngine compatability". It is only a light +# wrapper around the "line" define, which is equally dangerous, but at +# least named according to a proper resource model. +# +define append_if_no_such_line($file, $line) { + line { + $name: + ensure => present, + file => $file, + line => $line; + } +} + diff --git a/manifests/defines/config_file.pp b/manifests/defines/config_file.pp index 2272558..375e25c 100644 --- a/manifests/defines/config_file.pp +++ b/manifests/defines/config_file.pp @@ -2,6 +2,8 @@ # 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", @@ -12,10 +14,11 @@ # 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}"]; +# 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 diff --git a/manifests/defines/delete_lines.pp b/manifests/defines/delete_lines.pp new file mode 100644 index 0000000..28d2362 --- /dev/null +++ b/manifests/defines/delete_lines.pp @@ -0,0 +1,5 @@ +define delete_lines($file, $pattern) { + exec { "/bin/sed -i -r -e '/$pattern/d' $file": + onlyif => "/bin/grep -E '$pattern' '$file'", + } +} diff --git a/manifests/defines/line.pp b/manifests/defines/line.pp index 7ca3191..44c52a0 100644 --- a/manifests/defines/line.pp +++ b/manifests/defines/line.pp @@ -2,6 +2,17 @@ # 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", @@ -10,19 +21,21 @@ # } # # 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], -# } +# 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}'" ) } @@ -39,5 +52,3 @@ define line($file, $line, $ensure = 'present') { } } } - - diff --git a/manifests/defines/replace.pp b/manifests/defines/replace.pp index 7dabe59..f7da3b4 100644 --- a/manifests/defines/replace.pp +++ b/manifests/defines/replace.pp @@ -2,6 +2,16 @@ # Copyright (C) 2007 David Schmitt <david@schmitt.edv-bus.at> # See LICENSE for the full license granted to you. +# A hack to replace all ocurrances 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: |