diff options
author | Noah Kantrowitz <noah@coderanger.net> | 2018-05-01 20:57:41 -0700 |
---|---|---|
committer | Noah Kantrowitz <noah@coderanger.net> | 2018-05-01 20:57:41 -0700 |
commit | 5ecde4f735f67b661a2985b0ee8591ca5df8694f (patch) | |
tree | 1b6faa08e76089486699f566934a46e2f6092d9f | |
parent | 754bb43fac02fa10a79913ab085785f86daa9559 (diff) | |
download | ohai-5ecde4f735f67b661a2985b0ee8591ca5df8694f.tar.gz |
Add support for all OSes, with special guest star Windows.
This makes things work basically the same on all platforms, as much as possible.
Signed-off-by: Noah Kantrowitz <noah@coderanger.net>
-rw-r--r-- | lib/ohai/plugins/shard.rb | 33 | ||||
-rw-r--r-- | spec/unit/plugins/shard_spec.rb | 38 |
2 files changed, 67 insertions, 4 deletions
diff --git a/lib/ohai/plugins/shard.rb b/lib/ohai/plugins/shard.rb index 6998c559..7f6e34d1 100644 --- a/lib/ohai/plugins/shard.rb +++ b/lib/ohai/plugins/shard.rb @@ -17,7 +17,7 @@ # Ohai.plugin(:ShardSeed) do - depends "hostname", "dmi", "machine_id", "machinename", "fips" + depends "hostname", "dmi", "machine_id", "machinename", "fips", "hardware" provides "shard_seed" def get_dmi_property(dmi, thing) @@ -29,7 +29,12 @@ Ohai.plugin(:ShardSeed) do end def default_sources - [:machinename, :serial, :uuid] + case collect_os + when :linux, :darwin, :windows + [:machinename, :serial, :uuid] + else + [:machinename] + end end def default_digest_algorithm @@ -75,6 +80,28 @@ Ohai.plugin(:ShardSeed) do shard_seed digest_algorithm.hexdigest(data)[0...7].to_i(16) end + collect_data do + create_seed do |src| + raise "No such shard_seed source: #{src}" + end + end + + collect_data(:windows) do + require "wmi-lite/wmi" + wmi = WmiLite::Wmi.new + + create_seed do |src| + case src + when :serial + wmi.first_of("Win32_BIOS")["SerialNumber"] + when :uuid + wmi.first_of("Win32_ComputerSystemProduct")["UUID"] + else + raise "No such shard_seed source: #{src}" + end + end + end + collect_data(:darwin) do create_seed do |src| case src @@ -82,6 +109,8 @@ Ohai.plugin(:ShardSeed) do hardware["serial_number"] when :uuid hardware["platform_UUID"] + else + raise "No such shard_seed source: #{src}" end end end diff --git a/spec/unit/plugins/shard_spec.rb b/spec/unit/plugins/shard_spec.rb index 6e60711f..03fe8282 100644 --- a/spec/unit/plugins/shard_spec.rb +++ b/spec/unit/plugins/shard_spec.rb @@ -27,6 +27,7 @@ describe Ohai::System, "shard plugin" do let(:machine_id) { "0a1f869f457a4c8080ab19faf80af9cc" } let(:machinename) { "somehost004" } let(:fips) { false } + let(:os) { :linux } subject do plugin.run @@ -34,7 +35,6 @@ describe Ohai::System, "shard plugin" do end before(:each) do - allow(plugin).to receive(:collect_os).and_return(:linux) plugin["machinename"] = machinename plugin["machine_id"] = machine_id plugin["fqdn"] = fqdn @@ -42,7 +42,7 @@ describe Ohai::System, "shard plugin" do plugin["dmi"]["system"]["uuid"] = uuid plugin["dmi"]["system"]["serial_number"] = serial plugin["fips"] = { "kernel" => { "enabled" => fips } } - allow(plugin).to receive(:collect_os).and_return(:linux) + allow(plugin).to receive(:collect_os).and_return(os) end it "should provide a shard with a default-safe set of sources" do @@ -65,6 +65,40 @@ describe Ohai::System, "shard plugin" do expect(subject).to eq(117055036) end + context "with Darwin OS" do + let(:os) { :darwin } + before do + plugin["hardware"] = { "serial_number" => serial, "platform_UUID" => uuid } + end + + it "should provide a shard with a default-safe set of sources" do + expect(subject).to eq(27767217) + end + end + + context "with Windows OS" do + let(:os) { :windows } + before do + wmi = double("WmiLite::Wmi") + expect(WmiLite::Wmi).to receive(:new).and_return(wmi) + expect(wmi).to receive(:first_of).with("Win32_BIOS").and_return("SerialNumber" => serial) + expect(wmi).to receive(:first_of).with("Win32_ComputerSystemProduct").and_return("UUID" => uuid) + end + + it "should provide a shard with a default-safe set of sources" do + expect(subject).to eq(27767217) + end + end + + context "with a weird OS" do + let(:os) { :aix } + + it "should provide a shard with a default-safe set of sources" do + # Note: this is different than the other defaults. + expect(subject).to eq(253499154) + end + end + context "with FIPS mode enabled" do let(:fips) { true } |