summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/facter/facter_dot_d.rb2
-rw-r--r--lib/puppet/parser/functions/concat.rb10
-rw-r--r--lib/puppet/parser/functions/ensure_packages.rb16
-rw-r--r--lib/puppet/parser/functions/floor.rb7
-rw-r--r--lib/puppet/parser/functions/is_domain_name.rb3
-rw-r--r--lib/puppet/parser/functions/is_float.rb3
-rw-r--r--lib/puppet/parser/functions/is_function_available.rb3
-rw-r--r--lib/puppet/provider/file_line/ruby.rb16
-rw-r--r--lib/puppet/type/file_line.rb4
9 files changed, 49 insertions, 15 deletions
diff --git a/lib/facter/facter_dot_d.rb b/lib/facter/facter_dot_d.rb
index e414b20..2c096b0 100644
--- a/lib/facter/facter_dot_d.rb
+++ b/lib/facter/facter_dot_d.rb
@@ -40,7 +40,7 @@ class Facter::Util::DotD
def txt_parser(file)
File.readlines(file).each do |line|
- if line =~ /^(.+)=(.+)$/
+ if line =~ /^([^=]+)=(.+)$/
var = $1; val = $2
Facter.add(var) do
diff --git a/lib/puppet/parser/functions/concat.rb b/lib/puppet/parser/functions/concat.rb
index c86aa00..6c86382 100644
--- a/lib/puppet/parser/functions/concat.rb
+++ b/lib/puppet/parser/functions/concat.rb
@@ -23,12 +23,16 @@ Would result in:
a = arguments[0]
b = arguments[1]
- # Check that both args are arrays.
- unless a.is_a?(Array) and b.is_a?(Array)
+ # Check that the first parameter is an array
+ unless a.is_a?(Array)
raise(Puppet::ParseError, 'concat(): Requires array to work with')
end
- result = a.concat(b)
+ if b.is_a?(Array)
+ result = a.concat(b)
+ else
+ result = a << b
+ end
return result
end
diff --git a/lib/puppet/parser/functions/ensure_packages.rb b/lib/puppet/parser/functions/ensure_packages.rb
index 1e0f225..f1da4aa 100644
--- a/lib/puppet/parser/functions/ensure_packages.rb
+++ b/lib/puppet/parser/functions/ensure_packages.rb
@@ -5,19 +5,29 @@
module Puppet::Parser::Functions
newfunction(:ensure_packages, :type => :statement, :doc => <<-EOS
Takes a list of packages and only installs them if they don't already exist.
+It optionally takes a hash as a second parameter that will be passed as the
+third argument to the ensure_resource() function.
EOS
) do |arguments|
- if arguments.size != 1
+ if arguments.size > 2 or arguments.size == 0
raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " +
- "given (#{arguments.size} for 1)")
+ "given (#{arguments.size} for 1 or 2)")
+ elsif arguments.size == 2 and !arguments[1].is_a?(Hash)
+ raise(Puppet::ParseError, 'ensure_packages(): Requires second argument to be a Hash')
end
packages = Array(arguments[0])
+ if arguments[1]
+ defaults = { 'ensure' => 'present' }.merge(arguments[1])
+ else
+ defaults = { 'ensure' => 'present' }
+ end
+
Puppet::Parser::Functions.function(:ensure_resource)
packages.each { |package_name|
- function_ensure_resource(['package', package_name, {'ensure' => 'present' } ])
+ function_ensure_resource(['package', package_name, defaults ])
}
end
end
diff --git a/lib/puppet/parser/functions/floor.rb b/lib/puppet/parser/functions/floor.rb
index a401923..9a6f014 100644
--- a/lib/puppet/parser/functions/floor.rb
+++ b/lib/puppet/parser/functions/floor.rb
@@ -8,7 +8,12 @@ module Puppet::Parser::Functions
raise(Puppet::ParseError, "floor(): Wrong number of arguments " +
"given (#{arguments.size} for 1)") if arguments.size != 1
- arg = arguments[0]
+ begin
+ arg = Float(arguments[0])
+ rescue TypeError, ArgumentError => e
+ raise(Puppet::ParseError, "floor(): Wrong argument type " +
+ "given (#{arguments[0]} for Numeric)")
+ end
raise(Puppet::ParseError, "floor(): Wrong argument type " +
"given (#{arg.class} for Numeric)") if arg.is_a?(Numeric) == false
diff --git a/lib/puppet/parser/functions/is_domain_name.rb b/lib/puppet/parser/functions/is_domain_name.rb
index 5826dc0..b3fee96 100644
--- a/lib/puppet/parser/functions/is_domain_name.rb
+++ b/lib/puppet/parser/functions/is_domain_name.rb
@@ -20,6 +20,9 @@ Returns true if the string passed to this function is a syntactically correct do
label_min_length=1
label_max_length=63
+ # Only allow string types
+ return false unless domain.is_a?(String)
+
# Allow ".", it is the top level domain
return true if domain == '.'
diff --git a/lib/puppet/parser/functions/is_float.rb b/lib/puppet/parser/functions/is_float.rb
index 911f3c2..a2da943 100644
--- a/lib/puppet/parser/functions/is_float.rb
+++ b/lib/puppet/parser/functions/is_float.rb
@@ -15,6 +15,9 @@ Returns true if the variable passed to this function is a float.
value = arguments[0]
+ # Only allow Numeric or String types
+ return false unless value.is_a?(Numeric) or value.is_a?(String)
+
if value != value.to_f.to_s and !value.is_a? Float then
return false
else
diff --git a/lib/puppet/parser/functions/is_function_available.rb b/lib/puppet/parser/functions/is_function_available.rb
index 6cbd35c..6da82c8 100644
--- a/lib/puppet/parser/functions/is_function_available.rb
+++ b/lib/puppet/parser/functions/is_function_available.rb
@@ -15,6 +15,9 @@ true if the function exists, false if not.
"given #{arguments.size} for 1")
end
+ # Only allow String types
+ return false unless arguments[0].is_a?(String)
+
function = Puppet::Parser::Functions.function(arguments[0].to_sym)
function.is_a?(String) and not function.empty?
end
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb
index 94e7fac..2cbd172 100644
--- a/lib/puppet/provider/file_line/ruby.rb
+++ b/lib/puppet/provider/file_line/ruby.rb
@@ -9,7 +9,9 @@ Puppet::Type.type(:file_line).provide(:ruby) do
if resource[:match]
handle_create_with_match
elsif resource[:after]
- handle_create_with_after
+ handle_create_with_position :after
+ elsif resource[:before]
+ handle_create_with_position :before
else
append_line
end
@@ -49,29 +51,29 @@ Puppet::Type.type(:file_line).provide(:ruby) do
end
end
- def handle_create_with_after
- regex = Regexp.new(resource[:after])
+ def handle_create_with_position(position)
+ regex = resource[position] ? Regexp.new(resource[position]) : nil
count = lines.count {|l| l.match(regex)}
case count
- when 1 # find the line to put our line after
+ when 1 # find the line to put our line before/after
File.open(resource[:path], 'w') do |fh|
lines.each do |l|
- fh.puts(l)
+ fh.puts(l) if position == :after
if regex.match(l) then
fh.puts(resource[:line])
end
+ fh.puts(l) if position == :before
end
end
when 0 # append the line to the end of the file
append_line
else
- raise Puppet::Error, "#{count} lines match pattern '#{resource[:after]}' in file '#{resource[:path]}'. One or no line must match the pattern."
+ raise Puppet::Error, "#{count} lines match pattern '#{resource[position]}' in file '#{resource[:path]}'. One or no line must match the pattern."
end
end
- ##
# append the line to the file.
#
# @api private
diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb
index 323fc4c..bc6745f 100644
--- a/lib/puppet/type/file_line.rb
+++ b/lib/puppet/type/file_line.rb
@@ -46,6 +46,10 @@ Puppet::Type.newtype(:file_line) do
desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)'
end
+ newparam(:before) do
+ desc 'An optional value used to specify the line before which we will add any new lines. (Existing lines are added in place)'
+ end
+
newparam(:line) do
desc 'The line to be appended to the file located by the path parameter.'
end