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

Commit

Permalink
Merge branch 'master' into FACT-2542
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrei Filipovici committed Apr 28, 2020
2 parents 2694554 + fa6277c commit 9d0b30e
Show file tree
Hide file tree
Showing 17 changed files with 454 additions and 98 deletions.
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'

# Offense count: 15
# Configuration parameters: AssignmentOnly.
Expand Down
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ jobs:
- rvm: 2.6
script:
- bundle exec rubocop
- bundle exec rubycritic --no-browser -f console
- bundle exec rake spec
- bundle exec rake commits

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

0 comments on commit 9d0b30e

Please sign in to comment.