diff options
author | Tim Smith <tsmith@chef.io> | 2020-08-10 10:44:06 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-10 10:44:06 -0700 |
commit | 7899f2cda11088745ebe02c11fc345fb480233c4 (patch) | |
tree | 7fe0c6dba1bade116a348b503bdff7d713fca186 | |
parent | 49ac17e5db820e1f7ab9a051c5c834d069b0e772 (diff) | |
parent | 23cf5e3f6ae625a13ce198076b01f8ed249ec20d (diff) | |
download | ohai-7899f2cda11088745ebe02c11fc345fb480233c4.tar.gz |
Merge pull request #1491 from chef/avoid_vals
Minor memory optimizations
59 files changed, 108 insertions, 139 deletions
diff --git a/.expeditor/config.yml b/.expeditor/config.yml index 973aaa03..a077c467 100644 --- a/.expeditor/config.yml +++ b/.expeditor/config.yml @@ -28,8 +28,6 @@ github: version_constraint: 16.* - 15-stable: version_constraint: 15.* - - 14-stable: - version_constraint: 14.* changelog: rollup_header: Changes not yet released to rubygems.org diff --git a/.expeditor/verify.pipeline.yml b/.expeditor/verify.pipeline.yml index 8ac91308..91660d55 100644 --- a/.expeditor/verify.pipeline.yml +++ b/.expeditor/verify.pipeline.yml @@ -53,7 +53,7 @@ steps: - label: run-specs-windows command: - bundle config --local path vendor/bundle - - bundle install --jobs=7 --retry=3 + - bundle install --jobs=7 --retry=3 --without=profile - bundle exec rake spec expeditor: executor: @@ -25,3 +25,7 @@ group :debug do gem "pry-stack_explorer", "~> 0.4.0" # pin until we drop ruby < 2.6 gem "rb-readline" end + +group :profile do + gem "stackprof" +end diff --git a/lib/ohai/application.rb b/lib/ohai/application.rb index cdbac369..49b7d5e9 100644 --- a/lib/ohai/application.rb +++ b/lib/ohai/application.rb @@ -18,9 +18,9 @@ require "chef-config/path_helper" require "chef-config/workstation_config_loader" require_relative "../ohai" -require_relative "log" +require_relative "log" unless defined?(Ohai::Log) require "mixlib/cli" unless defined?(Mixlib::CLI) -require "benchmark" +require "benchmark" unless defined?(Benchmark) # The Application class is what is called by the Ohai CLI binary. It handles: # - CLI options and attribute arguments diff --git a/lib/ohai/loader.rb b/lib/ohai/loader.rb index a83cb56c..d90439d9 100644 --- a/lib/ohai/loader.rb +++ b/lib/ohai/loader.rb @@ -71,8 +71,7 @@ module Ohai from = [ Ohai.config[:plugin_path], from].flatten plugin_files_by_dir(from).collect do |plugin_file| logger.trace "Loading additional plugin: #{plugin_file}" - plugin = load_plugin_class(plugin_file) - load_v7_plugin(plugin) + load_v7_plugin(load_plugin_class(plugin_file)) end end diff --git a/lib/ohai/log.rb b/lib/ohai/log.rb index 6cf0ec2e..44e4daa3 100644 --- a/lib/ohai/log.rb +++ b/lib/ohai/log.rb @@ -16,7 +16,7 @@ # limitations under the License. # -require "mixlib/log" +require "mixlib/log" unless defined?(Mixlib::Log) module Ohai # the Ohai Logger which is just Mixlib::Log defaulting to STDERR and :info level diff --git a/lib/ohai/mixin/os.rb b/lib/ohai/mixin/os.rb index f4d4dd9f..44b790be 100644 --- a/lib/ohai/mixin/os.rb +++ b/lib/ohai/mixin/os.rb @@ -17,7 +17,7 @@ # limitations under the License. # -require "rbconfig" +require "rbconfig" unless defined?(RbConfig) module Ohai module Mixin diff --git a/lib/ohai/mixin/softlayer_metadata.rb b/lib/ohai/mixin/softlayer_metadata.rb index 6eb7813b..b2fc9c6c 100644 --- a/lib/ohai/mixin/softlayer_metadata.rb +++ b/lib/ohai/mixin/softlayer_metadata.rb @@ -17,7 +17,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "net/https" +require "net/http" unless defined?(Net::HTTP) require "uri" unless defined?(URI) # https://softlayer.github.io/reference/services/SoftLayer_Resource_Metadata/ diff --git a/lib/ohai/plugins/aix/network.rb b/lib/ohai/plugins/aix/network.rb index c0d5fb77..125bdb05 100644 --- a/lib/ohai/plugins/aix/network.rb +++ b/lib/ohai/plugins/aix/network.rb @@ -19,7 +19,6 @@ # Ohai.plugin(:Network) do - require "ipaddr" require_relative "../../mixin/network_helper" provides "network", "counters/network", "macaddress" @@ -27,6 +26,8 @@ Ohai.plugin(:Network) do include Ohai::Mixin::NetworkHelper collect_data(:aix) do + require "ipaddr" unless defined?(IPAddr) + # Loads following information. # :default_interface, :default_gateway - route -n get 0 # :interfaces diff --git a/lib/ohai/plugins/aix/uptime.rb b/lib/ohai/plugins/aix/uptime.rb index 470429ce..11840d88 100644 --- a/lib/ohai/plugins/aix/uptime.rb +++ b/lib/ohai/plugins/aix/uptime.rb @@ -21,7 +21,7 @@ Ohai.plugin(:Uptime) do provides "uptime", "uptime_seconds" collect_data(:aix) do - require "date" + require "date" unless defined?(DateTime) # below we're going to assume that PID 1 is init (this is true 99.99999% of the time) # output will look like this # 1148-20:54:50 diff --git a/lib/ohai/plugins/cloud.rb b/lib/ohai/plugins/cloud.rb index 45e5f8c5..d6df2571 100644 --- a/lib/ohai/plugins/cloud.rb +++ b/lib/ohai/plugins/cloud.rb @@ -320,7 +320,7 @@ Ohai.plugin(:Cloud) do end collect_data do - require "ipaddr" + require "ipaddr" unless defined?(IPAddr) @cloud_attr_obj = CloudAttrs.new diff --git a/lib/ohai/plugins/cpu.rb b/lib/ohai/plugins/cpu.rb index a33084de..3569935e 100644 --- a/lib/ohai/plugins/cpu.rb +++ b/lib/ohai/plugins/cpu.rb @@ -377,7 +377,7 @@ Ohai.plugin(:CPU) do end collect_data(:windows) do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) cpu Mash.new cores = 0 diff --git a/lib/ohai/plugins/docker.rb b/lib/ohai/plugins/docker.rb index 6bea2ace..d6626c5c 100644 --- a/lib/ohai/plugins/docker.rb +++ b/lib/ohai/plugins/docker.rb @@ -16,8 +16,6 @@ # Ohai.plugin(:Docker) do - require "json" - provides "docker" depends "virtualization" @@ -50,6 +48,8 @@ Ohai.plugin(:Docker) do end collect_data do + require "json" unless defined?(JSON) + if virtualization[:systems][:docker] docker_ohai_data(docker_info_json) end diff --git a/lib/ohai/plugins/dragonflybsd/platform.rb b/lib/ohai/plugins/dragonflybsd/platform.rb index 169488e2..ca80f7f7 100644 --- a/lib/ohai/plugins/dragonflybsd/platform.rb +++ b/lib/ohai/plugins/dragonflybsd/platform.rb @@ -20,10 +20,8 @@ Ohai.plugin(:Platform) do provides "platform", "platform_version", "platform_family" collect_data(:dragonflybsd) do - so = shell_out("uname -s") - platform so.stdout.split($/)[0].downcase - so = shell_out("uname -r") - platform_version so.stdout.split($/)[0] + platform shell_out("uname -s").stdout.split($/)[0].downcase + platform_version shell_out("uname -r").stdout.split($/)[0] platform_family "dragonflybsd" end end diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb index 82064365..b4fb735e 100644 --- a/lib/ohai/plugins/ec2.rb +++ b/lib/ohai/plugins/ec2.rb @@ -28,7 +28,6 @@ Ohai.plugin(:EC2) do require_relative "../mixin/ec2_metadata" require_relative "../mixin/http_helper" - require "base64" include Ohai::Mixin::Ec2Metadata include Ohai::Mixin::HttpHelper @@ -82,7 +81,7 @@ Ohai.plugin(:EC2) do # @return [Boolean] do we have a Xen Identifying Number or not? def has_ec2_identifying_number? if RUBY_PLATFORM.match?(/mswin|mingw32|windows/) - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) wmi = WmiLite::Wmi.new if /^ec2/.match?(wmi.first_of("Win32_ComputerSystemProduct")["identifyingnumber"]) logger.trace("Plugin EC2: has_ec2_identifying_number? == true") @@ -115,6 +114,8 @@ Ohai.plugin(:EC2) do end collect_data do + require "base64" + if looks_like_ec2? logger.trace("Plugin EC2: looks_like_ec2? == true") ec2 Mash.new diff --git a/lib/ohai/plugins/filesystem.rb b/lib/ohai/plugins/filesystem.rb index 77b77bad..4fccea03 100644 --- a/lib/ohai/plugins/filesystem.rb +++ b/lib/ohai/plugins/filesystem.rb @@ -23,8 +23,6 @@ # limitations under the License. # -require "set" - Ohai.plugin(:Filesystem) do provides "filesystem" @@ -286,12 +284,10 @@ Ohai.plugin(:Filesystem) do # Grab filesystem data from df run_with_check("df") do - so = shell_out("df -P") - fs.merge!(parse_common_df(so.stdout)) + fs.merge!(parse_common_df(shell_out("df -P").stdout)) # Grab filesystem inode data from df - so = shell_out("df -iP") - so.stdout.each_line do |line| + shell_out("df -iP").stdout.each_line do |line| case line when /^Filesystem\s+Inodes/ next @@ -310,8 +306,7 @@ Ohai.plugin(:Filesystem) do # Grab mount information from /bin/mount run_with_check("mount") do - so = shell_out("mount") - so.stdout.each_line do |line| + shell_out("mount").stdout.each_line do |line| if line =~ /^(.+?) on (.+?) type (.+?) \((.+?)\)$/ key = "#{$1},#{$2}" fs[key] ||= Mash.new @@ -346,8 +341,7 @@ Ohai.plugin(:Filesystem) do # this is to allow machines with large amounts of attached LUNs # to respond back to the command successfully run_with_check(cmdtype) do - so = shell_out(cmd, timeout: 60) - so.stdout.each_line do |line| + shell_out(cmd, timeout: 60).stdout.each_line do |line| parsed = parse_line(line, cmdtype) next if parsed.nil? @@ -383,7 +377,6 @@ Ohai.plugin(:Filesystem) do # we have to non-block read dev files. Ew. f = File.open("/proc/mounts") loop do - data = f.read_nonblock(4096) mounts << data # We should just catch EOFError, but the kernel had a period of @@ -392,9 +385,9 @@ Ohai.plugin(:Filesystem) do # whatever data we might have rescue Exception break - end f.close + mounts.each_line do |line| if line =~ /^(\S+) (\S+) (\S+) (\S+) \S+ \S+$/ key = "#{$1},#{$2}" @@ -718,7 +711,8 @@ Ohai.plugin(:Filesystem) do end collect_data(:windows) do - require "wmi-lite/wmi" + require "set" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) require_relative "../mash" fs = merge_info(logical_info, encryptable_info) diff --git a/lib/ohai/plugins/freebsd/platform.rb b/lib/ohai/plugins/freebsd/platform.rb index bef7b73e..957f0a30 100644 --- a/lib/ohai/plugins/freebsd/platform.rb +++ b/lib/ohai/plugins/freebsd/platform.rb @@ -20,10 +20,8 @@ Ohai.plugin(:Platform) do provides "platform", "platform_version", "platform_family" collect_data(:freebsd) do - so = shell_out("uname -s") - platform so.stdout.split($/)[0].downcase - so = shell_out("uname -r") - platform_version so.stdout.split($/)[0] + platform shell_out("uname -s").stdout.split($/)[0].downcase + platform_version shell_out("uname -r").stdout.split($/)[0] platform_family "freebsd" end end diff --git a/lib/ohai/plugins/gce.rb b/lib/ohai/plugins/gce.rb index f8c30b32..db336dfe 100644 --- a/lib/ohai/plugins/gce.rb +++ b/lib/ohai/plugins/gce.rb @@ -49,7 +49,7 @@ Ohai.plugin(:GCE) do # @return [Boolean] Are the manufacturer and model Google? def has_gce_system_info? if RUBY_PLATFORM.match?(/mswin|mingw32|windows/) - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) wmi = WmiLite::Wmi.new computer_system = wmi.first_of("Win32_ComputerSystem") if computer_system["Manufacturer"] =~ /^Google/ && computer_system["Model"] =~ /^Google/ diff --git a/lib/ohai/plugins/hostname.rb b/lib/ohai/plugins/hostname.rb index f4f95054..1360b227 100644 --- a/lib/ohai/plugins/hostname.rb +++ b/lib/ohai/plugins/hostname.rb @@ -26,9 +26,6 @@ # Ohai.plugin(:Hostname) do - require "socket" unless defined?(Socket) - require "ipaddr" - provides "domain", "hostname", "fqdn", "machinename" # hostname : short hostname @@ -47,6 +44,9 @@ Ohai.plugin(:Hostname) do # forward and reverse lookup to canonicalize FQDN (hostname -f equivalent) # this is ipv6-safe, works on ruby 1.8.7+ def resolve_fqdn + require "socket" unless defined?(Socket) + require "ipaddr" unless defined?(IPAddr) + hostname = from_cmd("hostname") addrinfo = Socket.getaddrinfo(hostname, nil).first iaddr = IPAddr.new(addrinfo[3]) @@ -161,7 +161,7 @@ Ohai.plugin(:Hostname) do end collect_data(:windows) do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) require "socket" unless defined?(Socket) wmi = WmiLite::Wmi.new diff --git a/lib/ohai/plugins/kernel.rb b/lib/ohai/plugins/kernel.rb index 7dc273fa..0b106026 100644 --- a/lib/ohai/plugins/kernel.rb +++ b/lib/ohai/plugins/kernel.rb @@ -252,7 +252,7 @@ Ohai.plugin(:Kernel) do collect_data(:windows) do require "win32ole" unless defined?(WIN32OLE) - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) WIN32OLE.codepage = WIN32OLE::CP_UTF8 diff --git a/lib/ohai/plugins/linux/hostnamectl.rb b/lib/ohai/plugins/linux/hostnamectl.rb index 960b7c42..10a7ec99 100644 --- a/lib/ohai/plugins/linux/hostnamectl.rb +++ b/lib/ohai/plugins/linux/hostnamectl.rb @@ -24,8 +24,7 @@ Ohai.plugin(:Hostnamectl) do hostnamectl_path = which("hostnamectl") if hostnamectl_path - hostnamectl_cmd = shell_out(hostnamectl_path) - hostnamectl_cmd.stdout.split("\n").each do |line| + shell_out(hostnamectl_path).stdout.split("\n").each do |line| key, val = line.split(":") hostnamectl[key.chomp.lstrip.tr(" ", "_").downcase] = val.chomp.lstrip end diff --git a/lib/ohai/plugins/linux/lsb.rb b/lib/ohai/plugins/linux/lsb.rb index eb15178c..233651c3 100644 --- a/lib/ohai/plugins/linux/lsb.rb +++ b/lib/ohai/plugins/linux/lsb.rb @@ -24,8 +24,7 @@ Ohai.plugin(:LSB) do if File.exist?("/usr/bin/lsb_release") # From package redhat-lsb on Fedora/Redhat, lsb-release on Debian/Ubuntu - so = shell_out("lsb_release -a") - so.stdout.lines do |line| + shell_out("lsb_release -a").stdout.lines do |line| case line when /^Distributor ID:\s+(.+)$/ lsb[:id] = $1 diff --git a/lib/ohai/plugins/linux/lspci.rb b/lib/ohai/plugins/linux/lspci.rb index 997544a5..4d51a7d0 100644 --- a/lib/ohai/plugins/linux/lspci.rb +++ b/lib/ohai/plugins/linux/lspci.rb @@ -24,7 +24,6 @@ Ohai.plugin(:Lspci) do collect_data(:linux) do devices = Mash.new - lspci = shell_out("lspci -vnnmk") h = /[0-9a-fA-F]/ # any hex digit hh = /#{h}#{h}/ # any 2 hex digits @@ -46,7 +45,7 @@ Ohai.plugin(:Lspci) do end end - lspci.stdout.split("\n").each do |line| + shell_out("lspci -vnnmk").stdout.split("\n").each do |line| dev = line.scan(/^(.*):\s(.*)$/)[0] next if dev.nil? diff --git a/lib/ohai/plugins/linux/machineid.rb b/lib/ohai/plugins/linux/machineid.rb index 56b22227..565bfde1 100644 --- a/lib/ohai/plugins/linux/machineid.rb +++ b/lib/ohai/plugins/linux/machineid.rb @@ -20,12 +20,12 @@ Ohai.plugin(:Machineid) do provides "machine_id" collect_data(:linux) do - mid = nil - if ::File.exist?("/etc/machine-id") mid = ::File.read("/etc/machine-id").chomp elsif ::File.exist?("/var/lib/dbus/machine-id") mid = ::File.read("/var/lib/dbus/machine-id").chomp + else + mid = nil end if mid diff --git a/lib/ohai/plugins/linux/network.rb b/lib/ohai/plugins/linux/network.rb index 7d0d2de8..b38df361 100644 --- a/lib/ohai/plugins/linux/network.rb +++ b/lib/ohai/plugins/linux/network.rb @@ -577,7 +577,7 @@ Ohai.plugin(:Network) do # If the 'ip' binary is available, this plugin may set {ip,mac,ip6}address. The network plugin should not overwrite these. # The older code section below that relies on the deprecated net-tools, e.g. netstat and ifconfig, provides less functionality. collect_data(:linux) do - require "ipaddr" + require "ipaddr" unless defined?(IPAddr) iface = Mash.new net_counters = Mash.new diff --git a/lib/ohai/plugins/linux/platform.rb b/lib/ohai/plugins/linux/platform.rb index 15c122bf..569be257 100644 --- a/lib/ohai/plugins/linux/platform.rb +++ b/lib/ohai/plugins/linux/platform.rb @@ -20,21 +20,6 @@ Ohai.plugin(:Platform) do provides "platform", "platform_version", "platform_family" depends "lsb" - # the platform mappings between the 'ID' field in /etc/os-release and the value - # ohai uses. If you're adding a new platform here and you want to change the name - # you'll want to add it here and then add a spec for the platform_id_remap method - PLATFORM_MAPPINGS ||= { - "rhel" => "redhat", - "amzn" => "amazon", - "ol" => "oracle", - "sles" => "suse", - "sles_sap" => "suse", - "opensuse-leap" => "opensuseleap", - "xenenterprise" => "xenserver", - "cumulus-linux" => "cumulus", - "archarm" => "arch", - }.freeze - # @deprecated def get_redhatish_platform(contents) contents[/^Red Hat/i] ? "redhat" : contents[/(\w+)/i, 1].downcase @@ -122,8 +107,20 @@ Ohai.plugin(:Platform) do # this catches the centos guest shell in the nexus switch which identifies itself as centos return "nexus_centos" if id == "centos" && os_release_file_is_cisco? - # remap based on the hash of platforms - PLATFORM_MAPPINGS[id] || id + # the platform mappings between the 'ID' field in /etc/os-release and the value + # ohai uses. If you're adding a new platform here and you want to change the name + # you'll want to add it here and then add a spec for the platform_id_remap method + { + "rhel" => "redhat", + "amzn" => "amazon", + "ol" => "oracle", + "sles" => "suse", + "sles_sap" => "suse", + "opensuse-leap" => "opensuseleap", + "xenenterprise" => "xenserver", + "cumulus-linux" => "cumulus", + "archarm" => "arch", + }[id] || id end # diff --git a/lib/ohai/plugins/netbsd/network.rb b/lib/ohai/plugins/netbsd/network.rb index 66cb6a20..ccd3ea8d 100644 --- a/lib/ohai/plugins/netbsd/network.rb +++ b/lib/ohai/plugins/netbsd/network.rb @@ -26,8 +26,7 @@ Ohai.plugin(:Network) do counters Mash.new unless counters counters[:network] ||= Mash.new - so = shell_out("route -n get default") - so.stdout.lines do |line| + shell_out("route -n get default").stdout.lines do |line| if line =~ /(\w+): ([\w\.]+)/ case $1 when "gateway" @@ -39,9 +38,8 @@ Ohai.plugin(:Network) do end iface = Mash.new - so = shell_out("#{Ohai.abs_path( "/sbin/ifconfig" )} -a") cint = nil - so.stdout.lines do |line| + shell_out("#{Ohai.abs_path( "/sbin/ifconfig" )} -a").stdout.lines do |line| if line =~ /^([0-9a-zA-Z\.]+):\s+/ cint = $1 iface[cint] = Mash.new @@ -85,8 +83,7 @@ Ohai.plugin(:Network) do end end - so = shell_out("arp -an") - so.stdout.lines do |line| + shell_out("arp -an").stdout.lines do |line| if line =~ /\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) on ([0-9a-zA-Z\.\:\-]+)/ next unless iface[$3] # this should never happen @@ -102,8 +99,7 @@ Ohai.plugin(:Network) do # Show the state of all network interfaces or a single interface # which have been auto-configured (interfaces statically configured # into a system, but not located at boot time are not shown). - so = shell_out("netstat -idn") - so.stdout.lines do |line| + shell_out("netstat -idn").stdout.lines do |line| # Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll Drop # em0 1500 <Link> 00:11:25:2d:90:be 3719557 0 3369969 0 0 0 # $1 $2 $3 $4 $5 $6 $7 $8 diff --git a/lib/ohai/plugins/netbsd/platform.rb b/lib/ohai/plugins/netbsd/platform.rb index 9293787b..81f4f660 100644 --- a/lib/ohai/plugins/netbsd/platform.rb +++ b/lib/ohai/plugins/netbsd/platform.rb @@ -20,10 +20,8 @@ Ohai.plugin(:Platform) do provides "platform", "platform_version", "platform_family" collect_data(:netbsd) do - so = shell_out("uname -s") - platform so.stdout.split($/)[0].downcase - so = shell_out("uname -r") - platform_version so.stdout.split($/)[0] + platform shell_out("uname -s").stdout.split($/)[0].downcase + platform_version shell_out("uname -r").stdout.split($/)[0] platform_family "netbsd" end end diff --git a/lib/ohai/plugins/network.rb b/lib/ohai/plugins/network.rb index c1b15ced..09bcd766 100644 --- a/lib/ohai/plugins/network.rb +++ b/lib/ohai/plugins/network.rb @@ -17,7 +17,6 @@ # Ohai.plugin(:NetworkAddresses) do - require "ipaddress" require_relative "../mixin/network_helper" include Ohai::Mixin::NetworkHelper @@ -134,6 +133,8 @@ Ohai.plugin(:NetworkAddresses) do # time as ipaddress. if ipaddress is set and macaddress is nil, that means # the interface ipaddress is bound to has the NOARP flag collect_data do + require "ipaddress" unless defined?(IPAddress) + results = {} network Mash.new unless network diff --git a/lib/ohai/plugins/openbsd/network.rb b/lib/ohai/plugins/openbsd/network.rb index 14664860..083d187e 100644 --- a/lib/ohai/plugins/openbsd/network.rb +++ b/lib/ohai/plugins/openbsd/network.rb @@ -26,8 +26,7 @@ Ohai.plugin(:Network) do counters Mash.new unless counters counters[:network] ||= Mash.new - so = shell_out("route -n get default") - so.stdout.lines do |line| + shell_out("route -n get default").stdout.lines do |line| if line =~ /(\w+): ([\w\.]+)/ case $1 when "gateway" @@ -39,9 +38,8 @@ Ohai.plugin(:Network) do end iface = Mash.new - so = shell_out( "#{Ohai.abs_path( "/sbin/ifconfig" )} -a" ) cint = nil - so.stdout.lines do |line| + shell_out( "#{Ohai.abs_path( "/sbin/ifconfig" )} -a" ).stdout.lines do |line| if line =~ /^([0-9a-zA-Z\.]+):\s+/ cint = $1 iface[cint] = Mash.new @@ -85,8 +83,7 @@ Ohai.plugin(:Network) do end end - so = shell_out("arp -an") - so.stdout.lines do |line| + shell_out("arp -an").stdout.lines do |line| if line =~ /\((\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\) at ([a-fA-F0-9\:]+) on ([0-9a-zA-Z\.\:\-]+)/ next unless iface[$3] # this should never happen @@ -102,8 +99,7 @@ Ohai.plugin(:Network) do # Show the state of all network interfaces or a single interface # which have been auto-configured (interfaces statically configured # into a system, but not located at boot time are not shown). - so = shell_out("netstat -idn") - so.stdout.lines do |line| + shell_out("netstat -idn").stdout.lines do |line| # Name Mtu Network Address Ipkts Ierrs Opkts Oerrs Coll Drop # em0 1500 <Link> 00:11:25:2d:90:be 3719557 0 3369969 0 0 0 # $1 $2 $3 $4 $5 $6 $7 $8 diff --git a/lib/ohai/plugins/openbsd/platform.rb b/lib/ohai/plugins/openbsd/platform.rb index a99cf7a5..3d43e57e 100644 --- a/lib/ohai/plugins/openbsd/platform.rb +++ b/lib/ohai/plugins/openbsd/platform.rb @@ -20,10 +20,8 @@ Ohai.plugin(:Platform) do provides "platform", "platform_version", "platform_family" collect_data(:openbsd) do - so = shell_out("uname -s") - platform so.stdout.split($/)[0].downcase - so = shell_out("uname -r") - platform_version so.stdout.split($/)[0] + platform shell_out("uname -s").stdout.split($/)[0].downcase + platform_version shell_out("uname -r").stdout.split($/)[0] platform_family "openbsd" end end diff --git a/lib/ohai/plugins/openstack.rb b/lib/ohai/plugins/openstack.rb index 50106d7e..361e0488 100644 --- a/lib/ohai/plugins/openstack.rb +++ b/lib/ohai/plugins/openstack.rb @@ -19,7 +19,6 @@ Ohai.plugin(:Openstack) do require_relative "../mixin/ec2_metadata" require_relative "../mixin/http_helper" - require "etc" unless defined?(Etc) include Ohai::Mixin::Ec2Metadata include Ohai::Mixin::HttpHelper @@ -59,6 +58,8 @@ Ohai.plugin(:Openstack) do end collect_data do + require "etc" unless defined?(Etc) + # fetch data if we look like openstack if openstack_hint? || openstack_virtualization? openstack Mash.new diff --git a/lib/ohai/plugins/packages.rb b/lib/ohai/plugins/packages.rb index f9653dbf..098a3126 100644 --- a/lib/ohai/plugins/packages.rb +++ b/lib/ohai/plugins/packages.rb @@ -79,7 +79,7 @@ Ohai.plugin(:Packages) do end when "arch" - require "date" + require "date" unless defined?(DateTime) # Set LANG=C to force an easy to parse date format so = shell_out("LANG=C pacman -Qi") diff --git a/lib/ohai/plugins/passwd.rb b/lib/ohai/plugins/passwd.rb index 2c43d73b..4af41450 100644 --- a/lib/ohai/plugins/passwd.rb +++ b/lib/ohai/plugins/passwd.rb @@ -1,6 +1,5 @@ Ohai.plugin(:Passwd) do - require "etc" unless defined?(Etc) provides "etc", "current_user" optional true @@ -14,6 +13,8 @@ Ohai.plugin(:Passwd) do end collect_data do + require "etc" unless defined?(Etc) + unless etc etc Mash.new diff --git a/lib/ohai/plugins/rackspace.rb b/lib/ohai/plugins/rackspace.rb index ccec304f..c831d1b5 100644 --- a/lib/ohai/plugins/rackspace.rb +++ b/lib/ohai/plugins/rackspace.rb @@ -15,7 +15,6 @@ # limitations under the License. Ohai.plugin(:Rackspace) do - require "resolv" provides "rackspace" depends "kernel", "network/interfaces" @@ -50,7 +49,7 @@ Ohai.plugin(:Rackspace) do def has_rackspace_manufacturer? return false unless RUBY_PLATFORM.match?(/mswin|mingw32|windows/) - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) wmi = WmiLite::Wmi.new if wmi.first_of("Win32_ComputerSystem")["PrimaryOwnerName"] == "Rackspace" logger.trace("Plugin Rackspace: has_rackspace_manufacturer? == true") @@ -147,6 +146,8 @@ Ohai.plugin(:Rackspace) do end collect_data do + require "resolv" + # Adds rackspace Mash if looks_like_rackspace? rackspace Mash.new diff --git a/lib/ohai/plugins/root_group.rb b/lib/ohai/plugins/root_group.rb index ea1af938..28dba9bb 100644 --- a/lib/ohai/plugins/root_group.rb +++ b/lib/ohai/plugins/root_group.rb @@ -19,7 +19,7 @@ Ohai.plugin(:RootGroup) do provides "root_group" collect_data(:windows) do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) wmi = WmiLite::Wmi.new # Per http://support.microsoft.com/kb/243330 SID: S-1-5-32-544 is the diff --git a/lib/ohai/plugins/shard.rb b/lib/ohai/plugins/shard.rb index 259bdc6b..cf7641b0 100644 --- a/lib/ohai/plugins/shard.rb +++ b/lib/ohai/plugins/shard.rb @@ -94,7 +94,7 @@ Ohai.plugin(:ShardSeed) do end collect_data(:windows) do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) wmi = WmiLite::Wmi.new create_seed do |src| diff --git a/lib/ohai/plugins/solaris2/dmi.rb b/lib/ohai/plugins/solaris2/dmi.rb index d4b91a3b..7d98c06d 100644 --- a/lib/ohai/plugins/solaris2/dmi.rb +++ b/lib/ohai/plugins/solaris2/dmi.rb @@ -86,7 +86,7 @@ Ohai.plugin(:DMI) do "SUN_OEM_EXT_MEMARRAY" => 144, # phys memory array extended info "SUN_OEM_EXT_MEMDEVICE" => 145, # memory device extended info "SMB_TYPE_OEM_HI" => 256, # end of OEM-specific type range - } + }.freeze # all output lines should fall within one of these patterns header_type_line = /^ID\s+SIZE\s+TYPE/ @@ -99,7 +99,6 @@ Ohai.plugin(:DMI) do dmi_record = nil field = nil - so = shell_out("smbios") # ==== EXAMPLE: ==== # ID SIZE TYPE # 0 40 SMB_TYPE_BIOS (BIOS information) @@ -111,7 +110,7 @@ Ohai.plugin(:DMI) do # SMB_BIOSFL_PCI (PCI is supported) # ... similar lines trimmed # note the second level of indentation is via a *tab* - so.stdout.lines do |raw_line| + shell_out("smbios").stdout.lines do |raw_line| next if header_type_line.match(raw_line) next if blank_line.match(raw_line) diff --git a/lib/ohai/plugins/solaris2/memory.rb b/lib/ohai/plugins/solaris2/memory.rb index 1c14be68..c2933b0f 100644 --- a/lib/ohai/plugins/solaris2/memory.rb +++ b/lib/ohai/plugins/solaris2/memory.rb @@ -20,8 +20,7 @@ Ohai.plugin(:Memory) do collect_data(:solaris2) do memory Mash.new memory[:swap] = Mash.new - meminfo = shell_out("prtconf | grep Memory").stdout - memory[:total] = "#{meminfo.split[2].to_i * 1024}kB" + memory[:total] = "#{shell_out("prtconf | grep Memory").stdout.split[2].to_i * 1024}kB" tokens = shell_out("swap -s").stdout.strip.split used_swap = tokens[8][0..-1].to_i # strip k from end diff --git a/lib/ohai/plugins/solaris2/network.rb b/lib/ohai/plugins/solaris2/network.rb index aa60b21e..1c7ff0f1 100644 --- a/lib/ohai/plugins/solaris2/network.rb +++ b/lib/ohai/plugins/solaris2/network.rb @@ -54,13 +54,11 @@ # inet6 fe80::203:baff:fe17:4444/128 # Extracted from http://illumos.org/hcl/ -unless defined?(ETHERNET_ENCAPS) - ETHERNET_ENCAPS = %w{ afe amd8111s arn atge ath bfe bge bnx bnxe ce cxgbe +ETHERNET_ENCAPS ||= %w{ afe amd8111s arn atge ath bfe bge bnx bnxe ce cxgbe dmfe e1000g efe elxl emlxs eri hermon hme hxge igb iprb ipw iwh iwi iwk iwp ixgb ixgbe mwl mxfe myri10ge nge ntxn nxge pcn platform qfe qlc ral rge rtls rtw rwd rwn sfe tavor vr wpi xge yge aggr}.freeze -end Ohai.plugin(:Network) do require_relative "../../mixin/network_helper" @@ -102,10 +100,8 @@ Ohai.plugin(:Network) do counters Mash.new unless counters counters[:network] ||= Mash.new - so = shell_out("ifconfig -a") cint = nil - - so.stdout.lines do |line| + shell_out("ifconfig -a").stdout.lines do |line| # regex: https://rubular.com/r/ZiIHbsnfiWPW1p if line =~ /^([0-9a-zA-Z\.\:\-]+): \S+ mtu (\d+)(?: index (\d+))?/ cint = $1 diff --git a/lib/ohai/plugins/solaris2/platform.rb b/lib/ohai/plugins/solaris2/platform.rb index 90cd0338..23ede135 100644 --- a/lib/ohai/plugins/solaris2/platform.rb +++ b/lib/ohai/plugins/solaris2/platform.rb @@ -26,8 +26,7 @@ Ohai.plugin(:Platform) do uname_exec = "uname" end - so = shell_out("#{uname_exec} -X") - so.stdout.lines do |line| + shell_out("#{uname_exec} -X").stdout.lines do |line| case line when /^Release =\s+(.+)$/ platform_version $1 diff --git a/lib/ohai/plugins/solaris2/virtualization.rb b/lib/ohai/plugins/solaris2/virtualization.rb index 3b85ea9d..6db8b28a 100644 --- a/lib/ohai/plugins/solaris2/virtualization.rb +++ b/lib/ohai/plugins/solaris2/virtualization.rb @@ -25,9 +25,7 @@ Ohai.plugin(:Virtualization) do depends "dmi" def collect_solaris_guestid - command = "/usr/sbin/zoneadm list -p" - so = shell_out(command) - so.stdout.split(":").first + shell_out("/usr/sbin/zoneadm list -p").stdout.split(":").first end collect_data(:solaris2) do @@ -56,8 +54,7 @@ Ohai.plugin(:Virtualization) do if File.executable?("/usr/sbin/zoneadm") zones = Mash.new - so = shell_out("zoneadm list -pc") - so.stdout.lines do |line| + shell_out("zoneadm list -pc").stdout.lines do |line| info = line.chomp.split(/:/) zones[info[1]] = { "id" => info[0], diff --git a/lib/ohai/plugins/uptime.rb b/lib/ohai/plugins/uptime.rb index 5883f13c..dafdfaea 100644 --- a/lib/ohai/plugins/uptime.rb +++ b/lib/ohai/plugins/uptime.rb @@ -84,7 +84,7 @@ Ohai.plugin(:Uptime) do end collect_data(:windows) do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) wmi = WmiLite::Wmi.new last_boot_up_time = wmi.first_of("Win32_OperatingSystem")["lastbootuptime"] uptime_seconds Time.new.to_i - Time.parse(last_boot_up_time).to_i diff --git a/lib/ohai/plugins/windows/dmi.rb b/lib/ohai/plugins/windows/dmi.rb index eea5b37f..d0d6cdd8 100644 --- a/lib/ohai/plugins/windows/dmi.rb +++ b/lib/ohai/plugins/windows/dmi.rb @@ -46,7 +46,7 @@ Ohai.plugin(:DMI) do collect_data(:windows) do require "ohai/common/dmi" - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) wmi = WmiLite::Wmi.new dmi Mash.new diff --git a/lib/ohai/plugins/windows/drivers.rb b/lib/ohai/plugins/windows/drivers.rb index 425d33dc..d9fb41fc 100644 --- a/lib/ohai/plugins/windows/drivers.rb +++ b/lib/ohai/plugins/windows/drivers.rb @@ -22,7 +22,7 @@ Ohai.plugin(:Drivers) do collect_data(:windows) do if configuration(:enabled) - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) kext = Mash.new pnp_drivers = Mash.new diff --git a/lib/ohai/plugins/windows/memory.rb b/lib/ohai/plugins/windows/memory.rb index e0bb46f0..b8bd84a1 100644 --- a/lib/ohai/plugins/windows/memory.rb +++ b/lib/ohai/plugins/windows/memory.rb @@ -17,7 +17,7 @@ Ohai.plugin(:Memory) do provides "memory" collect_data(:windows) do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) memory Mash.new memory[:swap] = Mash.new diff --git a/lib/ohai/plugins/windows/network.rb b/lib/ohai/plugins/windows/network.rb index d44d6079..2b8fdd62 100644 --- a/lib/ohai/plugins/windows/network.rb +++ b/lib/ohai/plugins/windows/network.rb @@ -118,7 +118,7 @@ Ohai.plugin(:Network) do collect_data(:windows) do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) iface = Mash.new iface_config = Mash.new diff --git a/lib/ohai/plugins/windows/virtualization.rb b/lib/ohai/plugins/windows/virtualization.rb index 0ce24418..0ed73d60 100644 --- a/lib/ohai/plugins/windows/virtualization.rb +++ b/lib/ohai/plugins/windows/virtualization.rb @@ -24,7 +24,7 @@ Ohai.plugin(:Virtualization) do include Ohai::Mixin::DmiDecode collect_data(:windows) do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) virtualization Mash.new unless virtualization virtualization[:systems] ||= Mash.new diff --git a/lib/ohai/runner.rb b/lib/ohai/runner.rb index 5961a2cf..c5039813 100644 --- a/lib/ohai/runner.rb +++ b/lib/ohai/runner.rb @@ -18,7 +18,7 @@ # require_relative "dsl" -require "benchmark" +require "benchmark" unless defined?(Benchmark) module Ohai class Runner diff --git a/lib/ohai/util/ip_helper.rb b/lib/ohai/util/ip_helper.rb index f22c4fdd..8b56eecf 100644 --- a/lib/ohai/util/ip_helper.rb +++ b/lib/ohai/util/ip_helper.rb @@ -14,11 +14,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "ipaddress" - module Ohai module Util module IpHelper + require "ipaddress" unless defined?(IPAddress) + # Corresponding to RFC 4192 + RFC 4193 IPV6_LINK_LOCAL_UNICAST_BLOCK = IPAddress("fe80::/10") IPV6_PRIVATE_ADDRESS_BLOCK = IPAddress("fc00::/7") diff --git a/spec/unit/plugins/cloud_spec.rb b/spec/unit/plugins/cloud_spec.rb index d805c93c..c8f24e7c 100644 --- a/spec/unit/plugins/cloud_spec.rb +++ b/spec/unit/plugins/cloud_spec.rb @@ -16,7 +16,7 @@ # require "spec_helper" -require "ipaddr" +require "ipaddr" unless defined?(IPAddr) describe "CloudAttrs object" do before do diff --git a/spec/unit/plugins/hostname_spec.rb b/spec/unit/plugins/hostname_spec.rb index c7e00f8b..ce672ebd 100644 --- a/spec/unit/plugins/hostname_spec.rb +++ b/spec/unit/plugins/hostname_spec.rb @@ -17,7 +17,7 @@ # require "spec_helper" -require "wmi-lite/wmi" +require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) require "socket" describe Ohai::System, "hostname plugin" do diff --git a/spec/unit/plugins/linux/network_spec.rb b/spec/unit/plugins/linux/network_spec.rb index 70b1ec45..5ee364c0 100644 --- a/spec/unit/plugins/linux/network_spec.rb +++ b/spec/unit/plugins/linux/network_spec.rb @@ -18,7 +18,7 @@ # require "spec_helper" -require "ipaddress" +require "ipaddress" unless defined?(IPAddress) describe Ohai::System, "Linux Network Plugin" do let(:plugin) { get_plugin("linux/network") } diff --git a/spec/unit/plugins/windows/dmi_spec.rb b/spec/unit/plugins/windows/dmi_spec.rb index f9f1c16d..aa086611 100644 --- a/spec/unit/plugins/windows/dmi_spec.rb +++ b/spec/unit/plugins/windows/dmi_spec.rb @@ -22,7 +22,7 @@ describe Ohai::System, "DMI", :windows_only do let(:plugin) { get_plugin("windows/dmi") } before do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) empty_wmi_object = WmiLite::Wmi::Instance.new(double(properties_: [])) %w{Processor Bios ComputerSystemProduct BaseBoard}.each do |type| diff --git a/spec/unit/plugins/windows/filesystem_spec.rb b/spec/unit/plugins/windows/filesystem_spec.rb index 7082259b..396eb300 100644 --- a/spec/unit/plugins/windows/filesystem_spec.rb +++ b/spec/unit/plugins/windows/filesystem_spec.rb @@ -17,7 +17,7 @@ # require "spec_helper" -require "wmi-lite/wmi" +require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) describe Ohai::System, "Windows Filesystem Plugin", :windows_only do let(:plugin) { get_plugin("filesystem") } diff --git a/spec/unit/plugins/windows/kernel_spec.rb b/spec/unit/plugins/windows/kernel_spec.rb index 6127848c..342b7423 100644 --- a/spec/unit/plugins/windows/kernel_spec.rb +++ b/spec/unit/plugins/windows/kernel_spec.rb @@ -21,7 +21,7 @@ describe Ohai::System, "Windows kernel plugin", :windows_only do let(:plugin) { get_plugin("kernel") } before do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) # Mock a Win32_OperatingSystem OLE32 WMI object caption = double("WIN32OLE", name: "Caption") diff --git a/spec/unit/plugins/windows/memory_spec.rb b/spec/unit/plugins/windows/memory_spec.rb index 38e88eda..bc788b86 100644 --- a/spec/unit/plugins/windows/memory_spec.rb +++ b/spec/unit/plugins/windows/memory_spec.rb @@ -18,7 +18,7 @@ require "spec_helper" describe Ohai::System, "Windows memory plugin", :windows_only do before do - require "wmi-lite/wmi" + require "wmi-lite/wmi" unless defined?(WmiLite::Wmi) @plugin = get_plugin("windows/memory") mock_os = { "TotalVisibleMemorySize" => "10485760", diff --git a/spec/unit/plugins/windows/network_spec.rb b/spec/unit/plugins/windows/network_spec.rb index ff732cc7..38d2c17a 100644 --- a/spec/unit/plugins/windows/network_spec.rb +++ b/spec/unit/plugins/windows/network_spec.rb @@ -17,7 +17,7 @@ # require "spec_helper" -require "ipaddress" +require "ipaddress" unless defined?(IPAddress) describe Ohai::System, "Windows Network Plugin" do let(:plugin) { get_plugin("windows/network") } diff --git a/spec/unit/util/ip_helper_spec.rb b/spec/unit/util/ip_helper_spec.rb index ec9b37ba..4bc4a7b6 100644 --- a/spec/unit/util/ip_helper_spec.rb +++ b/spec/unit/util/ip_helper_spec.rb @@ -14,7 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -require "ipaddress" +require "ipaddress" unless defined?(IPAddress) require "spec_helper" require "ohai/util/ip_helper" |