From 521bad6447abecbd1780777bfc8c5adb80de42bb Mon Sep 17 00:00:00 2001 From: mh Date: Thu, 14 Apr 2011 21:10:09 +0200 Subject: remove delete_lines, as this define is obsolete --- manifests/defines/delete_lines.pp | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 manifests/defines/delete_lines.pp diff --git a/manifests/defines/delete_lines.pp b/manifests/defines/delete_lines.pp deleted file mode 100644 index 28d2362..0000000 --- a/manifests/defines/delete_lines.pp +++ /dev/null @@ -1,5 +0,0 @@ -define delete_lines($file, $pattern) { - exec { "/bin/sed -i -r -e '/$pattern/d' $file": - onlyif => "/bin/grep -E '$pattern' '$file'", - } -} -- cgit v1.2.3 From 3827e73d67bece1b901d2bb2630a955aa737454c Mon Sep 17 00:00:00 2001 From: mh Date: Tue, 17 May 2011 21:10:40 +0200 Subject: add array union function --- lib/puppet/parser/functions/array_union.rb | 10 ++++++++ spec/unit/parser/functions/array_union.rb | 39 ++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 lib/puppet/parser/functions/array_union.rb create mode 100644 spec/unit/parser/functions/array_union.rb diff --git a/lib/puppet/parser/functions/array_union.rb b/lib/puppet/parser/functions/array_union.rb new file mode 100644 index 0000000..1ccad3f --- /dev/null +++ b/lib/puppet/parser/functions/array_union.rb @@ -0,0 +1,10 @@ +Puppet::Parser::Functions::newfunction( + :array_union, + :type => :rvalue, + :doc => "Unions two arrays + + Example: array_union(['a','b'],['c']) -> ['a','b','c']" +) do |args| + raise Puppet::ParseError, 'array_union() needs two arguments' if args.length != 2 + args[0].to_a | args[1].to_a +end diff --git a/spec/unit/parser/functions/array_union.rb b/spec/unit/parser/functions/array_union.rb new file mode 100644 index 0000000..37df2b3 --- /dev/null +++ b/spec/unit/parser/functions/array_union.rb @@ -0,0 +1,39 @@ +#! /usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe "the array_union function" do + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("array_union").should == "function_array_union" + end + + it "should raise a ParseError if there is less than 2 arguments" do + lambda { @scope.function_array_union([["foo"]]) }.should( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there is more than 2 arguments" do + lambda { @scope.function_array_union([["foo", "bar", "gazonk"]]) }.should( raise_error(Puppet::ParseError)) + end + + it "should union to arrays" do + result = @scope.function_array_union([['a','b'],['c']]) + result.should(eql(['a','b','c'])) + end + + it "should do nothing if an item is present" do + result = @scope.function_array_union([['a','b'],['b']]) + result.should(eql(['a','b'])) + end + + it "should leave the argument untouched" do + a = ['a','b'] + result = @scope.function_array_union([a,['c']]) + a.should(eql(['a','b'])) + end + +end -- cgit v1.2.3 From ad2be0a0f8eb24149761a8bade28ef9c222af1af Mon Sep 17 00:00:00 2001 From: mh Date: Tue, 17 May 2011 21:19:58 +0200 Subject: add hash_merge function --- lib/puppet/parser/functions/hash_merge.rb | 10 +++++++++ spec/unit/parser/functions/hash_merge.rb | 34 +++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 lib/puppet/parser/functions/hash_merge.rb create mode 100644 spec/unit/parser/functions/hash_merge.rb diff --git a/lib/puppet/parser/functions/hash_merge.rb b/lib/puppet/parser/functions/hash_merge.rb new file mode 100644 index 0000000..831c754 --- /dev/null +++ b/lib/puppet/parser/functions/hash_merge.rb @@ -0,0 +1,10 @@ +Puppet::Parser::Functions::newfunction( + :hash_merge, + :type => :rvalue, + :doc => "Merges two hashes + + Example: hash_merge({'a' => 1 ,'b' => 2},{'b' => 3, 'c' => 4}) -> {'a' => 1 ,'b' => 3, 'c' => 4}" +) do |args| + raise Puppet::ParseError, 'hash_merge() needs two arguments' if args.length != 2 + args[0].merge(args[1]) +end diff --git a/spec/unit/parser/functions/hash_merge.rb b/spec/unit/parser/functions/hash_merge.rb new file mode 100644 index 0000000..0bd8fc1 --- /dev/null +++ b/spec/unit/parser/functions/hash_merge.rb @@ -0,0 +1,34 @@ +#! /usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' + +describe "the hash_merge function" do + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("hash_merge").should == "function_hash_merge" + end + + it "should raise a ParseError if there is less than 2 arguments" do + lambda { @scope.function_hash_merge(["foo"]) }.should( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there is more than 2 arguments" do + lambda { @scope.function_hash_merge(["foo", "bar", "gazonk"]) }.should( raise_error(Puppet::ParseError)) + end + + it "should merge the two hashes" do + result = @scope.function_hash_merge([{'a' => 1 ,'b' => 2},{'b' => 3, 'c' => 4}]) + result.should == {'a' => 1 ,'b' => 3, 'c' => 4} + end + + it "should leave the argument untouched" do + a = {'a' => 1 ,'b' => 2} + result = @scope.function_hash_merge([{'a' => 1 ,'b' => 2},{'b' => 3, 'c' => 4}]) + a.should == {'a' => 1 ,'b' => 2} + end + +end -- cgit v1.2.3 From 5597f5bbcb7bf0f6191e6b9247db0af4892a766e Mon Sep 17 00:00:00 2001 From: mh Date: Fri, 15 Jul 2011 15:21:10 +0200 Subject: it is not anymore necessary to have an empty directory as source to purge things --- files/empty/.ignore | 0 manifests/defines/concatenated_file.pp | 2 -- manifests/defines/module_dir.pp | 2 -- manifests/moduledir.pp | 2 -- 4 files changed, 6 deletions(-) delete mode 100644 files/empty/.ignore diff --git a/files/empty/.ignore b/files/empty/.ignore deleted file mode 100644 index e69de29..0000000 diff --git a/manifests/defines/concatenated_file.pp b/manifests/defines/concatenated_file.pp index 5f1c275..8eed351 100644 --- a/manifests/defines/concatenated_file.pp +++ b/manifests/defines/concatenated_file.pp @@ -55,9 +55,7 @@ define concatenated_file ( 'present' => directory, default => $ensure }, - source => "puppet:///modules/common/empty", checksum => mtime, - ignore => '.ignore', recurse => true, purge => true, force => true, mode => $mode, owner => $owner, group => $group, notify => Exec["concat_${name}"]; diff --git a/manifests/defines/module_dir.pp b/manifests/defines/module_dir.pp index 227fe71..2852f83 100644 --- a/manifests/defines/module_dir.pp +++ b/manifests/defines/module_dir.pp @@ -31,10 +31,8 @@ define module_dir ( } else { file { $dir: - source => [ "puppet:///modules/${name}/modules_dir", "puppet:///modules/common/empty"], checksum => mtime, # ignore the placeholder - ignore => '.ignore', recurse => true, purge => true, force => true, mode => $mode, owner => $owner, group => $group; } diff --git a/manifests/moduledir.pp b/manifests/moduledir.pp index df83e60..85c29d6 100644 --- a/manifests/moduledir.pp +++ b/manifests/moduledir.pp @@ -7,8 +7,6 @@ class common::moduledir { # module-local data, e.g. for constructing config files file{$module_dir_path: ensure => directory, - source => "puppet:///modules/common/modules/", - ignore => '.ignore', recurse => true, purge => true, force => true, mode => 0755, owner => root, group => 0; } -- cgit v1.2.3 From 2352be71bf3fe16ccfec10dff48a78aeae64d834 Mon Sep 17 00:00:00 2001 From: Marcel Haerry Date: Wed, 3 Aug 2011 17:28:53 +0200 Subject: fix class loading so variables are set --- manifests/moduledir/common.pp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/manifests/moduledir/common.pp b/manifests/moduledir/common.pp index c615f84..80e200d 100644 --- a/manifests/moduledir/common.pp +++ b/manifests/moduledir/common.pp @@ -1,3 +1,4 @@ class common::moduledir::common{ - module_dir{'common': } + include ::common::moduledir + module_dir{'common': } } -- cgit v1.2.3 From e90839b24871f9cfd2166a116e45de5f27fe9e50 Mon Sep 17 00:00:00 2001 From: mh Date: Sat, 8 Oct 2011 20:07:53 +0200 Subject: remove a couple of functions that can now be found in the puppetlabs stdlib --- lib/puppet/parser/functions/array_del.rb | 11 -------- lib/puppet/parser/functions/hash_merge.rb | 10 -------- lib/puppet/parser/functions/join.rb | 10 -------- lib/puppet/parser/functions/strlength.rb | 6 ----- lib/puppet/parser/functions/uniq_flatten.rb | 10 -------- spec/unit/parser/functions/array_del.rb | 39 ----------------------------- spec/unit/parser/functions/hash_merge.rb | 34 ------------------------- 7 files changed, 120 deletions(-) delete mode 100644 lib/puppet/parser/functions/array_del.rb delete mode 100644 lib/puppet/parser/functions/hash_merge.rb delete mode 100644 lib/puppet/parser/functions/join.rb delete mode 100644 lib/puppet/parser/functions/strlength.rb delete mode 100644 lib/puppet/parser/functions/uniq_flatten.rb delete mode 100644 spec/unit/parser/functions/array_del.rb delete mode 100644 spec/unit/parser/functions/hash_merge.rb diff --git a/lib/puppet/parser/functions/array_del.rb b/lib/puppet/parser/functions/array_del.rb deleted file mode 100644 index e604916..0000000 --- a/lib/puppet/parser/functions/array_del.rb +++ /dev/null @@ -1,11 +0,0 @@ -Puppet::Parser::Functions::newfunction( - :array_del, - :type => :rvalue, - :doc => "Deletes items from an array - - Example: array_del(['a','b'],'b') -> ['a']" -) do |args| - raise Puppet::ParseError, 'array_del() needs two arguments' if args.length != 2 - (res=args[0].dup).to_a.delete(args[1]) - res -end diff --git a/lib/puppet/parser/functions/hash_merge.rb b/lib/puppet/parser/functions/hash_merge.rb deleted file mode 100644 index 831c754..0000000 --- a/lib/puppet/parser/functions/hash_merge.rb +++ /dev/null @@ -1,10 +0,0 @@ -Puppet::Parser::Functions::newfunction( - :hash_merge, - :type => :rvalue, - :doc => "Merges two hashes - - Example: hash_merge({'a' => 1 ,'b' => 2},{'b' => 3, 'c' => 4}) -> {'a' => 1 ,'b' => 3, 'c' => 4}" -) do |args| - raise Puppet::ParseError, 'hash_merge() needs two arguments' if args.length != 2 - args[0].merge(args[1]) -end diff --git a/lib/puppet/parser/functions/join.rb b/lib/puppet/parser/functions/join.rb deleted file mode 100644 index 95b664c..0000000 --- a/lib/puppet/parser/functions/join.rb +++ /dev/null @@ -1,10 +0,0 @@ -Puppet::Parser::Functions::newfunction( - :join, - :type => :rvalue, - :doc => "Joins the values of the array in arg1 with the string in arg2 - - Example: join(['a','b'],',') -> 'a,b'" -) do |args| - raise Puppet::ParseError, 'join() needs two arguments' if args.length != 2 - args[0].to_a.join(args[1]) -end diff --git a/lib/puppet/parser/functions/strlength.rb b/lib/puppet/parser/functions/strlength.rb deleted file mode 100644 index 147b24a..0000000 --- a/lib/puppet/parser/functions/strlength.rb +++ /dev/null @@ -1,6 +0,0 @@ -module Puppet::Parser::Functions - newfunction(:strlength, :type => :rvalue) do |args| - args[0].to_s.length - end -end - diff --git a/lib/puppet/parser/functions/uniq_flatten.rb b/lib/puppet/parser/functions/uniq_flatten.rb deleted file mode 100644 index 4841c4d..0000000 --- a/lib/puppet/parser/functions/uniq_flatten.rb +++ /dev/null @@ -1,10 +0,0 @@ -Puppet::Parser::Functions::newfunction( - :uniq_flatten, - :type => :rvalue, - :doc => "Flattens an array and make it uniq - - Example: uniq_flatten([['a','b'],'a']) -> ['a','b']" -) do |args| - raise Puppet::ParseError, 'uniq_flatten() needs one arguments' if args.length != 1 - args[0].to_a.flatten.collect(&:to_s).uniq -end diff --git a/spec/unit/parser/functions/array_del.rb b/spec/unit/parser/functions/array_del.rb deleted file mode 100644 index b375862..0000000 --- a/spec/unit/parser/functions/array_del.rb +++ /dev/null @@ -1,39 +0,0 @@ -#! /usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../../spec_helper' - -describe "the array_del function" do - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("array_del").should == "function_array_del" - end - - it "should raise a ParseError if there is less than 2 arguments" do - lambda { @scope.function_array_del(["foo"]) }.should( raise_error(Puppet::ParseError)) - end - - it "should raise a ParseError if there is more than 2 arguments" do - lambda { @scope.function_array_del(["foo", "bar", "gazonk"]) }.should( raise_error(Puppet::ParseError)) - end - - it "should remove an item if it's present" do - result = @scope.function_array_del(['a','b'],'b') - result.should(eql(['a'])) - end - - it "should do nothing if an item is not present" do - result = @scope.function_array_del(['a','b'],'c') - result.should(eql(['a','b'])) - end - - it "should leave the argument untouched" do - a = ['a','b'] - result = @scope.function_array_del(a,'b') - a.should(eql(['a','b'])) - end - -end diff --git a/spec/unit/parser/functions/hash_merge.rb b/spec/unit/parser/functions/hash_merge.rb deleted file mode 100644 index 0bd8fc1..0000000 --- a/spec/unit/parser/functions/hash_merge.rb +++ /dev/null @@ -1,34 +0,0 @@ -#! /usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../../spec_helper' - -describe "the hash_merge function" do - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("hash_merge").should == "function_hash_merge" - end - - it "should raise a ParseError if there is less than 2 arguments" do - lambda { @scope.function_hash_merge(["foo"]) }.should( raise_error(Puppet::ParseError)) - end - - it "should raise a ParseError if there is more than 2 arguments" do - lambda { @scope.function_hash_merge(["foo", "bar", "gazonk"]) }.should( raise_error(Puppet::ParseError)) - end - - it "should merge the two hashes" do - result = @scope.function_hash_merge([{'a' => 1 ,'b' => 2},{'b' => 3, 'c' => 4}]) - result.should == {'a' => 1 ,'b' => 3, 'c' => 4} - end - - it "should leave the argument untouched" do - a = {'a' => 1 ,'b' => 2} - result = @scope.function_hash_merge([{'a' => 1 ,'b' => 2},{'b' => 3, 'c' => 4}]) - a.should == {'a' => 1 ,'b' => 2} - end - -end -- cgit v1.2.3 From 50a1943175bd203116d100eb5cf4f01f2fc6ded1 Mon Sep 17 00:00:00 2001 From: mh Date: Fri, 8 Jun 2012 12:26:43 -0300 Subject: in 2.7 recmkdir is gone --- lib/puppet/parser/functions/tfile.rb | 3 ++- spec/unit/parser/functions/tfile.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/puppet/parser/functions/tfile.rb b/lib/puppet/parser/functions/tfile.rb index a984892..acb6609 100644 --- a/lib/puppet/parser/functions/tfile.rb +++ b/lib/puppet/parser/functions/tfile.rb @@ -9,7 +9,8 @@ Puppet::Parser::Functions::newfunction( unless File.exists?(path) dir = File.dirname(path) unless File.directory?(dir) - Puppet::Util.recmkdir(dir,0700) + require 'fileutils' + FileUtils.mkdir_p(dir, :mode => 0700) end require 'fileutils' FileUtils.touch(path) diff --git a/spec/unit/parser/functions/tfile.rb b/spec/unit/parser/functions/tfile.rb index 1cc37d4..5c8f636 100644 --- a/spec/unit/parser/functions/tfile.rb +++ b/spec/unit/parser/functions/tfile.rb @@ -44,7 +44,7 @@ describe "the tfile function" do it "should create the path if it does not exist" do File.stubs(:exists?).with('/some_path/aa').returns(false) File.stubs(:directory?).with('/some_path').returns(false) - Puppet::Util.expects(:recmkdir).with("/some_path",0700) + FileUtils.expects(:mkdir_p).with("/some_path",:mode => 0700) FileUtils.expects(:touch).with('/some_path/aa') result = @scope.function_tfile(['/some_path/aa']) result.should == "foo1\nfoo2\n" -- cgit v1.2.3 From 47c385f4794fae0257dd3ab85b637eca93ee38ec Mon Sep 17 00:00:00 2001 From: mh Date: Thu, 3 Jan 2013 16:49:04 +0100 Subject: cleanup module to work better on newer puppet versions --- manifests/defines/concatenated_file.pp | 121 --------------------------------- manifests/defines/config_file.pp | 55 --------------- manifests/defines/line.pp | 54 --------------- manifests/defines/link.pp | 12 ---- manifests/defines/module_dir.pp | 48 ------------- manifests/defines/module_file.pp | 47 ------------- manifests/defines/replace.pp | 40 ----------- manifests/init.pp | 6 -- manifests/module_dir.pp | 34 +++++++++ manifests/module_file.pp | 37 ++++++++++ manifests/moduledir.pp | 25 ++++--- manifests/moduledir/common.pp | 4 +- manifests/moduledir/common/cf.pp | 4 -- 13 files changed, 88 insertions(+), 399 deletions(-) delete mode 100644 manifests/defines/concatenated_file.pp delete mode 100644 manifests/defines/config_file.pp delete mode 100644 manifests/defines/line.pp delete mode 100644 manifests/defines/link.pp delete mode 100644 manifests/defines/module_dir.pp delete mode 100644 manifests/defines/module_file.pp delete mode 100644 manifests/defines/replace.pp delete mode 100644 manifests/init.pp create mode 100644 manifests/module_dir.pp create mode 100644 manifests/module_file.pp delete mode 100644 manifests/moduledir/common/cf.pp diff --git a/manifests/defines/concatenated_file.pp b/manifests/defines/concatenated_file.pp deleted file mode 100644 index 8eed351..0000000 --- a/manifests/defines/concatenated_file.pp +++ /dev/null @@ -1,121 +0,0 @@ -# common/manifests/defines/concatenated_file.pp -- create a file from snippets -# stored in a directory -# -# Copyright (C) 2007 David Schmitt -# 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}"], - } - -} - diff --git a/manifests/defines/config_file.pp b/manifests/defines/config_file.pp deleted file mode 100644 index 2be2460..0000000 --- a/manifests/defines/config_file.pp +++ /dev/null @@ -1,55 +0,0 @@ -# common/manifests/defines/config_file.pp -- create a config file with default permissions -# Copyright (C) 2007 David Schmitt -# 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 } } - } - -} - - diff --git a/manifests/defines/line.pp b/manifests/defines/line.pp deleted file mode 100644 index 44c52a0..0000000 --- a/manifests/defines/line.pp +++ /dev/null @@ -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 -# 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}'" - } - } - } -} diff --git a/manifests/defines/link.pp b/manifests/defines/link.pp deleted file mode 100644 index e8e4a9f..0000000 --- a/manifests/defines/link.pp +++ /dev/null @@ -1,12 +0,0 @@ -# common/manifests/defines/link.pp -- create a link -# -# Copyleft (C) 2009 immerdaadmin -# 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; } -} diff --git a/manifests/defines/module_dir.pp b/manifests/defines/module_dir.pp deleted file mode 100644 index 2852f83..0000000 --- a/manifests/defines/module_dir.pp +++ /dev/null @@ -1,48 +0,0 @@ -# common/manifests/defines/modules_dir.pp -- create a default directory -# for storing module specific information -# -# Copyright (C) 2007 David Schmitt -# 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 } -} diff --git a/manifests/defines/module_file.pp b/manifests/defines/module_file.pp deleted file mode 100644 index 5977b2d..0000000 --- a/manifests/defines/module_file.pp +++ /dev/null @@ -1,47 +0,0 @@ -# common/manifests/defines/modules_file.pp -- use a modules_dir to store module -# specific files -# -# Copyright (C) 2007 David Schmitt -# 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 - } -} diff --git a/manifests/defines/replace.pp b/manifests/defines/replace.pp deleted file mode 100644 index dd8db4d..0000000 --- a/manifests/defines/replace.pp +++ /dev/null @@ -1,40 +0,0 @@ -# common/manifests/defines/replace.pp -- replace a pattern in a file with a string -# Copyright (C) 2007 David Schmitt -# 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", - } -} diff --git a/manifests/init.pp b/manifests/init.pp deleted file mode 100644 index 3a9faf5..0000000 --- a/manifests/init.pp +++ /dev/null @@ -1,6 +0,0 @@ -# common/manifests/init.pp - Define common infrastructure for modules -# Copyright (C) 2007 David Schmitt -# See LICENSE for the full license granted to you. - -import "defines/*.pp" - diff --git a/manifests/module_dir.pp b/manifests/module_dir.pp new file mode 100644 index 0000000..2420da9 --- /dev/null +++ b/manifests/module_dir.pp @@ -0,0 +1,34 @@ +# common/manifests/modules_dir.pp -- create a default directory +# for storing module specific information +# +# Copyright (C) 2007 David Schmitt +# 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; + } +} diff --git a/manifests/module_file.pp b/manifests/module_file.pp new file mode 100644 index 0000000..c1070bc --- /dev/null +++ b/manifests/module_file.pp @@ -0,0 +1,37 @@ +# common/manifests/module_file.pp -- use a modules_dir to store module +# specific files +# +# Copyright (C) 2007 David Schmitt +# 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, + } + } +} diff --git a/manifests/moduledir.pp b/manifests/moduledir.pp index 85c29d6..f779085 100644 --- a/manifests/moduledir.pp +++ b/manifests/moduledir.pp @@ -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'; + } } diff --git a/manifests/moduledir/common.pp b/manifests/moduledir/common.pp index 80e200d..e74c601 100644 --- a/manifests/moduledir/common.pp +++ b/manifests/moduledir/common.pp @@ -1,4 +1,4 @@ +# setup a common dir class common::moduledir::common{ - include ::common::moduledir - module_dir{'common': } + common::module_dir{'common': } } diff --git a/manifests/moduledir/common/cf.pp b/manifests/moduledir/common/cf.pp deleted file mode 100644 index b3c089b..0000000 --- a/manifests/moduledir/common/cf.pp +++ /dev/null @@ -1,4 +0,0 @@ -class common::moduledir::common::cf { - include ::common::moduledir::common - module_dir{'common/cf': } -} -- cgit v1.2.3 From 4b4744e402f5cbc5f9856971d1a64002c7eb0796 Mon Sep 17 00:00:00 2001 From: o Date: Wed, 16 Apr 2014 14:21:32 +0200 Subject: add an array_join function --- lib/puppet/parser/functions/array_join.rb | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 lib/puppet/parser/functions/array_join.rb diff --git a/lib/puppet/parser/functions/array_join.rb b/lib/puppet/parser/functions/array_join.rb new file mode 100644 index 0000000..c715c5d --- /dev/null +++ b/lib/puppet/parser/functions/array_join.rb @@ -0,0 +1,8 @@ +Puppet::Parser::Functions::newfunction(:array_join, + :type => :rvalue, + :doc => "Join a list of strings with an optional delimiter.") do |args| + unless 1..2.include? args.length + raise Puppet::ParseError, 'array_join() takes 1 or 2 arguments' + end + args.first.join args.last +end -- cgit v1.2.3 From 2bd349f9159a2b8d18805658033cfe91f1d5375d Mon Sep 17 00:00:00 2001 From: o Date: Wed, 16 Apr 2014 14:27:21 +0200 Subject: fix syntax --- lib/puppet/parser/functions/array_join.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/puppet/parser/functions/array_join.rb b/lib/puppet/parser/functions/array_join.rb index c715c5d..c5fa1c8 100644 --- a/lib/puppet/parser/functions/array_join.rb +++ b/lib/puppet/parser/functions/array_join.rb @@ -1,7 +1,7 @@ Puppet::Parser::Functions::newfunction(:array_join, :type => :rvalue, :doc => "Join a list of strings with an optional delimiter.") do |args| - unless 1..2.include? args.length + unless (1..2).include?(args.length) raise Puppet::ParseError, 'array_join() takes 1 or 2 arguments' end args.first.join args.last -- cgit v1.2.3 From ee7b4657fc32d87b62b257d267e68063fb232c53 Mon Sep 17 00:00:00 2001 From: mh Date: Fri, 25 Apr 2014 18:06:34 +0200 Subject: remove functions that are either covered with builtin functions or stdlib functions --- lib/puppet/parser/functions/array_include.rb | 11 -------- lib/puppet/parser/functions/array_join.rb | 8 ------ lib/puppet/parser/functions/array_union.rb | 10 ------- spec/unit/parser/functions/array_include.rb | 33 ----------------------- spec/unit/parser/functions/array_union.rb | 39 ---------------------------- 5 files changed, 101 deletions(-) delete mode 100644 lib/puppet/parser/functions/array_include.rb delete mode 100644 lib/puppet/parser/functions/array_join.rb delete mode 100644 lib/puppet/parser/functions/array_union.rb delete mode 100644 spec/unit/parser/functions/array_include.rb delete mode 100644 spec/unit/parser/functions/array_union.rb diff --git a/lib/puppet/parser/functions/array_include.rb b/lib/puppet/parser/functions/array_include.rb deleted file mode 100644 index ce4748d..0000000 --- a/lib/puppet/parser/functions/array_include.rb +++ /dev/null @@ -1,11 +0,0 @@ -Puppet::Parser::Functions::newfunction( - :array_include, - :type => :rvalue, - :doc => "Checks whether an item is included or not - - Example: array_include(['a','b'],'b') -> true - Example: array_include(['a','b'],'c') -> false" -) do |args| - raise Puppet::ParseError, 'array_include() needs two arguments' if args.length != 2 - args[0].include?(args[1]) -end diff --git a/lib/puppet/parser/functions/array_join.rb b/lib/puppet/parser/functions/array_join.rb deleted file mode 100644 index c5fa1c8..0000000 --- a/lib/puppet/parser/functions/array_join.rb +++ /dev/null @@ -1,8 +0,0 @@ -Puppet::Parser::Functions::newfunction(:array_join, - :type => :rvalue, - :doc => "Join a list of strings with an optional delimiter.") do |args| - unless (1..2).include?(args.length) - raise Puppet::ParseError, 'array_join() takes 1 or 2 arguments' - end - args.first.join args.last -end diff --git a/lib/puppet/parser/functions/array_union.rb b/lib/puppet/parser/functions/array_union.rb deleted file mode 100644 index 1ccad3f..0000000 --- a/lib/puppet/parser/functions/array_union.rb +++ /dev/null @@ -1,10 +0,0 @@ -Puppet::Parser::Functions::newfunction( - :array_union, - :type => :rvalue, - :doc => "Unions two arrays - - Example: array_union(['a','b'],['c']) -> ['a','b','c']" -) do |args| - raise Puppet::ParseError, 'array_union() needs two arguments' if args.length != 2 - args[0].to_a | args[1].to_a -end diff --git a/spec/unit/parser/functions/array_include.rb b/spec/unit/parser/functions/array_include.rb deleted file mode 100644 index fecf495..0000000 --- a/spec/unit/parser/functions/array_include.rb +++ /dev/null @@ -1,33 +0,0 @@ -#! /usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../../spec_helper' - -describe "the array_include function" do - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("array_include").should == "function_array_include" - end - - it "should raise a ParseError if there is less than 2 arguments" do - lambda { @scope.function_array_include(["foo"]) }.should( raise_error(Puppet::ParseError)) - end - - it "should raise a ParseError if there is more than 2 arguments" do - lambda { @scope.function_array_include(["foo", "bar", "gazonk"]) }.should( raise_error(Puppet::ParseError)) - end - - it "should return true if an item is present in the array" do - result = @scope.function_array_include(['a','b'],'b') - result.should == true - end - - it "should return false if an item is not present" do - result = @scope.function_array_include(['a','b'],'c') - result.should == false - end - -end diff --git a/spec/unit/parser/functions/array_union.rb b/spec/unit/parser/functions/array_union.rb deleted file mode 100644 index 37df2b3..0000000 --- a/spec/unit/parser/functions/array_union.rb +++ /dev/null @@ -1,39 +0,0 @@ -#! /usr/bin/env ruby - -require File.dirname(__FILE__) + '/../../../spec_helper' - -describe "the array_union function" do - - before :each do - @scope = Puppet::Parser::Scope.new - end - - it "should exist" do - Puppet::Parser::Functions.function("array_union").should == "function_array_union" - end - - it "should raise a ParseError if there is less than 2 arguments" do - lambda { @scope.function_array_union([["foo"]]) }.should( raise_error(Puppet::ParseError)) - end - - it "should raise a ParseError if there is more than 2 arguments" do - lambda { @scope.function_array_union([["foo", "bar", "gazonk"]]) }.should( raise_error(Puppet::ParseError)) - end - - it "should union to arrays" do - result = @scope.function_array_union([['a','b'],['c']]) - result.should(eql(['a','b','c'])) - end - - it "should do nothing if an item is present" do - result = @scope.function_array_union([['a','b'],['b']]) - result.should(eql(['a','b'])) - end - - it "should leave the argument untouched" do - a = ['a','b'] - result = @scope.function_array_union([a,['c']]) - a.should(eql(['a','b'])) - end - -end -- cgit v1.2.3 From 16951d51b112a0e035bd62a403d6a1838d3ff997 Mon Sep 17 00:00:00 2001 From: mh Date: Fri, 25 Apr 2014 18:19:14 +0200 Subject: remove unnecessary file --- files/modules/README | 1 - 1 file changed, 1 deletion(-) delete mode 100644 files/modules/README diff --git a/files/modules/README b/files/modules/README deleted file mode 100644 index bc5bbbd..0000000 --- a/files/modules/README +++ /dev/null @@ -1 +0,0 @@ -this directory contains various data collected for system wide configurations -- cgit v1.2.3 From e9d8147584cf42dceec6d2cdd4314e8a85dc3aeb Mon Sep 17 00:00:00 2001 From: o Date: Thu, 9 Oct 2014 13:32:52 +0200 Subject: add a get_default function --- lib/puppet/parser/functions/get_default.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 lib/puppet/parser/functions/get_default.rb diff --git a/lib/puppet/parser/functions/get_default.rb b/lib/puppet/parser/functions/get_default.rb new file mode 100644 index 0000000..3f4359b --- /dev/null +++ b/lib/puppet/parser/functions/get_default.rb @@ -0,0 +1,15 @@ +# get_default($value, $default) : $value +# +# return $value || $default. +module Puppet::Parser::Functions + newfunction(:get_default, :type => :rvalue) do |args| + value = nil + args.each { |x| + if ! x.nil? and x.length > 0 + value = x + break + end + } + return value + end +end -- cgit v1.2.3