Module: Chef::Mixin::ShellOut

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PathSanity

enforce_path_sanity, sanitized_path

Class Method Details

.apply_default_env(options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

helper function to mangle options when `default_env` is true



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/chef/mixin/shell_out.rb', line 155

def self.apply_default_env(options)
  options = options.dup
  default_env = options.delete(:default_env)
  default_env = true if default_env.nil?
  if default_env
    env_key = options.key?(:env) ? :env : :environment
    options[env_key] = {
      "LC_ALL" => Chef::Config[:internal_locale],
      "LANGUAGE" => Chef::Config[:internal_locale],
      "LANG" => Chef::Config[:internal_locale],
      env_path => sanitized_path,
    }.update(options[env_key] || {})
  end
  options
end

.maybe_add_timeout(obj, options) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

helper sugar for resources that support passing timeouts to shell_out

module method to not pollute namespaces, but that means we need self injected as an arg



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/chef/mixin/shell_out.rb', line 132

def self.maybe_add_timeout(obj, options)
  options = options.dup
  force = options.delete(:argument_that_will_go_away_in_chef_15_so_do_not_use_it) # remove in Chef-15
  # historically resources have not properly declared defaults on their timeouts, so a default default of 900s was enforced here
  default_val = 900
  if !force
    return options if options.key?(:timeout) # leave this line in Chef-15, delete the rest of the conditional
  else
    default_val = options[:timeout] if options.key?(:timeout) # delete in Chef-15
  end
  # note that we can't define an empty Chef::Resource::LWRPBase because that breaks descendants tracker, so we'd have to instead require the file here, which would pull in half
  # of chef, so instead convert to using strings.  once descendants tracker is gone, we can just declare the empty classes instead and use `is_a?` against the symbols.
  # (be nice if ruby supported strings in `is_a?` for looser coupling).
  # FIXME: just use `if obj.respond_to?(:new_resource) && obj.new_resource.respond_to?(:timeout) && !options.key?(:timeout)` in Chef 15
  if obj.respond_to?(:new_resource) && ( force || ( obj.class.ancestors.map(&:name).include?("Chef::Provider") && !obj.class.ancestors.map(&:name).include?("Chef::Resource::LWRPBase") && !obj.class.ancestors.map(&:name).include?("Chef::Resource::ActionClass") && obj.new_resource.respond_to?(:timeout) && !options.key?(:timeout) ) )
    options[:timeout] = obj.new_resource.timeout ? obj.new_resource.timeout.to_f : default_val
  end
  options
end

Instance Method Details

#a_to_s(*args) ⇒ Object



103
104
105
106
# File 'lib/chef/mixin/shell_out.rb', line 103

def a_to_s(*args)
  Chef.deprecated(:shell_out, "a_to_s is deprecated use shell_out with splat-args")
  args.flatten.reject { |i| i.nil? || i == "" }.map(&:to_s).join(" ")
end

#clean_array(*args) ⇒ Object



171
172
173
174
# File 'lib/chef/mixin/shell_out.rb', line 171

def clean_array(*args)
  Chef.deprecated(:shell_out, "do not call clean_array directly, just use shell_out with splat args or an array")
  Chef::Mixin::ShellOut.clean_array(*args)
end

#shell_out(*args, **options) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/chef/mixin/shell_out.rb', line 108

def shell_out(*args, **options)
  options = options.dup
  options = Chef::Mixin::ShellOut.maybe_add_timeout(self, options)
  if options.empty?
    shell_out_compacted(*Chef::Mixin::ShellOut.clean_array(*args))
  else
    shell_out_compacted(*Chef::Mixin::ShellOut.clean_array(*args), **options)
  end
end

#shell_out!(*args, **options) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/chef/mixin/shell_out.rb', line 118

def shell_out!(*args, **options)
  options = options.dup
  options = Chef::Mixin::ShellOut.maybe_add_timeout(self, options)
  if options.empty?
    shell_out_compacted!(*Chef::Mixin::ShellOut.clean_array(*args))
  else
    shell_out_compacted!(*Chef::Mixin::ShellOut.clean_array(*args), **options)
  end
end

#shell_out_compact(*args, **options) ⇒ Object

PREFERRED APIS:

all consumers should now call shell_out!/shell_out.

on unix the shell_out API supports the clean_array() kind of syntax (below) so that array args are flat/compact/to_s'd. on windows, array args aren't supported to its up to the caller to join(“ ”) on arrays of strings.

the shell_out_compacted/shell_out_compacted! APIs are private but are intended for use in rspec tests, and should ideally always be used to make code refactorings that do not change behavior easier:

allow(provider).to receive(:shell_out_compacted!).with(“foo”, “bar”, “baz”) provider.shell_out!(“foo”, [ “bar”, nil, “baz”]) provider.shell_out!([“foo”, nil, “bar” ], [“baz”])

note that shell_out_compacted also includes adding the magical timeout option to force people to setup expectations on that value explicitly. it does not include the default_env mangling in order to avoid users having to setup an expectation on anything other than setting `default_env: false` and allow us to make tweak to the default_env without breaking a thousand unit tests.



49
50
51
52
53
54
55
56
# File 'lib/chef/mixin/shell_out.rb', line 49

def shell_out_compact(*args, **options)
  Chef.deprecated(:shell_out, "shell_out_compact should be replaced by shell_out")
  if options.empty?
    shell_out(*args)
  else
    shell_out(*args, **options)
  end
end

#shell_out_compact!(*args, **options) ⇒ Object



58
59
60
61
62
63
64
65
# File 'lib/chef/mixin/shell_out.rb', line 58

def shell_out_compact!(*args, **options)
  Chef.deprecated(:shell_out, "shell_out_compact! should be replaced by shell_out!")
  if options.empty?
    shell_out!(*args)
  else
    shell_out!(*args, **options)
  end
end

#shell_out_compact_timeout(*args, **options) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/chef/mixin/shell_out.rb', line 67

def shell_out_compact_timeout(*args, **options)
  Chef.deprecated(:shell_out, "shell_out_compact_timeout should be replaced by shell_out")
  if options.empty?
    shell_out(*args, argument_that_will_go_away_in_chef_15_so_do_not_use_it: true)
  else
    shell_out(*args, argument_that_will_go_away_in_chef_15_so_do_not_use_it: true, **options)
  end
end

#shell_out_compact_timeout!(*args, **options) ⇒ Object



76
77
78
79
80
81
82
83
# File 'lib/chef/mixin/shell_out.rb', line 76

def shell_out_compact_timeout!(*args, **options)
  Chef.deprecated(:shell_out, "shell_out_compact_timeout! should be replaced by shell_out!")
  if options.empty?
    shell_out!(*args, argument_that_will_go_away_in_chef_15_so_do_not_use_it: true)
  else
    shell_out!(*args, argument_that_will_go_away_in_chef_15_so_do_not_use_it: true, **options)
  end
end

#shell_out_with_systems_locale(*args, **options) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/chef/mixin/shell_out.rb', line 85

def shell_out_with_systems_locale(*args, **options)
  Chef.deprecated(:shell_out, "shell_out_with_systems_locale should be replaced by shell_out with the default_env option set to false")
  if options.empty?
    shell_out(*args, default_env: false)
  else
    shell_out(*args, default_env: false, **options)
  end
end

#shell_out_with_systems_locale!(*args, **options) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/chef/mixin/shell_out.rb', line 94

def shell_out_with_systems_locale!(*args, **options)
  Chef.deprecated(:shell_out, "shell_out_with_systems_locale! should be replaced by shell_out! with the default_env option set to false")
  if options.empty?
    shell_out!(*args, default_env: false)
  else
    shell_out!(*args, default_env: false, **options)
  end
end