Class: RemoteRuby::SSHAdapter

Inherits:
ConnectionAdapter show all
Defined in:
lib/remote_ruby/ssh_adapter.rb

Overview

An adapter for executing Ruby code on a remote host via SSH

Constant Summary collapse

UnableToExecuteError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, working_dir: nil, use_ssh_config_file: true, ruby_executable: 'ruby', **params) ⇒ SSHAdapter

Returns a new instance of SSHAdapter.



13
14
15
16
17
18
19
20
21
22
# File 'lib/remote_ruby/ssh_adapter.rb', line 13

def initialize(host:, working_dir: nil, use_ssh_config_file: true, ruby_executable: 'ruby', **params)
  super
  @host = host
  @working_dir = working_dir
  @config = Net::SSH.configuration_for(@host, use_ssh_config_file)
  @ruby_executable = ruby_executable

  @config = @config.merge(params)
  @user = @config[:user]
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/remote_ruby/ssh_adapter.rb', line 11

def config
  @config
end

#hostObject (readonly)

Returns the value of attribute host.



11
12
13
# File 'lib/remote_ruby/ssh_adapter.rb', line 11

def host
  @host
end

#ruby_executableObject (readonly)

Returns the value of attribute ruby_executable.



11
12
13
# File 'lib/remote_ruby/ssh_adapter.rb', line 11

def ruby_executable
  @ruby_executable
end

#userObject (readonly)

Returns the value of attribute user.



11
12
13
# File 'lib/remote_ruby/ssh_adapter.rb', line 11

def user
  @user
end

#working_dirObject (readonly)

Returns the value of attribute working_dir.



11
12
13
# File 'lib/remote_ruby/ssh_adapter.rb', line 11

def working_dir
  @working_dir
end

Instance Method Details

#connection_nameObject



37
38
39
# File 'lib/remote_ruby/ssh_adapter.rb', line 37

def connection_name
  "#{user}@#{host}:#{working_dir || '~'}> "
end

#open(code, stdin, stdout, stderr) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/remote_ruby/ssh_adapter.rb', line 24

def open(code, stdin, stdout, stderr)
  ret = nil
  Net::SSH.start(host, nil, config) do |ssh|
    with_temp_file(code, ssh) do |fname|
      res = run_code(ssh, fname, stdin, stdout, stderr)
      raise "Process exited with code #{res}" unless res.zero?

      ret = get_result(ssh, fname)
    end
  end
  ret
end