From 41bc722139028929b9ab1f9a14b318d1a23206f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Pinson?= Date: Fri, 18 Jan 2013 21:42:54 +0100 Subject: validate_augeas: Ensure augeas handler gets closed --- lib/puppet/parser/functions/validate_augeas.rb | 55 ++++++++++++++------------ 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'lib/puppet/parser/functions/validate_augeas.rb') diff --git a/lib/puppet/parser/functions/validate_augeas.rb b/lib/puppet/parser/functions/validate_augeas.rb index 01a2e91..273b954 100644 --- a/lib/puppet/parser/functions/validate_augeas.rb +++ b/lib/puppet/parser/functions/validate_augeas.rb @@ -17,7 +17,7 @@ module Puppet::Parser::Functions Or if you wanted to ensure that no users used the '/bin/barsh' shell, you could use: - + validate_augeas($passwdcontent, 'Passwd.lns', ['$file/*[shell="/bin/barsh"]'] If a fourth argument is specified, this will be the error message raised and @@ -36,35 +36,38 @@ module Puppet::Parser::Functions require 'augeas' aug = Augeas::open(nil, nil, Augeas::NO_MODL_AUTOLOAD) + begin + content = args[0] - content = args[0] - - # Test content in a temporary file - tmpfile = Tempfile.new("validate_augeas") - tmpfile.write(content) - tmpfile.close + # Test content in a temporary file + tmpfile = Tempfile.new("validate_augeas") + tmpfile.write(content) + tmpfile.close - # Check for syntax - lens = args[1] - aug.transform( - :lens => lens, - :name => 'Validate_augeas', - :incl => tmpfile.path - ) - aug.load! + # Check for syntax + lens = args[1] + aug.transform( + :lens => lens, + :name => 'Validate_augeas', + :incl => tmpfile.path + ) + aug.load! - unless aug.match("/augeas/files#{tmpfile.path}//error").empty? - error = aug.get("/augeas/files#{tmpfile.path}//error/message") - msg += " with error: #{error}" - raise Puppet::ParseError, (msg) - end + unless aug.match("/augeas/files#{tmpfile.path}//error").empty? + error = aug.get("/augeas/files#{tmpfile.path}//error/message") + msg += " with error: #{error}" + raise Puppet::ParseError, (msg) + end - # Launch unit tests - tests = args[2] || [] - aug.defvar('file', "/files#{tmpfile.path}") - tests.each do |t| - msg += " testing path #{t}" - raise Puppet::ParseError, (msg) unless aug.match(t).empty? + # Launch unit tests + tests = args[2] || [] + aug.defvar('file', "/files#{tmpfile.path}") + tests.each do |t| + msg += " testing path #{t}" + raise Puppet::ParseError, (msg) unless aug.match(t).empty? + end + ensure + aug.close end end end -- cgit v1.2.3