diff options
author | Tim Smith <tsmith@chef.io> | 2017-06-02 07:35:06 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-06-02 07:35:06 -0700 |
commit | 63056754808a718e82cdc88684c27a6280aa9349 (patch) | |
tree | 7bb8c8749879bb2ec887bbb1293ae2e2a5b4b0d4 | |
parent | 0b6588ed5838c27519255f2b07af4cea70e0720e (diff) | |
parent | b6b7511a3d35392bdd735d47d6d3e14099c648cd (diff) | |
download | ohai-63056754808a718e82cdc88684c27a6280aa9349.tar.gz |
Merge pull request #1008 from chef/hardware_shellout
Only shellout to sw_vers in darwin hardware once
-rw-r--r-- | lib/ohai/plugins/darwin/hardware.rb | 22 | ||||
-rw-r--r-- | spec/unit/plugins/darwin/hardware_spec.rb | 16 |
2 files changed, 16 insertions, 22 deletions
diff --git a/lib/ohai/plugins/darwin/hardware.rb b/lib/ohai/plugins/darwin/hardware.rb index 5497f36a..c9c4bab0 100644 --- a/lib/ohai/plugins/darwin/hardware.rb +++ b/lib/ohai/plugins/darwin/hardware.rb @@ -46,16 +46,22 @@ Ohai.plugin(:Hardware) do hw_hash[0]["_items"][0].delete("_name") hardware.merge!(hw_hash[0]["_items"][0]) - { - "operating_system" => "sw_vers -productName", - "operating_system_version" => "sw_vers -productVersion", - "build_version" => "sw_vers -buildVersion", - "architecture" => "uname -m", - }.each do |var, cmd| - os_info = shell_out(cmd).stdout - hardware[var] = os_info.strip unless os_info.nil? + # ProductName: Mac OS X + # ProductVersion: 10.12.5 + # BuildVersion: 16F73 + shell_out("sw_vers").stdout.lines.each do |line| + case line + when /^ProductName:\s*(.*)$/ + hardware["operating_system"] = Regexp.last_match[1].strip + when /^ProductVersion:\s*(.*)$/ + hardware["operating_system_version"] = Regexp.last_match[1].strip + when /^BuildVersion:\s*(.*)$/ + hardware["build_version"] = Regexp.last_match[1].strip + end end + hardware["architecture"] = shell_out("uname -m").stdout.strip + # Storage queries storage = [] storage_hash = system_profiler("SPStorageDataType") diff --git a/spec/unit/plugins/darwin/hardware_spec.rb b/spec/unit/plugins/darwin/hardware_spec.rb index 3f0f6b0d..f8c8e1d5 100644 --- a/spec/unit/plugins/darwin/hardware_spec.rb +++ b/spec/unit/plugins/darwin/hardware_spec.rb @@ -31,21 +31,9 @@ describe Ohai::System, "Darwin hardware plugin", :unix_only do ) allow(plugin).to receive(:shell_out).with( - "sw_vers -productName" + "sw_vers" ).and_return( - mock_shell_out(0, "Mac OS X", "") - ) - - allow(plugin).to receive(:shell_out).with( - "sw_vers -productVersion" - ).and_return( - mock_shell_out(0, "10.12", "") - ) - - allow(plugin).to receive(:shell_out).with( - "sw_vers -buildVersion" - ).and_return( - mock_shell_out(0, "16A239j", "") + mock_shell_out(0, "ProductName: Mac OS X\nProductVersion: 10.12\nBuildVersion: 16A239j", "") ) allow(plugin).to receive(:shell_out).with( |