Skip to content
This repository has been archived by the owner on Jun 19, 2020. It is now read-only.

(FACT-2555)Create OS hierarchy and mechanism for loading it #470

Merged
merged 28 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
205b1fa
(FACT-2555) If no module with the OS name was discovered, log a debug…
Apr 15, 2020
0fca15b
(FACT-2555) Add OsHierarchy Class and json with Os hierarchy definition.
Apr 15, 2020
3a665ac
(FACT-2555) Reproduce existing hierarchy in os_hierarchy.json
Apr 15, 2020
4d4d269
(FACT-2555) Use OsHierarchy in OsDetector. Rubocop fixes.
Apr 15, 2020
0d25cce
(FACT-2555) If the distribution cannot be detected, default to linux
Apr 15, 2020
3f9298c
(FACT-2555) Add comments.
Apr 15, 2020
05d2c60
(FACT-2555) Add mock for OsHierarchy in OsDetector tests.
Apr 15, 2020
78f444a
Merge branch 'master' into FACT-2555
Apr 15, 2020
e4be72b
(FACT-2555) Add test for fallback to linux in case no distribution is…
Apr 15, 2020
31fd9d1
(FACT-2555) Refactor OsDetector tests.
Apr 15, 2020
9e7de71
Merge branch 'master' into FACT-2555
Apr 24, 2020
38a4819
(FACT-2555) Try to use family to detect os placement in hierarchy.
Apr 24, 2020
4d98059
(FACT-2555) Add mechanism for using os family if no hierarchy can be …
Apr 27, 2020
25d213a
(FACT-2555) Add tests for os_hierarchy.
Apr 27, 2020
c377f8b
(FACT-2555) Small refactoring of os detector.
Apr 27, 2020
cae8b56
(FACT-2555) Add test for family.
Apr 27, 2020
8cdf10d
(FACT-2555) Force mount-point data to UTF-8 as it might be ASCI in so…
Apr 28, 2020
a218ac7
(FACT-2555) Move conversion to UTF-8 in helper.
Apr 28, 2020
0b220cd
(FACT-2555) Add test for filesusyem_helper. Fix existing tests.
Apr 28, 2020
228b42f
(FACT-2555) Fix os hierarchy for intermediary nodes. Add tests for in…
Apr 28, 2020
5678b28
(FACT-2555) Address PR comments.
Apr 28, 2020
436cc69
(FACT-2555) Move the action we are testing in the block.
Apr 28, 2020
783b300
(FACT-2555) Improve error handling for os_hierarchy.
Apr 28, 2020
11f73f2
Revert "(FACT-2555) Add test for filesusyem_helper. Fix existing tests."
Apr 28, 2020
35b7690
Revert "(FACT-2555) Move conversion to UTF-8 in helper."
Apr 28, 2020
4fed55b
Revert "(FACT-2555) Force mount-point data to UTF-8 as it might be AS…
Apr 28, 2020
e58b0d3
(FACT-2555) Add test for cases that throw exceptions.
Apr 28, 2020
4c95ce4
Merge branch 'master' into FACT-2555
Apr 28, 2020
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
1 change: 1 addition & 0 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ RSpec/FilePath:
- 'spec/framework/formatters/legacy_fact_formatter_spec.rb'
- 'spec/framework/formatters/yaml_fact_formatter_spec.rb'
- 'spec/framework/utils/utils_spec.rb'
- 'spec/framework/detector/os_hierarchy_spec.rb'
BogdanIrimie marked this conversation as resolved.
Show resolved Hide resolved

# Offense count: 15
# Configuration parameters: AssignmentOnly.
Expand Down
2 changes: 1 addition & 1 deletion lib/framework/core/fact_loaders/class_discoverer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def discover_classes(operating_system)
find_nested_classes(os_module_name, discovered_classes = [])
discovered_classes
rescue NameError
@log.error("There is no module named #{operating_system}")
@log.debug("There is no module named #{operating_system}")
[]
end

Expand Down
1 change: 1 addition & 0 deletions lib/framework/core/file_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def load_lib_dirs(*dirs)
require "#{ROOT_DIR}/lib/util/file_helper"

require "#{ROOT_DIR}/lib/resolvers/base_resolver"
require "#{ROOT_DIR}/lib/framework/detector/os_hierarchy"
require "#{ROOT_DIR}/lib/framework/detector/os_detector"

require "#{ROOT_DIR}/lib/framework/config/config_reader"
Expand Down
80 changes: 34 additions & 46 deletions lib/framework/detector/os_detector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,46 @@ class OsDetector
attr_reader :identifier, :version, :hierarchy

def initialize(*_args)
@os_hierarchy = Facter::OsHierarchy.new
@identifier = detect
@hierarchy = create_hierarchy(@identifier)
end

private

def detect
host_os = RbConfig::CONFIG['host_os']
@identifier = case host_os
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
:windows
when /darwin|mac os/
:macosx
when /linux/
detect_distro
when /bsd/
:bsd
when /solaris/
:solaris
when /aix/
:aix
else
raise "unknown os: #{host_os.inspect}"
end
identifier = case host_os
when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
:windows
when /darwin|mac os/
:macosx
when /linux/
detect_distro
when /bsd/
:bsd
when /solaris/
:solaris
when /aix/
:aix
else
raise "unknown os: #{host_os.inspect}"
end

@hierarchy = detect_hierarchy(identifier)
@identifier = identifier
end

private
def detect_hierarchy(identifier)
hierarchy = @os_hierarchy.construct_hierarchy(identifier)
hierarchy = @os_hierarchy.construct_hierarchy(detect_family) if hierarchy.empty?
hierarchy = @os_hierarchy.construct_hierarchy(:linux) if hierarchy.empty?

hierarchy
end

def detect_family
Facter::Resolvers::OsRelease.resolve(:id_like)
end

def detect_distro
[Facter::Resolvers::OsRelease,
Expand All @@ -43,33 +58,6 @@ def detect_distro
break if @identifier
end

@identifier
end

def create_hierarchy(operating_system)
return [] unless operating_system

case operating_system.to_sym
when :ubuntu
%w[Debian]
when :elementary
%w[Debian]
when :raspbian
%w[Debian]
when :fedora
%w[El]
when :amzn
%w[El]
when :rhel
%w[El]
when :centos
%w[El]
when :opensuse
%w[Sles]
when :bsd
%w[Solaris Bsd]
else
[operating_system.to_s.capitalize]
end
@identifier&.downcase&.to_sym
end
end
53 changes: 53 additions & 0 deletions lib/framework/detector/os_hierarchy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

module Facter
class OsHierarchy
def initialize
@log = Log.new(self)
json_file = Util::FileHelper.safe_read('os_hierarchy.json')

begin
@json_os_hierarchy = JSON.parse(json_file)
rescue JSON::ParserError => _e
@log.error('Could not parse os_hierarchy json')
end
end

def construct_hierarchy(searched_os)
return [] if searched_os.nil?

searched_os = searched_os.to_s.capitalize
if @json_os_hierarchy.nil?
@log.debug("There is no os_hierarchy, will fall back to: #{searched_os}")
return [searched_os]
end

@searched_path = []
search(@json_os_hierarchy, searched_os, [])

@searched_path.map { |os_name| os_name.to_s.capitalize }
end

private

def search(json_data, searched_element, path)
# we hit a dead end, the os was not found on this branch
# and we cannot go deeper
return unless json_data

json_data.each do |tree_node|
# we found the searched OS, so save the path from the tree
@searched_path = path.dup << tree_node if tree_node == searched_element

next unless tree_node.is_a?(Hash)

tree_node.each do |k, v|
return @searched_path = path.dup << k if k == searched_element

search(v, searched_element, path << k)
path.pop
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/framework/logging/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def warn(msg)

def error(msg, colorize = false)
@@has_errors = true
msg = colorize(msg, RED) if colorize && !OsDetector.instance.detect.eql?(:windows)
msg = colorize(msg, RED) if colorize && !OsDetector.instance.identifier.eql?(:windows)
@@logger.error(@class_name + ' - ' + msg)
end

Expand Down
3 changes: 2 additions & 1 deletion lib/resolvers/redhat_release_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def post_resolve(fact_name)
end

def read_redhat_release(fact_name)
output, _status = Open3.capture2('cat /etc/redhat-release')
output = Util::FileHelper.safe_read('/etc/redhat-release', nil)
return @fact_list[fact_name] = nil if output.nil?

build_fact_list(output)

Expand Down
4 changes: 3 additions & 1 deletion lib/resolvers/suse_release_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ def post_resolve(fact_name)
end

def read_suse_release(fact_name)
output, _status = Open3.capture2('cat /etc/SuSE-release')
output = Util::FileHelper.safe_read('/etc/SuSE-release', nil)
return @fact_list[fact_name] = nil if output.nil?

output_strings = output.split(' ')

@fact_list[:name] = output_strings[0]
Expand Down
33 changes: 33 additions & 0 deletions os_hierarchy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"Linux": [
{
"Debian": [
"Elementary",
"Ubuntu",
"Raspbian"
]
},
{
"El": [
"Fedora",
"Amzn",
"Centos"
]
},
{
"Sles": [
"Opensuse"
]
}
]
},
{
"Solaris": [
"Bsd"
]
},
"Macosx",
"Windows",
"Aix"
]
4 changes: 2 additions & 2 deletions spec/facter/resolvers/redhat_release_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

describe Facter::Resolvers::RedHatRelease do
before do
allow(Open3).to receive(:capture2)
.with('cat /etc/redhat-release')
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/etc/redhat-release', nil)
.and_return("Red Hat Enterprise Linux Server release 5.10 (Tikanga)\n")
end

Expand Down
4 changes: 2 additions & 2 deletions spec/facter/resolvers/suse_relese_resolver_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

describe Facter::Resolvers::SuseRelease do
before do
allow(Open3).to receive(:capture2)
.with('cat /etc/SuSE-release')
allow(Facter::Util::FileHelper).to receive(:safe_read)
.with('/etc/SuSE-release', nil)
.and_return("openSUSE 11.1 (i586)
VERSION = 11.1")
end
Expand Down
32 changes: 32 additions & 0 deletions spec/fixtures/broken_os_hierarchy
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[
{
"Linux": [
"Debian": [
"Elementary",
"Ubuntu",
"Raspbian"
]
},
{
"El": [
"Fedora",
"Amzn",
"Centos"
]
},
{
"Sles": [
"Opensuse"
]
}
]
},
{
"Solaris": [
"Bsd"
]
},
"Macosx",
"Windows",
"Aix"
]
33 changes: 33 additions & 0 deletions spec/fixtures/os_hierarchy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"Linux": [
{
"Debian": [
"Elementary",
"Ubuntu",
"Raspbian"
]
},
{
"El": [
"Fedora",
"Amzn",
"Centos"
]
},
{
"Sles": [
"Opensuse"
]
}
]
},
{
"Solaris": [
"Bsd"
]
},
"Macosx",
"Windows",
"Aix"
]
Loading