Skip to content

Commit

Permalink
Use .match? when we don't need the result
Browse files Browse the repository at this point in the history
Avoid creating a bunch of match objects when we're not going to use the match results.

Signed-off-by: Tim Smith <tsmith@chef.io>
  • Loading branch information
tas50 committed Jul 7, 2020
1 parent 8028951 commit fd52aea
Show file tree
Hide file tree
Showing 26 changed files with 59 additions and 59 deletions.
2 changes: 1 addition & 1 deletion lib/ohai/dsl/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def self.plugin(name, &block)
#
# @return [String]
def self.dev_null
if RUBY_PLATFORM =~ /mswin|mingw|windows/
if RUBY_PLATFORM.match?(/mswin|mingw|windows/)
"NUL"
else
"/dev/null"
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/mixin/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def shell_out(cmd, **options)
options = options.dup
# unless specified by the caller timeout after configured timeout (default 30 seconds)
options[:timeout] ||= Ohai::Config.ohai[:shellout_timeout]
unless RUBY_PLATFORM =~ /mswin|mingw32|windows/
unless RUBY_PLATFORM.match?(/mswin|mingw32|windows/)
options[:env] = options.key?(:env) ? options[:env].dup : {}
options[:env]["PATH"] ||= ((ENV["PATH"] || "").split(":") + %w{/usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /sbin /bin}).join(":")
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/mixin/dmi_decode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def guest_from_dmi_data(manufacturer, product, version)
when /VMware/
return "vmware"
when /Microsoft/
return "hyperv" if product =~ /Virtual Machine/
return "hyperv" if /Virtual Machine/.match?(product)
when /Amazon EC2/
return "amazonec2"
when /QEMU/
Expand Down
10 changes: 5 additions & 5 deletions lib/ohai/plugins/aix/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
sections.each do |line|
case title
when "network"
next if line =~ /^Interface|^---/
next if /^Interface|^---/.match?(line)

splat = line.strip.split
key = splat[0].downcase
Expand All @@ -68,7 +68,7 @@
}
wpars[wpar_name][title][key] = value
when "user-specified routes"
next if line =~ /^Type|^---/
next if /^Type|^---/.match?(line)

splat = line.strip.split
key = splat[2].downcase
Expand All @@ -78,7 +78,7 @@
}
wpars[wpar_name][title][key] = value
when "file systems"
next if line =~ /^MountPoint|^---/
next if /^MountPoint|^---/.match?(line)

splat = line.strip.split
key = splat[1].downcase
Expand All @@ -93,15 +93,15 @@
privileges ||= ""
wpars[wpar_name][title]["Privileges"] ||= []

if line =~ /^Privileges/
if /^Privileges/.match?(line)
privileges << line.split(":")[1].strip
else
privileges << line.strip
end

wpars[wpar_name][title]["Privileges"] += privileges.split(",")
when "device exports"
next if line =~ /^Name|^---/
next if /^Name|^---/.match?(line)

splat = line.strip.split
key = splat[0].downcase
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/plugins/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def has_dhcp_option_245?
has_245 = false
if File.exist?("/var/lib/dhcp/dhclient.eth0.leases")
File.open("/var/lib/dhcp/dhclient.eth0.leases").each do |line|
if line =~ /unknown-245/
if /unknown-245/.match?(line)
logger.trace("Plugin Azure: Found unknown-245 DHCP option used by Azure.")
has_245 = true
break
Expand Down
4 changes: 2 additions & 2 deletions lib/ohai/plugins/bsd/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
# Detect KVM/QEMU paravirt guests from cpu, report as KVM
# hw.model: QEMU Virtual CPU version 0.9.1
so = shell_out("sysctl -n hw.model")
if so.stdout =~ /QEMU Virtual CPU|KVM processor/
if /QEMU Virtual CPU|KVM processor/.match?(so.stdout)
virtualization[:system] = "kvm"
virtualization[:role] = "guest"
virtualization[:systems][:kvm] = "guest"
Expand All @@ -96,7 +96,7 @@
"xen"
when /kvm/
so = shell_out("sysctl -n kern.hostuuid")
so.stdout =~ /^ec2/ ? "amazonec2" : "kvm"
/^ec2/.match?(so.stdout) ? "amazonec2" : "kvm"
when /bhyve/
"bhyve"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/plugins/cpu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def parse_bsd_dmesg(&block)
cpu[index] = Mash.new
cpu[index][:status] = status
cpu[index][:location] = location
if status =~ /Available/
if /Available/.match?(status)
cpu[:available] += 1
lsattr = shell_out("lsattr -El #{name}").stdout.lines
lsattr.each do |attribute|
Expand Down
6 changes: 3 additions & 3 deletions lib/ohai/plugins/darwin/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def darwin_encaps_lookup(ifname)

def scope_lookup(scope)
return "Node" if scope.eql?("::1")
return "Link" if scope =~ /^fe80\:/
return "Site" if scope =~ /^fec0\:/
return "Link" if /^fe80\:/.match?(scope)
return "Site" if /^fec0\:/.match?(scope)

"Global"
end
Expand All @@ -79,7 +79,7 @@ def excluded_setting?(setting)
def locate_interface(ifaces, ifname, mac)
return ifname unless ifaces[ifname].nil?
# oh well, time to go hunting!
return ifname.chop if ifname =~ /\*$/
return ifname.chop if /\*$/.match?(ifname)

ifaces.each_key do |ifc|
ifaces[ifc][:addresses].each_key do |addr|
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/plugins/darwin/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def docker_exists?
virtualization[:systems][:parallels] = "host"
elsif ioreg_exists?
so = shell_out("ioreg -l")
if so.stdout =~ /pci1ab8,4000/
if /pci1ab8,4000/.match?(so.stdout)
virtualization[:system] = "parallels"
virtualization[:role] = "guest"
virtualization[:systems][:parallels] = "guest"
Expand Down
10 changes: 5 additions & 5 deletions lib/ohai/plugins/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
# @return [Boolean] do we have Amazon DMI data?
def has_ec2_amazon_dmi?
# detect a version of '4.2.amazon'
if file_val_if_exists("/sys/class/dmi/id/bios_vendor") =~ /Amazon/
if /Amazon/.match?(file_val_if_exists("/sys/class/dmi/id/bios_vendor"))
logger.trace("Plugin EC2: has_ec2_amazon_dmi? == true")
true
else
Expand All @@ -56,7 +56,7 @@ def has_ec2_amazon_dmi?
# @return [Boolean] do we have Amazon DMI data?
def has_ec2_xen_dmi?
# detect a version of '4.2.amazon'
if file_val_if_exists("/sys/class/dmi/id/bios_version") =~ /amazon/
if /amazon/.match?(file_val_if_exists("/sys/class/dmi/id/bios_version"))
logger.trace("Plugin EC2: has_ec2_xen_dmi? == true")
true
else
Expand All @@ -68,7 +68,7 @@ def has_ec2_xen_dmi?
# looks for a xen UUID that starts with ec2 from within the Linux sys tree
# @return [Boolean] do we have a Xen UUID or not?
def has_ec2_xen_uuid?
if file_val_if_exists("/sys/hypervisor/uuid") =~ /^ec2/
if /^ec2/.match?(file_val_if_exists("/sys/hypervisor/uuid"))
logger.trace("Plugin EC2: has_ec2_xen_uuid? == true")
return true
end
Expand All @@ -81,10 +81,10 @@ def has_ec2_xen_uuid?
# linux hosts
# @return [Boolean] do we have a Xen Identifying Number or not?
def has_ec2_identifying_number?
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
if RUBY_PLATFORM.match?(/mswin|mingw32|windows/)
require "wmi-lite/wmi"
wmi = WmiLite::Wmi.new
if wmi.first_of("Win32_ComputerSystemProduct")["identifyingnumber"] =~ /^ec2/
if /^ec2/.match?(wmi.first_of("Win32_ComputerSystemProduct")["identifyingnumber"])
logger.trace("Plugin EC2: has_ec2_identifying_number? == true")
true
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/plugins/eucalyptus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_mac_address(addresses)
def has_euca_mac?
network[:interfaces].each_value do |iface|
mac = get_mac_address(iface[:addresses])
if mac =~ /^[dD]0:0[dD]:/
if /^[dD]0:0[dD]:/.match?(mac)
logger.trace("Plugin Eucalyptus: has_euca_mac? == true (#{mac})")
return true
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ohai/plugins/gce.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
# this works even if the system lacks dmidecode use by the Dmi plugin
# @return [Boolean] do we have Google Compute Engine DMI data?
def has_gce_dmi?
if file_val_if_exists("/sys/class/dmi/id/product_name") =~ /Google Compute Engine/
if /Google Compute Engine/.match?(file_val_if_exists("/sys/class/dmi/id/product_name"))
logger.trace("Plugin GCE: has_gce_dmi? == true")
true
else
Expand All @@ -48,7 +48,7 @@ def file_val_if_exists(path)
# looks at the Manufacturer and Model WMI values to see if they starts with Google.
# @return [Boolean] Are the manufacturer and model Google?
def has_gce_system_info?
if RUBY_PLATFORM =~ /mswin|mingw32|windows/
if RUBY_PLATFORM.match?(/mswin|mingw32|windows/)
require "wmi-lite/wmi"
wmi = WmiLite::Wmi.new
computer_system = wmi.first_of("Win32_ComputerSystem")
Expand Down
4 changes: 2 additions & 2 deletions lib/ohai/plugins/hostname.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ def collect_hostname
machinename host["name"].to_s

info = Socket.gethostbyname(Socket.gethostname)
if info.first =~ /.+?\.(.*)/
if /.+?\.(.*)/.match?(info.first)
fqdn info.first
else
# host is not in dns. optionally use:
# C:\WINDOWS\system32\drivers\etc\hosts
info[3..info.length].reverse_each do |addr|
hostent = Socket.gethostbyaddr(addr)
if hostent.first =~ /.+?\.(.*)/
if /.+?\.(.*)/.match?(hostent.first)
fqdn hostent.first
break
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/plugins/linux/interrupts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def parse_smp_affinity(path, cpus)
interrupts[:irq][irqn][:events_by_cpu][cpu] = fields[cpu].to_i
end
# Only regular IRQs have extra fields and affinity settings
if /^\d+$/.match(irqn)
if /^\d+$/.match?(irqn)
interrupts[:irq][irqn][:type],
interrupts[:irq][irqn][:vector],
interrupts[:irq][irqn][:device] =
Expand Down
12 changes: 6 additions & 6 deletions lib/ohai/plugins/linux/network.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def check_routing_table(family, iface, default_route_table)
so.stdout.lines do |line|
line.strip!
logger.trace("Plugin Network: Parsing #{line}")
if line =~ /\\/
if /\\/.match?(line)
parts = line.split('\\')
route_dest = parts.shift.strip
route_endings = parts
Expand Down Expand Up @@ -191,11 +191,11 @@ def ethernet_ring_parameters(iface)
next if line.start_with?("Ring parameters for")
next if line.strip.nil?

if line =~ /Pre-set maximums/
if /Pre-set maximums/.match?(line)
type = "max"
next
end
if line =~ /Current hardware settings/
if /Current hardware settings/.match?(line)
type = "current"
next
end
Expand Down Expand Up @@ -224,11 +224,11 @@ def ethernet_channel_parameters(iface)
next if line.start_with?("Channel parameters for")
next if line.strip.nil?

if line =~ /Pre-set maximums/
if /Pre-set maximums/.match?(line)
type = "max"
next
end
if line =~ /Current hardware settings/
if /Current hardware settings/.match?(line)
type = "current"
next
end
Expand Down Expand Up @@ -308,7 +308,7 @@ def link_statistics(iface, net_counters)
net_counters[tmp_int] ||= Mash.new
end

if line =~ /^\s+(ip6tnl|ipip)/
if /^\s+(ip6tnl|ipip)/.match?(line)
iface[tmp_int][:tunnel_info] = {}
words = line.split
words.each_with_index do |word, index|
Expand Down
16 changes: 8 additions & 8 deletions lib/ohai/plugins/linux/platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def legacy_platform_detection
elsif File.exist?("/etc/debian_version")
# Ubuntu and Debian both have /etc/debian_version
# Ubuntu should always have a working lsb, debian does not by default
if lsb[:id] =~ /Ubuntu/i
if /Ubuntu/i.match?(lsb[:id])
platform "ubuntu"
platform_version lsb[:release]
else
Expand All @@ -222,7 +222,7 @@ def legacy_platform_detection
suse_version = suse_release.scan(/VERSION = (\d+)\nPATCHLEVEL = (\d+)/).flatten.join(".")
suse_version = suse_release[/VERSION = ([\d\.]{2,})/, 1] if suse_version == ""
platform_version suse_version
if suse_release =~ /^openSUSE/
if /^openSUSE/.match?(suse_release)
# opensuse releases >= 42 are openSUSE Leap
if platform_version.to_i < 42
platform "opensuse"
Expand Down Expand Up @@ -256,23 +256,23 @@ def legacy_platform_detection
platform_version shell_out("/bin/uname -r").stdout.strip
elsif File.exist?("/usr/lib/os-release")
contents = File.read("/usr/lib/os-release")
if /clear-linux-os/ =~ contents # Clear Linux https://clearlinux.org/
if /clear-linux-os/.match?(contents) # Clear Linux https://clearlinux.org/
platform "clearlinux"
platform_version contents[/VERSION_ID=(\d+)/, 1]
end
elsif lsb[:id] =~ /RedHat/i
elsif /RedHat/i.match?(lsb[:id])
platform "redhat"
platform_version lsb[:release]
elsif lsb[:id] =~ /Amazon/i
elsif /Amazon/i.match?(lsb[:id])
platform "amazon"
platform_version lsb[:release]
elsif lsb[:id] =~ /ScientificSL/i
elsif /ScientificSL/i.match?(lsb[:id])
platform "scientific"
platform_version lsb[:release]
elsif lsb[:id] =~ /XenServer/i
elsif /XenServer/i.match?(lsb[:id])
platform "xenserver"
platform_version lsb[:release]
elsif lsb[:id] =~ /XCP/i
elsif /XCP/i.match?(lsb[:id])
platform "xcp"
platform_version lsb[:release]
elsif lsb[:id] # LSB can provide odd data that changes between releases, so we currently fall back on it rather than dealing with its subtleties
Expand Down
14 changes: 7 additions & 7 deletions lib/ohai/plugins/linux/virtualization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def docker_exists?

# This file should exist on most Xen systems, normally empty for guests
if File.exist?("/proc/xen/capabilities")
if File.read("/proc/xen/capabilities") =~ /control_d/i
if /control_d/i.match?(File.read("/proc/xen/capabilities"))
logger.trace("Plugin Virtualization: /proc/xen/capabilities contains control_d. Detecting as Xen host")
virtualization[:role] = "host"
virtualization[:systems][:xen] = "host"
Expand All @@ -70,12 +70,12 @@ def docker_exists?
# Detect Virtualbox from kernel module
if File.exist?("/proc/modules")
modules = File.read("/proc/modules")
if modules =~ /^vboxdrv/
if /^vboxdrv/.match?(modules)
logger.trace("Plugin Virtualization: /proc/modules contains vboxdrv. Detecting as vbox host")
virtualization[:system] = "vbox"
virtualization[:role] = "host"
virtualization[:systems][:vbox] = "host"
elsif modules =~ /^vboxguest/
elsif /^vboxguest/.match?(modules)
logger.trace("Plugin Virtualization: /proc/modules contains vboxguest. Detecting as vbox guest")
virtualization[:system] = "vbox"
virtualization[:role] = "guest"
Expand All @@ -93,7 +93,7 @@ def docker_exists?

# 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/
if /QEMU Virtual CPU|Common KVM processor|Common 32-bit KVM processor/.match?(File.read("/proc/cpuinfo"))
logger.trace("Plugin Virtualization: /proc/cpuinfo lists a KVM paravirt CPU string. Detecting as kvm guest")
virtualization[:system] = "kvm"
virtualization[:role] = "guest"
Expand All @@ -105,7 +105,7 @@ def docker_exists?
# guests will have the hypervisor cpu feature that hosts don't have
if File.exist?("/sys/devices/virtual/misc/kvm")
virtualization[:system] = "kvm"
if File.read("/proc/cpuinfo") =~ /hypervisor/
if /hypervisor/.match?(File.read("/proc/cpuinfo"))
logger.trace("Plugin Virtualization: /sys/devices/virtual/misc/kvm present and /proc/cpuinfo lists the hypervisor feature. Detecting as kvm guest")
virtualization[:role] = "guest"
virtualization[:systems][:kvm] = "guest"
Expand Down Expand Up @@ -199,12 +199,12 @@ def docker_exists?
virtualization[:system] = $1
virtualization[:role] = "guest"
virtualization[:systems][$1.to_sym] = "guest"
elsif File.read("/proc/1/environ") =~ /container=lxc/
elsif /container=lxc/.match?(File.read("/proc/1/environ"))
logger.trace("Plugin Virtualization: /proc/1/environ indicates lxc container. Detecting as lxc guest")
virtualization[:system] = "lxc"
virtualization[:role] = "guest"
virtualization[:systems][:lxc] = "guest"
elsif File.read("/proc/1/environ") =~ /container=systemd-nspawn/
elsif /container=systemd-nspawn/.match?(File.read("/proc/1/environ"))
logger.trace("Plugin Virtualization: /proc/1/environ indicates nspawn container. Detecting as nspawn guest")
virtualization[:system] = "nspawn"
virtualization[:role] = "guest"
Expand Down
2 changes: 1 addition & 1 deletion lib/ohai/plugins/openstack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def openstack_hint?
# https://help.dreamhost.com/hc/en-us/articles/228377408-How-to-find-the-default-user-of-an-image
def openstack_provider
# dream host doesn't support windows so bail early if we're on windows
return "openstack" if RUBY_PLATFORM =~ /mswin|mingw32|windows/
return "openstack" if RUBY_PLATFORM.match?(/mswin|mingw32|windows/)

if Etc::Passwd.entries.map(&:name).include?("dhc-user")
"dreamhost"
Expand Down
Loading

0 comments on commit fd52aea

Please sign in to comment.