diff options
-rw-r--r-- | lib/puppet/parser/functions/array_include.rb | 11 | ||||
-rw-r--r-- | lib/puppet/parser/functions/gsub.rb | 17 | ||||
-rw-r--r-- | lib/puppet/parser/functions/split.rb | 23 | ||||
-rw-r--r-- | lib/puppet/parser/functions/tfile.rb | 18 | ||||
-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/line.pp | 1 | ||||
-rw-r--r-- | manifests/defines/module_dir.pp | 9 | ||||
-rw-r--r-- | manifests/defines/module_file.pp | 2 | ||||
-rw-r--r-- | manifests/defines/replace.pp | 2 | ||||
-rw-r--r-- | manifests/init.pp | 1 | ||||
-rw-r--r-- | spec/unit/parser/functions/array_include.rb | 33 | ||||
-rw-r--r-- | spec/unit/parser/functions/tfile.rb | 54 |
13 files changed, 135 insertions, 61 deletions
diff --git a/lib/puppet/parser/functions/array_include.rb b/lib/puppet/parser/functions/array_include.rb new file mode 100644 index 0000000..ce4748d --- /dev/null +++ b/lib/puppet/parser/functions/array_include.rb @@ -0,0 +1,11 @@ +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/gsub.rb b/lib/puppet/parser/functions/gsub.rb deleted file mode 100644 index e2410ff..0000000 --- a/lib/puppet/parser/functions/gsub.rb +++ /dev/null @@ -1,17 +0,0 @@ -module Puppet::Parser::Functions - # thin wrapper around the ruby gsub function - # gsub($string, $pattern, $replacement) will replace all occurrences of - # $pattern in $string with $replacement. $string can be either a singel - # value or an array. In the latter case, each element of the array will - # be processed in turn. - newfunction(:gsub, :type => :rvalue) do |args| - if args[0].is_a?(Array) - args[0].collect do |val| - val.gsub(/#{args[1]}/, args[2]) - end - else - args[0].gsub(/#{args[1]}/, args[2]) - end - end -end - diff --git a/lib/puppet/parser/functions/split.rb b/lib/puppet/parser/functions/split.rb deleted file mode 100644 index bffecc1..0000000 --- a/lib/puppet/parser/functions/split.rb +++ /dev/null @@ -1,23 +0,0 @@ -# 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/lib/puppet/parser/functions/tfile.rb b/lib/puppet/parser/functions/tfile.rb new file mode 100644 index 0000000..a984892 --- /dev/null +++ b/lib/puppet/parser/functions/tfile.rb @@ -0,0 +1,18 @@ +Puppet::Parser::Functions::newfunction( + :tfile, + :type => :rvalue, + :doc => "Returns the content of a file. If the file or the path does not + yet exist, it will create the path and touch the file." +) do |args| + raise Puppet::ParseError, 'tfile() needs one argument' if args.length != 1 + path = args.to_a.first + unless File.exists?(path) + dir = File.dirname(path) + unless File.directory?(dir) + Puppet::Util.recmkdir(dir,0700) + end + require 'fileutils' + FileUtils.touch(path) + end + File.read(path) +end diff --git a/manifests/defines/append_if_no_such_line.pp b/manifests/defines/append_if_no_such_line.pp deleted file mode 100644 index 6ccf9f9..0000000 --- a/manifests/defines/append_if_no_such_line.pp +++ /dev/null @@ -1,14 +0,0 @@ -# -# 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 375e25c..2be2460 100644 --- a/manifests/defines/config_file.pp +++ b/manifests/defines/config_file.pp @@ -3,7 +3,7 @@ # 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", @@ -14,11 +14,10 @@ # 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/line.pp b/manifests/defines/line.pp index 44c52a0..bbe0a54 100644 --- a/manifests/defines/line.pp +++ b/manifests/defines/line.pp @@ -36,6 +36,7 @@ # # Code with fixes gathered at # http://reductivelabs.com/trac/puppet/wiki/Recipes/SimpleText +>>>>>>> sarava/master define line($file, $line, $ensure = 'present') { case $ensure { default : { err ( "unknown ensure value '${ensure}'" ) } diff --git a/manifests/defines/module_dir.pp b/manifests/defines/module_dir.pp index 613cc49..227fe71 100644 --- a/manifests/defines/module_dir.pp +++ b/manifests/defines/module_dir.pp @@ -4,6 +4,15 @@ # 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" ]: } diff --git a/manifests/defines/module_file.pp b/manifests/defines/module_file.pp index 44f3968..5977b2d 100644 --- a/manifests/defines/module_file.pp +++ b/manifests/defines/module_file.pp @@ -4,6 +4,8 @@ # 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:///...", diff --git a/manifests/defines/replace.pp b/manifests/defines/replace.pp index f7da3b4..dd8db4d 100644 --- a/manifests/defines/replace.pp +++ b/manifests/defines/replace.pp @@ -2,7 +2,7 @@ # 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 +# 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. diff --git a/manifests/init.pp b/manifests/init.pp index 3770897..3a9faf5 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -3,3 +3,4 @@ # See LICENSE for the full license granted to you. import "defines/*.pp" + diff --git a/spec/unit/parser/functions/array_include.rb b/spec/unit/parser/functions/array_include.rb new file mode 100644 index 0000000..fecf495 --- /dev/null +++ b/spec/unit/parser/functions/array_include.rb @@ -0,0 +1,33 @@ +#! /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/tfile.rb b/spec/unit/parser/functions/tfile.rb new file mode 100644 index 0000000..1cc37d4 --- /dev/null +++ b/spec/unit/parser/functions/tfile.rb @@ -0,0 +1,54 @@ +#! /usr/bin/env ruby + +require File.dirname(__FILE__) + '/../../../spec_helper' +require 'mocha' + +describe "the tfile function" do + + before :each do + @scope = Puppet::Parser::Scope.new + end + + it "should exist" do + Puppet::Parser::Functions.function("tfile").should == "function_tfile" + end + + it "should raise a ParseError if there is less than 1 arguments" do + lambda { @scope.function_tfile([]) }.should( raise_error(Puppet::ParseError)) + end + + it "should raise a ParseError if there is more than 1 arguments" do + lambda { @scope.function_tfile(["bar", "gazonk"]) }.should( raise_error(Puppet::ParseError)) + end + + describe "when executed properly" do + + before :each do + File.stubs(:read).with('/some_path/aa').returns("foo1\nfoo2\n") + end + + it "should return the content of the file" do + File.stubs(:exists?).with('/some_path/aa').returns(true) + result = @scope.function_tfile(['/some_path/aa']) + result.should == "foo1\nfoo2\n" + end + + it "should touch a file if it does not exist" do + File.stubs(:exists?).with('/some_path/aa').returns(false) + File.stubs(:directory?).with('/some_path').returns(true) + FileUtils.expects(:touch).with('/some_path/aa') + result = @scope.function_tfile(['/some_path/aa']) + result.should == "foo1\nfoo2\n" + end + + 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(:touch).with('/some_path/aa') + result = @scope.function_tfile(['/some_path/aa']) + result.should == "foo1\nfoo2\n" + end + end + +end |