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