summaryrefslogtreecommitdiff
path: root/manifests/lens.pp
blob: bda7a0cc34c3c251894b803b462f5a204611afb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Definition: augeas::lens
#
# Deploy an Augeas lens (and its test file).
# Check the lens (and run the unit tests) automatically and remove the files if
# the checks fail.
#
# Parameters:
#   ['ensure']       - present/absent
#   ['lens_content'] - the content of the lens
#   ['lens_source']  - the source for the lens
#   ['test_content'] - optionally, the content of the test
#   ['test_source']  - optionally, the source for the test file.
#   ['stock_since']  - optionally, indicate in which version of Augeas
#                      the lens became stock, so it will not be deployed
#                      above that version.
#
# Example usage:
#
#   augeas::lens { 'networkmanager':
#     lens_content => file('networkmanager/lenses/networkmanager.aug'),
#     test_content => file('networkmanager/lenses/test_networkmanager.aug'),
#     stock_since  => '1.0.0',
#   }
#
define augeas::lens (
  $ensure       = present,
  $lens_content = undef,
  $lens_source  = undef,
  $test_content = undef,
  $test_source  = undef,
  $stock_since  = false,
) {
  include ::augeas

  if $lens_source != undef {
    if $lens_content != undef {
      fail "You can't set both \$lens_source and \$lens_content"
    } else {
      warning 'Passing "lens_source" is deprecated; please use "lens_content"'
    }
  } else {
    if $lens_content == undef {
      fail "You must set either \$lens_source or \$lens_content"
    }
  }

  if $test_source != undef {
    if $test_content != undef {
      fail "You can't set both \$test_source and \$test_content"
    } else {
      warning 'Passing "test_source" is deprecated; please use "test_content"'
    }
  }

  File {
    owner => 'root',
    group => 'root',
    mode => '0644',
  }

  if (!$stock_since or versioncmp($::augeasversion, $stock_since) < 0) {

    validate_re(
      $augeas::lens_dir,
      '/.*',
      "'${augeas::lens_dir}' is not a valid path for lens ${name}"
    )

    $lens_dest = "${augeas::lens_dir}/${name}.aug"
    $test_dest = "${augeas::lens_dir}/tests/test_${name}.aug"

    # lint:ignore:source_without_rights
    file { $lens_dest:
      ensure  => $ensure,
      source  => $lens_source,
      content => $lens_content,
    }
    # lint:endignore

    exec { "Typecheck lens ${name}":
      command     => "augparse -I ${augeas::lens_dir} ${lens_dest} || (rm -f ${lens_dest} && exit 1)",
      path        => "/opt/puppetlabs/puppet/bin:${::path}",
      refreshonly => true,
      subscribe   => File[$lens_dest],
    }

    if $test_source or $test_content {
      # lint:ignore:source_without_rights
      file { $test_dest:
        ensure  => $ensure,
        source  => $test_source,
        content => $test_content,
        notify  => Exec["Test lens ${name}"],
      }
      # lint:endignore

      exec { "Test lens ${name}":
        command     => "augparse -I ${augeas::lens_dir} ${test_dest} || (rm -f ${lens_dest} && rm -f ${test_dest} && exit 1)",
        path        => "/opt/puppetlabs/puppet/bin:${::path}",
        refreshonly => true,
        subscribe   => File[$lens_dest, $test_dest],
      }
    }
  }
}