Skip to content

Commit

Permalink
Merge pull request #751 from tas50/private_clouds_4_life
Browse files Browse the repository at this point in the history
Detect Openstack hosts
  • Loading branch information
tas50 committed Mar 9, 2016
2 parents 7a59cf9 + 0a62603 commit 97d8a88
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/ohai/plugins/linux/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def docker_exists?
which("docker")
end

def nova_exists?
which("nova")
end

collect_data(:linux) do
virtualization Mash.new unless virtualization
virtualization[:systems] = Mash.new unless virtualization[:systems]
Expand Down Expand Up @@ -74,6 +78,13 @@ def docker_exists?
end
end

# if nova binary is present we're on an openstack host
if nova_exists?
virtualization[:system] = "openstack"
virtualization[:role] = "host"
virtualization[:systems][:openstack] = "host"
end

# Detect paravirt KVM/QEMU from cpuinfo, report as KVM
if File.exist?("/proc/cpuinfo")
if File.read("/proc/cpuinfo") =~ /QEMU Virtual CPU|Common KVM processor|Common 32-bit KVM processor/
Expand Down
6 changes: 5 additions & 1 deletion lib/ohai/util/file_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ def which(cmd)
paths = ENV["PATH"].split(File::PATH_SEPARATOR) + [ "/bin", "/usr/bin", "/sbin", "/usr/sbin" ]
paths.each do |path|
filename = File.join(path, cmd)
return filename if File.executable?(filename)
if File.executable?(filename)
Ohai::Log.debug("#{self.name} plugin: found #{cmd} at #{filename}")
return filename
end
end
Ohai::Log.debug("#{self.name} plugin: did not find #{cmd}")
false
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/unit/plugins/linux/virtualization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
allow(File).to receive(:exist?).with("/.dockerinit").and_return(false)
allow(File).to receive(:exist?).with("/proc/bus/pci/devices").and_return(false)
allow(File).to receive(:exist?).with("/sys/devices/virtual/misc/kvm").and_return(false)

# default the which wrappers to nil
allow(plugin).to receive(:lxc_version_exists?).and_return(false)
allow(plugin).to receive(:docker_exists?).and_return(false)
allow(plugin).to receive(:nova_exists?).and_return(false)
end

describe "when we are checking for xen" do
Expand Down Expand Up @@ -77,6 +82,16 @@
end
end

describe "when we are checking for openstack" do
it "sets openstack host if nova binary exists" do
allow(plugin).to receive(:nova_exists?).and_return("/usr/bin/nova")
plugin.run
expect(plugin[:virtualization][:system]).to eq("openstack")
expect(plugin[:virtualization][:role]).to eq("host")
expect(plugin[:virtualization][:systems][:openstack]).to eq("host")
end
end

describe "when we are checking for kvm" do
it "sets kvm guest if /sys/devices/virtual/misc/kvm exists & hypervisor cpu feature is present" do
allow(File).to receive(:exist?).with("/sys/devices/virtual/misc/kvm").and_return(true)
Expand Down
1 change: 1 addition & 0 deletions spec/unit/util/file_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class FileHelperMock
let(:file_helper) { FileHelperMock.new }

before(:each) do
allow(file_helper).to receive(:name).and_return("Fakeclass")
allow(File).to receive(:executable?).and_return(false)
end

Expand Down

0 comments on commit 97d8a88

Please sign in to comment.