Class: Conjure::Service::RemoteShell

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

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ 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

#key_dataObject



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

def key_data
  File.read @options[:private_key_path] if @options[:private_key_path]
end

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

Yields:

  • (stream.sysread(1))


56
57
58
# File 'lib/conjure/service/remote_shell.rb', line 56

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

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



15
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
# File 'lib/conjure/service/remote_shell.rb', line 15

def run(command, options = {})
  stdout, stderr, exit_status = ["", "", nil]
  session.open_channel do |channel|
    channel.request_pty
    channel.exec command do |c, success|
      raise "Failed to execute command via SSH" unless success
      channel.on_data do |c, data|
        yield data if block_given?
        stdout << data
      end
      channel.on_extended_data { |c, type, data| stderr << data }
      channel.on_request("exit-status") { |c, data| exit_status = data.read_long }
    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.new stdout, stderr, exit_status
end

#sessionObject



42
43
44
45
46
47
48
49
50
# File 'lib/conjure/service/remote_shell.rb', line 42

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

#with_raw_ttyObject



60
61
62
63
64
65
# File 'lib/conjure/service/remote_shell.rb', line 60

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