Skip to content

Commit

Permalink
Rework the way lens_dir is retrieved
Browse files Browse the repository at this point in the history
While AIO Puppet has a well-known path to augeas lenses, non-AIO Puppet
use a sysstem path that is OS-dependant.

Rework the way this path is retrieved so that we can extand this easily
using Hiera.
  • Loading branch information
smortex committed Aug 7, 2024
1 parent ec80053 commit c68d69c
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .fixtures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
fixtures:
forge_modules:
augeas_core: "puppetlabs/augeas_core"
# stdlib: "puppetlabs/stdlib"
stdlib: "puppetlabs/stdlib"
4 changes: 4 additions & 0 deletions data/common.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
augeas::aio_lens_dir: /opt/puppetlabs/puppet/share/augeas/lenses
augeas::pe_lens_dir: /opt/puppet/share/augeas/lenses
augeas::system_lens_dir: /usr/share/augeas/lenses
2 changes: 2 additions & 0 deletions data/os/Archlinux.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
augeas::pe_lens_dir: /usr/share/augeas/lenses
2 changes: 2 additions & 0 deletions data/os/FreeBSD.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
---
augeas::files_group: "wheel"
augeas::pe_lens_dir: /usr/local/share/augeas/lenses
augeas::system_lens_dir: /usr/local/share/augeas/lenses
14 changes: 14 additions & 0 deletions functions/lens_dir.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# @summary function to return augeas lens directory
#
# @return [String]
function augeas::lens_dir() >> String {
if $augeas::lens_dir {
$augeas::lens_dir
} elsif versioncmp($facts['puppetversion'], '4.0.0') >= 0 and 'aio_agent_version' in $facts {
$augeas::aio_lens_dir
} elsif 'is_pe' in $facts and $facts['is_pe'] {
$augeas::pe_lens_dir
} else {
$augeas::system_lens_dir
}
}
2 changes: 1 addition & 1 deletion manifests/files.pp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class augeas::files {
include augeas

$lens_dir = $augeas::lens_dir
$lens_dir = augeas::lens_dir()

# ensure no file not managed by puppet ends up in there.
file { $lens_dir:
Expand Down
5 changes: 4 additions & 1 deletion manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
# ['lens_dir'] - the lens directory to use
# ['purge'] - whether to purge lens directories
class augeas (
Stdlib::Absolutepath $aio_lens_dir,
Stdlib::Absolutepath $pe_lens_dir,
Stdlib::Absolutepath $system_lens_dir,
String $files_owner = 'root',
String $files_group = 'root',
$version = present,
$ruby_package = $augeas::params::ruby_pkg,
$ruby_version = present,
$lens_dir = $augeas::params::lens_dir,
Optional[Stdlib::Absolutepath] $lens_dir = undef,
$purge = true,
) inherits augeas::params {
if versioncmp($facts['puppetversion'], '4.0.0') >= 0 {
Expand Down
12 changes: 6 additions & 6 deletions manifests/lens.pp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@
mode => '0644',
}

if (!$stock_since or versioncmp(String($facts['augeas']['version']), $stock_since) < 0) {
assert_type(Pattern[/^\/.*/], $augeas::lens_dir)
$lens_dir = augeas::lens_dir()

if (!$stock_since or versioncmp(String($facts['augeas']['version']), $stock_since) < 0) {
$lens_name = "${name}.aug"
$lens_dest = "${augeas::lens_dir}/${lens_name}"
$lens_dest = "${lens_dir}/${lens_name}"
$test_name = "tests/test_${name}.aug"
$test_dest = "${augeas::lens_dir}/${test_name}"
$test_dest = "${lens_dir}/${test_name}"

# lint:ignore:source_without_rights
file { $lens_dest:
Expand All @@ -76,7 +76,7 @@

exec { "Typecheck lens ${name}":
command => "augparse -I . ${lens_name} || (rm -f ${lens_name} && exit 1)",
cwd => $augeas::lens_dir,
cwd => $lens_dir,
path => "/opt/puppetlabs/puppet/bin:${facts['path']}",
refreshonly => true,
subscribe => File[$lens_dest],
Expand All @@ -94,7 +94,7 @@

exec { "Test lens ${name}":
command => "augparse -I . ${test_name} || (rm -f ${lens_name} && rm -f ${test_name}.aug && exit 1)",
cwd => $augeas::lens_dir,
cwd => $lens_dir,
path => "/opt/puppetlabs/puppet/bin:${facts['path']}",
refreshonly => true,
subscribe => File[$lens_dest, $test_dest],
Expand Down
9 changes: 0 additions & 9 deletions manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,4 @@

default: { fail("Unsupported OS family: ${facts['os']['family']}") }
}

if (versioncmp($facts['puppetversion'], '4.0.0') >= 0) and ($facts['ruby']['sitedir'] =~ /\/opt\/puppetlabs\/puppet/) {
$lens_dir = '/opt/puppetlabs/puppet/share/augeas/lenses'
} elsif ('is_pe' in $facts and str2bool("${facts['is_pe']}")) { # lint:ignore:only_variable_string
# puppet enterpise has a different lens location
$lens_dir = '/opt/puppet/share/augeas/lenses'
} else {
$lens_dir = '/usr/share/augeas/lenses'
}
}
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
{
"name": "puppetlabs/stdlib",
"version_requirement": ">= 2.3.0 < 10.0.0"
"version_requirement": ">= 4.13.0 < 10.0.0"
}
],
"operatingsystem_support": [
Expand Down
13 changes: 10 additions & 3 deletions spec/classes/augeas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@

describe 'augeas' do
let(:lens_dir) do
if (Puppet.version >= '4.0.0') && facts[:ruby]['sitedir'] =~ (%r{/opt/puppetlabs/puppet})
'/opt/puppetlabs/puppet/share/augeas/lenses'
else
case facts[:os]['family']
when 'FreeBSD'
'/usr/local/share/augeas/lenses'
when 'Archlinux'
'/usr/share/augeas/lenses'
else
if (Puppet.version >= '4.0.0') && facts[:ruby]['sitedir'] =~ (%r{/opt/puppetlabs/puppet})
'/opt/puppetlabs/puppet/share/augeas/lenses'
else
'/usr/share/augeas/lenses'
end
end
end

Expand Down
13 changes: 10 additions & 3 deletions spec/defines/augeas_lens_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@
let(:title) { 'foo' }

let(:lens_dir) do
if (Puppet.version >= '4.0.0') && facts[:ruby]['sitedir'] =~ (%r{/opt/puppetlabs/puppet})
'/opt/puppetlabs/puppet/share/augeas/lenses'
else
case facts[:os]['family']
when 'FreeBSD'
'/usr/local/share/augeas/lenses'
when 'Archlinux'
'/usr/share/augeas/lenses'
else
if (Puppet.version >= '4.0.0') && facts[:ruby]['sitedir'] =~ (%r{/opt/puppetlabs/puppet})
'/opt/puppetlabs/puppet/share/augeas/lenses'
else
'/usr/share/augeas/lenses'
end
end
end

Expand Down

0 comments on commit c68d69c

Please sign in to comment.