diff --git a/lib/train/extras/os_common.rb b/lib/train/extras/os_common.rb index d3737930..59dbd8e8 100644 --- a/lib/train/extras/os_common.rb +++ b/lib/train/extras/os_common.rb @@ -38,15 +38,10 @@ def to_hash end OS = { # rubocop:disable Style/MutableConstant - 'redhat' => %w{ - redhat oracle centos fedora amazon scientific xenserver wrlinux - }, - 'debian' => %w{ - debian ubuntu linuxmint raspbian - }, - 'suse' => %w{ - suse opensuse - }, + 'redhat' => REDHAT_FAMILY, + 'debian' => DEBIAN_FAMILY, + 'suse' => SUSE_FAMILY, + 'fedora' => %w{fedora}, 'bsd' => %w{ freebsd netbsd openbsd darwin }, @@ -67,7 +62,7 @@ def to_hash }, } - OS['linux'] = %w{linux alpine arch coreos exherbo gentoo slackware} + OS['redhat'] + OS['debian'] + OS['suse'] + OS['linux'] = %w{linux alpine arch coreos exherbo gentoo slackware fedora} + OS['redhat'] + OS['debian'] + OS['suse'] OS['unix'] = %w{unix aix hpux} + OS['linux'] + OS['solaris'] + OS['bsd'] diff --git a/lib/train/extras/os_detect_darwin.rb b/lib/train/extras/os_detect_darwin.rb index 241a10e9..cc3287d1 100644 --- a/lib/train/extras/os_detect_darwin.rb +++ b/lib/train/extras/os_detect_darwin.rb @@ -7,9 +7,12 @@ # OHAI https://github.com/chef/ohai # by Adam Jacob, Chef Software Inc # +require 'train/extras/uname' module Train::Extras module DetectDarwin + include Train::Extras::Uname + def detect_darwin cmd = @backend.run_command('/usr/bin/sw_vers') # TODO: print an error in this step of the detection, @@ -18,9 +21,6 @@ def detect_darwin # TODO: ditto on error return false if cmd.stdout.empty? - uname_m = @backend.run_command("uname -m").stdout.chomp - return false if uname_m.empty? - name = cmd.stdout[/^ProductName:\s+(.+)$/, 1] # TODO: ditto on error return false if name.nil? @@ -29,8 +29,12 @@ def detect_darwin @platform[:build] = cmd.stdout[/^BuildVersion:\s+(.+)$/, 1] # TODO: keep for now due to backwards compatibility with serverspec @platform[:family] = 'darwin' - @platform[:arch] = uname_m + detect_darwin_arch true end + + def detect_darwin_arch + @platform[:arch] = uname_m + end end end diff --git a/lib/train/extras/os_detect_linux.rb b/lib/train/extras/os_detect_linux.rb index e1be21e3..0180e7ab 100644 --- a/lib/train/extras/os_detect_linux.rb +++ b/lib/train/extras/os_detect_linux.rb @@ -9,10 +9,16 @@ # require 'train/extras/linux_lsb' +require 'train/extras/uname' module Train::Extras module DetectLinux # rubocop:disable Metrics/ModuleLength + DEBIAN_FAMILY = %w{debian ubuntu linuxmint raspbian}.freeze + REDHAT_FAMILY = %w{centos redhat oracle scientific enterpriseenterprise xenserver cloudlinux ibm_powerkvm nexus_centos wrlinux}.freeze + SUSE_FAMILY = %w{suse opensuse}.freeze + include Train::Extras::LinuxLSB + include Train::Extras::Uname def detect_linux_via_config # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity if !(raw = get_config('oracle-release')).nil? @@ -85,7 +91,7 @@ def detect_linux_via_config # rubocop:disable Metrics/AbcSize, Metrics/Cyclomati @platform[:release] = meta[:release] elsif !(os_info = fetch_os_release).nil? if os_info['ID_LIKE'] =~ /wrlinux/ - @platform[:name] = 'wrlinux' + @platform[:name] = 'wrlinux' @platform[:release] = os_info['VERSION'] end end @@ -96,30 +102,17 @@ def detect_linux_via_config # rubocop:disable Metrics/AbcSize, Metrics/Cyclomati end def family_for_platform - case @platform[:name] - when 'centos' + if DEBIAN_FAMILY.include?(@platform[:name]) + 'debian' + elsif REDHAT_FAMILY.include?(@platform[:name]) 'redhat' + elsif SUSE_FAMILY.include?(@platform[:name]) + 'suse' else @platform[:name] || @platform[:family] end end - def uname_s - @uname_s ||= backend.run_command('uname -s').stdout - end - - def uname_r - @uname_r ||= ( - res = backend.run_command('uname -r').stdout - res.strip! unless res.nil? - res - ) - end - - def uname_m - @uname_m ||= backend.run_command('uname -m').stdout.chomp - end - def redhatish_platform(conf) conf[/^red hat/i] ? 'redhat' : conf[/(\w+)/i, 1].downcase end @@ -130,21 +123,16 @@ def redhatish_version(conf) conf[/release ([\d\.]+)/, 1] end - def detect_linux_architecture - if uname_m.nil? || uname_m.empty? - false - else - @platform[:arch] = uname_m - true - end + def detect_linux_arch + @platform[:arch] = uname_m end def detect_linux # TODO: print an error in this step of the detection return false if uname_s.nil? || uname_s.empty? return false if uname_r.nil? || uname_r.empty? - return false if !detect_linux_architecture + detect_linux_arch return true if detect_linux_via_config return true if detect_linux_via_lsb # in all other cases we failed the detection diff --git a/lib/train/extras/uname.rb b/lib/train/extras/uname.rb new file mode 100644 index 00000000..4b45abe5 --- /dev/null +++ b/lib/train/extras/uname.rb @@ -0,0 +1,28 @@ +# encoding: utf-8 +# author: Dominik Richter +# author: Christoph Hartmann +# +# This is heavily based on: +# +# OHAI https://github.com/chef/ohai +# by Adam Jacob, Chef Software Inc +# +module Train::Extras + module Uname + def uname_s + @uname_s ||= backend.run_command('uname -s').stdout + end + + def uname_r + @uname_r ||= begin + res = backend.run_command('uname -r').stdout + res.strip! unless res.nil? + res + end + end + + def uname_m + @uname_m ||= backend.run_command('uname -m').stdout.chomp + end + end +end diff --git a/test/unit/extras/os_common_test.rb b/test/unit/extras/os_common_test.rb index 71def801..054f7c67 100644 --- a/test/unit/extras/os_common_test.rb +++ b/test/unit/extras/os_common_test.rb @@ -57,7 +57,8 @@ def mock_platform(x) describe 'with platform set to fedora' do let(:os) { mock_platform('fedora') } - it { os.redhat?.must_equal(true) } + it { os.fedora?.must_equal(true) } + it { os.redhat?.must_equal(false) } it { os.debian?.must_equal(false) } it { os.suse?.must_equal(false) } it { os.linux?.must_equal(true) } diff --git a/test/unit/extras/os_detect_linux_test.rb b/test/unit/extras/os_detect_linux_test.rb index 3397570c..3eaf2303 100644 --- a/test/unit/extras/os_detect_linux_test.rb +++ b/test/unit/extras/os_detect_linux_test.rb @@ -14,13 +14,13 @@ def initialize describe 'os_detect_linux' do let(:detector) { OsDetectLinuxTester.new } - describe '#detect_linux_architecture' do + describe '#detect_linux_arch' do it "sets the arch using uname" do be = mock("Backend") detector.stubs(:backend).returns(be) be.stubs(:run_command).with("uname -m").returns(mock("Output", stdout: "x86_64\n")) - detector.detect_linux_architecture.must_equal(true) - detector.platform[:arch].must_equal("x84_64") + detector.detect_linux_arch + detector.platform[:arch].must_equal("x86_64") end end @@ -37,7 +37,8 @@ def initialize detector.stubs(:get_config).with('/etc/enterprise-release').returns('data') detector.detect_linux_via_config.must_equal(true) - detector.platform[:family].must_equal('oracle') + detector.platform[:name].must_equal('oracle') + detector.platform[:family].must_equal('redhat') detector.platform[:release].must_equal('redhat-version') end end @@ -64,7 +65,8 @@ def initialize detector.stubs(:lsb).returns({ id: 'ubuntu', release: 'ubuntu-release' }) detector.detect_linux_via_config.must_equal(true) - detector.platform[:family].must_equal('ubuntu') + detector.platform[:name].must_equal('ubuntu') + detector.platform[:family].must_equal('debian') detector.platform[:release].must_equal('ubuntu-release') end end @@ -74,7 +76,8 @@ def initialize detector.stubs(:lsb).returns({ id: 'linuxmint', release: 'mint-release' }) detector.detect_linux_via_config.must_equal(true) - detector.platform[:family].must_equal('linuxmint') + detector.platform[:name].must_equal('linuxmint') + detector.platform[:family].must_equal('debian') detector.platform[:release].must_equal('mint-release') end end @@ -85,7 +88,8 @@ def initialize detector.expects(:unix_file?).with('/usr/bin/raspi-config').returns(true) detector.detect_linux_via_config.must_equal(true) - detector.platform[:family].must_equal('raspbian') + detector.platform[:name].must_equal('raspbian') + detector.platform[:family].must_equal('debian') detector.platform[:release].must_equal('deb-version') end end @@ -96,6 +100,7 @@ def initialize detector.expects(:unix_file?).with('/usr/bin/raspi-config').returns(false) detector.detect_linux_via_config.must_equal(true) + detector.platform[:name].must_equal('debian') detector.platform[:family].must_equal('debian') detector.platform[:release].must_equal('deb-version') end @@ -125,7 +130,8 @@ def initialize detector.stubs(:fetch_os_release).returns(data) detector.detect_linux_via_config.must_equal(true) - detector.platform[:family].must_equal('wrlinux') + detector.platform[:name].must_equal('wrlinux') + detector.platform[:family].must_equal('redhat') detector.platform[:release].must_equal('cisco123') end end