source: other-projects/hathitrust/vagrant-hadoop-cluster/trunk/modules/stdlib/lib/puppet/parser/functions/validate_augeas.rb@ 30903

Last change on this file since 30903 was 30903, checked in by davidb, 7 years ago

Vagrant provisioning files for a 4-node Hadoop cluster. See README.txt for more details

File size: 2.7 KB
Line 
1require 'tempfile'
2
3module Puppet::Parser::Functions
4 newfunction(:validate_augeas, :doc => <<-'ENDHEREDOC') do |args|
5 Perform validation of a string using an Augeas lens
6 The first argument of this function should be a string to
7 test, and the second argument should be the name of the Augeas lens to use.
8 If Augeas fails to parse the string with the lens, the compilation will
9 abort with a parse error.
10
11 A third argument can be specified, listing paths which should
12 not be found in the file. The `$file` variable points to the location
13 of the temporary file being tested in the Augeas tree.
14
15 For example, if you want to make sure your passwd content never contains
16 a user `foo`, you could write:
17
18 validate_augeas($passwdcontent, 'Passwd.lns', ['$file/foo'])
19
20 Or if you wanted to ensure that no users used the '/bin/barsh' shell,
21 you could use:
22
23 validate_augeas($passwdcontent, 'Passwd.lns', ['$file/*[shell="/bin/barsh"]']
24
25 If a fourth argument is specified, this will be the error message raised and
26 seen by the user.
27
28 A helpful error message can be returned like this:
29
30 validate_augeas($sudoerscontent, 'Sudoers.lns', [], 'Failed to validate sudoers content with Augeas')
31
32 ENDHEREDOC
33 unless Puppet.features.augeas?
34 raise Puppet::ParseError, ("validate_augeas(): this function requires the augeas feature. See http://docs.puppetlabs.com/guides/augeas.html#pre-requisites for how to activate it.")
35 end
36
37 if (args.length < 2) or (args.length > 4) then
38 raise Puppet::ParseError, ("validate_augeas(): wrong number of arguments (#{args.length}; must be 2, 3, or 4)")
39 end
40
41 msg = args[3] || "validate_augeas(): Failed to validate content against #{args[1].inspect}"
42
43 require 'augeas'
44 aug = Augeas::open(nil, nil, Augeas::NO_MODL_AUTOLOAD)
45 begin
46 content = args[0]
47
48 # Test content in a temporary file
49 tmpfile = Tempfile.new("validate_augeas")
50 begin
51 tmpfile.write(content)
52 ensure
53 tmpfile.close
54 end
55
56 # Check for syntax
57 lens = args[1]
58 aug.transform(
59 :lens => lens,
60 :name => 'Validate_augeas',
61 :incl => tmpfile.path
62 )
63 aug.load!
64
65 unless aug.match("/augeas/files#{tmpfile.path}//error").empty?
66 error = aug.get("/augeas/files#{tmpfile.path}//error/message")
67 msg += " with error: #{error}"
68 raise Puppet::ParseError, (msg)
69 end
70
71 # Launch unit tests
72 tests = args[2] || []
73 aug.defvar('file', "/files#{tmpfile.path}")
74 tests.each do |t|
75 msg += " testing path #{t}"
76 raise Puppet::ParseError, (msg) unless aug.match(t).empty?
77 end
78 ensure
79 aug.close
80 tmpfile.unlink
81 end
82 end
83end
Note: See TracBrowser for help on using the repository browser.