Skip to content

Commit

Permalink
Fixes #36547 - Fix parsing of Ubuntu version in fact parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
goarsna committed Aug 17, 2023
1 parent 9bb4bc7 commit 1014f78
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 6 deletions.
6 changes: 5 additions & 1 deletion app/models/operatingsystem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,11 @@ def self.find_by_to_label(str)
os = find_by_description(str.to_s)
return os if os
a = str.split(" ")
b = a[1].split('.') if a[1]
if a[0] =~ /ubuntu/i
b = a[1], nil if a[1]
else
b = a[1].split('.') if a[1]
end
cond = {:name => a[0]}
cond[:major] = b[0] if b && b[0]
cond[:minor] = b[1] if b && b[1]
Expand Down
8 changes: 7 additions & 1 deletion app/services/foreman_ansible/operating_system_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ def os_major
(facts[:ansible_distribution_major_version][%r{\/sid}i] ||
facts[:ansible_distribution_major_version] == 'n/a')
debian_os_major_sid
elsif os_name == 'Ubuntu'
facts[:ansible_distribution_major_version]
else
facts[:ansible_distribution_major_version] ||
facts[:ansible_lsb] && facts[:ansible_lsb]['major_release'] ||
Expand All @@ -68,8 +70,12 @@ def os_release
end

def os_minor
_, minor = os_release&.split('.', 2) ||
if os_name == 'Ubuntu'
minor = nil
else
_, minor = os_release&.split('.', 2) ||
(facts[:version].split('R') if os_name == 'junos')
end
# Until Foreman supports os.minor as something that's not a number,
# we should remove the extra dots in the version. E.g:
# '6.1.7601.65536' becomes '6.1.760165536'
Expand Down
2 changes: 2 additions & 0 deletions app/services/foreman_chef/fact_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def operatingsystem
# if we have no release information we can't assign OS properly (e.g. missing redhat-lsb)
if release.nil?
major, minor = 1, nil
elsif os_name == 'Ubuntu'
major, minor = release, nil
else
major, minor = release.split('.')
end
Expand Down
8 changes: 7 additions & 1 deletion app/services/katello/rhsm_fact_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ def operatingsystem
return nil if name.nil? || version.nil?

os_name = distribution_to_puppet_os(name)
major, minor = version.split('.')
version_splits = version.split('.')
if name =~ /ubuntu/i
major = "#{version_splits[0]}.#{version_splits[1]}"
minor = version_splits[2] if version_splits.length == 3
else
major, minor = version_splits
end
unless facts['ignore_os']
os_attributes = {:major => major, :minor => minor || '', :name => os_name}

Expand Down
19 changes: 16 additions & 3 deletions app/services/puppet_fact_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@ def operatingsystem
orel = os_release.dup

if orel.present?
major, minor = orel.split('.', 2)
major = major.to_s.gsub(/\D/, '')
minor = minor.to_s.gsub(/[^\d\.]/, '')
if os_name =~ /ubuntu/i
major = os_major_version
minor = os_minor_version
else
major, minor = orel.split('.')
major = major.to_s.gsub(/\D/, '')
minor = minor.to_s.gsub(/[^\d\.]/, '')
end
args = {:name => os_name, :major => major, :minor => minor}
os = Operatingsystem.find_or_initialize_by(args)
if os_name[/debian|ubuntu/i] || os.family == 'Debian'
Expand Down Expand Up @@ -227,6 +232,14 @@ def os_name
raise(::Foreman::Exception.new("invalid facts, missing operating system value"))
end

def os_major_version
facts.dig(:os, :release, :major)
end

def os_minor_version
facts.dig(:os, :release, :minor)
end

def os_release
case os_name
when /(windows)/i
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20230719080900_fix_ubuntu_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class FixUbuntuVersions < ActiveRecord::Migration[6.0]
def up
# For every Ubuntu operating system with a set minor version we
# * Check if there is already an operating system present with the same major and minor version combined in the major field
# * If not, we update the version
Operatingsystem.where('name ~* :name', name: 'ubuntu')
.merge(Operatingsystem.where.not(minor: [nil, ''])).each do |os|
if !Operatingsystem.where('name ~* :name', name: 'ubuntu').merge(Operatingsystem.where(major: "#{os.major}.#{os.minor}")).merge(Operatingsystem.where(minor: [nil, ''])).first
os.update(major: "#{os.major}.#{os.minor}", minor: nil)
end
end
end

0 comments on commit 1014f78

Please sign in to comment.