mixlib-shellout 3.0.9 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f397fa01d53b269d311c32ad263372d115735935d637385ac9fc735b504216bd
4
- data.tar.gz: c871d5ef88067456dc0b11d2fc3eae6cacaf3a2ccffd90a1b14e15555389301e
3
+ metadata.gz: 30e1e2ac3023606a67aec29e74171247e37518d698d0ca3836c05344207ec310
4
+ data.tar.gz: bc0f8c45ae8f390242e6abec532e2a4e10300d57d60ad5443e15e7a2832e28d0
5
5
  SHA512:
6
- metadata.gz: 00e5a3bd7d562a937d9719f61a8043181a5ab4e6b40ce698dffd1f1fc7ce5cc4376739cadeb259749cce448d84ba559134caf5474e9cede4f4f120fa96b3701f
7
- data.tar.gz: 76f80cba51a657c26a978668ecc04d9d96c2b8f6242968b55b022b97642963d7d5f1c2c482357508e46aae1b68dc43317d86ba3e38566b232c29c2d25b635046
6
+ metadata.gz: 656c0cd5bd821435ac0a79eaef5950d0b7a7fc94251d35a79e2ab53fa2ead1db4fd1aadf65a395224ba81a93b3c6e39fd1e8eb49b09fdfebd4b75406a689e301
7
+ data.tar.gz: 6bf939d4cb2fc86585b3d1eb6a2c39424cd1b624290b1dc72a5a5efa831804114a1a983ef27424aa17f3a98328b02b595a9945522717b4b32f74d96e5643299b
@@ -65,7 +65,7 @@ module Mixlib
65
65
  # as the subprocess is running.
66
66
  attr_accessor :live_stderr
67
67
 
68
- # ShellOut will push data from :input down the stdin of the subprocss.
68
+ # ShellOut will push data from :input down the stdin of the subprocess.
69
69
  # Normally set via options passed to new.
70
70
  # Default: nil
71
71
  attr_accessor :input
@@ -0,0 +1,209 @@
1
+ #--
2
+ # Author:: Daniel DeLeo (<dan@chef.io>)
3
+ # Copyright:: Copyright (c) Chef Software Inc.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require_relative "../shellout"
19
+ require "chef-utils"
20
+ require "chef-utils/dsl/path_sanity"
21
+ require "chef-utils/internal"
22
+
23
+ module Mixlib
24
+ class ShellOut
25
+ module Helper
26
+ include ChefUtils::Internal
27
+ include ChefUtils::DSL::PathSanity
28
+
29
+ # PREFERRED APIS:
30
+ #
31
+ # all consumers should now call shell_out!/shell_out.
32
+ #
33
+ # the shell_out_compacted/shell_out_compacted! APIs are private but are intended for use
34
+ # in rspec tests, and should ideally always be used to make code refactoring that do not
35
+ # change behavior easier:
36
+ #
37
+ # allow(provider).to receive(:shell_out_compacted!).with("foo", "bar", "baz")
38
+ # provider.shell_out!("foo", [ "bar", nil, "baz"])
39
+ # provider.shell_out!(["foo", nil, "bar" ], ["baz"])
40
+ #
41
+ # note that shell_out_compacted also includes adding the magical timeout option to force
42
+ # people to setup expectations on that value explicitly. it does not include the default_env
43
+ # mangling in order to avoid users having to setup an expectation on anything other than
44
+ # setting `default_env: false` and allow us to make tweak to the default_env without breaking
45
+ # a thousand unit tests.
46
+ #
47
+
48
+ def shell_out(*args, **options)
49
+ options = options.dup
50
+ options = __maybe_add_timeout(self, options)
51
+ if options.empty?
52
+ shell_out_compacted(*__clean_array(*args))
53
+ else
54
+ shell_out_compacted(*__clean_array(*args), **options)
55
+ end
56
+ end
57
+
58
+ def shell_out!(*args, **options)
59
+ options = options.dup
60
+ options = __maybe_add_timeout(self, options)
61
+ if options.empty?
62
+ shell_out_compacted!(*__clean_array(*args))
63
+ else
64
+ shell_out_compacted!(*__clean_array(*args), **options)
65
+ end
66
+ end
67
+
68
+ private
69
+
70
+ # helper sugar for resources that support passing timeouts to shell_out
71
+ #
72
+ # module method to not pollute namespaces, but that means we need self injected as an arg
73
+ # @api private
74
+ def __maybe_add_timeout(obj, options)
75
+ options = options.dup
76
+ # historically resources have not properly declared defaults on their timeouts, so a default default of 900s was enforced here
77
+ default_val = 900
78
+ return options if options.key?(:timeout)
79
+
80
+ # FIXME: need to nuke descendent tracker out of Chef::Provider so we can just define that class here without requiring the
81
+ # world, and then just use symbol lookup
82
+ if obj.class.ancestors.map(&:name).include?("Chef::Provider") && obj.respond_to?(:new_resource) && obj.new_resource.respond_to?(:timeout) && !options.key?(:timeout)
83
+ options[:timeout] = obj.new_resource.timeout ? obj.new_resource.timeout.to_f : default_val
84
+ end
85
+ options
86
+ end
87
+
88
+ # helper function to mangle options when `default_env` is true
89
+ #
90
+ # @api private
91
+ def __apply_default_env(options)
92
+ options = options.dup
93
+ default_env = options.delete(:default_env)
94
+ default_env = true if default_env.nil?
95
+ if default_env
96
+ env_key = options.key?(:env) ? :env : :environment
97
+ options[env_key] = {
98
+ "LC_ALL" => __config[:internal_locale],
99
+ "LANGUAGE" => __config[:internal_locale],
100
+ "LANG" => __config[:internal_locale],
101
+ __env_path_name => sanitized_path,
102
+ }.update(options[env_key] || {})
103
+ end
104
+ options
105
+ end
106
+
107
+ # this SHOULD be used for setting up expectations in rspec, see banner comment at top.
108
+ #
109
+ # the private constraint is meant to avoid code calling this directly, rspec expectations are fine.
110
+ #
111
+ def shell_out_compacted(*args, **options)
112
+ options = __apply_default_env(options)
113
+ if options.empty?
114
+ __shell_out_command(*args)
115
+ else
116
+ __shell_out_command(*args, **options)
117
+ end
118
+ end
119
+
120
+ # this SHOULD be used for setting up expectations in rspec, see banner comment at top.
121
+ #
122
+ # the private constraint is meant to avoid code calling this directly, rspec expectations are fine.
123
+ #
124
+ def shell_out_compacted!(*args, **options)
125
+ options = __apply_default_env(options)
126
+ cmd = if options.empty?
127
+ __shell_out_command(*args)
128
+ else
129
+ __shell_out_command(*args, **options)
130
+ end
131
+ cmd.error!
132
+ cmd
133
+ end
134
+
135
+ # Helper for subclasses to reject nil out of an array. It allows
136
+ # using the array form of shell_out (which avoids the need to surround arguments with
137
+ # quote marks to deal with shells).
138
+ #
139
+ # Usage:
140
+ # shell_out!(*clean_array("useradd", universal_options, useradd_options, new_resource.username))
141
+ #
142
+ # universal_options and useradd_options can be nil, empty array, empty string, strings or arrays
143
+ # and the result makes sense.
144
+ #
145
+ # keeping this separate from shell_out!() makes it a bit easier to write expectations against the
146
+ # shell_out args and be able to omit nils and such in the tests (and to test that the nils are
147
+ # being rejected correctly).
148
+ #
149
+ # @param args [String] variable number of string arguments
150
+ # @return [Array] array of strings with nil and null string rejection
151
+
152
+ def __clean_array(*args)
153
+ args.flatten.compact.map(&:to_s)
154
+ end
155
+
156
+ def __shell_out_command(*args, **options)
157
+ if __transport_connection
158
+ FakeShellOut.new(args, options, __transport_connection.run_command(args.join(" "))) # FIXME: train should accept run_command(*args)
159
+ else
160
+ cmd = if options.empty?
161
+ Mixlib::ShellOut.new(*args)
162
+ else
163
+ Mixlib::ShellOut.new(*args, **options)
164
+ end
165
+ cmd.live_stream ||= __io_for_live_stream
166
+ cmd.run_command
167
+ cmd
168
+ end
169
+ end
170
+
171
+ def __io_for_live_stream
172
+ if STDOUT.tty? && !__config[:daemon] && __log.debug?
173
+ STDOUT
174
+ else
175
+ nil
176
+ end
177
+ end
178
+
179
+ def __env_path_name
180
+ if ChefUtils.windows?
181
+ "Path"
182
+ else
183
+ "PATH"
184
+ end
185
+ end
186
+
187
+ class FakeShellOut
188
+ attr_reader :stdout, :stderr, :exitstatus, :status
189
+
190
+ def initialize(args, options, result)
191
+ @args = args
192
+ @options = options
193
+ @stdout = result.stdout
194
+ @stderr = result.stderr
195
+ @exitstatus = result.exit_status
196
+ @status = OpenStruct.new(success?: ( exitstatus == 0 ))
197
+ end
198
+
199
+ def error?
200
+ exitstatus != 0
201
+ end
202
+
203
+ def error!
204
+ raise Mixlib::ShellOut::ShellCommandFailed, "Unexpected exit status of #{exitstatus} running #{@args}" if error?
205
+ end
206
+ end
207
+ end
208
+ end
209
+ end
@@ -1,5 +1,5 @@
1
1
  module Mixlib
2
2
  class ShellOut
3
- VERSION = "3.0.9".freeze
3
+ VERSION = "3.1.0".freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mixlib-shellout
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.9
4
+ version: 3.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chef Software Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-30 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2020-07-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: chef-utils
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  description: Run external commands on Unix or Windows
14
28
  email: info@chef.io
15
29
  executables: []
@@ -19,6 +33,7 @@ files:
19
33
  - LICENSE
20
34
  - lib/mixlib/shellout.rb
21
35
  - lib/mixlib/shellout/exceptions.rb
36
+ - lib/mixlib/shellout/helper.rb
22
37
  - lib/mixlib/shellout/unix.rb
23
38
  - lib/mixlib/shellout/version.rb
24
39
  - lib/mixlib/shellout/windows.rb
@@ -34,7 +49,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
34
49
  requirements:
35
50
  - - ">="
36
51
  - !ruby/object:Gem::Version
37
- version: '2.2'
52
+ version: '2.4'
38
53
  required_rubygems_version: !ruby/object:Gem::Requirement
39
54
  requirements:
40
55
  - - ">="