Bug #10934

versioncmp called as a function in a template should accept multiple arguments, not an array.

Added by James Turnbull 6 months ago. Updated 6 months ago.

Status:Accepted Start date:11/18/2011
Priority:Normal Due date:
Assignee:- % Done:

0%

Category:functions
Target version:3.X
Affected Puppet version: Branch:
Keywords:versioncmp
Votes: 0

Description

Nigel: It’s possible this is a wider problem with all functions in templates, but it’s been observed here at least.

The primary point is that in a template we should have the function work exactly the same way as it does in the DSL, with multiple arguments, and not an array.

This should work without warnings in Ruby 1.8.x and 1.9.x


$version1="1.2.3" 
$version2="2.0" 
if versioncmp($version1,$version2) > 0 { 
notify{"${version1} > ${version2}": } 
} else { 
notify{"${version1} <= ${version2}": } 
}

Works nicely.. Unfortunately it doesn’t want to work with arrays:

$version1="1.2.3" 
$version2="2.0" 
$versions=[ $version1, $version2 ] 
if versioncmp($versions) > 0 { 
notify{"${version1} > ${version2}": } 
} else { 
notify{"${version1} <= ${version2}": } 
}

as it errors out with “versioncmp should have 2 arguments”.

Now we try to do the same in templates..

$version1="1.2.3" 
$version2="2.0" 
$versions=[ $version1, $version2 ] 
$out=inline_template('<%= (scope.function_versioncmp(version1,version2)>0) ? "#{version1} > #{version2}" : "#{version1} <= #{version2}" %>') 
notify { $out: }

works nicely with REE 1.8.7 although produces the following warning: “/usr/local/ree/lib/ruby/site_ruby/1.8/puppet/parser/functions/versioncmp.rb:30: warning: multiple values for a block parameter (2 for 1) from /usr/local/ree/lib/ruby/site_ruby/1.8/puppet/parser/scope.rb:428”. Unfortunately with Ruby 1.9 this warning becomes an error:

Error 400 on SERVER: Failed to parse inline template: wrong number of arguments (2 for 1). 
Using the following recipe gets rid of warning and makes REE 1.8.7 and Ruby 1.9 happy:
$version1="1.2.3" 
$version2="2.0" 
$versions=[ $version1, $version2 ] 
$out=inline_template('<%= (scope.function_versioncmp(versions)>0) ? "#{version1} > #{version2}" : "#{version1} <= #{version2}" %>') 
notify { $out: }

History

Updated by Nigel Kersten 6 months ago

  • Status changed from Needs Decision to Rejected

The function supports two arguments, and there is a difference between a single argument that is an array, and two individual arguments.

We’ve had problems in the past where we’ve tried to support both individual arguments and an array that contains those arguments, and we’re not going to head down that path again.

The fact that it works in 1.8.7 isn’t intentional (as evident by the warning)

You need to dereference the array elements as $versions[0] and $versions[1].

Updated by Nigel Kersten 6 months ago

  • Subject changed from versioncmp input parameters inconsistent to versioncmp called as a function in a template should accept multiple arguments, not an array.
  • Description updated (diff)
  • Status changed from Rejected to Accepted
  • Assignee deleted (Nigel Kersten)
  • Priority changed from High to Normal
  • Target version set to 3.X

Also available in: Atom PDF