Skip to content

Commit

Permalink
Fix broken dependency to puppetlabs/apache module
Browse files Browse the repository at this point in the history
* Copy apache_version from puppetlabs/apache v8.x module.
  Puppetlabs removed apache_version with the v9 release of the apache module.
  apache_version was used to distiguish between the needed settings.
  • Loading branch information
Phil Friderici committed Mar 28, 2023
1 parent b7248fb commit 95f5c20
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
29 changes: 29 additions & 0 deletions lib/facter/apache_version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

Facter.add(:apache_version) do
confine kernel: ['FreeBSD', 'Linux']
setcode do
apache_version = nil

if Facter::Core::Execution.which('httpd')
apache_version = Facter::Core::Execution.execute('httpd -V 2>&1')
Facter.debug "Matching httpd '#{apache_version}'"
elsif Facter::Core::Execution.which('apache2')
apache_version = Facter::Core::Execution.execute('apache2 -V 2>&1')
Facter.debug "Matching apache2 '#{apache_version}'"
elsif Facter::Core::Execution.which('apachectl')
apache_version = Facter::Core::Execution.execute('apachectl -v 2>&1')
Facter.debug "Matching apachectl '#{apache_version}'"
elsif Facter::Core::Execution.which('apache2ctl')
apache_version = Facter::Core::Execution.execute('apache2ctl -v 2>&1')
Facter.debug "Matching apache2ctl '#{apache_version}'"
end

unless apache_version.nil?
match = %r{^Server version: Apache\/(\d+.\d+(.\d+)?)}.match(apache_version)
unless match.nil?
match[1]
end
end
end
end
9 changes: 9 additions & 0 deletions manifests/web.pp
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,15 @@
default => $zabbix_api_access.map |$host| { "host ${host}" },
}

# Check which version of Apache we're using
if versioncmp($facts['apache_version'], '2.4') >= 0 {
$directory_allow = { 'require' => 'all granted', }
$directory_deny = { 'require' => 'all denied', }
} else {
$directory_allow = { 'allow' => 'from all', 'order' => 'Allow,Deny', }
$directory_deny = { 'deny' => 'from all', 'order' => 'Deny,Allow', }
}

apache::vhost { $zabbix_url:
docroot => '/usr/share/zabbix',
ip => $apache_listen_ip,
Expand Down
2 changes: 1 addition & 1 deletion spec/classes/web_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

context "on #{os}" do
let :facts do
facts
facts.deep_merge({ apache_version: '2.4' })
end

context 'with all defaults' do
Expand Down
37 changes: 37 additions & 0 deletions spec/unit/facter/util/fact_apache_version_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'spec_helper'

describe Facter::Util::Fact do
before(:each) do
Facter.clear
end

describe 'apache_version' do
context 'with value' do
before :each do
Facter.fact(:kernel).stubs(:value).returns('Linux')
Facter::Core::Execution.stubs(:which).with('httpd').returns(true)
Facter::Core::Execution.stubs(:execute).with('httpd -V 2>&1').returns('Server version: Apache/2.4.16 (Unix)\nServer built: Jul 31 2015 15:53:26')
end

it 'should return the correct version' do
expect(Facter.fact(:apache_version).value).to eq('2.4.16')
end
end
end

describe 'apache_version with empty OS' do
context 'with value' do
before :each do
Facter.fact(:kernel).stubs(:value).returns('Linux')
Facter::Core::Execution.stubs(:which).with('httpd').returns(true)
Facter::Core::Execution.stubs(:execute).with('httpd -V 2>&1').returns('Server version: Apache/2.4.6 ()\nServer built: Nov 21 2015 05:34:59')
end

it 'should return the correct version' do
expect(Facter.fact(:apache_version).value).to eq('2.4.6')
end
end
end
end

0 comments on commit 95f5c20

Please sign in to comment.