Class: Evrone::Common::Spawn::SSH

Inherits:
Object
  • Object
show all
Defined in:
lib/evrone/common/spawn/ssh.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ssh) ⇒ SSH

Returns a new instance of SSH.



22
23
24
# File 'lib/evrone/common/spawn/ssh.rb', line 22

def initialize(ssh)
  @ssh = ssh
end

Instance Attribute Details

#host ⇒ Object (readonly)

Returns the value of attribute host.



20
21
22
# File 'lib/evrone/common/spawn/ssh.rb', line 20

def host
  @host
end

#options ⇒ Object (readonly)

Returns the value of attribute options.



20
21
22
# File 'lib/evrone/common/spawn/ssh.rb', line 20

def options
  @options
end

#user ⇒ Object (readonly)

Returns the value of attribute user.



20
21
22
# File 'lib/evrone/common/spawn/ssh.rb', line 20

def user
  @user
end

Class Method Details

.open(host, user, options = {}, &block) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/evrone/common/spawn/ssh.rb', line 10

def open(host, user, options = {}, &block)
  ::Net::SSH.start(host, user, {
    forward_agent: true,
    paranoid:      false
  }.merge(options)) do |ssh|
    yield new(ssh)
  end
end

Instance Method Details

#spawn(*args, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/evrone/common/spawn/ssh.rb', line 26

def spawn(*args, &block)
  env     = args.first.is_a?(Hash) ? args.shift : {}
  options = args.last.is_a?(Hash)  ? args.pop : {}
  command = args.join(" ")

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

  channel = spawn_channel env, command, read_timeout, &block

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

  pool channel, timeout, read_timeout

  compute_exit_code command, exit_code, timeout, read_timeout
end