summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLamont Granquist <lamont@chef.io>2019-05-23 17:06:31 -0700
committerGitHub <noreply@github.com>2019-05-23 17:06:31 -0700
commit56a34f0a484b64480c4f1c99e0f5003ec4e82cc9 (patch)
tree84558cae2577c88df0be3b23ab1596c549c405c6
parentfe9a02be76d4e6a8c1a1e72019104b678ead6926 (diff)
parentace17c29b95c118b76f3d5440b614c0075992e0c (diff)
downloadchef-56a34f0a484b64480c4f1c99e0f5003ec4e82cc9.tar.gz
Merge pull request #8584 from chef/lcg/better-target-mode-shell-out
Better target mode shell_out
-rw-r--r--lib/chef/dsl/universal.rb2
-rw-r--r--lib/chef/guard_interpreter/default_guard_interpreter.rb6
-rw-r--r--lib/chef/mixin/shell_out.rb45
-rw-r--r--lib/chef/mixin/train_or_shell.rb83
-rw-r--r--lib/chef/provider/execute.rb5
-rw-r--r--lib/chef/provider/package/apt.rb4
-rw-r--r--lib/chef/resource/apt_package.rb3
7 files changed, 45 insertions, 103 deletions
diff --git a/lib/chef/dsl/universal.rb b/lib/chef/dsl/universal.rb
index cdcd5ee286..c9afa38f98 100644
--- a/lib/chef/dsl/universal.rb
+++ b/lib/chef/dsl/universal.rb
@@ -25,7 +25,6 @@ require_relative "../mixin/powershell_exec"
require_relative "../mixin/powershell_out"
require_relative "../mixin/shell_out"
require_relative "../mixin/lazy_module_include"
-require_relative "../mixin/train_or_shell"
class Chef
module DSL
@@ -55,7 +54,6 @@ class Chef
include Chef::Mixin::PowershellExec
include Chef::Mixin::PowershellOut
include Chef::Mixin::ShellOut
- include Chef::Mixin::TrainOrShell
extend Chef::Mixin::LazyModuleInclude
end
end
diff --git a/lib/chef/guard_interpreter/default_guard_interpreter.rb b/lib/chef/guard_interpreter/default_guard_interpreter.rb
index 57a82cd9d6..7ed0570291 100644
--- a/lib/chef/guard_interpreter/default_guard_interpreter.rb
+++ b/lib/chef/guard_interpreter/default_guard_interpreter.rb
@@ -16,13 +16,13 @@
# limitations under the License.
#
-require_relative "../mixin/train_or_shell"
+require_relative "../mixin/shell_out"
require_relative "../exceptions"
class Chef
class GuardInterpreter
class DefaultGuardInterpreter
- include Chef::Mixin::TrainOrShell
+ include Chef::Mixin::ShellOut
def initialize(command, opts)
@command = command
@@ -30,7 +30,7 @@ class Chef
end
def evaluate
- result = train_or_shell(@command, default_env: false, **@command_opts)
+ result = shell_out(@command, default_env: false, **@command_opts)
Chef::Log.debug "Command failed: #{result.stderr}" unless result.status.success?
result.status.success?
# Timeout fails command rather than chef-client run, see:
diff --git a/lib/chef/mixin/shell_out.rb b/lib/chef/mixin/shell_out.rb
index f439323411..d888dc3457 100644
--- a/lib/chef/mixin/shell_out.rb
+++ b/lib/chef/mixin/shell_out.rb
@@ -153,15 +153,23 @@ class Chef
args.flatten.compact.map(&:to_s)
end
+ def self.transport_connection
+ Chef.run_context.transport_connection
+ end
+
def self.shell_out_command(*args, **options)
- cmd = if options.empty?
- Mixlib::ShellOut.new(*args)
- else
- Mixlib::ShellOut.new(*args, **options)
- end
- cmd.live_stream ||= io_for_live_stream
- cmd.run_command
- cmd
+ if Chef::Config.target_mode?
+ FakeShellOut.new(args, options, transport_connection.run_command(args.join(" "))) # FIXME FIXME FIXME: join here is horrible
+ else
+ cmd = if options.empty?
+ Mixlib::ShellOut.new(*args)
+ else
+ Mixlib::ShellOut.new(*args, **options)
+ end
+ cmd.live_stream ||= io_for_live_stream
+ cmd.run_command
+ cmd
+ end
end
def self.io_for_live_stream
@@ -179,6 +187,27 @@ class Chef
"PATH"
end
end
+
+ class FakeShellOut
+ attr_reader :stdout, :stderr, :exitstatus, :status
+
+ def initialize(args, options, result)
+ @args = args
+ @options = options
+ @stdout = result.stdout
+ @stderr = result.stderr
+ @exitstatus = result.exit_status
+ @status = OpenStruct.new(success?: ( exitstatus == 0 ))
+ end
+
+ def error?
+ exitstatus != 0
+ end
+
+ def error!
+ raise Mixlib::ShellOut::ShellCommandFailed, "Unexpected exit status of #{exitstatus} running #{@args}" if error?
+ end
+ end
end
end
end
diff --git a/lib/chef/mixin/train_or_shell.rb b/lib/chef/mixin/train_or_shell.rb
deleted file mode 100644
index f1ea98547b..0000000000
--- a/lib/chef/mixin/train_or_shell.rb
+++ /dev/null
@@ -1,83 +0,0 @@
-#
-# Author:: Bryan McLellan <btm@loftninjas.org>
-# Copyright:: Copyright 2018, 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 "ostruct"
-require_relative "shell_out"
-require_relative "powershell_out"
-require_relative "../config"
-
-class Chef
- module Mixin
- module TrainOrShell
-
- include Chef::Mixin::ShellOut
- include Chef::Mixin::PowershellOut
-
- def train_or_shell(*args, **opts)
- if Chef::Config.target_mode?
- result = run_context.transport_connection.run_command(args)
- train_to_shellout_result(result.stdout, result.stderr, result.exit_status)
- else
- shell_out(*args, opts)
- end
- end
-
- def train_or_shell!(*args, **opts)
- if Chef::Config.target_mode?
- result = run_context.transport_connection.run_command(*args)
- raise Mixlib::ShellOut::ShellCommandFailed, "Unexpected exit status of #{result.exit_status} running #{args}" if result.exit_status != 0
- train_to_shellout_result(result.stdout, result.stderr, result.exit_status)
- else
- shell_out!(*args, opts)
- end
- end
-
- def train_or_powershell(*args, **opts)
- if Chef::Config.target_mode?
- run_context.transport_connection.run_command(args)
- train_to_shellout_result(result.stdout, result.stderr, result.exit_status)
- else
- powershell_out(*args)
- end
- end
-
- def train_or_powershell!(*args, **opts)
- if Chef::Config.target_mode?
- result = run_context.transport_connection.run_command(args)
- raise Mixlib::ShellOut::ShellCommandFailed, "Unexpected exit status of #{result.exit_status} running #{args}" if result.exit_status != 0
- train_to_shellout_result(result.stdout, result.stderr, result.exit_status)
- else
- powershell_out!(*args)
- end
- end
-
- private
-
- #
- # Train #run_command returns a Train::Extras::CommandResult which
- # includes `exit_status` but Mixlib::Shellout returns exitstatus
- # This wrapper makes the result look like Mixlib::ShellOut to make it
- # easier to swap providers from #shell_out to #train_or_shell
- #
- def train_to_shellout_result(stdout, stderr, exit_status)
- status = OpenStruct.new(success?: ( exit_status == 0 ))
- OpenStruct.new(stdout: stdout, stderr: stderr, exitstatus: exit_status, status: status)
- end
- end
- end
-end
diff --git a/lib/chef/provider/execute.rb b/lib/chef/provider/execute.rb
index 793d620c88..c4312b58b4 100644
--- a/lib/chef/provider/execute.rb
+++ b/lib/chef/provider/execute.rb
@@ -19,7 +19,6 @@
require_relative "../log"
require_relative "../provider"
require "forwardable" unless defined?(Forwardable)
-require_relative "../mixin/train_or_shell"
class Chef
class Provider
@@ -30,8 +29,6 @@ class Chef
def_delegators :new_resource, :command, :returns, :environment, :user, :domain, :password, :group, :cwd, :umask, :creates, :elevated, :default_env
- include Chef::Mixin::TrainOrShell
-
def load_current_resource
current_resource = Chef::Resource::Execute.new(new_resource.name)
current_resource
@@ -58,7 +55,7 @@ class Chef
converge_by("execute #{description}") do
begin
- train_or_shell!(command, opts)
+ shell_out!(command, opts)
rescue Mixlib::ShellOut::ShellCommandFailed
if sensitive?
ex = Mixlib::ShellOut::ShellCommandFailed.new("Command execution failed. STDOUT/STDERR suppressed for sensitive resource")
diff --git a/lib/chef/provider/package/apt.rb b/lib/chef/provider/package/apt.rb
index 99329ebdee..cd28746411 100644
--- a/lib/chef/provider/package/apt.rb
+++ b/lib/chef/provider/package/apt.rb
@@ -27,8 +27,8 @@ class Chef
include Chef::Provider::Package::Deb
use_multipackage_api
- provides :package, platform_family: "debian"
- provides :apt_package
+ provides :package, platform_family: "debian", target_mode: true
+ provides :apt_package, target_mode: true
def initialize(new_resource, run_context)
super
diff --git a/lib/chef/resource/apt_package.rb b/lib/chef/resource/apt_package.rb
index c142c142ce..dace0add45 100644
--- a/lib/chef/resource/apt_package.rb
+++ b/lib/chef/resource/apt_package.rb
@@ -22,7 +22,8 @@ class Chef
class Resource
class AptPackage < Chef::Resource::Package
resource_name :apt_package
- provides :package, platform_family: "debian"
+ provides :apt_package, target_mode: true
+ provides :package, platform_family: "debian", target_mode: true
description "Use the apt_package resource to manage packages on Debian and Ubuntu platforms."