]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
validate_augeas: Ensure augeas handler gets closed
authorRaphaël Pinson <raphael.pinson@camptocamp.com>
Fri, 18 Jan 2013 20:42:54 +0000 (21:42 +0100)
committerRaphaël Pinson <raphael.pinson@camptocamp.com>
Fri, 18 Jan 2013 21:02:13 +0000 (22:02 +0100)
lib/puppet/parser/functions/validate_augeas.rb

index 01a2e91a88741c0d61b53fb8eab072638de88c97..273b9548127bce53e7b2cd23a5ef48f3a5e08b0f 100644 (file)
@@ -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