|
1 # Definition: augeas::lens |
|
2 # |
|
3 # Deploy an Augeas lens (and its test file). |
|
4 # Check the lens (and run the unit tests) automatically and remove the files if |
|
5 # the checks fail. |
|
6 # |
|
7 # Parameters: |
|
8 # ['ensure'] - present/absent |
|
9 # ['lens_content'] - the content of the lens |
|
10 # ['lens_source'] - the source for the lens |
|
11 # ['test_content'] - optionally, the content of the test |
|
12 # ['test_source'] - optionally, the source for the test file. |
|
13 # ['stock_since'] - optionally, indicate in which version of Augeas |
|
14 # the lens became stock, so it will not be deployed |
|
15 # above that version. |
|
16 # |
|
17 # Example usage: |
|
18 # |
|
19 # augeas::lens { 'networkmanager': |
|
20 # lens_content => file('networkmanager/lenses/networkmanager.aug'), |
|
21 # test_content => file('networkmanager/lenses/test_networkmanager.aug'), |
|
22 # stock_since => '1.0.0', |
|
23 # } |
|
24 # |
|
25 define augeas::lens ( |
|
26 $ensure = present, |
|
27 $lens_content = undef, |
|
28 $lens_source = undef, |
|
29 $test_content = undef, |
|
30 $test_source = undef, |
|
31 $stock_since = false, |
|
32 ) { |
|
33 if !defined(Class['augeas']) { |
|
34 fail('You must declare the augeas class before using augeas::lens') |
|
35 } |
|
36 |
|
37 if $lens_source != undef { |
|
38 if $lens_content != undef { |
|
39 fail "You can't set both \$lens_source and \$lens_content" |
|
40 } else { |
|
41 warning 'Passing "lens_source" is deprecated; please use "lens_content"' |
|
42 } |
|
43 } else { |
|
44 if $lens_content == undef { |
|
45 fail "You must set either \$lens_source or \$lens_content" |
|
46 } |
|
47 } |
|
48 |
|
49 if $test_source != undef { |
|
50 if $test_content != undef { |
|
51 fail "You can't set both \$test_source and \$test_content" |
|
52 } else { |
|
53 warning 'Passing "test_source" is deprecated; please use "test_content"' |
|
54 } |
|
55 } |
|
56 |
|
57 File { |
|
58 owner => 'root', |
|
59 group => 'root', |
|
60 mode => '0644', |
|
61 } |
|
62 |
|
63 if (!$stock_since or versioncmp($::augeasversion, $stock_since) < 0) { |
|
64 |
|
65 validate_re( |
|
66 $augeas::lens_dir, |
|
67 '/.*', |
|
68 "'${augeas::lens_dir}' is not a valid path for lens ${name}" |
|
69 ) |
|
70 |
|
71 $lens_dest = "${augeas::lens_dir}/${name}.aug" |
|
72 $test_dest = "${augeas::lens_dir}/tests/test_${name}.aug" |
|
73 |
|
74 # lint:ignore:source_without_rights |
|
75 file { $lens_dest: |
|
76 ensure => $ensure, |
|
77 source => $lens_source, |
|
78 content => $lens_content, |
|
79 } |
|
80 # lint:endignore |
|
81 |
|
82 exec { "Typecheck lens ${name}": |
|
83 command => "augparse -I ${augeas::lens_dir} ${lens_dest} || (rm -f ${lens_dest} && exit 1)", |
|
84 path => "/opt/puppetlabs/puppet/bin:${::path}", |
|
85 refreshonly => true, |
|
86 subscribe => File[$lens_dest], |
|
87 } |
|
88 |
|
89 if $test_source or $test_content { |
|
90 # lint:ignore:source_without_rights |
|
91 file { $test_dest: |
|
92 ensure => $ensure, |
|
93 source => $test_source, |
|
94 content => $test_content, |
|
95 notify => Exec["Test lens ${name}"], |
|
96 } |
|
97 # lint:endignore |
|
98 |
|
99 exec { "Test lens ${name}": |
|
100 command => "augparse -I ${augeas::lens_dir} ${test_dest} || (rm -f ${lens_dest} && rm -f ${test_dest} && exit 1)", |
|
101 path => "/opt/puppetlabs/puppet/bin:${::path}", |
|
102 refreshonly => true, |
|
103 subscribe => File[$lens_dest, $test_dest], |
|
104 } |
|
105 } |
|
106 } |
|
107 } |