Class: Train::Extras::LinuxCommand
- Inherits:
-
CommandWrapperBase
- Object
- CommandWrapperBase
- Train::Extras::LinuxCommand
- Defined in:
- lib/train/extras/command_wrapper.rb
Overview
Wrap linux commands and add functionality like sudo.
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(backend, options) ⇒ LinuxCommand
constructor
A new instance of LinuxCommand.
- #run(command) ⇒ Object
- #verify ⇒ Object
- #with_sudo_pty ⇒ Object
Constructor Details
#initialize(backend, options) ⇒ LinuxCommand
Returns a new instance of LinuxCommand.
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/train/extras/command_wrapper.rb', line 42 def initialize(backend, ) @backend = backend () @shell = [:shell] @shell_options = [:shell_options] # e.g. '--login' @shell_command = [:shell_command] # e.g. '/bin/sh' @sudo = [:sudo] @sudo_options = [:sudo_options] @sudo_password = [:sudo_password] @sudo_command = [:sudo_command] @user = [:user] end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
40 41 42 |
# File 'lib/train/extras/command_wrapper.rb', line 40 def backend @backend end |
Class Method Details
.active?(options) ⇒ Boolean
104 105 106 107 108 109 |
# File 'lib/train/extras/command_wrapper.rb', line 104 def self.active?() .is_a?(Hash) && ( [:sudo] || [:shell] ) end |
Instance Method Details
#run(command) ⇒ Object
100 101 102 |
# File 'lib/train/extras/command_wrapper.rb', line 100 def run(command) shell_wrap(sudo_wrap(command)) end |
#verify ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/train/extras/command_wrapper.rb', line 66 def verify cmd = if @sudo # Wrap it up. It needs /dev/null on the outside to disable stdin "bash -c '(#{run("-v")}) < /dev/null'" else run("echo") end # rubocop:disable Style/BlockDelimiters res = with_sudo_pty { @backend.run_command(cmd) } return nil if res.exit_status == 0 rawerr = res.stdout + " " + res.stderr { "Sorry, try again" => ["Wrong sudo password.", :bad_sudo_password], "sudo: no tty present and no askpass program specified" => ["Sudo requires a password, please configure it.", :sudo_password_required], "sudo: command not found" => ["Can't find sudo command. Please either install and "\ "configure it on the target or deactivate sudo.", :sudo_command_not_found], "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.", :sudo_no_tty], }.each do |sudo, human| rawerr = human if rawerr.include? sudo end rawerr end |
#with_sudo_pty ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/train/extras/command_wrapper.rb', line 56 def with_sudo_pty old_pty = backend.[:pty] backend.[:pty] = true if @sudo yield ensure backend.[:pty] = old_pty end |