Class: Vx::Lib::Shell::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/vx/lib/shell/ssh.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ssh) ⇒ SSH

Returns a new instance of SSH.



12
13
14
# File 'lib/vx/lib/shell/ssh.rb', line 12

def initialize(ssh)
  @connection = ssh
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



10
11
12
# File 'lib/vx/lib/shell/ssh.rb', line 10

def connection
  @connection
end

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/vx/lib/shell/ssh.rb', line 10

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/vx/lib/shell/ssh.rb', line 10

def options
  @options
end

#userObject (readonly)

Returns the value of attribute user.



10
11
12
# File 'lib/vx/lib/shell/ssh.rb', line 10

def user
  @user
end

Instance Method Details

#exec(*args, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/vx/lib/shell/ssh.rb', line 16

def exec(*args, &block)
  options       = args.last.is_a?(Hash) ? args.pop : {}
  command       = args.first
  home          = options[:home] || "$HOME"

  exit_code     = nil
  timeout       = Shell::Timeout.new options.delete(:timeout)
  read_timeout  = Shell::ReadTimeout.new options.delete(:read_timeout)

  prefix = "/usr/bin/env - TERM=ansi USER=$USER HOME=#{home} SHELL=/bin/bash /bin/bash -l"
  if command
    command = "#{prefix} -c #{Shellwords.escape command}"
  else
    command = prefix
  end

  channel = spawn_channel command, read_timeout, options, &block

  channel.on_request("exit-status") do |_,data|
    exit_code = data.read_long
  end

  channel.on_request("exit-signal") do |_,data|
    exit_code = data.read_long * -1
  end

  pool channel, timeout, read_timeout

  compute_exit_code command, exit_code, timeout, read_timeout
end