From 66b5c39928cd08458b27d13e892be248a74c88ac Mon Sep 17 00:00:00 2001 From: enemarke Date: Fri, 14 Jul 2017 22:37:00 +0200 Subject: [PATCH 1/4] Incorporated interface_driver into ethtool for puppet 4 support --- lib/facter/ethtool.rb | 51 +++++++++-------- lib/facter/interface_driver.rb | 30 ---------- manifests/init.pp | 2 +- spec/fixtures/ethtool_outputs/enp0s25.txt | 22 ++++++++ .../ethtool_outputs/i/ubuntuxenial.txt | 10 ++++ spec/fixtures/ethtool_outputs/vboxnet0.txt | 16 ++++++ spec/unit/facts/ethtool_spec.rb | 56 ++++++------------- 7 files changed, 93 insertions(+), 94 deletions(-) delete mode 100644 lib/facter/interface_driver.rb create mode 100644 spec/fixtures/ethtool_outputs/enp0s25.txt create mode 100644 spec/fixtures/ethtool_outputs/i/ubuntuxenial.txt create mode 100644 spec/fixtures/ethtool_outputs/vboxnet0.txt diff --git a/lib/facter/ethtool.rb b/lib/facter/ethtool.rb index fad68ce..ca99789 100644 --- a/lib/facter/ethtool.rb +++ b/lib/facter/ethtool.rb @@ -9,13 +9,13 @@ def self.exists? end # Run ethtool on an interface - def self.ethtool(interface) - %x{/usr/sbin/ethtool #{Shellwords.escape(interface)} 2>/dev/null} + def self.ethtool(interface, option='') + %x{/usr/sbin/ethtool #{option} #{Shellwords.escape(interface)} 2>/dev/null} end # Get all interfaces on the system def self.interfaces - Dir.foreach('/sys/class/net').reject{|x| x.start_with?('.', 'veth')} + Dir.foreach('/sys/class/net').reject {|x| x.start_with?('.', 'veth')} end # Convert raw interface names into a canonical version @@ -27,11 +27,12 @@ def self.alphafy str def self.gather interfaces.inject({}) do |interfaces, interface| output = ethtool(interface) + output_i = ethtool(interface, '-i') metrics = {} # Extract the interface speed - speedline = output.split("\n").detect{|x| x.include?('Speed:')} + speedline = output.split("\n").detect {|x| x.include?('Speed:')} speed = speedline && speedline.scan(/\d+/).first metrics['speed'] = speed.to_i if speed @@ -40,6 +41,14 @@ def self.gather max_speed = linkmodes && linkmodes.scan(/\d+/).map(&:to_i).max metrics['max_speed'] = max_speed.to_i if max_speed + # Extract the interface driver info + driver_line = output_i.match(/^driver:\s+(?.*)/) + metrics['driver'] = driver_line[:driver].to_s if driver_line + + # Extract the interface driver info + driver_version_line = output_i.match(/^version:\s+(?.*)/) + metrics['driver_version'] = driver_version_line[:version].to_s if driver_version_line + # Gather the interface statistics next interfaces if metrics.empty? interfaces[alphafy(interface)] = metrics @@ -50,7 +59,7 @@ def self.gather # Gather all facts def self.facts # Ethtool isn't installed, don't collect facts - return if ! exists? + return if !exists? ifstats = gather @@ -63,29 +72,23 @@ def self.facts end # Legacy facts - ifstats.each do |interface, data| - next unless data['speed'] - Facter.add('speed_' + interface) do - confine :kernel => 'Linux' - setcode do - # Backwards compatibility - data['speed'].to_s - end - end - end - - ifstats.each do |interface, data| - next unless data['max_speed'] - Facter.add('maxspeed_' + interface) do - confine :kernel => 'Linux' - setcode do - # Backwards compatibility - data['max_speed'].to_s + ['speed', 'maxspeed', 'driver', 'driver_version'].each do |fact_name| + ifstats.each do |interface, data| + next unless data[fact_name] + Facter.add("#{fact_name}_" + interface) do + confine :kernel => 'Linux' + setcode do + # Backwards compatibility + if fact_name == 'maxspeed' + data['max_speed'].to_s + else + data[fact_name].to_s + end + end end end end end - end end diff --git a/lib/facter/interface_driver.rb b/lib/facter/interface_driver.rb deleted file mode 100644 index fdb0b9f..0000000 --- a/lib/facter/interface_driver.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'facter' -require 'facter/util/ip' -require 'json' -Facter::Util::IP.get_interfaces.each do |interface| - next if interface.start_with?('veth') - Facter.debug("Running ethtool on interface #{interface}") - data = {} - Facter::Util::Resolution.exec("ethtool -i #{interface} 2>/dev/null").split("\n").each do |line| - k, v = line.split(': ') - if v - data[k]=v - end - end - if data['driver'] - Facter.add('driver_' + Facter::Util::IP.alphafy(interface)) do - confine :kernel => "Linux" - setcode do - data['driver'] - end - end - end - if data['version'] - Facter.add('driver_version' + Facter::Util::IP.alphafy(interface)) do - confine :kernel => "Linux" - setcode do - data['version'] - end - end - end -end diff --git a/manifests/init.pp b/manifests/init.pp index 47b8db6..4cc8d1f 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -9,7 +9,7 @@ # is installed on the system. # class ethtool ( - $ensure_installed = true + Boolean $ensure_installed = true ) { validate_bool($ensure_installed) diff --git a/spec/fixtures/ethtool_outputs/enp0s25.txt b/spec/fixtures/ethtool_outputs/enp0s25.txt new file mode 100644 index 0000000..706b0df --- /dev/null +++ b/spec/fixtures/ethtool_outputs/enp0s25.txt @@ -0,0 +1,22 @@ +Settings for enp0s25: + Supported ports: [ TP ] + Supported link modes: 10baseT/Half 10baseT/Full + 100baseT/Half 100baseT/Full + 1000baseT/Full + Supported pause frame use: No + Supports auto-negotiation: Yes + Advertised link modes: 10baseT/Half 10baseT/Full + 100baseT/Half 100baseT/Full + 1000baseT/Full + Advertised pause frame use: No + Advertised auto-negotiation: Yes + Speed: Unknown! + Duplex: Unknown! (255) + Port: Twisted Pair + PHYAD: 2 + Transceiver: internal + Auto-negotiation: on + MDI-X: Unknown (auto) + Current message level: 0x00000007 (7) + drv probe link + Link detected: no \ No newline at end of file diff --git a/spec/fixtures/ethtool_outputs/i/ubuntuxenial.txt b/spec/fixtures/ethtool_outputs/i/ubuntuxenial.txt new file mode 100644 index 0000000..3b81fd5 --- /dev/null +++ b/spec/fixtures/ethtool_outputs/i/ubuntuxenial.txt @@ -0,0 +1,10 @@ +driver: ath10k_pci +version: 4.4.0-77-generic +firmware-version: WLAN.RM.2.0-00180-QCARMSWPZ-1 +expansion-rom-version: +bus-info: 0000:3a:00.0 +supports-statistics: yes +supports-test: no +supports-eeprom-access: no +supports-register-dump: no +supports-priv-flags: no diff --git a/spec/fixtures/ethtool_outputs/vboxnet0.txt b/spec/fixtures/ethtool_outputs/vboxnet0.txt new file mode 100644 index 0000000..004b8e1 --- /dev/null +++ b/spec/fixtures/ethtool_outputs/vboxnet0.txt @@ -0,0 +1,16 @@ +Settings for vboxnet0: + Supported ports: [ ] + Supported link modes: Not reported + Supported pause frame use: No + Supports auto-negotiation: No + Advertised link modes: Not reported + Advertised pause frame use: No + Advertised auto-negotiation: No + Speed: 10Mb/s + Duplex: Full + Port: Twisted Pair + PHYAD: 0 + Transceiver: internal + Auto-negotiation: off + MDI-X: Unknown + Link detected: no \ No newline at end of file diff --git a/spec/unit/facts/ethtool_spec.rb b/spec/unit/facts/ethtool_spec.rb index 1a86f82..b83608a 100644 --- a/spec/unit/facts/ethtool_spec.rb +++ b/spec/unit/facts/ethtool_spec.rb @@ -31,45 +31,23 @@ describe '#gather' do it 'returns a hash of interfaces and their speeds' do Ethtool::Facts.stubs(:interfaces).returns(['enp0s25', 'vboxnet0']) - Ethtool::Facts.stubs(:ethtool).with('enp0s25').returns('Settings for enp0s25: - Supported ports: [ TP ] - Supported link modes: 10baseT/Half 10baseT/Full - 100baseT/Half 100baseT/Full - 1000baseT/Full - Supported pause frame use: No - Supports auto-negotiation: Yes - Advertised link modes: 10baseT/Half 10baseT/Full - 100baseT/Half 100baseT/Full - 1000baseT/Full - Advertised pause frame use: No - Advertised auto-negotiation: Yes - Speed: Unknown! - Duplex: Unknown! (255) - Port: Twisted Pair - PHYAD: 2 - Transceiver: internal - Auto-negotiation: on - MDI-X: Unknown (auto) - Current message level: 0x00000007 (7) - drv probe link - Link detected: no') - Ethtool::Facts.stubs(:ethtool).with('vboxnet0').returns('Settings for vboxnet0: - Supported ports: [ ] - Supported link modes: Not reported - Supported pause frame use: No - Supports auto-negotiation: No - Advertised link modes: Not reported - Advertised pause frame use: No - Advertised auto-negotiation: No - Speed: 10Mb/s - Duplex: Full - Port: Twisted Pair - PHYAD: 0 - Transceiver: internal - Auto-negotiation: off - MDI-X: Unknown - Link detected: no') - expect(Ethtool::Facts.gather).to eq({'enp0s25'=>{'max_speed'=>1000},'vboxnet0'=>{'speed'=>10}}) + Ethtool::Facts.stubs(:ethtool).with('enp0s25'). + returns(IO.read("#{File.dirname(__FILE__)}/../../fixtures/ethtool_outputs/enp0s25.txt")) + Ethtool::Facts.stubs(:ethtool).with('enp0s25', '-i'). + returns(IO.read("#{File.dirname(__FILE__)}/../../fixtures/ethtool_outputs/i/ubuntuxenial.txt")) + Ethtool::Facts.stubs(:ethtool).with('vboxnet0'). + returns(IO.read("#{File.dirname(__FILE__)}/../../fixtures/ethtool_outputs/vboxnet0.txt")) + Ethtool::Facts.stubs(:ethtool).with('vboxnet0', '-i'). + returns(IO.read("#{File.dirname(__FILE__)}/../../fixtures/ethtool_outputs/i/ubuntuxenial.txt")) + + expect(Ethtool::Facts.gather).to eq({ + 'enp0s25' => {'max_speed' => 1000, + 'driver' => 'ath10k_pci', + 'driver_version' => '4.4.0-77-generic', }, + 'vboxnet0' => {'speed' => 10, + 'driver' => 'ath10k_pci', + 'driver_version' => '4.4.0-77-generic', } + }) end end From 0ab816b46f69aafe2b6d147d72b376f40ab54190 Mon Sep 17 00:00:00 2001 From: enemarke Date: Sun, 16 Jul 2017 20:33:21 +0200 Subject: [PATCH 2/4] Removed boolead due to failing build test. --- manifests/init.pp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifests/init.pp b/manifests/init.pp index 4cc8d1f..47b8db6 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -9,7 +9,7 @@ # is installed on the system. # class ethtool ( - Boolean $ensure_installed = true + $ensure_installed = true ) { validate_bool($ensure_installed) From c5506bbb2f18a1b6c651e915b32e316716f067a8 Mon Sep 17 00:00:00 2001 From: enemarke Date: Sun, 16 Jul 2017 21:09:12 +0200 Subject: [PATCH 3/4] Updated test env to run on puppet 4, also removed tests that ran on puppet < 3.8.7 --- .travis.yml | 26 +++++++------------------- Gemfile | 3 ++- 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/.travis.yml b/.travis.yml index af7f108..b97decf 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,33 +7,21 @@ rvm: - 1.9.3 - 2.0.0 - 2.1.0 + - 2.1.5 script: bundle exec rake test env: - - PUPPET_VERSION="~> 3.2.0" - - PUPPET_VERSION="~> 3.3.0" - - PUPPET_VERSION="~> 3.4.0" - - PUPPET_VERSION="~> 3.5.0" - - PUPPET_VERSION="~> 3.6.0" - - PUPPET_VERSION="~> 3.7.0" - - PUPPET_VERSION="~> 3.8.0" STRICT_VARIABLES=no FUTURE_PARSER=no - - PUPPET_VERSION="~> 3.8.0" STRICT_VARIABLES=yes FUTURE_PARSER=yes + - PUPPET_VERSION="~> 3.8.7" STRICT_VARIABLES=no FUTURE_PARSER=no + - PUPPET_VERSION="~> 3.8.7" STRICT_VARIABLES=yes FUTURE_PARSER=yes + - PUPPET_VERSION="~> 4.10.0" matrix: exclude: # Ruby 1.9.3 - rvm: 1.9.3 - env: PUPPET_VERSION="~> 2.7.0" - + env: PUPPET_VERSION="~> 4.10.0" # Ruby 2.0.0 - rvm: 2.0.0 - env: PUPPET_VERSION="~> 2.7.0" - + env: PUPPET_VERSION="~> 4.10.0" # Ruby 2.1.0 - rvm: 2.1.0 - env: PUPPET_VERSION="~> 2.7.0" - - rvm: 2.1.0 - env: PUPPET_VERSION="~> 3.2.0" - - rvm: 2.1.0 - env: PUPPET_VERSION="~> 3.3.0" - - rvm: 2.1.0 - env: PUPPET_VERSION="~> 3.4.0" + env: PUPPET_VERSION="~> 4.10.0" diff --git a/Gemfile b/Gemfile index 76ddbfb..fffbcf5 100644 --- a/Gemfile +++ b/Gemfile @@ -4,12 +4,13 @@ group :test do gem "rake" gem "json", RUBY_VERSION == '1.9.3' && '< 2.0.0' || nil gem "json_pure", RUBY_VERSION == '1.9.3' && '< 2.0.0' || nil - gem "puppet", ENV['PUPPET_VERSION'] || '~> 3.4.0' + gem "puppet", ENV['PUPPET_VERSION'] gem "puppet-lint" gem "rspec-puppet", :git => 'https://github.com/rodjek/rspec-puppet.git' gem "puppet-syntax" gem "metadata-json-lint" gem "puppetlabs_spec_helper" + gem "semantic_puppet" end group :development do From 6564faf7b01f6ea133cde007c898111e0ab70501 Mon Sep 17 00:00:00 2001 From: enemarke Date: Sun, 16 Jul 2017 21:20:07 +0200 Subject: [PATCH 4/4] Updated metadata to match metedata_lint --- Rakefile | 2 +- metadata.json | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Rakefile b/Rakefile index e28fd0d..3833264 100644 --- a/Rakefile +++ b/Rakefile @@ -36,5 +36,5 @@ task :test => [ :syntax, :lint, :spec, - :metadata, + :metadata_lint, ] diff --git a/metadata.json b/metadata.json index 7078847..4092b1e 100644 --- a/metadata.json +++ b/metadata.json @@ -1,5 +1,5 @@ { - "name": "bobtfish/ethtool", + "name": "bobtfish-ethtool", "version": "0.0.7", "author": "Tomas Doran", "summary": "Manage ethernet interfaces with ethtool", @@ -11,7 +11,7 @@ "dependencies": [ { "name": "puppetlabs-stdlib", - "version_requirement": ">= 1.0.0" + "version_requirement": ">= 1.0.0 < 5.0.0" } ], "operatingsystem_support": [