Skip to content

Commit

Permalink
Detect arch via uname -m on Linux and OS X
Browse files Browse the repository at this point in the history
Fixes #772
  • Loading branch information
stevendanna committed Aug 1, 2016
1 parent 2f4fdc3 commit b644f6f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 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 Down
4 changes: 4 additions & 0 deletions lib/train/extras/os_detect_darwin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ 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?
Expand All @@ -26,6 +29,7 @@ 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
true
end
end
Expand Down
18 changes: 16 additions & 2 deletions lib/train/extras/os_detect_linux.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,21 @@ def family_for_platform
end

def uname_s
@uname_s ||= @backend.run_command('uname -s').stdout
@uname_s ||= backend.run_command('uname -s').stdout
end

def uname_r
@uname_r ||= (
res = @backend.run_command('uname -r').stdout
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
Expand All @@ -126,10 +130,20 @@ 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
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

return true if detect_linux_via_config
return true if detect_linux_via_lsb
Expand Down
10 changes: 10 additions & 0 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_architecture' 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")
end
end

describe '#detect_linux_via_config' do

before do
Expand Down

0 comments on commit b644f6f

Please sign in to comment.