From b7d857adbd147ef7b610202d2eddf4e9f692589e Mon Sep 17 00:00:00 2001 From: Yannick Denzer Date: Tue, 22 Jan 2019 11:04:52 +0100 Subject: [PATCH] Add additional commands to detect Apache version and make sure the regular expression actually matched something --- lib/facter/apache_version.rb | 19 ++++++++++++++++--- spec/unit/apache_version_spec.rb | 8 ++++---- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/lib/facter/apache_version.rb b/lib/facter/apache_version.rb index cd29c1dbe3..ec577ca52c 100644 --- a/lib/facter/apache_version.rb +++ b/lib/facter/apache_version.rb @@ -1,14 +1,27 @@ Facter.add(:apache_version) do confine kernel: ['FreeBSD', 'Linux'] setcode do - if Facter::Util::Resolution.which('apachectl') + apache_version = nil + + if Facter::Util::Resolution.which('httpd') + apache_version = Facter::Util::Resolution.exec('httpd -V 2>&1') + Facter.debug "Matching httpd '#{apache_version}'" + elsif Facter::Util::Resolution.which('apache2') + apache_version = Facter::Util::Resolution.exec('apache2 -V 2>&1') + Facter.debug "Matching apache2 '#{apache_version}'" + elsif Facter::Util::Resolution.which('apachectl') apache_version = Facter::Util::Resolution.exec('apachectl -v 2>&1') Facter.debug "Matching apachectl '#{apache_version}'" - %r{^Server version: Apache/(\d+\.\d+(\.\d+)?)}.match(apache_version)[1] elsif Facter::Util::Resolution.which('apache2ctl') apache_version = Facter::Util::Resolution.exec('apache2ctl -v 2>&1') Facter.debug "Matching apache2ctl '#{apache_version}'" - %r{^Server version: Apache/(\d+\.\d+(\.\d+)?)}.match(apache_version)[1] + 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 diff --git a/spec/unit/apache_version_spec.rb b/spec/unit/apache_version_spec.rb index c2db7a2217..d43623cb71 100644 --- a/spec/unit/apache_version_spec.rb +++ b/spec/unit/apache_version_spec.rb @@ -7,8 +7,8 @@ describe 'apache_version' do context 'with value' do before :each do - expect(Facter::Util::Resolution).to receive(:which).with('apachectl') { true } - expect(Facter::Util::Resolution).to receive(:exec).with('apachectl -v 2>&1') { + expect(Facter::Util::Resolution).to receive(:which).with('httpd') { true } + expect(Facter::Util::Resolution).to receive(:exec).with('httpd -V 2>&1') { 'Server version: Apache/2.4.16 (Unix) Server built: Jul 31 2015 15:53:26' } @@ -22,8 +22,8 @@ describe 'apache_version with empty OS' do context 'with value' do before :each do - expect(Facter::Util::Resolution).to receive(:which).with('apachectl') { true } - expect(Facter::Util::Resolution).to receive(:exec).with('apachectl -v 2>&1') { + expect(Facter::Util::Resolution).to receive(:which).with('httpd') { true } + expect(Facter::Util::Resolution).to receive(:exec).with('httpd -V 2>&1') { 'Server version: Apache/2.4.6 () Server built: Nov 21 2015 05:34:59' }