From fe800255fda66386c3ea96b2120b62d13a6abd69 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Tue, 25 Aug 2020 15:50:23 -0700 Subject: [PATCH 1/4] Resolve new chefstyle warnings Signed-off-by: Tim Smith --- lib/ohai/plugins/ec2.rb | 2 +- lib/ohai/plugins/shard.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb index b4fb735e8..c526a37b5 100644 --- a/lib/ohai/plugins/ec2.rb +++ b/lib/ohai/plugins/ec2.rb @@ -114,7 +114,7 @@ def looks_like_ec2? end collect_data do - require "base64" + require "base64" unless defined?(Base64) if looks_like_ec2? logger.trace("Plugin EC2: looks_like_ec2? == true") diff --git a/lib/ohai/plugins/shard.rb b/lib/ohai/plugins/shard.rb index cf7641b03..bfd072125 100644 --- a/lib/ohai/plugins/shard.rb +++ b/lib/ohai/plugins/shard.rb @@ -52,7 +52,7 @@ def default_digest_algorithm def digest_algorithm case Ohai.config[:plugin][:shard_seed][:digest_algorithm] || default_digest_algorithm when "md5" - require "digest/md5" + require "digest/md5" unless defined?(Digest::MD5) Digest::MD5 when "sha256" require "openssl/digest" From 149f7bdcd52a64df91bb3dc75877999ee5349172 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Tue, 8 Sep 2020 10:25:26 -0700 Subject: [PATCH 2/4] Simplify things with &. We can avoid some duplicate checks by using &. in places Signed-off-by: Tim Smith --- lib/ohai/plugins/linux/network.rb | 8 +++----- lib/ohai/plugins/softlayer.rb | 2 +- lib/ohai/plugins/solaris2/network.rb | 12 +++++------- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index 54523e118..0f5c8c02a 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -141,11 +141,9 @@ def check_routing_table(family, iface, default_route_table) # using a temporary var to hold routes and their interface name def parse_routes(family, iface) iface.collect do |i, iv| - if iv[:routes] - iv[:routes].collect do |r| - r.merge(dev: i) if r[:family] == family[:name] - end.compact - end + iv[:routes]&.collect do |r| + r.merge(dev: i) if r[:family] == family[:name] + end&.compact end.compact.flatten end diff --git a/lib/ohai/plugins/softlayer.rb b/lib/ohai/plugins/softlayer.rb index 34b2c343c..fd48e9fa9 100644 --- a/lib/ohai/plugins/softlayer.rb +++ b/lib/ohai/plugins/softlayer.rb @@ -39,7 +39,7 @@ def looks_like_softlayer? logger.trace("Plugin Softlayer: looks_like_softlayer? == true") metadata = fetch_metadata softlayer Mash.new - metadata.each { |k, v| softlayer[k] = v } if metadata + metadata&.each { |k, v| softlayer[k] = v } else logger.trace("Plugin Softlayer: looks_like_softlayer? == false") end diff --git a/lib/ohai/plugins/solaris2/network.rb b/lib/ohai/plugins/solaris2/network.rb index 1c7ff0f1b..3f15bfcde 100644 --- a/lib/ohai/plugins/solaris2/network.rb +++ b/lib/ohai/plugins/solaris2/network.rb @@ -86,7 +86,7 @@ def arpname_to_ifname(iface, arpname) def full_interface_name(iface, part_name, index) iface.each do |name, attrs| - next unless attrs && attrs.respond_to?(:[]) + next unless attrs&.respond_to?(:[]) return name if /^#{part_name}($|:)/.match(name) && attrs[:index] == index end @@ -155,12 +155,10 @@ def full_interface_name(iface, part_name, index) break end end - if iface[ifn][:arp] - iface[ifn][:arp].each_key do |addr| - if addr.eql?(iaddr) - iface[ifn][:addresses][iface[ifn][:arp][iaddr]] = { "family" => "lladdr" } - break - end + iface[ifn][:arp]&.each_key do |addr| + if addr.eql?(iaddr) + iface[ifn][:addresses][iface[ifn][:arp][iaddr]] = { "family" => "lladdr" } + break end end end From 3485634758d5a5e16e3a579a77b6d18a1fab5176 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Tue, 8 Sep 2020 11:46:40 -0700 Subject: [PATCH 3/4] Simplify respond_to? There's no need for the &. here Signed-off-by: Tim Smith --- lib/ohai/plugins/solaris2/network.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ohai/plugins/solaris2/network.rb b/lib/ohai/plugins/solaris2/network.rb index 3f15bfcde..c4a223ff3 100644 --- a/lib/ohai/plugins/solaris2/network.rb +++ b/lib/ohai/plugins/solaris2/network.rb @@ -86,7 +86,7 @@ def arpname_to_ifname(iface, arpname) def full_interface_name(iface, part_name, index) iface.each do |name, attrs| - next unless attrs&.respond_to?(:[]) + next unless attrs.respond_to?(:[]) return name if /^#{part_name}($|:)/.match(name) && attrs[:index] == index end From 08347833c30a20799c3a4b7240d8e712f23c2ca3 Mon Sep 17 00:00:00 2001 From: Tim Smith Date: Tue, 8 Sep 2020 11:51:33 -0700 Subject: [PATCH 4/4] Bail out early to avoid safe operator Also add some notes here that we should convert this to a filter_map when we can as that's easier to read and faster. Perf benchmarks from the tubes: Signed-off-by: Tim Smith --- lib/ohai/plugins/linux/network.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index 0f5c8c02a..0858c1a9f 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -141,10 +141,12 @@ def check_routing_table(family, iface, default_route_table) # using a temporary var to hold routes and their interface name def parse_routes(family, iface) iface.collect do |i, iv| - iv[:routes]&.collect do |r| + next unless iv[:routes] + + iv[:routes].collect do |r| r.merge(dev: i) if r[:family] == family[:name] - end&.compact - end.compact.flatten + end.compact # @todo: when we drop ruby 2.6 this should be a filter_map + end.compact.flatten # @todo: when we drop ruby 2.6 this should be a filter_map end # determine layer 1 details for the interface using ethtool