1 require 'puppet/util/execution'
4 module Puppet::Parser::Functions
5 newfunction(:validate_cmd, :doc => <<-'ENDHEREDOC') do |args|
6 Perform validation of a string with an external command.
7 The first argument of this function should be a string to
8 test, and the second argument should be a path to a test command
9 taking a % as a placeholder for the file path (will default to the end).
10 If the command, launched against a tempfile containing the passed string,
11 returns a non-null value, compilation will abort with a parse error.
13 If a third argument is specified, this will be the error message raised and
16 A helpful error message can be returned like this:
20 # Defaults to end of path
21 validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content')
24 validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to validate config content')
27 if (args.length < 2) or (args.length > 3) then
28 raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)")
31 msg = args[2] || "validate_cmd(): failed to validate content with command #{args[1].inspect}"
36 # Test content in a temporary file
37 tmpfile = Tempfile.new("validate_cmd")
39 tmpfile.write(content)
42 if checkscript =~ /\s%(\s|$)/
43 check_with_correct_location = checkscript.gsub(/%/,tmpfile.path)
45 check_with_correct_location = "#{checkscript} #{tmpfile.path}"
48 if Puppet::Util::Execution.respond_to?('execute')
49 Puppet::Util::Execution.execute(check_with_correct_location)
51 Puppet::Util.execute(check_with_correct_location)
53 rescue Puppet::ExecutionFailure => detail
55 raise Puppet::ParseError, msg
56 rescue StandardError => detail
57 msg += "\n#{detail.class.name} #{detail}"
58 raise Puppet::ParseError, msg