2013-02-05 09:01:48 +01:00
|
|
|
require 'puppet/util/execution'
|
2014-10-25 01:35:34 +02:00
|
|
|
require 'tempfile'
|
2013-02-05 09:01:48 +01:00
|
|
|
|
2012-12-06 11:01:19 +01:00
|
|
|
module Puppet::Parser::Functions
|
|
|
|
newfunction(:validate_cmd, :doc => <<-'ENDHEREDOC') do |args|
|
|
|
|
Perform validation of a string with an external command.
|
|
|
|
The first argument of this function should be a string to
|
|
|
|
test, and the second argument should be a path to a test command
|
2014-12-09 15:20:31 +01:00
|
|
|
taking a % as a placeholder for the file path (will default to the end).
|
|
|
|
If the command, launched against a tempfile containing the passed string,
|
|
|
|
returns a non-null value, compilation will abort with a parse error.
|
2012-12-06 11:01:19 +01:00
|
|
|
|
|
|
|
If a third argument is specified, this will be the error message raised and
|
|
|
|
seen by the user.
|
|
|
|
|
|
|
|
A helpful error message can be returned like this:
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
2014-12-09 15:20:31 +01:00
|
|
|
# Defaults to end of path
|
2012-12-06 11:01:19 +01:00
|
|
|
validate_cmd($sudoerscontent, '/usr/sbin/visudo -c -f', 'Visudo failed to validate sudoers content')
|
|
|
|
|
2014-12-09 15:20:31 +01:00
|
|
|
# % as file location
|
|
|
|
validate_cmd($haproxycontent, '/usr/sbin/haproxy -f % -c', 'Haproxy failed to validate config content')
|
|
|
|
|
2012-12-06 11:01:19 +01:00
|
|
|
ENDHEREDOC
|
|
|
|
if (args.length < 2) or (args.length > 3) then
|
|
|
|
raise Puppet::ParseError, ("validate_cmd(): wrong number of arguments (#{args.length}; must be 2 or 3)")
|
|
|
|
end
|
|
|
|
|
|
|
|
msg = args[2] || "validate_cmd(): failed to validate content with command #{args[1].inspect}"
|
|
|
|
|
|
|
|
content = args[0]
|
|
|
|
checkscript = args[1]
|
|
|
|
|
|
|
|
# Test content in a temporary file
|
|
|
|
tmpfile = Tempfile.new("validate_cmd")
|
2013-01-18 21:29:29 +01:00
|
|
|
begin
|
|
|
|
tmpfile.write(content)
|
2013-09-09 16:20:36 +02:00
|
|
|
tmpfile.close
|
2014-12-09 15:20:31 +01:00
|
|
|
|
2014-12-19 00:08:13 +01:00
|
|
|
if checkscript =~ /\s%(\s|$)/
|
2014-12-09 15:20:31 +01:00
|
|
|
check_with_correct_location = checkscript.gsub(/%/,tmpfile.path)
|
|
|
|
else
|
|
|
|
check_with_correct_location = "#{checkscript} #{tmpfile.path}"
|
|
|
|
end
|
|
|
|
|
2013-02-07 08:56:52 +01:00
|
|
|
if Puppet::Util::Execution.respond_to?('execute')
|
2014-12-09 15:20:31 +01:00
|
|
|
Puppet::Util::Execution.execute(check_with_correct_location)
|
2013-02-07 08:56:52 +01:00
|
|
|
else
|
2014-12-09 15:20:31 +01:00
|
|
|
Puppet::Util.execute(check_with_correct_location)
|
2013-02-07 08:56:52 +01:00
|
|
|
end
|
2013-02-05 09:01:48 +01:00
|
|
|
rescue Puppet::ExecutionFailure => detail
|
|
|
|
msg += "\n#{detail}"
|
|
|
|
raise Puppet::ParseError, msg
|
2016-03-29 01:59:54 +02:00
|
|
|
rescue StandardError => detail
|
2014-10-30 04:03:07 +01:00
|
|
|
msg += "\n#{detail.class.name} #{detail}"
|
2014-10-29 00:46:16 +01:00
|
|
|
raise Puppet::ParseError, msg
|
2013-01-18 21:29:29 +01:00
|
|
|
ensure
|
|
|
|
tmpfile.unlink
|
|
|
|
end
|
2012-12-06 11:01:19 +01:00
|
|
|
end
|
|
|
|
end
|