Merge remote branch 'shared/master'
Conflicts: lib/puppet/parser/functions/split.rb
This commit is contained in:
commit
1f1e5599b7
9 changed files with 110 additions and 44 deletions
11
lib/puppet/parser/functions/array_del.rb
Normal file
11
lib/puppet/parser/functions/array_del.rb
Normal file
|
@ -0,0 +1,11 @@
|
|||
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
|
10
lib/puppet/parser/functions/join.rb
Normal file
10
lib/puppet/parser/functions/join.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
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
|
|
@ -1,9 +0,0 @@
|
|||
# return the sha1 hash
|
||||
require 'digest/sha1'
|
||||
|
||||
module Puppet::Parser::Functions
|
||||
newfunction(:sha1, :type => :rvalue) do |args|
|
||||
Digest::SHA1.hexdigest(args[0])
|
||||
end
|
||||
end
|
||||
|
|
@ -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
|
10
lib/puppet/parser/functions/uniq_flatten.rb
Normal file
10
lib/puppet/parser/functions/uniq_flatten.rb
Normal file
|
@ -0,0 +1,10 @@
|
|||
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
|
|
@ -28,6 +28,7 @@
|
|||
# dir => "/etc/some.conf.d",
|
||||
# }
|
||||
define concatenated_file (
|
||||
$ensure = 'present',
|
||||
# where the snippets are located
|
||||
$dir = '',
|
||||
# a file with content to prepend
|
||||
|
@ -50,6 +51,10 @@ define concatenated_file (
|
|||
} else {
|
||||
file {
|
||||
$dir_real:
|
||||
ensure => $ensure ? {
|
||||
'present' => directory,
|
||||
default => $ensure
|
||||
},
|
||||
source => "puppet:///modules/common/empty",
|
||||
checksum => mtime,
|
||||
ignore => '.ignore',
|
||||
|
@ -61,40 +66,41 @@ define concatenated_file (
|
|||
|
||||
file {
|
||||
$tmp_file:
|
||||
ensure => present, checksum => md5,
|
||||
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 => present, checksum => md5,
|
||||
ensure => $ensure, checksum => md5,
|
||||
source => $tmp_file,
|
||||
mode => $mode, owner => $owner, group => $group,
|
||||
require => File[$tmp_file];
|
||||
}
|
||||
|
||||
# if there is a header or footer file, add it
|
||||
$additional_cmd = $header ? {
|
||||
if $ensure == 'present' {
|
||||
# if there is a header or footer file, add it
|
||||
$additional_cmd = $header ? {
|
||||
'' => $footer ? {
|
||||
'' => '',
|
||||
default => "| cat - '${footer}' "
|
||||
'' => '',
|
||||
default => "| cat - '${footer}' "
|
||||
},
|
||||
default => $footer ? {
|
||||
'' => "| cat '${header}' - ",
|
||||
default => "| cat '${header}' - '${footer}' "
|
||||
'' => "| cat '${header}' - ",
|
||||
default => "| cat '${header}' - '${footer}' "
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
# use >| to force clobbering the target file
|
||||
exec { "concat_${name}":
|
||||
# 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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
6
spec/spec.opts
Normal file
6
spec/spec.opts
Normal file
|
@ -0,0 +1,6 @@
|
|||
--format
|
||||
s
|
||||
--colour
|
||||
--loadby
|
||||
mtime
|
||||
--backtrace
|
16
spec/spec_helper.rb
Normal file
16
spec/spec_helper.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
require 'pathname'
|
||||
dir = Pathname.new(__FILE__).parent
|
||||
$LOAD_PATH.unshift(dir, dir + 'lib', dir + '../lib')
|
||||
require 'puppet'
|
||||
gem 'rspec', '>= 1.2.9'
|
||||
require 'spec/autorun'
|
||||
|
||||
Dir[File.join(File.dirname(__FILE__), 'support', '*.rb')].each do |support_file|
|
||||
require support_file
|
||||
end
|
||||
|
||||
# We need this because the RAL uses 'should' as a method. This
|
||||
# allows us the same behaviour but with a different method name.
|
||||
class Object
|
||||
alias :must :should
|
||||
end
|
39
spec/unit/parser/functions/array_del.rb
Normal file
39
spec/unit/parser/functions/array_del.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
#! /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
|
Loading…
Reference in a new issue