Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #14545 - Correctly parse y.z minor OS version from facts #3836

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions app/services/fact_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,4 @@ def normalize_interfaces(interfaces)
def not_implemented_error(method)
"#{method} fact parsing not implemented in #{self.class}"
end

def is_numeric?(string)
!!Integer(string)
rescue ArgumentError, TypeError
false
end
end
14 changes: 7 additions & 7 deletions app/services/puppet_fact_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def operatingsystem
if os_name == "Archlinux"
# Archlinux is rolling release, so it has no release. We use 1.0 always
args = {:name => os_name, :major => "1", :minor => "0"}
os = Operatingsystem.where(args).first || Operatingsystem.new(args)
os = Operatingsystem.find_or_initialize_by(args)
elsif orel.present?
if os_name == "Debian" && orel[/testing|unstable/i]
case facts[:lsbdistcodename]
Expand All @@ -27,14 +27,14 @@ def operatingsystem
elsif os_name[/FreeBSD/i]
orel.gsub!(/\-RELEASE\-p[0-9]+/, '')
end
major, minor = orel.split(".")
major.to_s.gsub!(/\D/, '') unless is_numeric? major
minor.to_s.gsub!(/\D/, '') unless is_numeric? minor
args = {:name => os_name, :major => major.to_s, :minor => minor.to_s}
os = Operatingsystem.where(args).first || Operatingsystem.create!(args)
major, minor = orel.split('.', 2)
major.to_s.gsub!(/\D/, '')
minor.to_s.gsub!(/[^\d\.]/, '')
args = {:name => os_name, :major => major, :minor => minor}
os = Operatingsystem.find_or_initialize_by(args)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No reason to save the model twice for new OSes, if there is a failing validation it will be thrown in the .save! below.

os.release_name = facts[:lsbdistcodename] if facts[:lsbdistcodename] && (os_name[/debian|ubuntu/i] || os.family == 'Debian')
else
os = Operatingsystem.find_by_name(os_name) || Operatingsystem.create!(:name => os_name)
os = Operatingsystem.find_or_initialize_by(:name => os_name)
end
if os.description.blank?
if os_name == 'SLES'
Expand Down
11 changes: 11 additions & 0 deletions test/unit/puppet_fact_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ def setup
refute @importer.operatingsystem.description
end

test 'should accept y.z minor version' do
FactoryGirl.create(:operatingsystem, name: "CentOS",
major: "7",
minor: "2.1511",
description: "CentOS Linux 7.2.1511")
assert_valid PuppetFactParser.new({ "operatingsystem" => "CentOS",
"lsbdistdescription" => "CentOS Linux release 7.2.1511 (Core) ",
"operatingsystemrelease" => "7.2.1511"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not that I care, but here's an example where enabling the cop to enforce one type of hash syntax would've been good instead of allowing 2 different hashing syntaxes to coexist.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me it kind of makes sense to use the newer syntax when passing a hash as function arguments. The second hash I think expects string keys rather then symbols, so I kept it with the old style.

}).operatingsystem

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indent the right brace the same as the first position after the preceding left parenthesis.

end

test "should set os.major and minor correctly from AIX facts" do
@importer = PuppetFactParser.new(aix_facts)
assert_equal 'AIX', @importer.operatingsystem.family
Expand Down