diff options
author | Tim Smith <tsmith@chef.io> | 2020-08-25 18:17:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-25 18:17:05 -0700 |
commit | 49668f8ad931e683fd87dff067596616fa154cbc (patch) | |
tree | 44817e6d5fa36015548f3f1248e89efd7b822693 | |
parent | 654df0481f3c35c027a9afc313c74583f3d4112f (diff) | |
parent | 238a4cb9baf9728ee9076ceefcfb6324960a0262 (diff) | |
download | ohai-49668f8ad931e683fd87dff067596616fa154cbc.tar.gz |
Merge pull request #1501 from chef/lcg/chef-utils-helpers
Migrate to the chef-utils helpers for which/shell_out
-rw-r--r-- | Rakefile | 2 | ||||
-rw-r--r-- | lib/ohai/dsl/plugin.rb | 10 | ||||
-rw-r--r-- | lib/ohai/mixin/chef_utils_wiring.rb | 39 | ||||
-rw-r--r-- | lib/ohai/mixin/command.rb | 57 | ||||
-rw-r--r-- | lib/ohai/mixin/shell_out.rb | 50 | ||||
-rw-r--r-- | lib/ohai/mixin/which.rb | 38 | ||||
-rw-r--r-- | lib/ohai/plugins/ec2.rb | 2 | ||||
-rw-r--r-- | lib/ohai/plugins/shard.rb | 2 | ||||
-rw-r--r-- | lib/ohai/system.rb | 5 | ||||
-rw-r--r-- | lib/ohai/util/file_helper.rb | 41 | ||||
-rw-r--r-- | spec/unit/mixin/shell_out_spec.rb (renamed from spec/unit/mixin/command_spec.rb) | 60 | ||||
-rw-r--r-- | spec/unit/mixin/which_spec.rb (renamed from spec/unit/util/file_helper_spec.rb) | 11 |
12 files changed, 195 insertions, 122 deletions
@@ -40,4 +40,4 @@ task :console do IRB.start end -task default: %i{style spec} +task default: %i{spec style} diff --git a/lib/ohai/dsl/plugin.rb b/lib/ohai/dsl/plugin.rb index c1722370..165ecd39 100644 --- a/lib/ohai/dsl/plugin.rb +++ b/lib/ohai/dsl/plugin.rb @@ -1,7 +1,7 @@ # # Author:: Adam Jacob (<adam@chef.io>) # Author:: Claire McQuin (<claire@chef.io>) -# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. +# Copyright:: Copyright (c) Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,10 +19,10 @@ # require_relative "../mixin/os" -require_relative "../mixin/command" +require_relative "../mixin/shell_out" require_relative "../mixin/seconds_to_human" +require_relative "../mixin/which" require_relative "../hints" -require_relative "../util/file_helper" module Ohai @@ -83,9 +83,9 @@ module Ohai class Plugin include Ohai::Mixin::OS - include Ohai::Mixin::Command + include Ohai::Mixin::ShellOut include Ohai::Mixin::SecondsToHuman - include Ohai::Util::FileHelper + include Ohai::Mixin::Which attr_reader :data attr_reader :failed diff --git a/lib/ohai/mixin/chef_utils_wiring.rb b/lib/ohai/mixin/chef_utils_wiring.rb new file mode 100644 index 00000000..3ca61c25 --- /dev/null +++ b/lib/ohai/mixin/chef_utils_wiring.rb @@ -0,0 +1,39 @@ +# +# Copyright:: Copyright (c) Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require_relative "../config" + +module Ohai + module Mixin + # Common Dependency Injection wiring for ChefUtils-related modules + module ChefUtilsWiring + private + + def __config + Ohai::Config + end + + def __log + logger + end + + def __transport_connection + # Chef.run_context&.transport_connection + end + end + end +end diff --git a/lib/ohai/mixin/command.rb b/lib/ohai/mixin/command.rb index 503b70d3..030844a7 100644 --- a/lib/ohai/mixin/command.rb +++ b/lib/ohai/mixin/command.rb @@ -1,54 +1,3 @@ -# -# Author:: Adam Jacob (<adam@chef.io>) -# Author:: Tim Smith (<tsmith@chef.io>) -# Copyright:: Copyright (c) 2008-2016 Chef Software, Inc. -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -require_relative "../exception" -require_relative "../log" -require "mixlib/shellout" unless defined?(Mixlib::ShellOut::DEFAULT_READ_TIMEOUT) - -module Ohai - module Mixin - module Command - # DISCLAIMER: Logging only works in the context of a plugin!! - # accept a command and any of the mixlib-shellout options - 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.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 - so = Mixlib::ShellOut.new(cmd, options) - begin - so.run_command - logger.trace("Plugin #{name}: ran '#{cmd}' and returned #{so.exitstatus}") - so - rescue Errno::ENOENT => e - logger.trace("Plugin #{name}: ran '#{cmd}' and failed #{e.inspect}") - raise Ohai::Exceptions::Exec, e - rescue Mixlib::ShellOut::CommandTimeout => e - logger.trace("Plugin #{name}: ran '#{cmd}' and timed out after #{options[:timeout]} seconds") - raise Ohai::Exceptions::Exec, e - end - end - - module_function :shell_out - end - end -end +$stderr.puts "WARN: Ohai::Mixin::Command is deprecated, please use Ohai::Mixin::ShellOut or remove if the reference is unnecessary" +require_relative "shell_out" +Ohai::Mixin::Command = Ohai::Mixin::ShellOut unless defined?(Ohai::Mixin::Command) diff --git a/lib/ohai/mixin/shell_out.rb b/lib/ohai/mixin/shell_out.rb new file mode 100644 index 00000000..18c2ee38 --- /dev/null +++ b/lib/ohai/mixin/shell_out.rb @@ -0,0 +1,50 @@ +# +# Author:: Adam Jacob (<adam@chef.io>) +# Author:: Tim Smith (<tsmith@chef.io>) +# Copyright:: Copyright (c) Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +require_relative "../exception" +require_relative "../log" + +require "mixlib/shellout/helper" unless defined?(Mixlib::ShellOut::Helper) +require_relative "chef_utils_wiring" unless defined?(Ohai::Mixin::ChefUtilsWiring) + +module Ohai + module Mixin + module ShellOut + include Mixlib::ShellOut::Helper + include Ohai::Mixin::ChefUtilsWiring + + 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] + begin + so = super(cmd, **options) + logger.trace("Plugin #{name}: ran '#{cmd}' and returned #{so.exitstatus}") + so + rescue Errno::ENOENT => e + logger.trace("Plugin #{name}: ran '#{cmd}' and failed #{e.inspect}") + raise Ohai::Exceptions::Exec, e + rescue Mixlib::ShellOut::CommandTimeout => e + logger.trace("Plugin #{name}: ran '#{cmd}' and timed out after #{options[:timeout]} seconds") + raise Ohai::Exceptions::Exec, e + end + end + end + end +end diff --git a/lib/ohai/mixin/which.rb b/lib/ohai/mixin/which.rb new file mode 100644 index 00000000..b21ee640 --- /dev/null +++ b/lib/ohai/mixin/which.rb @@ -0,0 +1,38 @@ +# +# Copyright:: Copyright (c) Chef Software Inc. +# License:: Apache License, Version 2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require "chef-utils/dsl/which" unless defined?(ChefUtils::DSL::Which) +require "chef-utils/dsl/default_paths" unless defined?(ChefUtils::DSL::DefaultPaths) +require_relative "chef_utils_wiring" unless defined?(Ohai::Mixin::ChefUtilsWiring) + +module Ohai + module Mixin + module Which + include ChefUtils::DSL::Which + include ChefUtils::DSL::DefaultPaths + include ChefUtilsWiring + + private + + # we dep-inject default paths into this API for historical reasons + # + # @api private + def __extra_path + __default_paths + end + end + end +end diff --git a/lib/ohai/plugins/ec2.rb b/lib/ohai/plugins/ec2.rb index b4fb735e..c526a37b 100644 --- a/lib/ohai/plugins/ec2.rb +++ b/lib/ohai/plugins/ec2.rb @@ -114,7 +114,7 @@ Ohai.plugin(:EC2) do 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 cf7641b0..bfd07212 100644 --- a/lib/ohai/plugins/shard.rb +++ b/lib/ohai/plugins/shard.rb @@ -52,7 +52,7 @@ Ohai.plugin(:ShardSeed) do 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" diff --git a/lib/ohai/system.rb b/lib/ohai/system.rb index 6cddf774..ee196414 100644 --- a/lib/ohai/system.rb +++ b/lib/ohai/system.rb @@ -1,6 +1,6 @@ # # Author:: Adam Jacob (<adam@chef.io>) -# Copyright:: Copyright (c) 2008-2019, Chef Software Inc. +# Copyright:: Copyright (c) Chef Software Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -22,13 +22,12 @@ require_relative "log" require_relative "mash" require_relative "runner" require_relative "dsl" -require_relative "mixin/command" +require_relative "mixin/shell_out" require_relative "mixin/os" require_relative "mixin/string" require_relative "mixin/constant_helper" require_relative "provides_map" require_relative "hints" -require "mixlib/shellout" unless defined?(Mixlib::ShellOut::DEFAULT_READ_TIMEOUT) require_relative "config" require "ffi_yajl" unless defined?(FFI_Yajl) diff --git a/lib/ohai/util/file_helper.rb b/lib/ohai/util/file_helper.rb index 87358d95..019d39d7 100644 --- a/lib/ohai/util/file_helper.rb +++ b/lib/ohai/util/file_helper.rb @@ -1,38 +1,5 @@ -# Author:: Lamont Granquist (<lamont@chef.io>) -# -# Copyright:: Copyright (c) 2013-14 Chef Software, Inc. -# -# License:: Apache License, Version 2.0 -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -# Copied from chef/lib/chef/util/selinux.rb - -module Ohai - module Util - module FileHelper - def which(cmd) - paths = ENV["PATH"].split(File::PATH_SEPARATOR) + [ "/bin", "/usr/bin", "/sbin", "/usr/sbin" ] - paths.each do |path| - filename = File.join(path, cmd) - if File.executable?(filename) - logger.trace("Plugin #{name}: found #{cmd} at #{filename}") - return filename - end - end - logger.trace("Plugin #{name}: did not find #{cmd}") - false - end - end - end +$stderr.puts "WARN: Ohai::Util::FileHelper is deprecated, please use Ohai::Mixin::Which or remove if the reference is unnecessary" +require_relative "../mixin/which" +module Ohai::Util + FileHelper = Ohai::Mixin::Which end diff --git a/spec/unit/mixin/command_spec.rb b/spec/unit/mixin/shell_out_spec.rb index d6df3b0d..e7e194c7 100644 --- a/spec/unit/mixin/command_spec.rb +++ b/spec/unit/mixin/shell_out_spec.rb @@ -18,20 +18,50 @@ require "spec_helper" -describe Ohai::Mixin::Command, "shell_out" do +describe Ohai::Mixin::ShellOut, "shell_out" do let(:cmd) { "sparkle-dream --version" } - let(:shell_out) { double("Mixlib::ShellOut") } + let(:shell_out) { double("Mixlib::ShellOut", live_stream: nil, :live_stream= => nil) } let(:plugin_name) { :OSSparkleDream } - let(:options) { windows? ? { timeout: 30 } : { timeout: 30, env: { "PATH" => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" } } } + let(:timeout) { 30 } - let(:logger) { instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil) } + let(:options) do + # this just replicates the behavior of default_paths in chef-utils + default_paths = [ Gem.bindir, RbConfig::CONFIG["bindir"] ].compact.uniq + + if windows? + default_paths = default_paths.join(";") + else + default_paths = ( default_paths + [ "/usr/local/sbin", "/usr/local/bin", "/usr/sbin", "/usr/bin", "/sbin", "/bin" ] ).compact.uniq.join(":") + end + + path_var = windows? ? "Path" : "PATH" + + default_locale = ChefConfig::Config.guess_internal_locale + { + timeout: timeout, + environment: { + "LANG" => default_locale, + "LANGUAGE" => default_locale, + "LC_ALL" => default_locale, + path_var => default_paths, + }, + } + end + + let(:logger) { instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil, debug?: false) } + + class DummyPlugin + include Ohai::Mixin::ShellOut + end + + let(:instance) { DummyPlugin.new } before do - allow(described_class).to receive(:logger).and_return(logger) - allow(described_class).to receive(:name).and_return(plugin_name) + allow(instance).to receive(:logger).and_return(logger) + allow(instance).to receive(:name).and_return(plugin_name) @original_env = ENV.to_hash ENV.clear end @@ -58,7 +88,7 @@ describe Ohai::Mixin::Command, "shell_out" do expect(logger).to receive(:trace) .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and returned 256") - described_class.shell_out(cmd) + instance.shell_out(cmd) end end @@ -76,9 +106,9 @@ describe Ohai::Mixin::Command, "shell_out" do expect(logger) .to receive(:trace) .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and failed " \ - "#<Errno::ENOENT: No such file or directory - sparkle-dream>") + "#<Errno::ENOENT: No such file or directory - sparkle-dream>") - expect { described_class.shell_out(cmd) } + expect { instance.shell_out(cmd) } .to raise_error(Ohai::Exceptions::Exec) end end @@ -97,15 +127,15 @@ describe Ohai::Mixin::Command, "shell_out" do expect(logger) .to receive(:trace) .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and timed " \ - "out after 30 seconds") + "out after 30 seconds") - expect { described_class.shell_out(cmd) } + expect { instance.shell_out(cmd) } .to raise_error(Ohai::Exceptions::Exec) end end describe "when a timeout option is provided" do - let(:options) { windows? ? { timeout: 10 } : { timeout: 10, env: { "PATH" => "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin" } } } + let(:timeout) { 10 } it "runs the command with the provided timeout" do expect(Mixlib::ShellOut) @@ -123,7 +153,7 @@ describe Ohai::Mixin::Command, "shell_out" do expect(logger).to receive(:trace) .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and returned 256") - described_class.shell_out(cmd, options) + instance.shell_out(cmd, timeout: 10) end describe "when the command times out" do @@ -140,9 +170,9 @@ describe Ohai::Mixin::Command, "shell_out" do expect(logger) .to receive(:trace) .with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and timed " \ - "out after 10 seconds") + "out after 10 seconds") - expect { described_class.shell_out(cmd, options) } + expect { instance.shell_out(cmd, timeout: 10) } .to raise_error(Ohai::Exceptions::Exec) end end diff --git a/spec/unit/util/file_helper_spec.rb b/spec/unit/mixin/which_spec.rb index 8fc542f0..d6236757 100644 --- a/spec/unit/util/file_helper_spec.rb +++ b/spec/unit/mixin/which_spec.rb @@ -1,6 +1,6 @@ # Author:: Bryan McLellan <btm@loftninjas.org> # -# Copyright:: Copyright (c) 2014-2016 Chef Software, Inc. +# Copyright:: Copyright (c) Chef Software Inc. # # License:: Apache License, Version 2.0 # @@ -17,26 +17,27 @@ # limitations under the License. require "spec_helper" -require "ohai/util/file_helper" class FileHelperMock - include Ohai::Util::FileHelper + include Ohai::Mixin::Which end -describe "Ohai::Util::FileHelper" do +describe "Ohai::Mixin::Which" do let(:file_helper) { FileHelperMock.new } before do + old_env = ENV + ENV["Path"] = ENV["PATH"] = "/usr/bin" allow(file_helper).to receive(:name).and_return("Fakeclass") logger = instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil) allow(file_helper).to receive(:logger).and_return(logger) allow(File).to receive(:executable?).and_return(false) + ENV = old_env end describe "which" do it "returns the path to an executable that is in the path" do allow(File).to receive(:executable?).with("/usr/bin/skyhawk").and_return(true) - expect(file_helper.which("skyhawk")).to eql "/usr/bin/skyhawk" end |