summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2020-08-21 16:30:54 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2020-08-21 16:30:54 -0700
commitf11b854cd189e8ad311592d507a0849cfbc0da52 (patch)
treef1814b84c9d2a9520a59a55a13eb27feb6ee21aa
parent654df0481f3c35c027a9afc313c74583f3d4112f (diff)
downloadohai-f11b854cd189e8ad311592d507a0849cfbc0da52.tar.gz
Migrate to the chef-utils helpers
This replaces the which/shell_out helpers with ones from chef-utils. Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
-rw-r--r--Rakefile2
-rw-r--r--lib/ohai/dsl/plugin.rb10
-rw-r--r--lib/ohai/mixin/chef_utils_wiring.rb41
-rw-r--r--lib/ohai/mixin/command.rb56
-rw-r--r--lib/ohai/mixin/shell_out.rb50
-rw-r--r--lib/ohai/mixin/which.rb38
-rw-r--r--lib/ohai/system.rb5
-rw-r--r--lib/ohai/util/file_helper.rb40
-rw-r--r--spec/unit/mixin/shell_out_spec.rb (renamed from spec/unit/mixin/command_spec.rb)46
9 files changed, 176 insertions, 112 deletions
diff --git a/Rakefile b/Rakefile
index 93aff295..00d67eda 100644
--- a/Rakefile
+++ b/Rakefile
@@ -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..527ac497
--- /dev/null
+++ b/lib/ohai/mixin/chef_utils_wiring.rb
@@ -0,0 +1,41 @@
+#
+# 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 "../log"
+# require_relative "../config"
+# require_relative "../chef_class"
+
+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..f335db93 100644
--- a/lib/ohai/mixin/command.rb
+++ b/lib/ohai/mixin/command.rb
@@ -1,54 +1,2 @@
-#
-# 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
+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..32c83f0f
--- /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/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..6a52be15 100644
--- a/lib/ohai/util/file_helper.rb
+++ b/lib/ohai/util/file_helper.rb
@@ -1,38 +1,4 @@
-# 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
+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..6c54b166 100644
--- a/spec/unit/mixin/command_spec.rb
+++ b/spec/unit/mixin/shell_out_spec.rb
@@ -18,20 +18,42 @@
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(:options) do
+ if windows?
+ { timeout: timeout }
+ else
+ {
+ timeout: timeout,
+ environment: {
+ "LANG" => "en_US.UTF-8",
+ "LANGUAGE" => "en_US.UTF-8",
+ "LC_ALL" => "en_US.UTF-8",
+ "PATH" => "/Users/lamont/.asdf/installs/ruby/2.7.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
+ },
+ }
+ end
+ end
+
+ let(:logger) { instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil, debug?: false) }
+
+ class DummyPlugin
+ include Ohai::Mixin::ShellOut
+ end
- let(:logger) { instance_double("Mixlib::Log::Child", trace: nil, debug: nil, warn: nil) }
+ 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 +80,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
@@ -78,7 +100,7 @@ describe Ohai::Mixin::Command, "shell_out" do
.with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and failed " \
"#<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
@@ -99,13 +121,13 @@ describe Ohai::Mixin::Command, "shell_out" do
.with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and timed " \
"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 +145,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
@@ -142,7 +164,7 @@ describe Ohai::Mixin::Command, "shell_out" do
.with("Plugin OSSparkleDream: ran 'sparkle-dream --version' and timed " \
"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