Sha256: 59a0860e40e1bd339425e7cee0b72b1e9a6f842877ff6d2c2c04872eca3eba4c

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

#
# 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 "ohai/exception"
require "ohai/log"
require "mixlib/shellout"

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)
        # unless specified by the caller timeout after 30 seconds
        options[:timeout] ||= 30
        so = Mixlib::ShellOut.new(cmd, options)
        begin
          so.run_command
          Ohai::Log.debug("Plugin #{self.name}: ran '#{cmd}' and returned #{so.exitstatus}")
          so
        rescue Errno::ENOENT => e
          Ohai::Log.debug("Plugin #{self.name}: ran '#{cmd}' and failed #{e.inspect}")
          raise Ohai::Exceptions::Exec, e
        rescue Mixlib::ShellOut::CommandTimeout => e
          Ohai::Log.debug("Plugin #{self.name}: ran '#{cmd}' and timed out after #{options[:timeout]} seconds")
          raise Ohai::Exceptions::Exec, e
        end
      end

      module_function :shell_out
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
ohai-13.0.1 lib/ohai/mixin/command.rb
ohai-13.0.0 lib/ohai/mixin/command.rb