Class: Conjure::Service::RemoteShell

Inherits:
Object
  • Object
show all
Defined in:
lib/conjure/service/remote_shell.rb

Defined Under Namespace

Classes: Result

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RemoteShell

Returns a new instance of RemoteShell.



11
12
13
# File 'lib/conjure/service/remote_shell.rb', line 11

def initialize(options = {})
  @options = options
end

Class Attribute Details

.ssh_serviceObject

Returns the value of attribute ssh_service.



7
8
9
# File 'lib/conjure/service/remote_shell.rb', line 7

def ssh_service
  @ssh_service
end

Instance Method Details

#poll_stream(stream) {|stream.sysread(1)| ... } ⇒ Object

Yields:

  • (stream.sysread(1))


47
48
49
# File 'lib/conjure/service/remote_shell.rb', line 47

def poll_stream(stream, &block)
  yield stream.sysread(1) if IO.select([stream], nil, nil, 0.01)
end

#run(command, options = {}, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/conjure/service/remote_shell.rb', line 15

def run(command, options = {}, &block)
  Log.debug "   [ssh] #{command}"
  result = nil
  session.open_channel do |channel|
    channel.request_pty
    channel.exec command do |c, success|
      raise "Failed to execute command via SSH" unless success
      result = Result.new(channel, &block)
    end
    if options[:stream_stdin]
      channel.on_process do
        poll_stream(STDIN) { |data| channel.send_data data }
      end
    end
  end
  if options[:stream_stdin]
    with_raw_tty { session.loop 0.01 }
  else
    session.loop
  end
  result
end

#sessionObject



38
39
40
41
42
43
44
45
# File 'lib/conjure/service/remote_shell.rb', line 38

def session
  session_options = {
    :auth_methods => ["publickey"],
    :paranoid => false,
    :forward_agent => true,
  }
  @session ||= self.class.ssh_service.start @options[:ip_address], @options[:username], session_options
end

#with_raw_ttyObject



51
52
53
54
55
56
# File 'lib/conjure/service/remote_shell.rb', line 51

def with_raw_tty
  system "stty raw -echo"
  yield
ensure
  system "stty -raw echo"
end