Skip to content

Commit

Permalink
Merge pull request #2738 from mhashizume/FACT-3471/main/azure-linux
Browse files Browse the repository at this point in the history
Add support for Microsoft Azure Linux to Facter 4.x
  • Loading branch information
cthorn42 authored Jul 16, 2024
2 parents 8dfaabf + d533faf commit a37196b
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/facter/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ module Config
Meego
Oel
Ovs
Mariner
Azurelinux
]
},
{
Expand All @@ -40,8 +42,7 @@ module Config
'Photon',
'Slackware',
'Mageia',
'Openwrt',
'Mariner'
'Openwrt'
]
},
{
Expand Down
35 changes: 35 additions & 0 deletions lib/facter/facts/azurelinux/os/release.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

module Facts
module Azurelinux
module Os
class Release
FACT_NAME = 'os.release'
ALIASES = %w[operatingsystemmajrelease operatingsystemrelease].freeze

def call_the_resolver
version = from_specific_file || from_os_release

return Facter::ResolvedFact.new(FACT_NAME, nil) unless version

[Facter::ResolvedFact.new(FACT_NAME, version),
Facter::ResolvedFact.new(ALIASES.first, version['major'], :legacy),
Facter::ResolvedFact.new(ALIASES.last, version['full'], :legacy)]
end

def from_specific_file
version = Facter::Resolvers::SpecificReleaseFile.resolve(:release,
{ release_file: '/etc/azurelinux-release',
regex: /AZURELINUX_BUILD_NUMBER=([0-9.]+)/ })
Facter::Util::Facts.release_hash_from_matchdata(version)
end

def from_os_release
version = Facter::Resolvers::OsRelease.resolve(:version_id)

Facter::Util::Facts.release_hash_from_string(version)
end
end
end
end
end
3 changes: 3 additions & 0 deletions lib/facter/framework/core/file_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,9 @@
when 'archlinux'
require_relative '../../facts/archlinux/os/release'

when 'azurelinux'
require_relative '../../facts/azurelinux/os/release'

when 'bsd'
require_relative '../../facts/bsd/kernelmajversion'
require_relative '../../facts/bsd/kernelversion'
Expand Down
2 changes: 2 additions & 0 deletions lib/facter/resolvers/os_release.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def join_os_name
@fact_list[:name] = if os_name.downcase.start_with?('red', 'oracle', 'arch', 'manjaro')
os_name = os_name.split(' ')[0..1].join
os_name
elsif os_name.downcase.end_with?('azure linux')
os_name.split(' ')[1..2].join
elsif os_name.downcase.end_with?('mariner')
os_name.split(' ')[-1].strip
else
Expand Down
2 changes: 1 addition & 1 deletion lib/facter/util/facts/facts_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Facts

PHYSICAL_HYPERVISORS = %w[physical xen0 vmware_server vmware_workstation openvzhn vserver_host].freeze
REDHAT_FAMILY = %w[redhat rhel fedora centos scientific ascendos cloudlinux psbm
oraclelinux ovs oel amazon xenserver xcp-ng virtuozzo photon mariner].freeze
oraclelinux ovs oel amazon xenserver xcp-ng virtuozzo photon mariner azurelinux].freeze
DEBIAN_FAMILY = %w[debian ubuntu huaweios linuxmint devuan kde].freeze
SUSE_FAMILY = %w[sles sled suse].freeze
GENTOO_FAMILY = ['gentoo'].freeze
Expand Down
56 changes: 56 additions & 0 deletions spec/facter/facts/azurelinux/os/release_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

describe Facts::Azurelinux::Os::Release do
describe '#call_the_resolver' do
subject(:fact) { Facts::Azurelinux::Os::Release.new }

before do
allow(Facter::Resolvers::SpecificReleaseFile).to receive(:resolve)
.with(:release, { release_file: '/etc/azurelinux-release',
regex: /AZURELINUX_BUILD_NUMBER=([0-9.]+)/ })
.and_return(value)
end

context 'when version is retrieved from specific file' do
let(:value) { /AZURELINUX_BUILD_NUMBER=([0-9.]+)/.match('AZURELINUX_BUILD_NUMBER=3.0.20240401') }
let(:release) { { 'full' => '3.0.20240401', 'major' => '3', 'minor' => '0' } }

it 'returns operating system name fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \
contain_exactly(an_object_having_attributes(name: 'os.release', value: release),
an_object_having_attributes(name: 'operatingsystemmajrelease',
value: release['major'], type: :legacy),
an_object_having_attributes(name: 'operatingsystemrelease',
value: release['full'], type: :legacy))
end
end

context 'when version is retrieved from os-release file' do
let(:value) { nil }
let(:os_release) { '3.0.20240401' }
let(:release) { { 'full' => '3.0.20240401', 'major' => '3', 'minor' => '0' } }

before do
allow(Facter::Resolvers::OsRelease).to receive(:resolve).with(:version_id).and_return(os_release)
end

it 'returns operating system name fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \
contain_exactly(an_object_having_attributes(name: 'os.release', value: release),
an_object_having_attributes(name: 'operatingsystemmajrelease',
value: release['major'], type: :legacy),
an_object_having_attributes(name: 'operatingsystemrelease',
value: release['full'], type: :legacy))
end

context 'when release can\'t be received' do
let(:os_release) { nil }

it 'returns operating system name fact' do
expect(fact.call_the_resolver).to be_an_instance_of(Facter::ResolvedFact).and \
have_attributes(name: 'os.release', value: nil)
end
end
end
end
end

0 comments on commit a37196b

Please sign in to comment.