Module: Chef::Mixin::ShellOut
- Included in:
- Application::Base, Application::WindowsService, Cookbook::GemInstaller, Cookbook::SyntaxCheck, DSL::Universal, GuardInterpreter::DefaultGuardInterpreter, Knife::CookbookSCMRepo, Knife::Ssh, Knife::SupermarketShare, HomebrewUser, PowershellOut, Platform::Rebooter, Provider::AptRepository, Provider::Cron::Unix, Provider::Group, Provider::Ifconfig, Provider::Mount, Provider::Package, Provider::Package::Cab, Provider::Package::Dnf, Provider::Package::Dnf::PythonHelper, Provider::Package::Msu, Provider::Package::Openbsd, Provider::Package::Rubygems::AlternateGemEnvironment, Provider::Package::Windows::Exe, Provider::Package::Windows::MSI, Provider::Package::Yum, Provider::Package::Yum::PythonHelper, Provider::Service::Freebsd, Provider::Service::Openbsd, Provider::Service::Windows, Provider::SystemdUnit, Provider::WindowsTask, Resource::Conditional, Resource::DnfPackage, Resource::FreebsdPackage, Resource::OpenbsdPackage, Util::Selinux
- Defined in:
- lib/chef/mixin/shell_out.rb
Defined Under Namespace
Classes: FakeShellOut
Class Method Summary collapse
-
.apply_default_env(options) ⇒ Object
private
helper function to mangle options when ‘default_env` is true.
-
.maybe_add_timeout(obj, options) ⇒ Object
private
helper sugar for resources that support passing timeouts to shell_out.
Instance Method Summary collapse
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
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/chef/mixin/shell_out.rb', line 89 def self.apply_default_env() = .dup default_env = .delete(:default_env) default_env = true if default_env.nil? if default_env env_key = .key?(:env) ? :env : :environment [env_key] = { "LC_ALL" => Chef::Config[:internal_locale], "LANGUAGE" => Chef::Config[:internal_locale], "LANG" => Chef::Config[:internal_locale], env_path => ChefUtils::DSL::PathSanity.sanitized_path, }.update([env_key] || {}) end 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
72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/chef/mixin/shell_out.rb', line 72 def self.maybe_add_timeout(obj, ) = .dup # historically resources have not properly declared defaults on their timeouts, so a default default of 900s was enforced here default_val = 900 return if .key?(:timeout) # FIXME: need to nuke descendents tracker out of Chef::Provider so we can just define that class here without requiring the # world, and then just use symbol lookup if obj.class.ancestors.map(&:name).include?("Chef::Provider") && obj.respond_to?(:new_resource) && obj.new_resource.respond_to?(:timeout) && !.key?(:timeout) [:timeout] = obj.new_resource.timeout ? obj.new_resource.timeout.to_f : default_val end end |
Instance Method Details
#shell_out(*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.
48 49 50 51 52 53 54 55 56 |
# File 'lib/chef/mixin/shell_out.rb', line 48 def shell_out(*args, **) = .dup = Chef::Mixin::ShellOut.maybe_add_timeout(self, ) if .empty? shell_out_compacted(*Chef::Mixin::ShellOut.clean_array(*args)) else shell_out_compacted(*Chef::Mixin::ShellOut.clean_array(*args), **) end end |
#shell_out!(*args, **options) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/chef/mixin/shell_out.rb', line 58 def shell_out!(*args, **) = .dup = Chef::Mixin::ShellOut.maybe_add_timeout(self, ) if .empty? shell_out_compacted!(*Chef::Mixin::ShellOut.clean_array(*args)) else shell_out_compacted!(*Chef::Mixin::ShellOut.clean_array(*args), **) end end |