summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2015-08-11 17:01:19 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2015-08-11 17:01:19 -0700
commit35a19e5ce5f59f1a9cf63e6431a8f4e59bd3cda6 (patch)
tree8ec3f18bef6d3cbb5f3db155517e281534f15313
parent6d99d88b6ed41f21869d1c091928235ffc145865 (diff)
parent8570ac53b1151aa562b6bfa5b303fa1094000b7e (diff)
downloadohai-35a19e5ce5f59f1a9cf63e6431a8f4e59bd3cda6.tar.gz
Merge pull request #597 from chef/lcg/cisco-refactor
Lcg/cisco refactor
-rw-r--r--lib/ohai/plugins/linux/platform.rb65
-rw-r--r--spec/unit/plugins/linux/platform_spec.rb68
2 files changed, 112 insertions, 21 deletions
diff --git a/lib/ohai/plugins/linux/platform.rb b/lib/ohai/plugins/linux/platform.rb
index a383c222..40c29b6b 100644
--- a/lib/ohai/plugins/linux/platform.rb
+++ b/lib/ohai/plugins/linux/platform.rb
@@ -28,18 +28,48 @@ Ohai.plugin(:Platform) do
contents[/Rawhide/i] ? contents[/((\d+) \(Rawhide\))/i, 1].downcase : contents[/release ([\d\.]+)/, 1]
end
- def os_release_file_is_cisco?
- return false unless File.exist?('/etc/os-release')
- os_release_info = File.read('/etc/os-release').split.inject({}) do |map, key_value_line|
- key, _separator, value = key_value_line.partition('=')
- map[key] = value
+ #
+ # Reads an os-release-info file and parse it into a hash
+ #
+ # @param file [String] the filename to read (e.g. '/etc/os-release')
+ #
+ # @returns [Hash] the file parsed into a Hash or nil
+ #
+ def read_os_release_info(file)
+ return nil unless File.exist?(file)
+ File.read(file).split.inject({}) do |map, line|
+ key, value = line.split('=')
+ map[key] = value.gsub(/\A"|"\Z/, '') if value
map
end
- if os_release_info['CISCO_RELEASE_INFO'] && File.exist?(os_release_info['CISCO_RELEASE_INFO'])
- os_release_info
- else
- false
- end
+ end
+
+ #
+ # Cached /etc/os-release info Hash. Also has logic for Cisco Nexus
+ # switches that pulls the chained CISCO_RELEASE_INFO file into the Hash (other
+ # distros can also reuse this method safely).
+ #
+ # @returns [Hash] the canonical, cached Hash of /etc/os-release info or nil
+ #
+ def os_release_info
+ @os_release_info ||=
+ begin
+ os_release_info = read_os_release_info('/etc/os-release')
+ cisco_release_info = os_release_info['CISCO_RELEASE_INFO'] if os_release_info
+ if cisco_release_info && File.exist?(cisco_release_info)
+ os_release_info.merge!(read_os_release_info(cisco_release_info))
+ end
+ os_release_info
+ end
+ end
+
+ #
+ # If /etc/os-release indicates we are Cisco based
+ #
+ # @returns [Boolean] if we are Cisco according to /etc/os-release
+ #
+ def os_release_file_is_cisco?
+ File.exist?('/etc/os-release') && os_release_info['CISCO_RELEASE_INFO']
end
collect_data(:linux) do
@@ -74,10 +104,9 @@ Ohai.plugin(:Platform) do
platform get_redhatish_platform(contents)
platform_version contents.match(/(\d\.\d\.\d)/)[0]
elsif File.exist?("/etc/redhat-release")
- if File.exist?('/etc/os-release') && (os_release_info = os_release_file_is_cisco? ) # check if Cisco
- platform os_release_info['ID']
- platform_family os_release_info['ID_LIKE']
- platform_version os_release_info['VERSION'] || ""
+ if os_release_file_is_cisco? # Cisco guestshell
+ platform 'nexus_centos'
+ platform_version os_release_info['VERSION']
else
contents = File.read("/etc/redhat-release").chomp
platform get_redhatish_platform(contents)
@@ -113,6 +142,12 @@ Ohai.plugin(:Platform) do
# no way to determine platform_version in a rolling release distribution
# kernel release will be used - ex. 3.13
platform_version `uname -r`.strip
+ elsif os_release_file_is_cisco?
+ raise "unknown Cisco /etc/os-release ID field" unless os_release_info['ID'].include?('nexus')
+ raise "unknown Cisco /etc/os-release ID-LIKE field" unless os_release_info['ID_LIKE'].include?('wrlinux')
+ platform 'nexus'
+ platform_family 'wrlinux'
+ platform_version os_release_info['VERSION']
elsif lsb[:id] =~ /RedHat/i
platform "redhat"
platform_version lsb[:release]
@@ -135,7 +170,7 @@ Ohai.plugin(:Platform) do
platform_family "debian"
when /fedora/, /pidora/
platform_family "fedora"
- when /oracle/, /centos/, /redhat/, /scientific/, /enterpriseenterprise/, /amazon/, /xenserver/, /cloudlinux/, /ibm_powerkvm/, /parallels/ # Note that 'enterpriseenterprise' is oracle's LSB "distributor ID"
+ when /oracle/, /centos/, /redhat/, /scientific/, /enterpriseenterprise/, /amazon/, /xenserver/, /cloudlinux/, /ibm_powerkvm/, /parallels/, /nexus_centos/ # Note that 'enterpriseenterprise' is oracle's LSB "distributor ID"
platform_family "rhel"
when /suse/
platform_family "suse"
diff --git a/spec/unit/plugins/linux/platform_spec.rb b/spec/unit/plugins/linux/platform_spec.rb
index 80f4bc4e..dbd5a431 100644
--- a/spec/unit/plugins/linux/platform_spec.rb
+++ b/spec/unit/plugins/linux/platform_spec.rb
@@ -1,6 +1,6 @@
#
-# Author:: Adam Jacob (<adam@opscode.com>)
-# Copyright:: Copyright (c) 2008 Opscode, Inc.
+# Author:: Adam Jacob (<adam@chef.io>)
+# Copyright:: Copyright (c) 2008-2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -35,6 +35,7 @@ describe Ohai::System, "Linux plugin platform" do
let(:have_parallels_release) { false }
let(:have_raspi_config) { false }
let(:have_os_release) { false }
+ let(:have_cisco_release) { false }
before(:each) do
@plugin = get_plugin("linux/platform")
@@ -53,6 +54,7 @@ describe Ohai::System, "Linux plugin platform" do
allow(File).to receive(:exist?).with("/etc/parallels-release").and_return(have_parallels_release)
allow(File).to receive(:exist?).with("/usr/bin/raspi-config").and_return(have_raspi_config)
allow(File).to receive(:exist?).with("/etc/os-release").and_return(have_os_release)
+ allow(File).to receive(:exist?).with("/etc/shared/os-release").and_return(have_cisco_release)
allow(File).to receive(:read).with("PLEASE STUB ALL File.read CALLS")
end
@@ -366,8 +368,8 @@ OS_RELEASE
end
before do
- expect(File).to receive(:read).with("/etc/os-release").and_return(os_release_content)
expect(File).to receive(:read).with("/etc/redhat-release").and_return("CentOS release 7.1")
+ expect(File).to receive(:read).with("/etc/os-release").and_return(os_release_content)
end
it "correctly detects EL7" do
@@ -378,6 +380,61 @@ OS_RELEASE
end
+ context "on 'guestshell' with /etc/os-release and overrides for Cisco Nexus" do
+
+ let(:have_os_release) { true }
+
+ let(:os_release_content) do
+ <<-OS_RELEASE
+NAME="CentOS Linux"
+VERSION="7 (Core)"
+ID="centos"
+ID_LIKE="rhel fedora"
+VERSION_ID="7"
+PRETTY_NAME="CentOS Linux 7 (Core)"
+ANSI_COLOR="0;31"
+CPE_NAME="cpe:/o:centos:centos:7"
+HOME_URL="https://www.centos.org/"
+BUG_REPORT_URL="https://bugs.centos.org/"
+
+CENTOS_MANTISBT_PROJECT="CentOS-7"
+CENTOS_MANTISBT_PROJECT_VERSION="7"
+REDHAT_SUPPORT_PRODUCT="centos"
+REDHAT_SUPPORT_PRODUCT_VERSION="7"
+
+CISCO_RELEASE_INFO=/etc/shared/os-release
+OS_RELEASE
+ end
+
+ let(:have_cisco_release) { true }
+
+ let(:cisco_release_content) do
+ <<-CISCO_RELEASE
+ID=nexus
+ID_LIKE=wrlinux
+NAME=Nexus
+VERSION="7.0(3)I2(0.475E.6)"
+VERSION_ID="7.0(3)I2"
+PRETTY_NAME="Nexus 7.0(3)I2"
+HOME_URL=http://www.cisco.com
+BUILD_ID=6
+CISCO_RELEASE_INFO=/etc/os-release
+CISCO_RELEASE
+ end
+
+ before do
+ expect(File).to receive(:read).at_least(:once).with("/etc/os-release").and_return(os_release_content)
+ expect(File).to receive(:read).with("/etc/shared/os-release").and_return(cisco_release_content)
+ end
+
+ it "should set platform to nexus_guestshell and platform_family to rhel" do
+ @plugin.run
+ expect(@plugin[:platform]).to start_with("nexus")
+ expect(@plugin[:platform]).to eq("nexus_centos")
+ expect(@plugin[:platform_family]).to eq("rhel")
+ expect(@plugin[:platform_version]).to eq("7.0(3)I2(0.475E.6)")
+ end
+ end
end
end
@@ -595,16 +652,15 @@ OS_RELEASE
describe "on Wind River Linux for Cisco Nexus" do
- let(:have_redhat_release) { true }
-
let(:have_os_release) { true }
it "should set platform to nexus and platform_family to wrlinux" do
@plugin.lsb = nil
- expect(File).to receive(:read).with("/etc/os-release").and_return("ID_LIKE=wrlinux\nID=nexus\nCISCO_RELEASE_INFO=/etc/os-release")
+ expect(File).to receive(:read).twice.with("/etc/os-release").and_return("ID=nexus\nID_LIKE=wrlinux\nNAME=Nexus\nVERSION=\"7.0(3)I2(0.475E.6)\"\nVERSION_ID=\"7.0(3)I2\"\nPRETTY_NAME=\"Nexus 7.0(3)I2\"\nHOME_URL=http://www.cisco.com\nBUILD_ID=6\nCISCO_RELEASE_INFO=/etc/os-release")
@plugin.run
expect(@plugin[:platform]).to eq("nexus")
expect(@plugin[:platform_family]).to eq("wrlinux")
+ expect(@plugin[:platform_version]).to eq("7.0(3)I2(0.475E.6)")
end
end
end