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

Bring train platform data more in line with ohai's platform data #126

Merged
merged 4 commits into from
Aug 2, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
16 changes: 6 additions & 10 deletions lib/train/extras/os_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class OSCommon
include Train::Extras::DetectWindows
include Train::Extras::DetectEsx

attr_accessor :backend
def initialize(backend, platform = nil)
@backend = backend
@platform = platform || {}
Expand All @@ -37,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
},
Expand All @@ -66,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']
Copy link
Contributor

Choose a reason for hiding this comment

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

do we have to add amazon as a platform here?


OS['unix'] = %w{unix aix hpux} + OS['linux'] + OS['solaris'] + OS['bsd']

Expand Down
8 changes: 8 additions & 0 deletions lib/train/extras/os_detect_darwin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -26,7 +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'
detect_darwin_arch
true
end

def detect_darwin_arch
@platform[:arch] = uname_m
end
end
end
79 changes: 48 additions & 31 deletions lib/train/extras/os_detect_linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,96 +9,108 @@
#

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?
@platform[:family] = 'oracle'
@platform[:name] = 'oracle'
@platform[:release] = redhatish_version(raw)
elsif !(raw = get_config('/etc/enterprise-release')).nil?
@platform[:family] = 'oracle'
@platform[:name] = 'oracle'
@platform[:release] = redhatish_version(raw)
elsif !(raw = get_config('/etc/debian_version')).nil?
case lsb[:id]
when /ubuntu/i
@platform[:family] = 'ubuntu'
@platform[:name] = 'ubuntu'
@platform[:release] = lsb[:release]
when /linuxmint/i
@platform[:family] = 'linuxmint'
@platform[:name] = 'linuxmint'
@platform[:release] = lsb[:release]
else
@platform[:family] = unix_file?('/usr/bin/raspi-config') ? 'raspbian' : 'debian'
@platform[:name] = unix_file?('/usr/bin/raspi-config') ? 'raspbian' : 'debian'
@platform[:release] = raw.chomp
end
elsif !(raw = get_config('/etc/parallels-release')).nil?
@platform[:family] = redhatish_platform(raw)
@platform[:name] = redhatish_platform(raw)
@platform[:release] = raw[/(\d\.\d\.\d)/, 1]
elsif !(raw = get_config('/etc/redhat-release')).nil?
# TODO: Cisco
# TODO: fully investigate os-release and integrate it;
# here we just use it for centos
if !(osrel = get_config('/etc/os-release')).nil? && osrel =~ /centos/i
@platform[:family] = 'centos'
else
@platform[:family] = redhatish_platform(raw)
end
@platform[:name] = if !(osrel = get_config('/etc/os-release')).nil? && osrel =~ /centos/i
'centos'
else
redhatish_platform(raw)
end

@platform[:release] = redhatish_version(raw)
elsif !(raw = get_config('/etc/system-release')).nil?
# Amazon Linux
@platform[:family] = redhatish_platform(raw)
@platform[:name] = redhatish_platform(raw)
@platform[:release] = redhatish_version(raw)
elsif !(suse = get_config('/etc/SuSE-release')).nil?
version = suse.scan(/VERSION = (\d+)\nPATCHLEVEL = (\d+)/).flatten.join('.')
version = suse[/VERSION = ([\d\.]{2,})/, 1] if version == ''
@platform[:release] = version
@platform[:family] = 'suse'
@platform[:family] = 'opensuse' if suse =~ /^openSUSE/
@platform[:name] = if suse =~ /^openSUSE/
'opensuse'
else
'suse'
end
elsif !(raw = get_config('/etc/arch-release')).nil?
@platform[:family] = 'arch'
@platform[:name] = 'arch'
# Because this is a rolling release distribution,
# use the kernel release, ex. 4.1.6-1-ARCH
@platform[:release] = uname_r
elsif !(raw = get_config('/etc/slackware-version')).nil?
@platform[:family] = 'slackware'
@platform[:name] = 'slackware'
@platform[:release] = raw.scan(/(\d+|\.+)/).join
elsif !(raw = get_config('/etc/exherbo-release')).nil?
@platform[:family] = 'exherbo'
@platform[:name] = 'exherbo'
# Because this is a rolling release distribution,
# use the kernel release, ex. 4.1.6
@platform[:release] = uname_r
elsif !(raw = get_config('/etc/gentoo-release')).nil?
@platform[:family] = 'gentoo'
@platform[:name] = 'gentoo'
Copy link
Contributor

@chris-rock chris-rock Aug 1, 2016

Choose a reason for hiding this comment

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

Should we add the family gentoo here as well? same applies e.g. for coreos and alpine. We should always fill the family.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Family is set to the name if no other name is set at line 110.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perfect. Missed that. That is great!

@platform[:release] = raw.scan(/(\d+|\.+)/).join
elsif !(raw = get_config('/etc/alpine-release')).nil?
@platform[:family] = 'alpine'
@platform[:name] = 'alpine'
@platform[:release] = raw.strip
elsif !(raw = get_config('/etc/coreos/update.conf')).nil?
@platform[:family] = 'coreos'
@platform[:name] = 'coreos'
meta = lsb_config(raw)
@platform[:release] = meta[:release]
elsif !(os_info = fetch_os_release).nil?
if os_info['ID_LIKE'] =~ /wrlinux/
@platform[:family] = 'wrlinux'
@platform[:name] = 'wrlinux'
@platform[:release] = os_info['VERSION']
end
end

!@platform[:family].nil? && !@platform[:release].nil?
end
@platform[:family] = family_for_platform

def uname_s
@uname_s ||= @backend.run_command('uname -s').stdout
!@platform[:family].nil? && !@platform[:release].nil?
end

def uname_r
@uname_r ||= (
res = @backend.run_command('uname -r').stdout
res.strip! unless res.nil?
res
)
def family_for_platform
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 redhatish_platform(conf)
Expand All @@ -111,11 +123,16 @@ def redhatish_version(conf)
conf[/release ([\d\.]+)/, 1]
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?

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
Expand Down
28 changes: 28 additions & 0 deletions lib/train/extras/uname.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion test/unit/extras/os_common_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
39 changes: 34 additions & 5 deletions test/unit/extras/os_detect_linux_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ def initialize
describe 'os_detect_linux' do
let(:detector) { OsDetectLinuxTester.new }

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_arch
detector.platform[:arch].must_equal("x86_64")
end
end

describe '#detect_linux_via_config' do

before do
Expand All @@ -27,11 +37,25 @@ 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

describe "/etc/redhat-release" do
describe "and /etc/os-release" do
it "sets the correct family, name, and release on centos" do
detector.stubs(:get_config).with("/etc/redhat-release").returns("CentOS Linux release 7.2.1511 (Core) \n")
detector.stubs(:get_config).with("/etc/os-release").returns("NAME=\"CentOS Linux\"\nVERSION=\"7 (Core)\"\nID=\"centos\"\nID_LIKE=\"rhel fedora\"\n")
detector.detect_linux_via_config.must_equal(true)
detector.platform[:name].must_equal('centos')
detector.platform[:family].must_equal('redhat')
detector.platform[:release].must_equal('redhat-version')
end
end
end

describe '/etc/debian_version' do

before { detector.stubs(:get_config).with('/etc/debian_version').returns('deb-version') }
Expand All @@ -41,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
Expand All @@ -51,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
Expand All @@ -62,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
Expand All @@ -73,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
Expand Down Expand Up @@ -102,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
Expand Down