class Train::Extras::LinuxCommand

Wrap linux commands and add functionality like sudo.

Public Class Methods

active?(options) click to toggle source
# File lib/train/extras/command_wrapper.rb, line 78
def self.active?(options)
  options.is_a?(Hash) && options[:sudo]
end
new(backend, options) click to toggle source
# File lib/train/extras/command_wrapper.rb, line 38
def initialize(backend, options)
  @backend = backend
  validate_options(options)

  @sudo = options[:sudo]
  @sudo_options = options[:sudo_options]
  @sudo_password = options[:sudo_password]
  @sudo_command = options[:sudo_command]
  @user = options[:user]
  @prefix = build_prefix
end

Public Instance Methods

run(command) click to toggle source

(see CommandWrapperBase::run)

# File lib/train/extras/command_wrapper.rb, line 74
def run(command)
  @prefix + command
end
verify() click to toggle source

(see CommandWrapperBase::verify)

# File lib/train/extras/command_wrapper.rb, line 51
def verify
  res = @backend.run_command(run('echo'))
  return nil if res.exit_status == 0
  rawerr = res.stdout + ' ' + res.stderr

  {
    'Sorry, try again' => 'Wrong sudo password.',
    'sudo: no tty present and no askpass program specified' =>
      'Sudo requires a password, please configure it.',
    'sudo: command not found' =>
      "Can't find sudo command. Please either install and "\
        'configure it on the target or deactivate sudo.',
    'sudo: sorry, you must have a tty to run sudo' =>
      'Sudo requires a TTY. Please see the README on how to configure '\
        'sudo to allow for non-interactive usage.',
  }.each do |sudo, human|
    rawerr = human if rawerr.include? sudo
  end

  rawerr
end

Private Instance Methods

build_prefix() click to toggle source
# File lib/train/extras/command_wrapper.rb, line 84
def build_prefix
  return '' unless @sudo
  return '' if @user == 'root'

  res = (@sudo_command || 'sudo') + ' '

  unless @sudo_password.nil?
    b64pw = Base64.strict_encode64(@sudo_password + "\n")
    res = "echo #{b64pw} | base64 -d | #{res}-S "
  end

  res << @sudo_options.to_s + ' ' unless @sudo_options.nil?

  res
end