Module: Poise::Utils::ShellOut

Extended by:
ShellOut
Includes:
Chef::Mixin::ShellOut
Included in:
Provider, Resource, ShellOut
Defined in:
lib/poise/utils/shell_out.rb

Overview

A mixin to provider a better shell_out.

Examples:

Poise::Utils::ShellOut.poise_shell_out('ruby myapp.rb', user: 'myuser')

Since:

  • 2.5.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.poise_shell_out(*command_args, **options) ⇒ Mixlib::ShellOut

An enhanced version of Chef's shell_out which sets some default parameters. If possible it will set $HOME, $USER, $LOGNAME, and the group to run as.

Parameters:

  • command_args (Array)

    Command arguments to be passed to shell_out.

  • options (Hash<Symbol, Object>)

    Options to be passed to shell_out, with modifications.

Returns:

  • (Mixlib::ShellOut)

Since:

  • 2.5.0



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/poise/utils/shell_out.rb', line 41

def poise_shell_out(*command_args, **options)
  # Allow the env option shorthand.
  options[:environment] ||= {}
  if options[:env]
    options[:environment].update(options[:env])
    options.delete(:env)
  end
  # Convert environment keys to strings to be safe.
  options[:environment] = options[:environment].inject({}) do |memo, (key, value)|
    memo[key.to_s] = value.to_s
    memo
  end
  # Populate some standard environment variables.
  ent = begin
    if options[:user].is_a?(Integer)
      Etc.getpwuid(options[:user])
    elsif options[:user]
      Etc.getpwnam(options[:user])
    end
  rescue ArgumentError
    nil
  end
  username = ent ? ent.name : options[:name]
  if username
    options[:environment]['HOME'] ||= Dir.home(username)
    options[:environment]['USER'] ||= username
    # On the off chance they set one manually but not the other.
    options[:environment]['LOGNAME'] ||= options[:environment]['USER']
  end
  # Set the default group on Unix.
  options[:group] ||= ent.gid if ent
  # Mixlib-ShellOut doesn't support array commands on Windows and has
  # super wonky escaping for cmd.exe.
  if respond_to?(:node) && node.platform_family?('windows')
    command_args = [Poise::Utils::Win32.reparse_command(*command_args)]
  end
  # Call Chef's shell_out wrapper.
  shell_out(*command_args, **options)
end

.poise_shell_out!(*command_args) ⇒ Mixlib::ShellOut

The error! version of #poise_shell_out.

Returns:

  • (Mixlib::ShellOut)

See Also:

Since:

  • 2.5.0



85
86
87
# File 'lib/poise/utils/shell_out.rb', line 85

def poise_shell_out!(*command_args)
  poise_shell_out(*command_args).tap(&:error!)
end

Instance Method Details

#poise_shell_out(*command_args, **options) ⇒ Mixlib::ShellOut

An enhanced version of Chef's shell_out which sets some default parameters. If possible it will set $HOME, $USER, $LOGNAME, and the group to run as.

Parameters:

  • command_args (Array)

    Command arguments to be passed to shell_out.

  • options (Hash<Symbol, Object>)

    Options to be passed to shell_out, with modifications.

Returns:

  • (Mixlib::ShellOut)

Since:

  • 2.5.0



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/poise/utils/shell_out.rb', line 41

def poise_shell_out(*command_args, **options)
  # Allow the env option shorthand.
  options[:environment] ||= {}
  if options[:env]
    options[:environment].update(options[:env])
    options.delete(:env)
  end
  # Convert environment keys to strings to be safe.
  options[:environment] = options[:environment].inject({}) do |memo, (key, value)|
    memo[key.to_s] = value.to_s
    memo
  end
  # Populate some standard environment variables.
  ent = begin
    if options[:user].is_a?(Integer)
      Etc.getpwuid(options[:user])
    elsif options[:user]
      Etc.getpwnam(options[:user])
    end
  rescue ArgumentError
    nil
  end
  username = ent ? ent.name : options[:name]
  if username
    options[:environment]['HOME'] ||= Dir.home(username)
    options[:environment]['USER'] ||= username
    # On the off chance they set one manually but not the other.
    options[:environment]['LOGNAME'] ||= options[:environment]['USER']
  end
  # Set the default group on Unix.
  options[:group] ||= ent.gid if ent
  # Mixlib-ShellOut doesn't support array commands on Windows and has
  # super wonky escaping for cmd.exe.
  if respond_to?(:node) && node.platform_family?('windows')
    command_args = [Poise::Utils::Win32.reparse_command(*command_args)]
  end
  # Call Chef's shell_out wrapper.
  shell_out(*command_args, **options)
end

#poise_shell_out!(*command_args) ⇒ Mixlib::ShellOut

The error! version of #poise_shell_out.

Returns:

  • (Mixlib::ShellOut)

See Also:

Since:

  • 2.5.0



85
86
87
# File 'lib/poise/utils/shell_out.rb', line 85

def poise_shell_out!(*command_args)
  poise_shell_out(*command_args).tap(&:error!)
end