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.



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

def initialize(ssh)
  @connection = ssh
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#hostObject (readonly)

Returns the value of attribute host.



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

def host
  @host
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#userObject (readonly)

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

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



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

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

Instance Method Details

#spawn(*args, &block) ⇒ Object



25
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 25

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)

  command = build_command(env, command, options)
  channel = spawn_channel 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