Skip to content

Commit 8a8c09e

Browse files
Chad MetcalfJeff McCune
authored andcommitted
Add an ensure_packages function.
Its often the case that modules need to install a handful of packages. In some cases its worth breaking these dependencies out into their own modules (e.g., Java). In others it makes more sense to keep them in the module. This can be problematic when multiple modules depend on common packages (git, python ruby, etc). ensure_resource was a good first step towards solving this problem. ensure_resource does not handle arrays and for 3 or more packages stamping out ensure_resource declarations is tedious. ensure_packages is a convenience function that takes an array of packages and wraps calls to ensure_resource. Currently users cannot specify package versions. But the function could be extended to use a hash if that functionality would be useful.
1 parent 6f76d8d commit 8a8c09e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# ensure_packages.rb
3+
#
4+
require 'puppet/parser/functions'
5+
6+
module Puppet::Parser::Functions
7+
newfunction(:ensure_packages, :type => :statement, :doc => <<-EOS
8+
Takes a list of packages and only installs them if they don't already exist.
9+
EOS
10+
) do |arguments|
11+
12+
raise(Puppet::ParseError, "ensure_packages(): Wrong number of arguments " +
13+
"given (#{arguments.size} for 1)") if arguments.size != 1
14+
raise(Puppet::ParseError, "ensure_packages(): Requires array " +
15+
"given (#{arguments[0].class})") if !arguments[0].kind_of?(Array)
16+
17+
Puppet::Parser::Functions.function(:ensure_resource)
18+
arguments[0].each { |package_name|
19+
function_ensure_resource(['package', package_name, {'ensure' => 'present' } ])
20+
}
21+
end
22+
end
23+
24+
# vim: set ts=2 sw=2 et :

0 commit comments

Comments
 (0)