diff options
author | Bryan McLellan <btm@loftninjas.org> | 2018-10-16 15:05:47 -0400 |
---|---|---|
committer | Bryan McLellan <btm@loftninjas.org> | 2019-05-06 12:56:55 -0400 |
commit | 9d5f5c40362d1fd7b0323cf0880300d6165b3a94 (patch) | |
tree | 2bb91f44829455b5566c0de4ad6bb154dd610e4b /lib/chef/mixin | |
parent | 7560313217c851c5b018b27d6ea4bae8c3af0ff1 (diff) | |
download | chef-9d5f5c40362d1fd7b0323cf0880300d6165b3a94.tar.gz |
Target Mode initial implementation
Signed-off-by: Bryan McLellan <btm@chef.io>
Diffstat (limited to 'lib/chef/mixin')
-rw-r--r-- | lib/chef/mixin/train_or_shell.rb | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/lib/chef/mixin/train_or_shell.rb b/lib/chef/mixin/train_or_shell.rb new file mode 100644 index 0000000000..c976094b14 --- /dev/null +++ b/lib/chef/mixin/train_or_shell.rb @@ -0,0 +1,74 @@ +# +# 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" + +class Chef + module Mixin + module TrainOrShell + # + # 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 + + 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 + end + end +end |